Check for multiple device mounts overridable by '-o force'.
[captive.git] / src / client / lufs / mount.captive.in
index cd2fa4d..1c4d1bc 100755 (executable)
@@ -38,7 +38,8 @@ $captive_sandbox_server_bin=~s#\$\Q{prefix}\E#'@prefix@';#ge;
 $captive_sandbox_server_bin="/usr/sbin/captive-sandbox-server" if $captive_sandbox_server_bin=~/^@/;
 
 
-my $fsname=($0=~m#([^/]*)$#)[0] or die "Cannot detetect my basename from: $0";
+my $ME=($0=~m#([^/]*)$#)[0] or die "Cannot detetect my basename from: $0";
+my $fsname=$ME;
 $fsname=~s/^mount[.]captive-// or die "Cannot detect captive filesystem module from my name: $fsname";
 my $image=shift @ARGV or die "Missing argv[1]: device or image pathname";
 -r $image or die "Image pathname not readable: $image";
@@ -76,30 +77,54 @@ if (!-r $ntoskrnl) {
 if ($fsname eq "ntfs") {
        for my $fh (*STDERR,*STDOUT) {
                if (-t $fh) {
-                       print $fh 'Captive NTFS v@VERSION@. Updates: http://www.jankratochvil.net/project/captive/'."\n";
+                       print $fh 'Captive NTFS v@VERSION@.  Check new version: http://www.jankratochvil.net/'."\n";
                        last;
                        }
                }
        }
 
 
-# Double-dashes are generally optional here.
-# Convert ',' to ';' to prevent its parsing by LUFS to keep its ordering
+# Keep @opt_captive ordering
 # to let the options be overridable by user (such as 'ro').
-$oo=";$oo;";
-$oo=~tr/,/;/;
-$oo=~s/;(ro|rw);/;--$1;/g;
+my @opt_captive=();
+my @opt_lufs=();
+my $opt_force;
+for (split /,/,$oo) {
+       $_="--$_" if $_ eq "ro" || $_ eq "rw";
+       $opt_force=1 if $_ eq "force";
+       push @opt_captive,$_ if  /^--/;
+       push @opt_lufs,$_    if !/^--/;
+       }
+
+if (!$opt_force) {
+       local *MTAB;
+       if (!open MTAB,"/etc/mtab") {
+               warn "Cannot open /etc/mtab: $!";
+               }
+       else {
+               local $/=undef();
+               my $mtab=<MTAB>;
+               close MTAB;
+               die ""
+                                               ."$ME: $image already mounted\n"
+                                               ."$ME: according to mtab, $image is mounted on $1\n"
+                                               ."$ME: Use '-o force' to mount the image notwithstanding.\n"
+                               if $mtab=~/^\Q$image\E\s+(\S+)/m;
+               }
+       }
+
 $oo=""
                ."mntent.mnt_fsname=$image,mntent.mnt_type=captive-$fsname,"    # Double-dashes forbidden.
                ."dir_cache_entries=0,"
                ."image=$image,"
+               .join(",",@opt_lufs).","
                ."captive_options="
                                .($fsname eq "cdfs" ? "--cdrom;--ro;" : "--disk;--rw;")
                                ."--load-module=$ntoskrnl;--filesystem=$filesystem;"
                                ."--sandbox-server=$captive_sandbox_server_bin;"
                                ."--bug-pathname=$vardir/bug-%FT%T.captivebug.xml.gz;"
                                ."--syslog;"
-                               .$oo;
+                               .join(";",@opt_captive);
 
 warn "$0: '-n' not supported - ignored" if $nomtab;