};
static int pakfire_populate_dev(Pakfire pakfire) {
+ char path[PATH_MAX];
+
// Create device nodes
for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) {
DEBUG(pakfire, "Creating device node %s\n", devnode->path);
- char* path = pakfire_make_path(pakfire, devnode->path);
+ int r = pakfire_make_path(pakfire, path, devnode->path);
+ if (r < 0)
+ return 1;
+
dev_t dev = makedev(devnode->major, devnode->minor);
- int r = mknod(path, devnode->mode, dev);
+ r = mknod(path, devnode->mode, dev);
if (r) {
ERROR(pakfire, "Could not create %s: %s\n", devnode->path, strerror(errno));
- free(path);
-
return r;
}
-
- free(path);
}
// Create symlinks
for (const struct pakfire_symlink* s = symlinks; s->target; s++) {
DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target);
- char* path = pakfire_make_path(pakfire, s->path);
+ int r = pakfire_make_path(pakfire, path, s->path);
+ if (r < 0)
+ return 1;
- int r = symlink(s->target, path);
+ r = symlink(s->target, path);
if (r) {
ERROR(pakfire, "Could not create symlink %s: %s\n", s->path, strerror(errno));
- free(path);
-
return r;
}
-
- free(path);
}
return 0;
}
static int pakfire_read_repo_config(Pakfire pakfire) {
- char* path = pakfire_make_path(pakfire, PAKFIRE_CONFIG_PATH "/repos");
- if (!path)
- return ENOMEM;
+ char path[PATH_MAX];
+
+ int r = pakfire_make_path(pakfire, path, PAKFIRE_CONFIG_PATH "/repos");
+ if (r < 0)
+ return 1;
- int r = 0;
char* paths[2] = {
path, NULL,
};
+ r = 1;
FTS* d = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL);
if (!d)
ERROR:
if (d)
fts_close(d);
- free(path);
return r;
}
}
static int pakfire_read_config(Pakfire pakfire, const char* path) {
- char* default_path = NULL;
+ char default_path[PATH_MAX];
// Use default path if none set
if (!path) {
- default_path = pakfire_make_path(pakfire, PAKFIRE_CONFIG_PATH "/general.conf");
- if (!default_path)
+ int r = pakfire_make_path(pakfire, default_path, PAKFIRE_CONFIG_PATH "/general.conf");
+ if (r < 0)
return 1;
path = default_path;
FILE* f = fopen(path, "r");
if (!f) {
// Silently ignore when there is no default configuration file
- if (default_path && errno == ENOENT)
+ if (*default_path && errno == ENOENT)
return 0;
return 1;
ERROR:
fclose(f);
- if (default_path)
- free(default_path);
-
return r;
}
static int pakfire_read_os_release(Pakfire pakfire) {
+ char path[PATH_MAX];
char* line = NULL;
size_t l = 0;
- int r = 1;
- char* path = pakfire_make_path(pakfire, "/etc/os-release");
- if (!path)
- return ENOMEM;
+ int r = pakfire_make_path(pakfire, path, "/etc/os-release");
+ if (r < 0)
+ return 1;
+
+ r = 1;
FILE* f = fopen(path, "r");
if (!f) {
// Ignore when the file does not exist
- if (errno == ENOENT)
+ if (errno == ENOENT) {
+ r = 0;
goto ERROR;
+ }
ERROR(pakfire, "Could not open %s: %s\n", path, strerror(errno));
- r = 1;
goto ERROR;
}
fclose(f);
if (line)
free(line);
- free(path);
return r;
}
if (r)
goto ERROR;
+ // Make path for private files
+ char private_dir[PATH_MAX];
+ r = pakfire_make_path(p, private_dir, PAKFIRE_PRIVATE_DIR);
+ if (r < 0)
+ goto ERROR;
+
// Make sure that our private directory exists
- char* private_dir = pakfire_make_path(p, PAKFIRE_PRIVATE_DIR);
r = pakfire_mkdir(private_dir, 0);
if (r && errno != EEXIST) {
- ERROR(p, "Could not create private directory %s: %s\n",
- private_dir, strerror(errno));
- free(private_dir);
-
+ ERROR(p, "Could not create private directory %s: %s\n", private_dir, strerror(errno));
goto ERROR;
}
return pakfire->path;
}
-PAKFIRE_EXPORT char* pakfire_make_path(Pakfire pakfire, const char* path) {
- char buffer[PATH_MAX];
-
+PAKFIRE_EXPORT int __pakfire_make_path(Pakfire pakfire,
+ char* dst, size_t length, const char* path) {
// Make sure that path never starts with /
while (path && *path == '/')
path++;
- int r = pakfire_path_join(buffer, pakfire->path, path);
- if (r < 0)
- return NULL;
-
- return strdup(buffer);
+ return pakfire_path_join(dst, pakfire->path, path);
}
PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags) {
+ char mountpoint[PATH_MAX];
+
if (!dst)
dst = src;
- char* mountpoint = pakfire_make_path(pakfire, dst);
- if (!mountpoint)
+ int r = pakfire_make_path(pakfire, mountpoint, dst);
+ if (r < 0)
return 1;
DEBUG(pakfire, "Mounting %s to %s\n", src, mountpoint);
// Make sure the directory exists
- int r = pakfire_mkdir(mountpoint, 0);
+ r = pakfire_mkdir(mountpoint, 0);
if (r && errno != EEXIST)
- goto ERROR;
+ return r;
// Perform mount
- r = __mount(pakfire, src, mountpoint, NULL, flags|MS_BIND, NULL);
-
-ERROR:
- free(mountpoint);
- return r;
+ return __mount(pakfire, src, mountpoint, NULL, flags|MS_BIND, NULL);
}
static int pakfire_copy(Pakfire pakfire, const char* src, const char* dst) {
return 1;
}
- char* path = pakfire_make_path(pakfire, dst);
- if (!path)
+ char path[PATH_MAX];
+ int r = pakfire_make_path(pakfire, path, dst);
+ if (r < 0)
return 1;
- int r = pakfire_copy(pakfire, src, path);
- free(path);
-
- return r;
+ return pakfire_copy(pakfire, src, path);
}
PAKFIRE_EXPORT int pakfire_copy_out(Pakfire pakfire, const char* src, const char* dst) {
return 1;
}
- char* path = pakfire_make_path(pakfire, src);
- if (!path)
+ char path[PATH_MAX];
+ int r = pakfire_make_path(pakfire, path, src);
+ if (r < 0)
return 1;
- int r = pakfire_copy(pakfire, path, dst);
- free(path);
-
- return r;
+ return pakfire_copy(pakfire, path, dst);
}
PAKFIRE_EXPORT const char* pakfire_get_arch(Pakfire pakfire) {