| 1 | # | ||
| 2 | # Fuse::Filesys::Virtual::FSWrapper | ||
| 3 | # | ||
| 4 | # | ||
| 5 | |||
| 6 | package Fuse::Filesys::Virtual::FSWrapper; | ||
| 7 | |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Fuse::Filesys::Virtual::FSWrapper - Filesys::Virtual wrapper | ||
| 14 | |||
| 15 | =head1 VERSION | ||
| 16 | |||
| 17 | Version 0.02 | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | our $VERSION = '0.02'; | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | Internal module for Fuse::Filesys::Virtual, provides some functions | ||
| 26 | to Filesys::Virtual object. | ||
| 27 | |||
| 28 | use Fuse::Filesys::Virtual::FSWrapper; | ||
| 29 | |||
| 30 | my $fs = Filesys::Virtual::Foo->new(); | ||
| 31 | my $wfs = Fuse::Filesys::Virtual::FSWrapper->new($fs); | ||
| 32 | |||
| 33 | ... | ||
| 34 | |||
| 35 | $wfs->rename("/path/to/oldname", "path/to/newname"); | ||
| 36 | |||
| 37 | =head1 EXPORT | ||
| 38 | |||
| 39 | Nothing. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 44 | 0 | ▶ | use Filesys::Virtual; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 45 | use Fcntl ':mode'; | ||
| 46 | use POSIX (); | ||
| 47 | |||
| 48 | our $AUTOLOAD; | ||
| 49 | |||
| 50 | # | ||
| 51 | # get entries | ||
| 52 | # (\@dirs, \@files); | ||
| 53 | # | ||
| 54 | sub _list_recurse { | ||
| 55 | my $self = shift; | ||
| 56 | my ($cur) = @_; | ||
| 57 | |||
| 58 | my @dirs; | ||
| 59 | my @files; | ||
| 60 | |||
| 61 | if ($self->test('d', $cur)) { | ||
| 62 | my @entries = | ||
| 63 | grep { $_ ne '.' && $_ ne '..' } $self->list($cur); | ||
| 64 | |||
| 65 | for my $e (@entries) { | ||
| 66 | my $path = ($cur =~ /\/$/) ? "$cur$e" : "$cur/$e"; | ||
| 67 | |||
| 68 | if ($self->test('d', $path)) { | ||
| 69 | push(@dirs, $path); | ||
| 70 | |||
| 71 | my ($d, $f) = $self->_list_recurse($path); | ||
| 72 | push(@dirs, @{$d}); | ||
| 73 | push(@files, @{$f}); | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | push(@files, $path); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | push(@files, $cur); | ||
| 82 | } | ||
| 83 | |||
| 84 | return (\@dirs, \@files); | ||
| 85 | } | ||
| 86 | |||
| 87 | =head1 FUNCTIONS | ||
| 88 | |||
| 89 | =head2 new | ||
| 90 | |||
| 91 | Wrap Filesys::Virtual object. | ||
| 92 | wrapped object is returned. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub new { | ||
| 97 | my $class = shift; | ||
| 98 | my ($fs) = @_; | ||
| 99 | |||
| 100 | my $self = { | ||
| 101 | filesys => $fs, | ||
| 102 | }; | ||
| 103 | bless $self, $class; | ||
| 104 | |||
| 105 | return $self; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub _copy_file { | ||
| 110 | my $self = shift; | ||
| 111 | my ($srcname, $destname) = @_; | ||
| 112 | |||
| 113 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | ||
| 114 | $atime,$mtime,$ctime,$blksize,$blocks) | ||
| 115 | = $self->stat($srcname); | ||
| 116 | |||
| 117 | my $in = $self->open_read($srcname) | ||
| 118 | or croak "$srcname: cannot open: $!"; | ||
| 119 | |||
| 120 | my $out = $self->open_write($destname, undef) | ||
| 121 | or croak "$destname: cannot create: $!"; | ||
| 122 | |||
| 123 | my $buf; | ||
| 124 | my $buflen = 4096; | ||
| 125 | while(read($in, $buf, $buflen)) { | ||
| 126 | print $out $buf; | ||
| 127 | } | ||
| 128 | |||
| 129 | $self->close_write($out); | ||
| 130 | $self->close_read($in); | ||
| 131 | |||
| 132 | eval { $self->utime($atime, $mtime, $destname); }; | ||
| 133 | eval { $self->chmod($mode, $destname); }; | ||
| 134 | } | ||
| 135 | |||
| 136 | # | ||
| 137 | # rename a file (not a directory) | ||
| 138 | # | ||
| 139 | sub _rename_file { | ||
| 140 | my $self = shift; | ||
| 141 | my ($oldname, $newname) = @_; | ||
| 142 | |||
| 143 | eval { | ||
| 144 | $self->_copy_file($oldname, $newname); | ||
| 145 | |||
| 146 | $self->delete($oldname) or croak "cannot delete $oldname: $!"; | ||
| 147 | }; | ||
| 148 | if ($@) { | ||
| 149 | # print STDERR "$@"; | ||
| 150 | my $err = $! || 1; | ||
| 151 | |||
| 152 | $self->delete($newname); | ||
| 153 | $! = $err; | ||
| 154 | |||
| 155 | return; # undef | ||
| 156 | } | ||
| 157 | |||
| 158 | $self->delete($oldname); | ||
| 159 | |||
| 160 | return 1; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | # | ||
| 165 | # rename a directory (recursive copy) | ||
| 166 | # | ||
| 167 | sub _rename_dir { | ||
| 168 | my $self = shift; | ||
| 169 | my ($oldname, $newname) = @_; | ||
| 170 | |||
| 171 | my ($srcdirs, $srcfiles) = $self->_list_recurse($oldname); | ||
| 172 | unshift(@{$srcdirs}, $oldname); | ||
| 173 | |||
| 174 | my %dirstats; | ||
| 175 | |||
| 176 | for my $dir (@{$srcdirs}) { | ||
| 177 | my $destdir = $dir; | ||
| 178 | $destdir =~ s/^\Q$oldname\E/$newname/; | ||
| 179 | |||
| 180 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | ||
| 181 | $atime,$mtime,$ctime,$blksize,$blocks) | ||
| 182 | = $self->stat($dir); | ||
| 183 | |||
| 184 | unless ($self->test('d', $destdir)) { | ||
| 185 | $self->mkdir($destdir, $mode); | ||
| 186 | } | ||
| 187 | $dirstats{$destdir} = { atime => $atime, mtime => $mtime }; | ||
| 188 | } | ||
| 189 | |||
| 190 | for my $file (@{$srcfiles}) { | ||
| 191 | my $destfile = $file; | ||
| 192 | $destfile =~ s/^\Q$oldname\E/$newname/; | ||
| 193 | |||
| 194 | $self->_copy_file($file, $destfile); | ||
| 195 | } | ||
| 196 | |||
| 197 | for my $d (keys %dirstats) { | ||
| 198 | eval { $self->utime($dirstats{$d}->{atime}, | ||
| 199 | $dirstats{$d}->{mtime}, | ||
| 200 | $d); }; | ||
| 201 | } | ||
| 202 | |||
| 203 | # | ||
| 204 | # TODO: | ||
| 205 | # If error is found while deleting original files, | ||
| 206 | # should I rollback filesys? | ||
| 207 | # | ||
| 208 | for my $file (@{$srcfiles}) { | ||
| 209 | $self->delete($file); | ||
| 210 | } | ||
| 211 | for my $dir (reverse @{$srcdirs}) { | ||
| 212 | $self->rmdir($dir); | ||
| 213 | } | ||
| 214 | |||
| 215 | return 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | =head2 rename (OLDNAME, NEWNAME) | ||
| 219 | |||
| 220 | rename oldname to newname | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub rename { | ||
| 225 | my $self = shift; | ||
| 226 | my ($oldname, $newname) = @_; | ||
| 227 | |||
| 228 | my $dest = $newname; | ||
| 229 | |||
| 230 | if ($self->test('d', $newname)) { | ||
| 231 | $dest .= '/' unless ($dest =~ /\/$/); | ||
| 232 | my @segs = split(/\//, $oldname); | ||
| 233 | $dest .= $segs[$#segs]; | ||
| 234 | } | ||
| 235 | |||
| 236 | if ($self->test('d', $oldname)) { | ||
| 237 | return $self->_rename_dir($oldname, $dest); | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | return $self->_rename_file($oldname, $dest); | ||
| 241 | } | ||
| 242 | } | ||
| 243 | |||
| 244 | # Fix nlink | ||
| 245 | # ex. File::Find does NOT follow directory having nlink <= 2. | ||
| 246 | sub _fix_stat { | ||
| 247 | my $self = shift; | ||
| 248 | my ($fname) = @_; | ||
| 249 | |||
| 250 | my @s = $self->_call_original_method("stat", $fname); | ||
| 251 | return @s unless (@s); | ||
| 252 | |||
| 253 | if (@s && POSIX::S_ISDIR($s[2])) { | ||
| 254 | $s[3] = $self->_count_nlink($fname); | ||
| 255 | } | ||
| 256 | |||
| 257 | return @s; | ||
| 258 | } | ||
| 259 | |||
| 260 | =head2 stat | ||
| 261 | |||
| 262 | Same as Filesys::Virtual::stat. | ||
| 263 | (but nlink is corrected for directory.) | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub stat { | ||
| 268 | my $self = shift; | ||
| 269 | $self->_fix_stat(@_); | ||
| 270 | } | ||
| 271 | |||
| 272 | sub _count_nlink { | ||
| 273 | my $self = shift; | ||
| 274 | my ($fname) = @_; | ||
| 275 | |||
| 276 | my $nlink = 2; # '.', '..' | ||
| 277 | |||
| 278 | for my $name ($self->{filesys}->list($fname)) { | ||
| 279 | next if ($name eq '.' || $name eq '..'); | ||
| 280 | my $tmp = "$fname/$name"; | ||
| 281 | $tmp =~ s/\/\//\//g; | ||
| 282 | $nlink++ if ($self->{filesys}->test('d', $tmp)); | ||
| 283 | } | ||
| 284 | |||
| 285 | return $nlink; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _fake_test { | ||
| 289 | my $self = shift; | ||
| 290 | my ($test, $fname) = @_; | ||
| 291 | |||
| 292 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | ||
| 293 | $atime,$mtime,$ctime,$blksize,$blocks) = $self->stat($fname); | ||
| 294 | |||
| 295 | if ($test eq 'd') { | ||
| 296 | return POSIX::S_ISDIR($mode) ? 1 : undef; | ||
| 297 | } | ||
| 298 | elsif ($test eq 'f') { | ||
| 299 | return POSIX::S_ISREG($mode) ? 1 : undef; | ||
| 300 | } | ||
| 301 | elsif ($test eq 'e') { | ||
| 302 | return defined($ino) ? 1 : undef; | ||
| 303 | } | ||
| 304 | |||
| 305 | return undef; | ||
| 306 | } | ||
| 307 | |||
| 308 | # | ||
| 309 | # other method is same as original... | ||
| 310 | # | ||
| 311 | sub AUTOLOAD { | ||
| 312 | my $self = shift; | ||
| 313 | |||
| 314 | my $method = $AUTOLOAD; | ||
| 315 | $method =~ s/.*:://; | ||
| 316 | |||
| 317 | $self->_call_original_method($method, @_); | ||
| 318 | } | ||
| 319 | |||
| 320 | sub _call_original_method { | ||
| 321 | my $self = shift; | ||
| 322 | my $method = shift; | ||
| 323 | |||
| 324 | { | ||
| 325 | no warnings "redefine"; | ||
| 326 | local *Filesys::Virtual::carp = sub { | ||
| 327 | my $msg = shift; | ||
| 328 | Carp::croak($msg) if ($msg =~ / Unimplemented/); | ||
| 329 | Carp::carp($msg); | ||
| 330 | }; | ||
| 331 | |||
| 332 | return $self->{filesys}->$method(@_); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | sub DESTROY { | ||
| 337 | } | ||
| 338 | |||
| 339 | 1; | ||