File: Device.pm

package info (click to toggle)
clamtk 4.27-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,796 kB
  • ctags: 73
  • sloc: perl: 3,554; makefile: 11
file content (363 lines) | stat: -rw-r--r-- 11,868 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# ClamTk, copyright (C) 2004-2010 Dave M
#
# This file is part of ClamTk.
#
# ClamTk is free software; you can redistribute it and/or modify it
# under the terms of either:
#
# a) the GNU General Public License as published by the Free Software
# Foundation; either version 1, or (at your option) any later version, or
#
# b) the "Artistic License".
package ClamTk::Device;
# We can't do realtime monitoring of devices
# (like an option to popup an alert when a device is plugged in)
# because we'd need Net::DBus::GLib - not available on many
# distros. Although, Debian finally got it... :)

use strict;
#use warnings;
$|++;

use Glib qw/TRUE FALSE/;
use Gtk2 '-init';

use ClamTk::GUI;

use encoding 'utf8';

use Locale::gettext;
use POSIX qw/locale_h/;
textdomain('clamtk');
setlocale( LC_MESSAGES, '' );
bind_textdomain_codeset( 'clamtk', 'UTF-8' );

# $devs = holds device information
my $devs;

my $udi = '';

sub look_for_device {
    my $dwin = Gtk2::Window->new;
    $dwin->set_title( 'ClamTk ' . gettext('Virus Scanner') );

    # Can't do size_request here because the window ($dwin)
    # won't expand based on # of devices found

    $dwin->signal_connect( destroy => sub { $dwin->destroy } );
    if ( -e '/usr/share/pixmaps/clamtk.png' ) {
        $dwin->set_default_icon_from_file('/usr/share/pixmaps/clamtk.png');
    } elsif ( -e '/usr/share/pixmaps/clamtk.xpm' ) {
        $dwin->set_default_icon_from_file('/usr/share/pixmaps/clamtk.xpm');
    }
    my $dbox = Gtk2::VBox->new( FALSE, 5 );
    $dwin->add($dbox);
    Gtk2->main_iteration while ( Gtk2->events_pending );

    # $count = for iterating through $devs
    my $count = 0;

    # $mount_point holds the (wait for it) mount point
    # info and returns it for scanning
    my $mount_point;

    # The following are for images/language purposes
    my $cd_label     = gettext('CD/DVD');
    my $usb_label    = gettext('USB device');
    my $floppy_label = gettext('Floppy disk');

    # In a perlfect world, we would use Net::DBus for this.
    # Of course, CentOS doesn't HAVE it... grrrr...

    my %hash = get_lshal();

    for my $d ( keys %hash ) {
        while ( my ( $t, $u ) = each %{ $hash{$d} } ) {
            # See if there's anything mounted.
            $mount_point = $hash{$d}{'volume.mount_point'};

            # Grab its parent device.
            my $parent = $hash{$d}{'info.parent'};

            # First we'll see if it's a CD or DVD.
            # I'd like to ignore ISOs due to size, but
            # I'm not sure we can do that.
            if ( $hash{$parent}{'storage.drive_type'} eq 'cdrom' ) {
                next unless $mount_point;
                next unless ( no_dupes($mount_point) );
                $devs->{$count}->{'device'} = $cd_label;
                $devs->{$count}->{'label'}  = $hash{$d}->{'volume.label'};
                $devs->{$count}->{'mount'}  = $mount_point;
                $count++;
                next;
            }

            # Now see if it's a USB flash device. This is
            # tricky but always seems to work (for me :).
            # The 'storage.removable' field seems to be the
            # difference between a flash drive and a usb hard drive:
            # 'true' eq flash_drive, 'false' eq usb hard drive
            elsif ( $hash{$d}{'info.udi'} =~ /volume/ ) {
                if (   $hash{$parent}{'storage.removable'} eq 'true'
                    && $hash{$parent}{'storage.bus'} eq 'usb' )
                {
                    next unless $mount_point;
                    next unless ( no_dupes($mount_point) );

                    $devs->{$count}->{'device'} = $usb_label;
                    $devs->{$count}->{'label'} =
                        $hash{$parent}{'storage.vendor'};
                    $devs->{$count}->{'mount'} = $mount_point;

                    $devs->{$count}->{'label'} ||= $hash{$d}{'info.product'};

                    next
                        unless ( $devs->{$count}->{'label'}
                        and $devs->{$count}->{'mount'} );
                    $count++;
                }
            }
            # And finally, floppies, assuming anyone uses them.
            # The problem is that the section that shows them
            # identifies them as floppies, but doesn't give a mount.
            elsif ( $hash{$d}{'storage.drive_type'} eq 'floppy' ) {
                my $device = $hash{$d}{'block.device'};
                chomp($device);
                next unless ($device);

                # Now we cheat. Hopefully /proc/mounts has it,
                # so just look for the line with the block
                # device and grab the mount point.
                open( my $P, '<', '/proc/mounts' ) or next;
                while (<$P>) {
                    # $_ will look like this:
                    # /dev/fd0	/media/floppy	vfat	...
                    # We just need the first two values:
                    my ( $dev, $mount ) = ( split(/\s+/) )[ 0, 1 ];
                    if ( $dev eq $device ) {
                        $mount_point = $mount;
                        last;
                    }
                }
                close($P);

                # If we didn't match the device
                # and mount point, just leave
                next unless ($mount_point);

                # Ensure there are no dupes
                if ( no_dupes($mount_point) ) {

                    my $label = $hash{$d}{'info.product'};
                    chomp($label);
                    $label ||= $hash{$d}{'storage.vendor'};
                    chomp($label);
                    $label ||= '';
                    $devs->{$count}->{'device'} = $floppy_label;
                    $devs->{$count}->{'label'}  = $label;
                    $devs->{$count}->{'mount'}  = $mount_point;
                    $count++;
                } else {
                    next;
                }
            } else {
                next;
            }
        }
    }

    # Return unless we have devices.
    # This could get confusing for users not
    # understanding the 'mount' thing.
    if ( !scalar( keys %$devs ) ) {
        my $mount_msg = gettext('No devices were found.');
        $mount_msg .= "\n\n";
        $mount_msg
            .= gettext(
            'If you have connected a device, you may need to mount it first.'
            );
        show_message_dialog( $dwin, 'info', 'ok', gettext($mount_msg) );
        $dwin->destroy;
        return 0;
    }

    my $tt = Gtk2::Tooltips->new();

    my $num = scalar( keys %$devs );

    my $dframe = Gtk2::Frame->new( gettext('Devices available') );
    $dbox->pack_start( $dframe, TRUE, TRUE, 0 );
    $dframe->set_border_width(5);
    $dframe->set_shadow_type('etched-in');

    # This is much better as a tooltip...
    # an extra label skews the display.
    $tt->set_tip( $dframe, gettext('Select a device or press Cancel') );

    # This buttonbox will hold the devices found.
    my $m_bbox = Gtk2::HButtonBox->new;
    $dframe->add($m_bbox);
    $m_bbox->set_layout('start');
    $m_bbox->set_border_width(10);

    # Get the information from the devices for display
    for ( 0 .. $num ) {
        my ($label) = $devs->{$_}->{'label'};
        next if ( !$label );
        my ($mount)  = $devs->{$_}->{'mount'};
        my ($device) = $devs->{$_}->{'device'};

        # We'll use the same technique for images -
        # stock gtk2 icons, but the label is customized
        my $gui_img = Gtk2::Image->new_from_stock(
              ( $device eq $cd_label )     ? 'gtk-cdrom'
            : ( $device eq $usb_label )    ? 'gtk-harddisk'
            : ( $device eq $floppy_label ) ? 'gtk-floppy'
            : '',
            'small-toolbar'
        );
        my $gui_btn = Gtk2::Button->new();
        $gui_btn->set_property( 'image' => $gui_img );
        $gui_btn->set_label($label);
        $gui_btn->set_relief('half');
        $tt->set_tip( $gui_btn, "$device ($mount)" );
        $m_bbox->add($gui_btn);
        $gui_btn->signal_connect(
            clicked => sub {
                $dwin->destroy;
                ClamTk::GUI->getfile( 'device', $mount );
            }
        );
    }

    # This hbuttonbox holds the cancel button.
    # A future option might be 'Help'
    my $bottom_row = Gtk2::HButtonBox->new();
    $dbox->pack_start( $bottom_row, FALSE, FALSE, 5 );
    $bottom_row->set_layout('end');
    $bottom_row->set_border_width(5);

    # Cancel button for devices
    my $cancel = Gtk2::Button->new_from_stock('gtk-cancel');
    $bottom_row->add($cancel);
    $cancel->signal_connect(
        clicked => sub {
            $dwin->destroy;
            return 0;
        }
    );
    # Give the cancel button the focus;
    # looks better than when a device has it.
    # It also allows the user to press enter
    # to kill the window.
    $cancel->grab_focus();

    # If there's only one device, set the size of the window.
    # Otherwise it's too small.
    if ( $num == 1 ) {
        $dwin->set_size_request( 300, 150 );
        $dwin->queue_draw();
    }

    $dwin->show_all();
    return;
}

