Brackup/Mount.pm untested

filter: Statementstmt 0.0% 0 / 111 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 186 CC 20 SCAR 52.1
CC
20
Cov
0%
mount
306.0
_build_metadata
20.0
BEGIN
2.0
CRAP 182.7
1  
2 package Brackup::Mount;
3  
4 # Note: This package and the brackup-mount utility that calls it
5 # both depend on Fuse. Fuse isn't a dependency of the Brackup
6 # distribution as a whole, so don't load this from anywhere else
7 # in Brackup.
8 use Fuse;
9  
10 use Brackup;
11 use Brackup::Restore;
12 use POSIX qw(ENOENT EISDIR EROFS ENOTDIR EBUSY EINVAL O_WRONLY O_RDWR);
13 use Fcntl qw(S_IFREG S_IFDIR S_IFLNK);
14 use File::Temp;
15 use IO::File;
16  
17 use strict;
18  
19 sub mount {
20 my ($class, $metafile, $mountpoint) = @_;
21  
22 my $p = Brackup::Metafile->open($metafile) or die "Failed to open metafile $metafile";
23  
24 my $header = $p->readline();
25 my $driver_header = Brackup::Restore::_driver_meta($header);
26  
27 my $driver_class = $header->{BackupDriver};
28 die "No driver specified" unless $driver_class;
29  
30 eval "use $driver_class; 1;" or die "Failed to load driver ($driver_class) to restore from: $@\n";
31  
32 my $target = "$driver_class"->new_from_backup_header($driver_header);
33  
34 my $meta = $class->_build_metadata($header, $p);
35  
36 my $tempdir = File::Temp::tempdir(CLEANUP => 1);
37 my $file_temp_path = sub {
38 my ($record) = @_;
39  
40 return $tempdir."/".$record->{digest};
41 };
42  
43 return Fuse::main(
44 mountpoint => $mountpoint,
45 mountopts => "",
46 threaded => 0,
47 debug => 0,
48  
49 getattr => sub {
50 my ($path) = @_;
51 my $record = $meta->{$path};
52 return -ENOENT unless $record;
53  
54 return (
55 0, # device number (?)
56 0, # inode
57 $record->{mode},
58 0, # nlink
59 $<, # uid
60 0, # gid
61 0, # rdev
62 $record->{size},
63 $record->{atime},
64 $record->{mtime},
65 $record->{mtime}, # ctime
66 1024, # blocksize
67 1, # blocks
68 );
69 },
70  
71 getdir => sub {
72 my ($path) = @_;
73  
74 my $record = $meta->{$path};
75 return -ENOENT unless $record;
76 return -ENOTDIR unless $record->{type} eq 'd';
77  
78 return ('..', @{$record->{child_nodes}}, 0);
79 },
80  
81 readlink => sub {
82 my ($path) = @_;
83  
84 my $record = $meta->{$path};
85 return -ENOENT unless $record;
86  
87 return $record->{link} || 0;
88 },
89  
90 open => sub {
91 my ($path, $mode) = @_;
92  
93 my $record = $meta->{$path};
94 return -ENOENT unless $record;
95 return -EISDIR if $record->{type} eq 'd';
96 return -EROFS if ($mode & O_WRONLY) || ($mode & O_RDWR);
97  
98 # Fetch the data relating to this file to a local
99 # file and open it.
100  
101 unless ($record->{fh}) {
102 my $fn = $file_temp_path->($record);
103  
104 # HACK: Do a bit of grovelling in Brackup::Restore's innards.
105 # We want to restore the file, and the process is quite involved,
106 # so we call Brackup::Restore::_restore_file in a kinda wacky
107 # way to trick it into putting the file where we want it.
108 my $fake_object = bless {
109 _target => $target,
110 _meta => $header,
111 }, 'Brackup::Restore';
112 Brackup::Restore::_restore_file($fake_object, $fn, $record->{meta});
113  
114 my $fh = IO::File->new($fn, '<');
115 $record->{fh} = $fh;
116 }
117 $record->{opencount}++;
118  
119 return 0;
120 },
121  
122 read => sub {
123 my ($path, $size, $offset) = @_;
124  
125 my $record = $meta->{$path};
126 return -ENOENT unless $record;
127 return -EISDIR if $record->{type} eq 'd';
128  
129 return -EBUSY unless $record->{opencount} > 0;
130  
131 my $fh = $record->{fh};
132  
133 my $buf = "";
134 $fh->seek($offset, 0);
135 my $amount_read = $fh->read($buf, $size);
136 my $err = -$!;
137  
138 if (defined $amount_read) {
139 return $buf;
140 }
141 else {
142 return $err+0;
143 }
144 },
145  
146 release => sub {
147 my ($path, $mode) = @_;
148  
149 # A previously-opened file has been closed.
150  
151 my $record = $meta->{$path};
152 return -ENOENT unless $record;
153  
154 my $fh = $record->{fh};
155 return -EINVAL unless $fh;
156  
157 $record->{opencount}--;
158  
159 if ($record->{opencount} == 0) {
160 close($fh);
161 unlink($file_temp_path->($record));
162 $record->{fh} = undef;
163 }
164 },
165  
166 statfs => sub {
167 return (1024, scalar(keys %$meta)+0, 0, 0, 0, 1024);
168 },
169 );
170 }
171  
172 sub _build_metadata {
173 my ($class, $header, $p) = @_;
174  
175 my %meta = ();
176  
177 while (my $it = $p->readline) {
178 my $path = $it->{Path};
179 my $type = $it->{Type} || 'f';
180 my $mode = oct($it->{Mode} || ($type eq 'd' ? $header->{DefaultDirMode} : $header->{DefaultFileMode}));
181  
182 # Add the type to the mode
183 $mode |= ({
184 d => S_IFDIR,
185 f => S_IFREG,
186 l => S_IFLNK,
187 }->{$type} || 0);
188  
189 my $record = {
190 path => $path,
191 type => $type,
192 mode => $mode,
193 size => $it->{Size},
194 mtime => $it->{Mtime},
195 atime => $it->{Atime},
196 link => $it->{Link},
197 child_nodes => [],
198 fh => undef,
199 digest => $it->{Digest},
200 opencount => 0,
201 meta => $it,
202 };
203  
204 my $parent_path = undef;
205 my $local_name = undef;
206  
207 if ($path =~ m!^(.+)/([^/]+)$!) {
208 $parent_path = '/'.$1;
209 $local_name = $2;
210 }
211 else {
212 $parent_path = '/';
213 $local_name = $path;
214 }
215  
216 my $path_key = $path eq '.' ? '/' : '/'.$path;
217  
218 $meta{$path_key} = $record;
219 push @{$meta{$parent_path}{child_nodes}}, $local_name;
220 }
221  
222 return \%meta;
223 }
224  
225 =head1 NAME
226  
227 Brackup::Mount - Mount a backup as a usable filesystem using FUSE
228  
229 =cut
230  
231 1;