| 1 | package Evo::Fs; | ||
| 2 | 3 | ▶ | use Evo '-Export *; -Class; ::Stat; -Path; Carp croak; -Path'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | die "Win isn't supported yet. Pull requests are welcome!" if $^O eq 'MSWin32'; | ||
| 4 | |||
| 5 | use Fcntl qw(:seek O_RDWR O_RDONLY O_WRONLY O_RDWR O_CREAT O_TRUNC O_APPEND O_EXCL :flock); | ||
| 6 | use Evo 'File::Spec; File::Path; Cwd() abs_path; File::Basename fileparse; Symbol()'; | ||
| 7 | use Time::HiRes (); | ||
| 8 | use Evo 'List::Util first; File::Copy ()'; | ||
| 9 | use Errno qw(EAGAIN EWOULDBLOCK); | ||
| 10 | use Scalar::Util; | ||
| 11 | |||
| 12 | sub SKIP_HIDDEN : Export : prototype() { | ||
| 13 | sub($dir) { | ||
| 14 | my @dirs = File::Spec->splitdir($dir); | ||
| 15 | $dirs[-1] !~ /^\./; | ||
| 16 | }; | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | # ========= CLASS ========= | ||
| 21 | |||
| 22 | |||
| 23 | has root => | ||
| 24 | check sub($v) { File::Spec->file_name_is_absolute($v) ? 1 : (0, "root should be absolute") }; | ||
| 25 | |||
| 26 | |||
| 27 | sub cd ($self, $rel) { | ||
| 28 | my $root = Evo::Path->from_string('', $self->root . '')->append_unsafe($rel)->to_string; | ||
| 29 | ref($self)->new(root => $root); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub path2real ($self, $rel) { | ||
| 33 | Evo::Path->from_string($rel, $self->root . '')->to_string; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub exists ($self, $path) { | ||
| 37 | -e $self->path2real($path); | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub mkdir ($self, $path, $perm = undef) { | ||
| 42 | my $real = $self->path2real($path); | ||
| 43 | &CORE::mkdir($real, defined $perm ? $perm : ()) or croak "$real: $!"; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub make_tree ($self, $path, $perms = undef) { | ||
| 47 | my $real = $self->path2real($path); | ||
| 48 | my %opts = (error => \my $err); | ||
| 49 | $opts{chmod} = $perms if defined $perms; | ||
| 50 | File::Path::make_path($real, \%opts); | ||
| 51 | croak join('; ', map { $_->%* } @$err) if @$err; # TODO: test | ||
| 52 | } | ||
| 53 | |||
| 54 | sub symlink ($self, $to_path, $link_path) { | ||
| 55 | CORE::symlink($self->path2real($to_path), $self->path2real($link_path)) | ||
| 56 | or croak "symlink $to_path $link_path: $!"; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub link ($self, $to_path, $link_path) { | ||
| 60 | CORE::link($self->path2real($to_path), $self->path2real($link_path)) | ||
| 61 | or croak "hardlink $to_path $link_path: $!"; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub is_symlink ($self, $path) { | ||
| 65 | -l $self->path2real($path); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub utimes ($self, $path, $atime = undef, $mtime = undef) { | ||
| 70 | my $real = $self->path2real($path); | ||
| 71 | utime($atime // undef, $mtime // undef, $real) or croak "utimes $path: $!"; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub close ($self, $fh) { | ||
| 75 | close $fh; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub stat ($self, $path) { | ||
| 79 | my %opts; | ||
| 80 | my @stat = Time::HiRes::stat $self->path2real($path) or croak "stat $path: $!"; | ||
| 81 | @opts{qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks)} = @stat; | ||
| 82 | Evo::Fs::Stat->new(%opts, _data => \@stat); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub rename ($self, $old, $new) { | ||
| 86 | rename $self->path2real($old), $self->path2real($new) or croak "rename $!"; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | my %seek_map = (start => SEEK_SET, cur => SEEK_CUR, end => SEEK_END,); | ||
| 91 | |||
| 92 | my %open_map = ( | ||
| 93 | r => O_RDONLY, | ||
| 94 | 'r+' => O_RDWR, | ||
| 95 | |||
| 96 | w => O_WRONLY | O_CREAT | O_TRUNC, | ||
| 97 | wx => O_WRONLY | O_CREAT | O_EXCL, | ||
| 98 | 'w+' => O_RDWR | O_CREAT | O_TRUNC, | ||
| 99 | 'wx+' => O_RDWR | O_CREAT | O_EXCL, | ||
| 100 | a => O_WRONLY | O_CREAT | O_APPEND, | ||
| 101 | ax => O_WRONLY | O_CREAT | O_APPEND | O_EXCL, | ||
| 102 | |||
| 103 | 'a+' => O_RDWR | O_CREAT | O_APPEND, | ||
| 104 | 'ax+' => O_RDWR | O_CREAT | O_APPEND | O_EXCL, | ||
| 105 | ); | ||
| 106 | |||
| 107 | # self, fh, path, mode, perm? | ||
| 108 | sub sysopen ($, $, $, $, @) { | ||
| 109 | croak "Bad mode $_[3]" unless exists $open_map{$_[3]}; | ||
| 110 | &CORE::sysopen($_[1], $_[0]->path2real($_[2]), $open_map{$_[3]}, (defined($_[4]) ? $_[4] : ())) | ||
| 111 | or croak "sysopen: $!"; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub sysseek ($self, $fh, $pos, $whence = 'start') { | ||
| 116 | croak "Bad whence $whence" unless exists $seek_map{$whence}; | ||
| 117 | &CORE::sysseek($fh, $pos, $seek_map{$whence}) // croak "Can't sysseek $!"; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub syswrite ($, $, $, @) { # other lengh, scalar offset | ||
| 121 | shift; | ||
| 122 | &CORE::syswrite(@_) // croak "Can't write: $!"; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub sysread ($, $, $, $, @) { # @other = string offset | ||
| 126 | shift; | ||
| 127 | &CORE::sysread(@_) // croak "Can't read: $!"; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub unlink ($self, $path) { | ||
| 131 | unlink $self->path2real($path) or croak "$path $!"; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub remove_tree ($self, $path, $opts = {}) { | ||
| 135 | my $real = $self->path2real($path); | ||
| 136 | croak "remove_tree $real: Not a directory" unless $self->stat($path)->is_dir; | ||
| 137 | File::Path::remove_tree($real, {%$opts, error => \my $err}); | ||
| 138 | croak join('; ', map { $_->%* } @$err) if @$err; # TODO: test | ||
| 139 | } | ||
| 140 | |||
| 141 | sub ls ($self, $path) { | ||
| 142 | my $real = $self->path2real($path); | ||
| 143 | opendir(my $dh, $real) || croak "Can't opendir $real: $!"; | ||
| 144 | my @result = grep { $_ ne '.' && $_ ne '..' } readdir($dh); | ||
| 145 | closedir $dh; | ||
| 146 | @result; | ||
| 147 | } | ||
| 148 | |||
| 149 | my %flock_map = ( | ||
| 150 | ex => LOCK_EX, | ||
| 151 | ex_nb => LOCK_EX | LOCK_NB, | ||
| 152 | sh => LOCK_SH, | ||
| 153 | sh_nb => LOCK_SH | LOCK_NB, | ||
| 154 | un => LOCK_UN | ||
| 155 | ); | ||
| 156 | |||
| 157 | |||
| 158 | sub flock ($self, $fh, $flag) { | ||
| 159 | croak "Bad flag $flag" unless exists $flock_map{$flag}; | ||
| 160 | my $res = flock($fh, $flock_map{$flag}); | ||
| 161 | croak "$!" unless $res || $! == EAGAIN || $! == EWOULDBLOCK; | ||
| 162 | $res; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub open ($self, $path, $mode, @rest) { | ||
| 166 | $self->make_tree((fileparse($path))[1]) unless ($mode eq 'r' && $mode eq 'r+'); | ||
| 167 | $self->sysopen(my $fh, $path, $mode, @rest); | ||
| 168 | $fh; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub append ($self, $path, $) { | ||
| 173 | my $fh = $self->open($path, 'a'); | ||
| 174 | $self->flock($fh, 'ex'); | ||
| 175 | $self->syswrite($fh, $_[2]); | ||
| 176 | $self->flock($fh, 'un'); | ||
| 177 | CORE::close $fh; | ||
| 178 | return; | ||
| 179 | } | ||
| 180 | |||
| 181 | # don't copy 3rd arg | ||
| 182 | sub write ($self, $path, $) { | ||
| 183 | my $fh = $self->open($path, 'w'); | ||
| 184 | $self->flock($fh, 'ex'); | ||
| 185 | $self->syswrite($fh, $_[2]); | ||
| 186 | $self->flock($fh, 'un'); | ||
| 187 | CORE::close $fh; | ||
| 188 | return; | ||
| 189 | } | ||
| 190 | |||
| 191 | sub read_ref ($self, $path) { | ||
| 192 | my $fh = $self->open($path, 'r'); | ||
| 193 | $self->flock($fh, 'sh'); | ||
| 194 | $self->sysread($fh, \my $buf, $self->stat($path)->size); | ||
| 195 | $self->flock($fh, 'un'); | ||
| 196 | CORE::close $fh; | ||
| 197 | \$buf; | ||
| 198 | } | ||
| 199 | |||
| 200 | sub read ($self, $path) { | ||
| 201 | $self->read_ref($path)->$*; | ||
| 202 | } | ||
| 203 | |||
| 204 | sub write_many ($self, %map) { | ||
| 205 | $self->write($_, $map{$_}) for keys %map; | ||
| 206 | $self; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub find_files ($self, $start, $fhs_fn, $pick = undef) { | ||
| 210 | my %seen; | ||
| 211 | my $fn = sub ($path) { | ||
| 212 | my $stat = $self->stat($path); | ||
| 213 | return unless $stat->is_file; | ||
| 214 | $fhs_fn->($path); | ||
| 215 | }; | ||
| 216 | $self->traverse($start, $fn, $pick); | ||
| 217 | } | ||
| 218 | |||
| 219 | # make faster? | ||
| 220 | sub traverse ($self, $start, $fn, $pick_d = undef) { | ||
| 221 | |||
| 222 | $start = [$start] unless ref $start eq 'ARRAY'; | ||
| 223 | my %seen_dirs; # don't go into the same dir twice | ||
| 224 | my %seen_children; # don't fire the same file twice | ||
| 225 | |||
| 226 | my @stack = map { Evo::Path->new(base => $_); } map { | ||
| 227 | my $path = $_; | ||
| 228 | my $stat = $self->stat($path); | ||
| 229 | $seen_dirs{($stat->dev, '-', $stat->ino)}++ ? () : ($path); | ||
| 230 | } reverse $start->@*; | ||
| 231 | |||
| 232 | while (@stack) { | ||
| 233 | my $cur_dir = pop @stack; | ||
| 234 | |||
| 235 | my (@dirs, @children); | ||
| 236 | foreach my $cur_child (sort $self->ls($cur_dir)) { | ||
| 237 | |||
| 238 | my $path = $cur_dir->append($cur_child); | ||
| 239 | next unless $self->exists($path); # broken link | ||
| 240 | my $stat = $self->stat($path); | ||
| 241 | |||
| 242 | |||
| 243 | my $bool | ||
| 244 | = $stat->is_dir | ||
| 245 | && $stat->can_exec | ||
| 246 | && $stat->can_read | ||
| 247 | && !$seen_dirs{$stat->dev, '-', $stat->ino}++ | ||
| 248 | && (!$pick_d || $pick_d->($path)); | ||
| 249 | |||
| 250 | unshift @dirs, $path if $bool; | ||
| 251 | push @children, $path if !$seen_children{$stat->dev, '-', $stat->ino}++; | ||
| 252 | |||
| 253 | } | ||
| 254 | $fn->($_) for @children; | ||
| 255 | push @stack, @dirs; | ||
| 256 | } | ||
| 257 | } | ||
| 258 | |||
| 259 | my sub _copy_file ($self, $from, $to) { | ||
| 260 | File::Copy::cp $self->path2real($from), $self->path2real($to) or die "Copy failed: $!"; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub copy_dir ($self, $from, $to) { | ||
| 264 | $to = Evo::Path->new(base => $to); | ||
| 265 | $self->make_tree($to); | ||
| 266 | my @stack = ($from); | ||
| 267 | while (@stack) { | ||
| 268 | my $cur_dir = shift @stack; | ||
| 269 | $self->traverse( | ||
| 270 | $cur_dir, | ||
| 271 | sub($path) { | ||
| 272 | my $stat = $self->stat($path); | ||
| 273 | my $dest = $to->append(join '/', $path->children->@*); | ||
| 274 | if ($stat->is_dir) { | ||
| 275 | $self->mkdir($dest) unless $self->exists($dest); | ||
| 276 | } | ||
| 277 | elsif ($stat->is_file) { | ||
| 278 | _copy_file($self, $path, $dest); | ||
| 279 | } | ||
| 280 | |||
| 281 | #else { croak "Can't copy $path, not a dir neither a file"; } | ||
| 282 | } | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | sub copy_file ($self, $from, $to) { | ||
| 288 | $self->make_tree((fileparse($to))[1]); | ||
| 289 | _copy_file($self, $from, $to); | ||
| 290 | } | ||
| 291 | |||
| 292 | # ========= MODULE ========= | ||
| 293 | |||
| 294 | my $FSROOT = __PACKAGE__->new(root => '/'); | ||
| 295 | sub FSROOT : Export {$FSROOT} | ||
| 296 | |||
| 297 | |||
| 298 | 1; | ||
| 299 | |||
| 300 | __END__ | ||