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
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>
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
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)
#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"
* 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) {
loopdev = spec;
}
gotloop:
- if (loopdev && !is_loop_autoclear(loopdev))
- del_loop(loopdev);
+ if (loopdev && !loopdev_is_autoclear(loopdev))
+ loopdev_delete(loopdev);
writemtab:
if (!nomtab &&
}
/* 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);
* (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) {