1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # PTP filesystem for Midnight Commander using gphoto2 |
---|
4 | # |
---|
5 | # (c) 2016 Vadim Druzhin <cdslow@mail.ru> |
---|
6 | # |
---|
7 | # Public Domain with NO WARRANTY |
---|
8 | |
---|
9 | use warnings; |
---|
10 | use strict; |
---|
11 | use English '-no_match_vars'; |
---|
12 | |
---|
13 | my $GP = 'gphoto2'; |
---|
14 | my $MAX_IMAGE_NUMBER = 16384; |
---|
15 | |
---|
16 | $ENV{LC_ALL} = 'C'; |
---|
17 | |
---|
18 | my @MONTHS = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; |
---|
19 | my %MON_IDX; |
---|
20 | |
---|
21 | foreach(@MONTHS) |
---|
22 | { $MON_IDX{$_} = keys(%MON_IDX) + 1; } |
---|
23 | |
---|
24 | sub list_file |
---|
25 | { |
---|
26 | my($dir, $name, $size, $y, $m, $d, $t) = @_; |
---|
27 | my $tm = sprintf '%02d-%02d-%04d %s', $MON_IDX{$m}, $d, $y, $t; |
---|
28 | print "-rw-r--r-- 1 root root ${size} ${tm} ${dir}/${name}\n"; |
---|
29 | return; |
---|
30 | } |
---|
31 | |
---|
32 | sub count_files |
---|
33 | { |
---|
34 | defined $MAX_IMAGE_NUMBER and return $MAX_IMAGE_NUMBER; |
---|
35 | |
---|
36 | local $_ = undef; |
---|
37 | |
---|
38 | my $cmd = "${GP} -L"; |
---|
39 | open my $in, q{-|}, $cmd or die "${cmd}: ${ERRNO}\n"; |
---|
40 | |
---|
41 | my $count = 0; |
---|
42 | while(<$in>) |
---|
43 | { |
---|
44 | /^#(\d+)/sx and $count = $1; |
---|
45 | } |
---|
46 | |
---|
47 | close $in; |
---|
48 | |
---|
49 | return $count; |
---|
50 | } |
---|
51 | |
---|
52 | sub list |
---|
53 | { |
---|
54 | local $_ = undef; |
---|
55 | |
---|
56 | my $cmd = undef; |
---|
57 | |
---|
58 | if(!defined $ENV{MC_TEST_EXTFS_LIST_CMD}) |
---|
59 | { |
---|
60 | my $count = count_files; |
---|
61 | $count == 0 and return; |
---|
62 | $cmd = "${GP} --show-info 1-${count} 2> /dev/null"; |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | $cmd = $ENV{MC_TEST_EXTFS_LIST_CMD}; |
---|
67 | } |
---|
68 | |
---|
69 | open my $in, q{-|}, $cmd or die "${cmd}: ${ERRNO}\n"; |
---|
70 | |
---|
71 | my($name, $dir, $size); |
---|
72 | my($y, $m, $d, $t); |
---|
73 | my $file_flag = 0; |
---|
74 | |
---|
75 | while(<$in>) |
---|
76 | { |
---|
77 | if(m{^Information on file '([^']+)' [(]folder '/?([^']+)'[)]:}s) |
---|
78 | { |
---|
79 | $name = $1; |
---|
80 | $dir = $2; |
---|
81 | } |
---|
82 | elsif(/^File:/sx) |
---|
83 | { $file_flag = 1; } |
---|
84 | elsif($file_flag && /^\s+Size:\s+(\d+)\s+byte[(]s[)]/sx) |
---|
85 | { $size = $1; } |
---|
86 | elsif($file_flag && /^\s+Time:\s+\S+\s+(\S+)\s+(\d+)\s+(\d{2}:\d{2}:\d{2})\s+(\d+)/sx) |
---|
87 | { |
---|
88 | $m = $1; |
---|
89 | $d = $2; |
---|
90 | $t = $3; |
---|
91 | $y = $4; |
---|
92 | } |
---|
93 | elsif($file_flag && /^\S/sx) |
---|
94 | { |
---|
95 | $file_flag = 0; |
---|
96 | list_file($dir, $name, $size, $y, $m, $d, $t); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | close $in; |
---|
101 | |
---|
102 | return; |
---|
103 | } |
---|
104 | |
---|
105 | sub copyout |
---|
106 | { |
---|
107 | my($from, $to) = @_; |
---|
108 | my @cmd = ($GP, '--get-file', $from, '--filename', $to, '--quiet'); |
---|
109 | exec @cmd or die join(q{ }, @cmd).": ${ERRNO}\n"; |
---|
110 | } |
---|
111 | |
---|
112 | sub rm |
---|
113 | { |
---|
114 | my($file) = @_; |
---|
115 | my @cmd = ($GP, '--delete-file', $file, '--quiet'); |
---|
116 | exec @cmd or die join(q{ }, @cmd).": ${ERRNO}\n"; |
---|
117 | } |
---|
118 | |
---|
119 | if($ARGV[0] eq 'list') |
---|
120 | { list; } |
---|
121 | elsif($ARGV[0] eq 'copyout') |
---|
122 | { copyout($ARGV[2], $ARGV[3]); } |
---|
123 | elsif($ARGV[0] eq 'rm') |
---|
124 | { rm($ARGV[2]); } |
---|
125 | else |
---|
126 | { exit 1; } |
---|
127 | |
---|
128 | 1; |
---|