]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Factorize handle of create=dir and create=file
authorNicolas Cornu <ncornu@aldebaran.com>
Thu, 6 Aug 2015 08:35:16 +0000 (10:35 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Fri, 28 Aug 2015 22:05:37 +0000 (18:05 -0400)
Signed-off-by: Nicolas Cornu <ncornu@aldebaran.com>
src/lxc/conf.c

index a7e0d7122fcd2ed28c46c2dc1e5b9bacd4e3c199..41a39eb83ba2be90f252d99e08e17e8769072661 100644 (file)
@@ -2135,36 +2135,46 @@ static void cull_mntent_opt(struct mntent *mntent)
        }
 }
 
-static inline int mount_entry_on_systemfs(struct mntent *mntent)
+static int mount_entry_create_dir_file(const struct mntent *mntent,
+                                      const char* path)
 {
-       unsigned long mntflags;
-       char *mntdata;
+       char *pathdirname = NULL;
        int ret;
        FILE *pathfile = NULL;
-       char* pathdirname = NULL;
-       bool optional = hasmntopt(mntent, "optional") != NULL;
 
        if (hasmntopt(mntent, "create=dir")) {
-               if (mkdir_p(mntent->mnt_dir, 0755) < 0) {
-                       WARN("Failed to create mount target '%s'", mntent->mnt_dir);
+               if (mkdir_p(path, 0755) < 0) {
+                       WARN("Failed to create mount target '%s'", path);
                        ret = -1;
                }
        }
 
-       if (hasmntopt(mntent, "create=file") && access(mntent->mnt_dir, F_OK)) {
-               pathdirname = strdup(mntent->mnt_dir);
+       if (hasmntopt(mntent, "create=file") && access(path, F_OK)) {
+               pathdirname = strdup(path);
                pathdirname = dirname(pathdirname);
                if (mkdir_p(pathdirname, 0755) < 0) {
                        WARN("Failed to create target directory");
                }
-               pathfile = fopen(mntent->mnt_dir, "wb");
+               pathfile = fopen(path, "wb");
                if (!pathfile) {
-                       WARN("Failed to create mount target '%s'", mntent->mnt_dir);
+                       WARN("Failed to create mount target '%s'", path);
                        ret = -1;
                }
                else
                        fclose(pathfile);
        }
+       free(pathdirname);
+       return ret;
+}
+
+static inline int mount_entry_on_systemfs(struct mntent *mntent)
+{
+       unsigned long mntflags;
+       char *mntdata;
+       int ret;
+       bool optional = hasmntopt(mntent, "optional") != NULL;
+
+       ret = mount_entry_create_dir_file(mntent, mntent->mnt_dir);
 
        cull_mntent_opt(mntent);
 
@@ -2176,7 +2186,6 @@ static inline int mount_entry_on_systemfs(struct mntent *mntent)
        ret = mount_entry(mntent->mnt_fsname, mntent->mnt_dir,
                          mntent->mnt_type, mntflags, mntdata, optional);
 
-       free(pathdirname);
        free(mntdata);
 
        return ret;
@@ -2192,8 +2201,6 @@ static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
        char *mntdata;
        int r, ret = 0, offset;
        const char *lxcpath;
-       FILE *pathfile = NULL;
-       char *pathdirname = NULL;
        bool optional = hasmntopt(mntent, "optional") != NULL;
 
        lxcpath = lxc_global_config_value("lxc.lxcpath");
@@ -2232,27 +2239,8 @@ skipabs:
                goto out;
        }
 
-       if (hasmntopt(mntent, "create=dir")) {
-               if (mkdir_p(path, 0755) < 0) {
-                       WARN("Failed to create mount target '%s'", path);
-                       ret = -1;
-               }
-       }
+       ret = mount_entry_create_dir_file(mntent, path);
 
-       if (hasmntopt(mntent, "create=file") && access(path, F_OK)) {
-               pathdirname = strdup(path);
-               pathdirname = dirname(pathdirname);
-               if (mkdir_p(pathdirname, 0755) < 0) {
-                       WARN("Failed to create target directory");
-               }
-               pathfile = fopen(path, "wb");
-               if (!pathfile) {
-                       WARN("Failed to create mount target '%s'", path);
-                       ret = -1;
-               }
-               else
-                       fclose(pathfile);
-       }
        cull_mntent_opt(mntent);
 
        if (parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata) < 0) {
@@ -2266,7 +2254,6 @@ skipabs:
        free(mntdata);
 
 out:
-       free(pathdirname);
        return ret;
 }
 
@@ -2277,8 +2264,6 @@ static int mount_entry_on_relative_rootfs(struct mntent *mntent,
        unsigned long mntflags;
        char *mntdata;
        int ret;
-       FILE *pathfile = NULL;
-       char *pathdirname = NULL;
        bool optional = hasmntopt(mntent, "optional") != NULL;
 
        /* relative to root mount point */
@@ -2288,27 +2273,8 @@ static int mount_entry_on_relative_rootfs(struct mntent *mntent,
                return -1;
        }
 
-       if (hasmntopt(mntent, "create=dir")) {
-               if (mkdir_p(path, 0755) < 0) {
-                       WARN("Failed to create mount target '%s'", path);
-                       ret = -1;
-               }
-       }
+       ret = mount_entry_create_dir_file(mntent, path);
 
-       if (hasmntopt(mntent, "create=file") && access(path, F_OK)) {
-               pathdirname = strdup(path);
-               pathdirname = dirname(pathdirname);
-               if (mkdir_p(pathdirname, 0755) < 0) {
-                       WARN("Failed to create target directory");
-               }
-               pathfile = fopen(path, "wb");
-               if (!pathfile) {
-                       WARN("Failed to create mount target '%s'", path);
-                       ret = -1;
-               }
-               else
-                       fclose(pathfile);
-       }
        cull_mntent_opt(mntent);
 
        if (parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata) < 0) {
@@ -2319,7 +2285,6 @@ static int mount_entry_on_relative_rootfs(struct mntent *mntent,
        ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type,
                          mntflags, mntdata, optional);
 
-       free(pathdirname);
        free(mntdata);
 
        return ret;
@@ -4876,7 +4841,7 @@ struct lxc_list *sort_cgroup_settings(struct lxc_list* cgroup_settings)
                        /* Store the memsw_limit location */
                        memsw_limit = item;
                } else if (strcmp(cg->subsystem, "memory.limit_in_bytes") == 0 && memsw_limit != NULL) {
-                       /* lxc.cgroup.memory.memsw.limit_in_bytes is found before 
+                       /* lxc.cgroup.memory.memsw.limit_in_bytes is found before
                         * lxc.cgroup.memory.limit_in_bytes, swap these two items */
                        item->elem = memsw_limit->elem;
                        memsw_limit->elem = it->elem;