summaryrefslogtreecommitdiff
path: root/hal-automounter.pl
blob: 20ab56359285e3b874a31ef32a0390cb470f502d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/home/dakkar/perl5/perlbrew/perls/perl-5.16.0/bin/perl 
use strict;
use warnings;
use IO::Socket::INET; # for locking 
use Net::DBus;
use Net::DBus::Reactor;
use File::Temp;
use Cwd 'realpath';
 
my $lock=IO::Socket::INET->new(LocalAddr=>'127.0.0.1',LocalPort=>9999,Type=>SOCK_STREAM,Listen=>1)
    or die 'already running';
 
sub add_to_fstab {
    my ($dev_path,$mount_point,$fs_type,$opts)=@_;
 
    my @orig_stat=stat('/etc/fstab');
    my $tfh=File::Temp->new(DIR=>'/etc');
    $tfh->unlink_on_destroy(1);
 
    while(1) {
        seek $tfh,0,0;
        truncate $tfh,0;
        open my $in_fh,'<','/etc/fstab';
        while (my $line=<$in_fh>) {
            next if $line =~ m{\A \s\#}smx;
            my ($in_dev,$in_path,$in_fs,$in_opts)=
                ($line=~m{\A \s* (\S+) \s+ (\S+) \s+ (\S+) \s+ (\S+) }smx)
                    or next;
            if (-e $in_dev
                    && (realpath($in_deveq realpath($dev_path))) {
                # it's already in there, abort 
                return $in_path;
            }
        }
        continue {
            print {$tfh$line;
        };
        print {$tfh"$dev_path $mount_point $fs_type $opts 0 0 # dakkar\n";
        if ((stat('/etc/fstab'))[9] == $orig_stat[9]) {
            rename "$tfh",'/etc/fstab';
            chmod $orig_stat[2],'/etc/fstab';
            chown @orig_stat[4,5],'/etc/fstab';
            return;
        }
        # fstab has changed, re-run the whole thing 
    }
}
 
sub remove_from_fstab {
    my ($mount_point)=@_;
 
    my @orig_stat=stat('/etc/fstab');
    my $tfh=File::Temp->new(DIR=>'/etc');
    $tfh->unlink_on_destroy(1);
 
    my $was_there=0;
 
    while(1) {
        seek $tfh,0,0;
        truncate $tfh,0;
        open my $in_fh,'<','/etc/fstab';
        while (my $line=<$in_fh>) {
            next if $line =~ m{\A \s\#}smx;
            my ($in_dev,$in_path,$in_fs,$in_opts,$comms)=
                ($line=~m{\A \s* (\S+) \s+ (\S+) \s+ (\S+) \s+ (\S+) \s\d\s\d+ (?: \s\# (.*) )?}smx);
            if (defined($comms) && ($comms eq " dakkar\n")
                    && ($in_path eq $mount_point)) {
                # ok, it's the right line, kill it 
                $line='';$was_there=1;
            }
        }
        continue {
            print {$tfh$line;
        };
        if ((stat('/etc/fstab'))[9] == $orig_stat[9]) {
            rename "$tfh",'/etc/fstab';
            chmod $orig_stat[2],'/etc/fstab';
            chown @orig_stat[4,5],'/etc/fstab';
            return 1 if $was_there;
            return;
        }
        # fstab has changed, re-run the whole thing 
    }
}
 
my $bus=Net::DBus->system();
my $udisks=$bus->get_service('org.freedesktop.UDisks');
 
my %mounted;
 
sub sanitize {
    my ($path)=@_;
 
    $path=~s{\s}{-}g;
    $path=~s{/+}{_}g;
 
    return $path;
}
 
sub safe_get_property {
    my ($dev,$prop)=@_;
 
    local $@;
    return eval { $dev->$prop };
}
 
sub device_added {
    my ($node,$dev) = @_;
 
    $dev ||= $udisks->get_object($node,'org.freedesktop.UDisks.Device');
 
    return unless safe_get_property($dev,'IdUsage'eq 'filesystem';
    #return unless safe_get_property($dev,'DeviceIsRemovable'); 
    return unless safe_get_property($dev,'DeviceIsMediaAvailable');
    return if safe_get_property($dev,'DeviceIsMounted');
 
    return if exists $mounted{$node};
 
    my $uuid=safe_get_property($dev,'IdUuid');
    my $dev_path=safe_get_property($dev,'DeviceFile')
        ||($uuid?'/dev/disk/by-uuid/'.$uuid:undef)
        || 'unknown';
 
    if ($dev_path eq 'unknown') {
        warn "unknown path for $node\n";
        return;
    }
 
    my $label=safe_get_property($dev,'IdLabel');
    my $fstype=safe_get_property($dev,'IdType');
 
    return unless defined $fstype;
 
    my $mountpoint='/mnt/'.(sanitize($label)||$uuid);
 
    if (-e $mountpoint) {
        $mountpoint.='-0';
        while (-e $mountpoint) {++$mountpoint};
    }
 
    my $manual_mountpoint=add_to_fstab($dev_path,$mountpoint,$fstype,'users,noauto,noatime,nodiratime,nosuid,nodev');
 
    $mountpoint=$manual_mountpoint if defined $manual_mountpoint;
 
    $mounted{$node}=$mountpoint;
    mkdir $mountpoint;
    chown -1,scalar(getgrnam('plugdev')),$mountpoint;
    chmod 0775,$mountpoint;
 
    return;
}
 
sub device_removed {
    my ($node,$dev) = @_;
 
    if (exists $mounted{$node}) {
        system('umount',$mounted{$node});
        if (remove_from_fstab($mounted{$node})) {
            rmdir $mounted{$node};
        }
        delete $mounted{$node};
    }
    return;
}
 
sub device_changed {
    my ($node) = @_;
 
    my $dev=$udisks->get_object($node,'org.freedesktop.UDisks.Device');
    my $has_media = safe_get_property($dev,'DeviceIsMediaAvailable');
 
    if ($has_media) {
        device_added($node,$dev);
    }
    else {
        device_removed($node,$dev);
    }
}
 
my $reactor=Net::DBus::Reactor->main();
 
my $manager = $udisks->get_object('/org/freedesktop/UDisks',
                                  'org.freedesktop.UDisks');
 
$manager->connect_to_signal('DeviceAdded',\&device_added);
$manager->connect_to_signal('DeviceChanged',\&device_changed);
$manager->connect_to_signal('DeviceRemoved',\&device_removed);
 
$reactor->run();