Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / client / lufs / mount.captive.in
1 #! /usr/bin/perl -w
2
3 # $Id$
4 # External mount command for mount(8) to interface lufsd(1)
5 # Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; exactly version 2 of June 1991 is required
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 use strict;
22 use Carp qw(cluck confess);
23 use Getopt::Long;
24 # Do not use any non-core Perl modules here.
25 # Be perl-5.0 compatible (is it needed if we require Linux kernel >= 2.4 ?)
26
27
28 my $vardir='@localstatedir@/lib/captive';
29 $vardir=~s#\$\Q{prefix}\E#'@prefix@';#ge;
30 $vardir="/var/lib/captive" if $vardir=~/^@/;
31 my $lufsd_bin='@PATH_LUFSD@';
32 $lufsd_bin="/usr/bin/lufsd" if $lufsd_bin=~/^@/;
33 my $captive_sandbox_server_bin='@libexecdir@/captive-sandbox-server';
34 $captive_sandbox_server_bin=~s#\$\Q{exec_prefix}\E#'@exec_prefix@';#ge;
35 $captive_sandbox_server_bin=~s#\$\Q{prefix}\E#'@prefix@';#ge;
36 $captive_sandbox_server_bin="/usr/libexec/captive-sandbox-server" if $captive_sandbox_server_bin=~/^@/;
37
38
39 my $ME=($0=~m#([^/]*)$#)[0] or die "Cannot detetect my basename from: $0";
40 my $fsname=$ME;
41 $fsname=~s/^mount[.]captive-// or die "Cannot detect captive filesystem module from my name: $fsname";
42 my $image=shift @ARGV or die "Missing argv[1]: device or image pathname";
43 -r $image or die "Image pathname not readable: $image";
44 warn "warning: '-o loop' is not neccessary for Captive filesystem mounts"
45                 if $image=~m#^/dev/loop#;
46 my $dir=shift @ARGV or die "Missing argv[2]: mount point directory";
47 -d $dir or die "Mount point directory not a valid directory: $dir";
48 shift @ARGV if my $nomtab =$ARGV[0] && $ARGV[0] eq "-n";
49 shift @ARGV if my $verbose=$ARGV[0] && $ARGV[0] eq "-v";
50 my $oo="";
51 do { shift @ARGV; $oo=shift @ARGV; } if $ARGV[0] && $ARGV[0] eq "-o";
52 die "Excessive arguments: @ARGV" if @ARGV;
53
54 sub die_install
55 {
56         die
57 "You should run captive-install-acquire(1) of 'captive-install' package,
58 otherwise you can also acquire this file from URL:
59 http://www.microsoft.com/WindowsXP/pro/downloads/servicepacks/sp1/checkedbuild.asp
60 ";
61 }
62
63 my $filesystem=$vardir."/".$fsname.".sys";
64 if (!-r $filesystem) {
65         warn "W32 filesystem .sys module not found: $filesystem";
66         die_install();
67         }
68 my $ntoskrnl=$vardir."/ntoskrnl.exe";
69 if (!-r $ntoskrnl) {
70         warn "W32 ntoskrnl.exe not found: $ntoskrnl";
71         die_install();
72         }
73
74 # Keep @opt_captive ordering
75 # to let the options be overridable by user (such as 'ro').
76 my @opt_captive=();
77 my @opt_lufs=();
78 my $opt_force;
79 my $opt_rwmode="--blind";
80 for (split /,/,$oo) {
81         $_="--$_" if $_ eq "ro" || $_ eq "rw";
82         $opt_rwmode=$_ if /^--(?:ro|rw|blind)$/;
83         $opt_force=1 if $_ eq "force";
84         push @opt_captive,$_ if  /^--/;
85         push @opt_lufs,$_    if !/^--/;
86         }
87
88 # Shameless advertisement:
89 if ($fsname eq "ntfs") {
90         for my $fh (*STDERR,*STDOUT) {
91                 if (-t $fh) {
92                         print $fh 'Captive NTFS v@VERSION@.  Check a new version at: http://www.jankratochvil.net/'."\n";
93                         last;
94                         }
95                 }
96         }
97
98 if (!$opt_force) {
99         local *MTAB;
100         if (!open MTAB,"/etc/mtab") {
101                 warn "Cannot open /etc/mtab: $!";
102                 }
103         else {
104                 local $/=undef();
105                 my $mtab=<MTAB>;
106                 close MTAB;
107                 die ""
108                                                 ."$ME: $image already mounted\n"
109                                                 ."$ME: according to mtab, $image is mounted on $1\n"
110                                                 ."$ME: Use '-o force' to mount the image notwithstanding.\n"
111                                 if $mtab=~/^\Q$image\E\s+(\S+)/m;
112                 }
113         }
114
115 $oo=""
116                 ."fs=captivefs,"
117                 ."mntent.mnt_fsname=$image,mntent.mnt_type=captive-$fsname,"    # Double-dashes forbidden.
118                 ."dir_cache_entries=0,"
119                 ."image=$image,"
120                 .join(",",@opt_lufs).","
121                 ."captive_options="
122                                 .($fsname eq "cdfs" ? "--cdrom;--ro;" : "--disk;--rw;")
123                                 ."--load-module=$ntoskrnl;--filesystem=$filesystem;"
124                                 ."--sandbox-server=$captive_sandbox_server_bin;"
125                                 ."--bug-pathname=$vardir/bug-%FT%T.captivebug.xml.gz;"
126                                 ."--syslog;"
127                                 .join(";",@opt_captive);
128
129 warn "$0: '-n' not supported - ignored" if $nomtab;
130
131 # Use "'$oo'" to permit another expansion by bash(1) during lufsmnt(8) execution.
132 my @argv=($lufsd_bin,"none",$dir,"-o",$oo);
133
134 print STDERR "$0: @argv\n" if $verbose;
135 exec $lufsd_bin @argv;
136 die "Failed to execute: @argv";