mnt: simplify mountPt
authorRobert Swiecki <robert@swiecki.net>
Wed, 21 Feb 2018 02:29:26 +0000 (03:29 +0100)
committerRobert Swiecki <robert@swiecki.net>
Wed, 21 Feb 2018 02:29:26 +0000 (03:29 +0100)
mnt.cc

diff --git a/mnt.cc b/mnt.cc
index 30e26a5..b3ba1b8 100644 (file)
--- a/mnt.cc
+++ b/mnt.cc
@@ -121,11 +121,11 @@ static bool isDir(const char* path) {
 }
 
 static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
-       char dst[PATH_MAX];
-       snprintf(dst, sizeof(dst), "%s/%s", newroot, mpt->dst.c_str());
-
        LOG_D("Mounting '%s'", describeMountPt(*mpt).c_str());
 
+       char dstpath[PATH_MAX];
+       snprintf(dstpath, sizeof(dstpath), "%s/%s", newroot, mpt->dst.c_str());
+
        char srcpath[PATH_MAX];
        if (!mpt->src.empty()) {
                snprintf(srcpath, sizeof(srcpath), "%s", mpt->src.c_str());
@@ -133,46 +133,38 @@ static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
                snprintf(srcpath, sizeof(srcpath), "none");
        }
 
-       if (mpt->is_symlink) {
-               if (!util::createDirRecursively(dst)) {
-                       LOG_W("Couldn't create upper directories for '%s'", dst);
-                       return false;
-               }
-       } else if (mpt->is_dir) {
-               if (!util::createDirRecursively(dst)) {
-                       LOG_W("Couldn't create upper directories for '%s'", dst);
-                       return false;
-               }
-               if (mkdir(dst, 0711) == -1 && errno != EEXIST) {
-                       PLOG_W("mkdir('%s')", dst);
-               }
-       } else {
-               if (!util::createDirRecursively(dst)) {
-                       LOG_W("Couldn't create upper directories for '%s'", dst);
-                       return false;
-               }
-               int fd = TEMP_FAILURE_RETRY(open(dst, O_CREAT | O_RDONLY | O_CLOEXEC, 0644));
-               if (fd >= 0) {
-                       close(fd);
-               } else {
-                       PLOG_W("open('%s', O_CREAT|O_RDONLY|O_CLOEXEC, 0644)", dst);
-               }
+       if (!util::createDirRecursively(dstpath)) {
+               LOG_W("Couldn't create upper directories for '%s'", dstpath);
+               return false;
        }
 
        if (mpt->is_symlink) {
-               LOG_D("symlink('%s', '%s')", srcpath, dst);
-               if (symlink(srcpath, dst) == -1) {
+               LOG_D("symlink('%s', '%s')", srcpath, dstpath);
+               if (symlink(srcpath, dstpath) == -1) {
                        if (mpt->is_mandatory) {
-                               PLOG_W("symlink('%s', '%s')", srcpath, dst);
+                               PLOG_W("symlink('%s', '%s')", srcpath, dstpath);
                                return false;
                        } else {
                                PLOG_W("symlink('%s', '%s'), but it's not mandatory, continuing",
-                                   srcpath, dst);
+                                   srcpath, dstpath);
                        }
                }
                return true;
        }
 
+       if (mpt->is_dir) {
+               if (mkdir(dstpath, 0711) == -1 && errno != EEXIST) {
+                       PLOG_W("mkdir('%s')", dstpath);
+               }
+       } else {
+               int fd = TEMP_FAILURE_RETRY(open(dstpath, O_CREAT | O_RDONLY | O_CLOEXEC, 0644));
+               if (fd >= 0) {
+                       close(fd);
+               } else {
+                       PLOG_W("open('%s', O_CREAT|O_RDONLY|O_CLOEXEC, 0644)", dstpath);
+               }
+       }
+
        if (!mpt->src_content.empty()) {
                static uint64_t df_counter = 0;
                snprintf(
@@ -183,8 +175,7 @@ static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
                        PLOG_W("open(srcpath, O_CREAT|O_EXCL|O_CLOEXEC|O_WRONLY, 0644) failed");
                        return false;
                }
-               if (util::writeToFd(fd, mpt->src_content.data(), mpt->src_content.length()) ==
-                   false) {
+               if (!util::writeToFd(fd, mpt->src_content.data(), mpt->src_content.length())) {
                        LOG_W("Writting %zu bytes to '%s' failed", mpt->src_content.length(),
                            srcpath);
                        close(fd);
@@ -198,16 +189,16 @@ static bool mountPt(mount_t* mpt, const char* newroot, const char* tmpdir) {
         * Initially mount it as RW, it will be remounted later on if needed
         */
        unsigned long flags = mpt->flags & ~(MS_RDONLY);
-       if (mount(srcpath, dst, mpt->fs_type.c_str(), flags, mpt->options.c_str()) == -1) {
+       if (mount(srcpath, dstpath, mpt->fs_type.c_str(), flags, mpt->options.c_str()) == -1) {
                if (errno == EACCES) {
                        PLOG_W(
-                           "mount('%s') src:'%s' dst:'%s' failed. "
+                           "mount('%s') src:'%s' dstpath:'%s' failed. "
                            "Try fixing this problem by applying 'chmod o+x' to the '%s' "
                            "directory and its ancestors",
-                           describeMountPt(*mpt).c_str(), srcpath, dst, srcpath);
+                           describeMountPt(*mpt).c_str(), srcpath, dstpath, srcpath);
                } else {
-                       PLOG_W("mount('%s') src:'%s' dst:'%s' failed",
-                           describeMountPt(*mpt).c_str(), srcpath, dst);
+                       PLOG_W("mount('%s') src:'%s' dstpath:'%s' failed",
+                           describeMountPt(*mpt).c_str(), srcpath, dstpath);
                        if (mpt->fs_type.compare("proc") == 0) {
                                PLOG_W(
                                    "procfs can only be mounted if the original /proc doesn't have "
@@ -535,12 +526,12 @@ const std::string describeMountPt(const mount_t& mpt) {
            .append("' options:'")
            .append(mpt.options)
            .append("'");
+
        if (mpt.is_dir) {
                descr.append(" is_dir:true");
        } else {
                descr.append(" is_dir:false");
        }
-
        if (!mpt.is_mandatory) {
                descr.append(" mandatory:false");
        }