]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: bind: Support binding files, too
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 May 2021 20:28:31 +0000 (20:28 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 May 2021 20:28:31 +0000 (20:28 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c

index 6b382e951504ce035f01fd8e6352e15d1eef1ba8..373c0916de651c9f90937e5a08094a80307ff720 100644 (file)
@@ -967,6 +967,7 @@ int __pakfire_make_path(Pakfire pakfire, char* dst, size_t length, const char* p
 }
 
 PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags) {
+       struct stat st;
        char mountpoint[PATH_MAX];
 
        if (!dst)
@@ -978,15 +979,38 @@ PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* ds
 
        DEBUG(pakfire, "Mounting %s to %s\n", src, mountpoint);
 
-       // Does the source exist?
-       r = pakfire_mkdir(src, 0);
-       if (r && errno != EEXIST)
-               return r;
+       r = stat(src, &st);
+       if (r < 0) {
+               ERROR(pakfire, "Could not stat %s: %s\n", src, strerror(errno));
+               return 1;
+       }
 
-       // Make sure the directory exists
-       r = pakfire_mkdir(mountpoint, 0);
-       if (r && errno != EEXIST)
-               return r;
+       // Make sure the mountpoint exists
+       switch (st.st_mode & S_IFMT) {
+               case S_IFDIR:
+                       r = pakfire_mkdir(mountpoint, 0);
+                       if (r && errno != EEXIST)
+                               return r;
+                       break;
+
+               case S_IFREG:
+               case S_IFLNK:
+                       // Make parent directory
+                       r = pakfire_mkparentdir(mountpoint, 0);
+                       if (r)
+                               return r;
+
+                       // Create a file
+                       FILE* f = fopen(mountpoint, "w");
+                       if (!f)
+                               return 1;
+                       fclose(f);
+                       break;
+
+               default:
+                       errno = ENOTSUP;
+                       return 1;
+       }
 
        // Perform mount
        return __mount(pakfire, src, mountpoint, NULL, flags|MS_BIND, NULL);