]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - lib/loopdev.c
losetup: add --loop-ref and REF column
[thirdparty/util-linux.git] / lib / loopdev.c
index dd9ead3ee3ec093e85d5af6667c14042e67b3942..b3c537cd583b3df28a73191132576d6de2d2bd84 100644 (file)
@@ -747,6 +747,26 @@ char *loopcxt_get_backing_file(struct loopdev_cxt *lc)
        return res;
 }
 
+/*
+ * @lc: context
+ *
+ * Returns (allocated) string with loop reference. The same as backing file by
+ * default.
+ */
+char *loopcxt_get_refname(struct loopdev_cxt *lc)
+{
+       char *res = NULL;
+       struct loop_info64 *lo = loopcxt_get_info(lc);
+
+       if (lo) {
+               lo->lo_file_name[LO_NAME_SIZE - 1] = '\0';
+               res = strdup((char *) lo->lo_file_name);
+       }
+
+       DBG(CXT, ul_debugobj(lc, "get_refname [%s]", res));
+       return res;
+}
+
 /*
  * @lc: context
  * @offset: returns offset number for the given device
@@ -1180,6 +1200,28 @@ int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags)
        return 0;
 }
 
+/*
+ * @lc: context
+ * @refname: reference name (used to overwrite lo_file_name where is backing
+ *           file by default)
+ *
+ * The setting is removed by loopcxt_set_device() loopcxt_next()!
+ *
+ * Returns: 0 on success, <0 on error.
+ */
+int loopcxt_set_refname(struct loopdev_cxt *lc, const char *refname)
+{
+       if (!lc)
+               return -EINVAL;
+
+       memset(lc->config.info.lo_file_name, 0, sizeof(lc->config.info.lo_file_name));
+       if (refname)
+               xstrncpy((char *)lc->config.info.lo_file_name, refname, LO_NAME_SIZE);
+
+       DBG(CXT, ul_debugobj(lc, "set refname=%s", (char *)lc->config.info.lo_file_name));
+       return 0;
+}
+
 /*
  * @lc: context
  * @filename: backing file path (the path will be canonicalized)
@@ -1197,9 +1239,10 @@ int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename)
        if (!lc->filename)
                return -errno;
 
-       xstrncpy((char *)lc->config.info.lo_file_name, lc->filename, LO_NAME_SIZE);
+       if (!lc->config.info.lo_file_name[0])
+               loopcxt_set_refname(lc, lc->filename);
 
-       DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->config.info.lo_file_name));
+       DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->filename));
        return 0;
 }