return NULL;
// Determine the path of the parent
- char* path = pakfire_dirname(cgroup->path);
+ const char* path = pakfire_dirname(cgroup->path);
if (!path) {
ERROR(cgroup->pakfire, "Could not determine path for parent cgroup: %m\n");
return NULL;
// dirname() returns . if no directory component could be found
if (strcmp(path, ".") == 0)
- *path = '\0';
+ path = NULL;
// Open the cgroup
r = pakfire_cgroup_open(&parent, cgroup->pakfire, path, 0);
parent = NULL;
}
- // Cleanup
- free(path);
-
return parent;
}
}
// Set BASEDIR
- char* dirname = pakfire_dirname(path);
+ const char* dirname = pakfire_dirname(path);
if (dirname) {
const char* root = pakfire_get_path(pakfire);
pakfire_parser_set(parser, NULL, "BASEDIR",
pakfire_path_relpath(root, dirname), 0);
- free(dirname);
}
long processors_online = sysconf(_SC_NPROCESSORS_ONLN);
int r = 1;
// Find the parent directory
- char* dirname = pakfire_dirname(path);
+ const char* dirname = pakfire_dirname(path);
if (!dirname)
return 1;
DEBUG(pakfire, "Adding all files in '%s' to package...\n", dirname);
char* paths[2] = {
- dirname, NULL,
+ (char*)dirname, NULL,
};
FTS* f = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL);
if (f)
fts_close(f);
- free(dirname);
-
return r;
}
// Or use the filename
} else {
- char* filename = pakfire_basename(url);
- if (filename) {
+ const char* filename = pakfire_basename(url);
+ if (filename)
pakfire_string_set(transfer->title, filename);
- free(filename);
- }
}
// Copy URL
int pakfire_path_exists(const char* path);
time_t pakfire_path_age(const char* path);
-char* pakfire_basename(const char* path);
-char* pakfire_dirname(const char* path);
+const char* pakfire_basename(const char* path);
+const char* pakfire_dirname(const char* path);
char* pakfire_remove_trailing_newline(char* str);
}
PAKFIRE_EXPORT void pakfire_package_set_path(struct pakfire_package* pkg, const char* path) {
- char* basename = pakfire_basename(path);
- char* dirname = pakfire_dirname(path);
+ const char* basename = pakfire_basename(path);
+ const char* dirname = pakfire_dirname(path);
- if (basename) {
+ if (basename)
pakfire_package_set_string(pkg, SOLVABLE_MEDIAFILE, basename);
- free(basename);
- }
- if (dirname) {
+ if (dirname)
pakfire_package_set_string(pkg, SOLVABLE_MEDIABASE, dirname);
- free(dirname);
- }
pakfire_string_set(pkg->path, path);
}
Repodata* repodata = pakfire_repo_get_repodata(repo);
pakfire_repo_unref(repo);
- char* basename = pakfire_basename(path);
- char* dirname = pakfire_dirname(path);
+ const char* basename = pakfire_basename(path);
+ const char* dirname = pakfire_dirname(path);
// Convert directory into ID
Id did = repodata_str2dir(repodata, dirname, 1);
repodata_add_dirstr(repodata, pkg->id,
SOLVABLE_FILELIST, did, basename);
- free(basename);
- free(dirname);
-
return 0;
}
// Make sure that our private directory exists
r = pakfire_mkdir(private_dir, 0755);
- if (r && errno != EEXIST) {
+ if (r) {
ERROR(p, "Could not create private directory %s: %m\n", private_dir);
goto ERROR;
}
return r;
}
-char* pakfire_basename(const char* path) {
- char* name = strdup(path);
+const char* pakfire_basename(const char* path) {
+ static char buffer[PATH_MAX];
+ int r;
- char* r = basename(name);
+ // Copy string
+ r = pakfire_string_set(buffer, path);
if (r)
- r = strdup(r);
-
- free(name);
+ return NULL;
- return r;
+ return basename(buffer);
}
-char* pakfire_dirname(const char* path) {
- char* parent = strdup(path);
+const char* pakfire_dirname(const char* path) {
+ static char buffer[PATH_MAX];
+ int r;
- char* r = dirname(parent);
+ // Copy string
+ r = pakfire_string_set(buffer, path);
if (r)
- r = strdup(r);
-
- free(parent);
+ return NULL;
- return r;
+ return dirname(buffer);
}
char* pakfire_remove_trailing_newline(char* str) {
}
int pakfire_mkparentdir(const char* path, mode_t mode) {
- int r;
-
- char* dirname = pakfire_dirname(path);
+ const char* dirname = pakfire_dirname(path);
if (!dirname)
return 1;
- // We have arrived at the top of the tree
- if (*dirname == '.' || strcmp(dirname, "/") == 0)
- return 0;
+ return pakfire_mkdir(dirname, mode);
+}
- // Ensure the parent directory exists
- r = pakfire_mkparentdir(dirname, mode);
- if (r)
- goto END;
+static int pakfire_try_mkdir(const char* path, const mode_t mode) {
+ struct stat st;
+ int r;
- // Create this directory
- r = mkdir(dirname, mode);
+ // Call stat() on path
+ r = stat(path, &st);
- // Ignore when the directory already exists
- if (r && errno == EEXIST)
- r = 0;
+ // Path exists, but is it a directory?
+ if (r == 0) {
+ if (S_ISDIR(st.st_mode))
+ return 0;
-END:
- free(dirname);
+ // Not a directory
+ errno = ENOTDIR;
+ return 1;
- return r;
+ // Path does not exist
+ } else if (r && errno == ENOENT) {
+ // Try to create it
+ r = mkdir(path, mode);
+ if (r)
+ return r;
+
+ // Raise any other errors
+ } else {
+ return r;
+ }
+
+ // Success
+ return 0;
}
int pakfire_mkdir(const char* path, mode_t mode) {
- int r = pakfire_mkparentdir(path, mode);
+ char buffer[PATH_MAX];
+ int r;
+
+ // Copy dirname into buffer
+ r = pakfire_string_set(buffer, path);
if (r)
return r;
- // Finally, create the directory we want
- return mkdir(path, mode);
+ // Recursively create all directories
+ for (char* p = buffer + 1; *p; p++) {
+ if (*p == '/') {
+ // Cut off at slash
+ *p = '\0';
+
+ // Try to create partition directory
+ r = pakfire_try_mkdir(buffer, mode);
+ if (r)
+ return r;
+
+ // Reset slash
+ *p = '/';
+ }
+ }
+
+ // Create final directory
+ return pakfire_try_mkdir(path, mode);
}
FILE* pakfire_mktemp(char* path) {
char small_buffer[2];
int r;
- ASSERT(pakfire_format_size(buffer, 0) == 2);
+ ASSERT_SUCCESS(pakfire_format_size(buffer, 0));
ASSERT_STRING_EQUALS(buffer, "0 ");
- ASSERT(pakfire_format_size(buffer, 1024) == 2);
+ ASSERT_SUCCESS(pakfire_format_size(buffer, 1024));
ASSERT_STRING_EQUALS(buffer, "1k");
- ASSERT(pakfire_format_size(buffer, 1024 * 1024) == 4);
+ ASSERT_SUCCESS(pakfire_format_size(buffer, 1024 * 1024) );
ASSERT_STRING_EQUALS(buffer, "1.0M");
- ASSERT(pakfire_format_size(small_buffer, 0) == 2);
- ASSERT_STRING_EQUALS(small_buffer, "");
+ ASSERT_ERRNO(pakfire_format_size(small_buffer, 0), ENOMEM);
return EXIT_SUCCESS;
# #
#############################################################################*/
-#include <stdlib.h>
-
#include <pakfire/util.h>
#include "../testsuite.h"
static int test_basename(const struct test* t) {
const char* dir = "/a/b/c";
- char* output = pakfire_basename(dir);
- ASSERT_STRING_EQUALS(output, "c");
- free(output);
+ ASSERT_STRING_EQUALS(pakfire_basename(dir), "c");
return EXIT_SUCCESS;
static int test_dirname(const struct test* t) {
const char* dir = "/a/b/c";
- char* output = pakfire_dirname(dir);
- ASSERT_STRING_EQUALS(output, "/a/b");
- free(output);
+ ASSERT_STRING_EQUALS(pakfire_dirname(dir), "/a/b");
+
+ return EXIT_SUCCESS;
+
+FAIL:
+ return EXIT_FAILURE;
+}
+
+static int test_mkdir(const struct test* t) {
+ char path[PATH_MAX];
+
+ // Create some random path
+ ASSERT_SUCCESS(pakfire_path(t->pakfire, path, "%s", "/a/b/c/d/e"));
+
+ // Create this directory
+ ASSERT_SUCCESS(pakfire_mkdir(path, 0755));
+
+ // Check if this exists
+ ASSERT_SUCCESS(!pakfire_path_exists(path));
+
+ // Create path again (nothing should happen)
+ ASSERT_SUCCESS(pakfire_mkdir(path, 0755));
+
+ // Create the parent directory for /test/1/file.txt
+ ASSERT_SUCCESS(pakfire_mkparentdir("/test/1/file.txt", 0755));
+
+ // Check that only the parent directory exists
+ ASSERT_SUCCESS(!pakfire_path_exists("/test/1"));
+ ASSERT_SUCCESS(!!pakfire_path_exists("/test/1/file.txt"));
return EXIT_SUCCESS;
int main(int argc, const char* argv[]) {
testsuite_add_test(test_basename);
testsuite_add_test(test_dirname);
+ testsuite_add_test(test_mkdir);
return testsuite_run(argc, argv);
}