sub no_dupes {
    # Accepts the mount_point (mp) to search.
    # Returns TRUE (1) if no_dupes are found;
    # Otherwise, returns FALSE (0) if they are.
    # Confused? In other words, 
    # TRUE  = there are no dupes 
    # FALSE = there ARE dupes
    my $mp = shift;
    # A quick look to ensure we haven't seen this before.
    my $keys  = scalar keys %$devs;
    my $total = 0;
    if ($keys) {
        for ( 0 .. $keys - 1 ) {
            $total++ if ( $devs->{$_}->{'mount'} eq $mp );
        }
    }
    if ($total) {
        return 0;
    } else {
        return 1;
    }
}

sub get_lshal {
    my %gather;

    my $cmd = qx| which lshal |;
    chomp($cmd);
    # This return is almost worthless. If you don't have lshal
    # installed, $cmd = 'which: no lshal in (blah:/usr/blah)'
    # and "*** unhandled exception in callback:".  But this way,
    # we return and say we didn't find anything. Much neater.
    return unless ( -e $cmd );

    my $pid = open( my $LIST, "-|", $cmd );
    defined($pid) or die "couldn't fork! $!\n";

    while ( defined( my $line = <$LIST> ) ) {
        next if ( $line =~ /^Dumping/ );
        next if ( $line =~ /^\-/ );
        next if ( $line =~ /^\s*$/ );
        chomp($line);

        my ( $key, $value ) = split( /=/, $line );
        next unless ( defined($key) );
        next unless ( defined($value) );
        chomp( $key, $value );

        $key =~ s/^\s+//g;
        $key =~ s/\s+$//g;
        $key =~ s/^\'//;
        $key =~ s/\'$//;

        if ( $key eq 'udi' ) {
            $udi = $value;
            $udi =~ s/^\s+//g;
            $udi =~ s/^\'//;
            $udi =~ s/\s+$//g;
            $udi =~ s/\'$//;
        } else {
            # This is a pain... storage.removable returns
            # 'true' or 'false', which requires a different regex
            # because of the single quotes we normally look for.
            # storage.removable = true  (bool)
            # vs.
            # info.product = 'Computer'  (string)
            if ( $key eq 'storage.removable' ) {
                if ( $value =~ /\s+(.*?)\s+/ ) {
                    $gather{$udi}{$key} = $1;
                }
            } elsif ( $value =~ /'(.*?)'/ ) {
                $gather{$udi}{$key} = $1;
            }
        }
    }
    close($LIST);
    return %gather;
}

sub show_message_dialog {
    my ( $parent, $type, $button, $message ) = @_;
    # $parent = $dwin
    # $type = info, warning, error, question
    # $button = ok, ok-cancel, close, ...
    # $message = <a message>

    my $dialog;
    $dialog =
        Gtk2::MessageDialog->new_with_markup( $parent,
        [qw(modal destroy-with-parent)],
        $type, $button, $message );

    $dialog->run;
    $dialog->destroy;
    return;
}

1;