overlay: call volume_init() before mount
[project/fstools.git] / libfstools / overlay.c
index bb506ee33c60e14ce12b563cf569b6007b938e45..7c9b480ba963818a70c30048ce4e1a1a181a2793 100644 (file)
 
 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
 
+static bool keep_sysupgrade;
+
+static int
+handle_rmdir(const char *dir)
+{
+       struct dirent *dt;
+       struct stat st;
+       DIR *d;
+       int fd;
+
+       d = opendir(dir);
+       if (!d)
+               return -1;
+
+       fd = dirfd(d);
+
+       while ((dt = readdir(d)) != NULL) {
+               if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
+                       continue;
+
+               if (keep_sysupgrade && !strcmp(dt->d_name, "sysupgrade.tgz"))
+                       continue;
+
+               unlinkat(fd, dt->d_name, 0);
+       }
+
+       closedir(d);
+       rmdir(dir);
+
+       return 0;
+}
+
 void
 foreachdir(const char *dir, int (*cb)(const char*))
 {
@@ -51,6 +83,13 @@ foreachdir(const char *dir, int (*cb)(const char*))
        cb(dir);
 }
 
+void
+overlay_delete(const char *dir, bool _keep_sysupgrade)
+{
+       keep_sysupgrade = _keep_sysupgrade;
+       foreachdir(dir, handle_rmdir);
+}
+
 static int
 overlay_mount(struct volume *v, char *fs)
 {
@@ -216,14 +255,14 @@ static int overlay_mount_fs(struct volume *v)
                break;
        }
 
+       volume_init(v);
+
        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));
                return -1;
        }
 
-       volume_init(v);
-
        return -1;
 }
 
@@ -248,7 +287,7 @@ int mount_overlay(struct volume *v)
                return 0;
        }
 
-       ULOG_INFO("switching to overlay\n");
+       ULOG_INFO("switching to jffs2 overlay\n");
        if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
                ULOG_ERR("switching to jffs2 failed - fallback to ramoverlay\n");
                return ramoverlay();