]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
umount: use new lib/loopdev.c code
authorKarel Zak <kzak@redhat.com>
Fri, 30 Sep 2011 09:09:38 +0000 (11:09 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 30 Sep 2011 09:09:38 +0000 (11:09 +0200)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/loopdev.h
lib/loopdev.c
mount/Makefile.am
mount/umount.c

index 6136535e3b7bb71c75ddfe0dff7f70925c9a31f9..e25aa96b627fa44efa911ea74b28ed9188191f5e 100644 (file)
@@ -119,6 +119,7 @@ extern char *loopdev_find_by_backing_file(const char *filename,
                                          uint64_t offset, int flags);
 extern int loopcxt_find_unused(struct loopdev_cxt *lc);
 extern int loopdev_delete(const char *device);
+extern int loopdev_count_by_backing_file(const char *filename, char **loopdev);
 
 /*
  * Low-level
index 11555b0a84dfd9fa53e1c2ade300a5c62c71a7e1..f83a443a0c6c0e1ed224a6eed97d6c2e1fb5ece0 100644 (file)
@@ -1076,6 +1076,46 @@ char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, int fl
        return res;
 }
 
+/*
+ * Returns number of loop devices associated with @file, if only one loop
+ * device is associeted with the given @filename and @loopdev is not NULL then
+ * @loopdev returns name of the device.
+ */
+int loopdev_count_by_backing_file(const char *filename, char **loopdev)
+{
+       struct loopdev_cxt lc;
+       int count = 0;
+
+       if (!filename)
+               return -1;
+
+       loopcxt_init(&lc, 0);
+       if (loopcxt_init_iterator(&lc, LOOPITER_FL_USED))
+               return -1;
+
+       while(loopcxt_next(&lc) == 0) {
+               char *backing = loopcxt_get_backing_file(&lc);
+
+               if (!backing || strcmp(backing, filename)) {
+                       free(backing);
+                       continue;
+               }
+
+               free(backing);
+               if (loopdev && count == 0)
+                       *loopdev = loopcxt_strdup_device(&lc);
+               count++;
+       }
+
+       loopcxt_deinit(&lc);
+
+       if (loopdev && count > 1) {
+               free(*loopdev);
+               *loopdev = NULL;
+       }
+       return count;
+}
+
 
 #ifdef TEST_PROGRAM_LOOPDEV
 #include <errno.h>
index 8f4a4870f10bf02b5e72ec1c1580ca626d84ead9..052d63cdfeeb754e5b8b3e2e78937675cfc52448 100644 (file)
@@ -11,10 +11,20 @@ srcs_common = sundries.c $(top_srcdir)/lib/canonicalize.c sundries.h
 hdrs_mount = fstab.h mount_mntent.h mount_constants.h getusername.h
 
 # generic sources for mount and umount
-srcs_mount = fstab.c mount_mntent.c getusername.c devname.c devname.h \
-       $(srcs_common) $(hdrs_mount) $(top_srcdir)/lib/env.c \
-       $(top_srcdir)/lib/linux_version.c $(top_srcdir)/lib/blkdev.c \
-       $(top_srcdir)/lib/fsprobe.c $(top_srcdir)/lib/mangle.c
+srcs_mount =   $(srcs_common) $(hdrs_mount) \
+               fstab.c \
+               mount_mntent.c \
+               getusername.c \
+               devname.c devname.h \
+               $(top_srcdir)/lib/env.c \
+               $(top_srcdir)/lib/linux_version.c \
+               $(top_srcdir)/lib/blkdev.c \
+               $(top_srcdir)/lib/fsprobe.c \
+               $(top_srcdir)/lib/mangle.c \
+               $(top_srcdir)/lib/at.c \
+               $(top_srcdir)/lib/sysfs.c \
+               $(top_srcdir)/lib/loopdev.c \
+               $(top_srcdir)/lib/strutils.c
 
 # generic flags for all programs (except losetup)
 # -- note that pkg-config autoconf macros (pkg.m4) does not differentiate
@@ -24,20 +34,12 @@ ldadd_static =
 cflags_common = $(AM_CFLAGS)
 ldflags_static = -all-static
 
-mount_SOURCES = mount.c \
-               $(srcs_mount) \
-               $(top_srcdir)/lib/setproctitle.c \
-               $(top_srcdir)/lib/strutils.c \
-               $(top_srcdir)/lib/at.c \
-               $(top_srcdir)/lib/sysfs.c \
-               $(top_srcdir)/lib/loopdev.c
-
+mount_SOURCES = mount.c $(srcs_mount) $(top_srcdir)/lib/setproctitle.c
 mount_CFLAGS = $(SUID_CFLAGS) $(cflags_common)
 mount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
 mount_LDADD = $(ldadd_common)
 
-umount_SOURCES = umount.c $(srcs_mount) $(top_srcdir)/lib/strutils.c \
-                lomount.c lomount.h loop.h
+umount_SOURCES = umount.c $(srcs_mount)
 umount_CFLAGS = $(SUID_CFLAGS) $(cflags_common)
 umount_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
 umount_LDADD = $(ldadd_common)
index c7ebf7d3c863e51f556260cc6afd173a61a4936c..f29d7cafb8908ca6586a9f73e6f7493b8a17cdfc 100644 (file)
@@ -16,8 +16,7 @@
 #include "sundries.h"
 #include "getusername.h"
 #include "pathnames.h"
-#include "lomount.h"
-#include "loop.h"
+#include "loopdev.h"
 #include "fstab.h"
 #include "env.h"
 #include "nls.h"
@@ -299,7 +298,7 @@ umount_one (const char *spec, const char *node, const char *type,
         * Ignore the option "-d" for non-loop devices and loop devices with
         * LO_FLAGS_AUTOCLEAR flag.
         */
-       if (delloop && is_loop_device(spec))
+       if (delloop && is_loopdev(spec))
                myloop = 1;
 
        if (restricted) {
@@ -407,8 +406,8 @@ umount_one (const char *spec, const char *node, const char *type,
                        loopdev = spec;
        }
  gotloop:
-       if (loopdev && !is_loop_autoclear(loopdev))
-               del_loop(loopdev);
+       if (loopdev && !loopdev_is_autoclear(loopdev))
+               loopdev_delete(loopdev);
 
  writemtab:
        if (!nomtab &&
@@ -559,8 +558,8 @@ is_valid_loop(struct mntentchn *mc, struct mntentchn *fs)
        }
 
        /* check association */
-       if (loopfile_used_with((char *) mc->m.mnt_fsname,
-                               fs->m.mnt_fsname, offset) == 1) {
+       if (loopdev_is_used((char *) mc->m.mnt_fsname, fs->m.mnt_fsname,
+                               offset, LOOPDEV_FL_OFFSET) == 1) {
                if (verbose > 1)
                        printf(_("device %s is associated with %s\n"),
                               mc->m.mnt_fsname, fs->m.mnt_fsname);
@@ -644,20 +643,18 @@ try_loopdev:
         * (only if it is a regular file)
         */
        if (!mc && !loopdev && !stat(file, &statbuf) && S_ISREG(statbuf.st_mode)) {
-               switch (find_loopdev_by_backing_file(file, &loopdev)) {
-               case 0:
+               int count = loopdev_count_by_backing_file(file, &loopdev);
+
+               if (count == 1) {
                        if (verbose)
                                printf(_("%s is associated with %s\n"),
                                       arg, loopdev);
                        file = loopdev;
                        goto try_loopdev;
-                       break;
-               case 2:
-                       if (verbose)
-                               printf(_("%s is associated with more than one loop device: not unmounting\n"),
-                                      arg);
-                       break;
-               }
+
+               } else if (count > 1 && verbose)
+                       printf(_("%s is associated with more than one "
+                                "loop device: not unmounting\n"), arg);
        }
 
        if (mc) {