libfstools: support file paths longer than 255 chars
[project/fstools.git] / libfstools / overlay.c
index ed0f304..e610b8d 100644 (file)
@@ -67,15 +67,29 @@ handle_rmdir(const char *dir)
 void
 foreachdir(const char *dir, int (*cb)(const char*))
 {
+       static char *globdir = NULL;
+       static size_t globdirlen = 0;
        struct stat s = { 0 };
-       char globdir[256];
+       size_t dirlen = strlen(dir);
        glob_t gl;
        int j;
 
-       if (dir[strlen(dir) - 1] == '/')
-               snprintf(globdir, 256, "%s*", dir);
+       if (dirlen + sizeof("/*") > globdirlen) {
+               /* Alloc extra 256 B to avoid too many reallocs */
+               size_t len = dirlen + sizeof("/*") + 256;
+               char *tmp;
+
+               tmp = realloc(globdir, len);
+               if (!tmp)
+                       return;
+               globdir = tmp;
+               globdirlen = len;
+       }
+
+       if (dir[dirlen - 1] == '/')
+               sprintf(globdir, "%s*", dir);
        else
-               snprintf(globdir, 256, "%s/*", dir); /**/
+               sprintf(globdir, "%s/*", dir);
 
        if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
                for (j = 0; j < gl.gl_pathc; j++) {
@@ -102,12 +116,12 @@ static int
 overlay_mount(struct volume *v, char *fs)
 {
        if (mkdir("/tmp/overlay", 0755)) {
-               ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
+               ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
                return -1;
        }
 
        if (mount(v->blk, "/tmp/overlay", fs, MS_NOATIME, NULL)) {
-               ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n", fs, v->blk, strerror(errno));
+               ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n", fs, v->blk);
                return -1;
        }
 
@@ -129,27 +143,27 @@ switch2jffs(struct volume *v)
        ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
        unlink("/tmp/.switch_jffs2");
        if (ret) {
-               ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %s\n", v->blk, strerror(errno));
+               ULOG_ERR("failed - mount -t jffs2 %s /rom/overlay: %m\n", v->blk);
                return -1;
        }
 
        if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
-               ULOG_ERR("failed - mount -o remount,ro none: %s\n", strerror(errno));
+               ULOG_ERR("failed - mount -o remount,ro none: %m\n");
                return -1;
        }
 
        if (system("cp -a /tmp/root/* /rom/overlay")) {
-               ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %s\n", strerror(errno));
+               ULOG_ERR("failed - cp -a /tmp/root/* /rom/overlay: %m\n");
                return -1;
        }
 
        if (pivot("/rom", "/mnt")) {
-               ULOG_ERR("failed - pivot /rom /mnt: %s\n", strerror(errno));
+               ULOG_ERR("failed - pivot /rom /mnt: %m\n");
                return -1;
        }
 
        if (mount_move("/mnt", "/tmp/root", "")) {
-               ULOG_ERR("failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
+               ULOG_ERR("failed - mount -o move /mnt /tmp/root %m\n");
                return -1;
        }
 
@@ -210,7 +224,7 @@ static char *overlay_fs_name(int type)
 int
 jffs2_switch(struct volume *v)
 {
-       char *mp;
+       char *mp, *fs_name;
        int type;
 
        if (find_overlay_mount("overlayfs:/tmp/root"))
@@ -229,6 +243,8 @@ jffs2_switch(struct volume *v)
        }
 
        type = volume_identify(v);
+       fs_name = overlay_fs_name(type);
+
        switch (type) {
        case FS_NONE:
                ULOG_ERR("no jffs2 marker found\n");
@@ -241,15 +257,19 @@ jffs2_switch(struct volume *v)
                ULOG_INFO("performing overlay whiteout\n");
                umount2("/tmp/root", MNT_DETACH);
                foreachdir("/overlay/", handle_whiteout);
+
+               /* try hard to be in sync */
+               ULOG_INFO("syncronizing overlay\n");
+               system("cp -a /tmp/root/upper/* / 2>/dev/null");
                break;
 
        case FS_EXT4:
        case FS_F2FS:
        case FS_UBIFS:
-               if (overlay_mount(v, overlay_fs_name(type)))
+               if (overlay_mount(v, fs_name))
                        return -1;
                if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
-                       ULOG_ERR("switching to jffs2 failed\n");
+                       ULOG_ERR("switching to %s failed\n", fs_name);
                        return -1;
                }
                break;
@@ -265,13 +285,13 @@ static int overlay_mount_fs(struct volume *v)
        char *fstype = overlay_fs_name(volume_identify(v));
 
        if (mkdir("/tmp/overlay", 0755)) {
-               ULOG_ERR("failed to mkdir /tmp/overlay: %s\n", strerror(errno));
+               ULOG_ERR("failed to mkdir /tmp/overlay: %m\n");
                return -1;
        }
 
        if (mount(v->blk, "/tmp/overlay", fstype, MS_NOATIME, NULL)) {
-               ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %s\n",
-                        fstype, v->blk, strerror(errno));
+               ULOG_ERR("failed to mount -t %s %s /tmp/overlay: %m\n",
+                        fstype, v->blk);
                return -1;
        }
 
@@ -320,7 +340,7 @@ int fs_state_set(const char *dir, enum fs_state state)
 
 int mount_overlay(struct volume *v)
 {
-       char *mp;
+       char *mp, *fs_name;
 
        if (!v)
                return -1;
@@ -354,9 +374,10 @@ int mount_overlay(struct volume *v)
                break;
        }
 
-       ULOG_INFO("switching to jffs2 overlay\n");
+       fs_name = overlay_fs_name(volume_identify(v));
+       ULOG_INFO("switching to %s overlay\n", fs_name);
        if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
-               ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");
+               ULOG_ERR("switching to %s failed - fallback to ramoverlay\n", fs_name);
                return ramoverlay();
        }