]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Constify pakfire_basename/dirname
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 15:10:08 +0000 (16:10 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 15:10:08 +0000 (16:10 +0100)
Those return a pointer to a statically allocated buffer
which should not be freed. To make that obvious, we make
it const.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/util.h
src/libpakfire/package.c
src/libpakfire/pakfire.c
src/libpakfire/util.c

index 3447d5e5f48db06a8b209ca01c28bdea9b66b2ca..11fc4107969d9c29c3ce4317261b543ee44dc9d7 100644 (file)
@@ -44,8 +44,8 @@ char* pakfire_format_date(time_t t);
 
 char* pakfire_path_join(const char* first, const char* second);
 
-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);
 int pakfire_access(Pakfire pakfire, const char* dir, const char* file, int mode);
 int pakfire_mkdir(Pakfire pakfire, const char* path, mode_t mode);
 
index f2c3b53610cdf65e6ea2d5247acd25899ba2e8b2..ab0d4ad6aae919a34226dff6e6808c694211d049 100644 (file)
@@ -1000,8 +1000,8 @@ PAKFIRE_EXPORT PakfireFile pakfire_package_filelist_append(PakfirePackage pkg, c
 
        Id handle = pakfire_package_get_handle(pkg);
 
-       char* dirname  = pakfire_dirname(filename);
-       char* basename = pakfire_basename(filename);
+       const char* dirname  = pakfire_dirname(filename);
+       const char* basename = pakfire_basename(filename);
 
        Id did = repodata_str2dir(repodata, dirname, 1);
        if (!did)
index 3a14029b87fbf1f6148e995b5aa3d501b2d69708..99548c508a5e65684772d34e58ea2590f29da841 100644 (file)
@@ -410,7 +410,7 @@ PAKFIRE_EXPORT FILE* pakfire_cache_open(Pakfire pakfire, const char* path, const
        char* cache_path = pakfire_get_cache_path(pakfire, path);
 
        // Ensure that the parent directory exists
-       char* cache_dirname = pakfire_dirname(cache_path);
+       const char* cache_dirname = pakfire_dirname(cache_path);
 
        int r = pakfire_mkdir(pakfire, cache_dirname, S_IRUSR|S_IWUSR|S_IXUSR);
        if (r)
@@ -421,7 +421,6 @@ PAKFIRE_EXPORT FILE* pakfire_cache_open(Pakfire pakfire, const char* path, const
 
 FAIL:
        pakfire_free(cache_path);
-       pakfire_free(cache_dirname);
 
        return f;
 }
index 529d0cb10c14c533ebaa582315a98a16303743e1..65de4eec65f4be5c2e97304a86754904559ca872 100644 (file)
@@ -144,16 +144,22 @@ PAKFIRE_EXPORT char* pakfire_path_join(const char* first, const char* second) {
        return buffer;
 }
 
-char* pakfire_basename(const char* path) {
+const char* pakfire_basename(const char* path) {
        char* name = pakfire_strdup(path);
 
-       return basename(name);
+       const char* r = basename(name);
+       pakfire_free(name);
+
+       return r;
 }
 
-char* pakfire_dirname(const char* path) {
+const char* pakfire_dirname(const char* path) {
        char* parent = pakfire_strdup(path);
 
-       return dirname(parent);
+       const char* r = dirname(parent);
+       pakfire_free(parent);
+
+       return r;
 }
 
 PAKFIRE_EXPORT int pakfire_access(Pakfire pakfire, const char* dir, const char* file, int mode) {
@@ -187,13 +193,11 @@ int pakfire_mkdir(Pakfire pakfire, const char* path, mode_t mode) {
                return 0;
 
        // If parent does not exists, we try to create it.
-       char* parent = pakfire_dirname(path);
+       const char* parent = pakfire_dirname(path);
        r = pakfire_access(pakfire, parent, NULL, F_OK);
        if (r)
                r = pakfire_mkdir(pakfire, parent, 0);
 
-       pakfire_free(parent);
-
        if (r)
                return r;