/usr/share/perl5/Filesys/Virtual/Plain.pm is in libfilesys-virtual-plain-perl 0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | package Filesys::Virtual::Plain;
###########################################################################
### Filesys::Virtual::Plain
### L.M.Orchard (deus_x@pobox_com)
### David Davis (xantus@cpan.org)
###
###
### Copyright (c) 1999 Leslie Michael Orchard. All Rights Reserved.
### This module is free software; you can redistribute it and/or
### modify it under the same terms as Perl itself.
###
### Changes Copyright (c) 2003-2004 David Davis and Teknikill Software
###########################################################################
use strict;
use Filesys::Virtual;
use Carp;
use User::pwent;
use User::grent;
use IO::File;
our $AUTOLOAD;
our $VERSION = '0.10';
our @ISA = qw(Filesys::Virtual);
our %_fields = (
'cwd' => 1,
'root_path' => 1,
'home_path' => 1,
);
sub AUTOLOAD {
my $self = shift;
my $field = $AUTOLOAD;
$field =~ s/.*:://;
return if $field eq 'DESTROY';
croak("No such property or method '$AUTOLOAD'") if (!$self->_field_exists($field));
{
no strict "refs";
*{$AUTOLOAD} = sub {
my $self = shift;
return (@_) ? ($self->{$field} = shift) : $self->{$field};
};
}
return (@_) ? ($self->{$field} = shift) : $self->{$field};
}
=pod
=head1 NAME
Filesys::Virtual::Plain - A Plain virtual filesystem
=head1 SYNOPSIS
use Filesys::Virtual::Plain;
my $fs = Filesys::Virtual::Plain->new();
$fs->login('xantus', 'supersekret');
print foreach ($fs->list('/'));
=head1 DESCRIPTION
This module is used by other modules to provide a pluggable filesystem.
=head1 CONSTRUCTOR
=head2 new()
You can pass the initial cwd, root_path, and home_path as a hash.
=head1 METHODS
=cut
sub new {
my $class = shift;
my $self = {};
bless($self, $class);
$self->_init(@_);
return $self;
}
sub _init {
my ($self, $params) = @_;
foreach my $field (keys %_fields) {
next if (!$self->_field_exists($field));
$self->$field($params->{$field});
}
}
sub _field_exists {
return (defined $_fields{$_[1]});
}
=pod
=head2 login($username, $password, $become)
Logs in a user. Returns 0 on failure. If $username is 'anonymous' then it
will try to login as 'ftp' with no password. If $become is defined then it
will try to change ownership of the process to the uid/gid of the logged in
user. BEWARE of the consequences of using $become. login() also sets the
uid, gid, home, gids, home_path, and chdir to the users'.
=cut
sub login {
my $self = shift;
my $username = shift;
my $password = shift;
my $become = shift;
my $pw;
if ($username eq "anonymous") {
### Anonymous login
$pw = getpwnam("ftp");
unless (defined $pw) {
return 0;
}
} else {
### Given username / password
$pw = getpwnam($username);
unless (defined $pw) {
return 0;
}
my $cpassword = $pw->passwd();
my $crpt = crypt($password, $cpassword);
unless ($crpt eq $cpassword) {
return 0;
}
}
if (defined $become) {
$< = $> = $pw->uid();
$( = $) = $pw->gid();
}
$self->{uid} = $pw->uid();
$self->{gid} = $pw->gid();
$self->{home} = $pw->dir();
$self->{gids}{$pw->gid()} = 1;
$self->chdir($pw->dir());
$self->home_path($pw->dir());
return 1;
}
=pod
=head2 cwd
Gets or sets the current directory, assumes / if blank.
This is used in conjunction with the root_path for file operations.
No actual change directory takes place.
=cut
sub cwd {
my $self = shift;
if (@_) {
$self->{cwd} = shift;
} else {
$self->{cwd} ||= '/';
}
return $self->{cwd};
}
=pod
=head2 root_path($path)
Get or set the root path. All file paths are off this and cwd
For example:
$self->root_path('/home/ftp');
$self->cwd('/test');
$self->size('testfile.txt');
The size command would get the size for file /home/ftp/test/testfile.txt
not /test/testfile.txt
=cut
sub root_path {
my ($self) = shift;
if (@_) {
my $root_path = shift;
### Does the root path end with a '/'? If so, remove it.
$root_path = (substr($root_path, length($root_path)-1, 1) eq '/') ?
substr($root_path, 0, length($root_path)-1) : $root_path;
$self->{root_path} = $root_path;
}
return $self->{root_path};
}
=pod
=head2 chmod($mode,$file)
chmod's a file.
=cut
sub chmod {
my ($self, $mode, $fn) = @_;
$fn = $self->_path_from_root($fn);
return (chmod($mode,$fn)) ? 1 : 0;
}
=pod
=head2 modtime($file)
Gets the modification time of a file in YYYYMMDDHHMMSS format.
=cut
sub modtime {
my ($self, $fn) = @_;
$fn = $self->_path_from_root($fn);
return (0,"");
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = CORE::stat($fn);
my ($sec, $min, $hr, $dd, $mm, $yy, $wd, $yd, $isdst) =
localtime($mtime); $yy += 1900; $mm++;
return (1,"$yy$mm$dd$hr$min$sec");
}
=pod
=head2 size($file)
Gets the size of a file in bytes.
=cut
sub size {
my ($self, $fn) = @_;
$fn = $self->_path_from_root($fn);
return (CORE::stat($fn))[7];
}
=pod
=head2 delete($file)
Deletes a file, returns 1 or 0 on success or failure.
=cut
sub delete {
my ($self, $fn) = @_;
$fn = $self->_path_from_root($fn);
return ((-e $fn) && (!-d $fn) && (unlink($fn))) ? 1 : 0;
}
=pod
=head2 chdir($dir)
Changes the cwd to a new path from root_path.
Returns undef on failure or the new path on success.
=cut
sub chdir {
my ($self, $dir) = @_;
my $new_cwd = $self->_resolve_path($dir);
my $full_path = $self->root_path().$new_cwd;
return ((-e $full_path) && (-d $full_path)) ? $self->cwd($new_cwd) : undef;
}
=pod
=head2 mkdir($dir, $mode)
Creats a directory with $mode (defaults to 0755) and chown()'s the directory
with the uid and gid. The return value is from mkdir().
=cut
sub mkdir {
my ($self, $dir, $mode) = @_;
$dir = $self->_path_from_root($dir);
return 2 if (-d $dir);
$mode ||= 0755;
my $ret = (mkdir($dir, $mode)) ? 1 : 0;
if ($ret) {
chown($self->{uid}, $self->{gid}, $dir);
}
return $ret;
}
=pod
=head2 rmdir($dir)
Deletes a directory or file if -d test fails. Returns 1 on success or 0 on
failure.
=cut
sub rmdir {
my ($self, $dir) = @_;
$dir = $self->_path_from_root($dir);
if (-e $dir) {
if (-d $dir) {
return 1 if (rmdir($dir));
} else {
return 1 if (unlink($dir));
}
}
return 0;
}
=pod
=head2 list($dir)
Returns an array of the files in a directory.
=cut
sub list {
my ($self, $dirfile) = @_;
$dirfile = $self->_path_from_root($dirfile);
my @ls;
if(-e $dirfile) {
if(!-d $dirfile) {
### This isn't a directory, so derive its short name, and push it.
my @parts = split(/\//, $dirfile);
push(@ls, pop @parts);
} else {
### Open the directory and get a file list.
opendir(DIR, $dirfile);
my @files = readdir(DIR);
closedir(DIR);
### Process the files...
@ls = (sort @files);
}
}
return @ls;
}
=pod
=head2 list_details($dir)
Returns an array of the files in ls format.
=cut
sub list_details {
my ($self, $dirfile) = @_;
$dirfile = $self->_path_from_root($dirfile);
my @ls;
if( -e $dirfile ) {
if(! -d $dirfile ) {
### This isn't a directory, so derive its short name, and produce
### an ls line.
my @parts = split(/\//, $dirfile);
my $fn = pop @parts;
push(@ls, $self->_ls_stat($dirfile, $fn));
} else {
### Open the directory and get a file list.
opendir(DIR, $dirfile);
my @files = readdir(DIR);
closedir(DIR);
### Make sure the directory path ends in '/'
$dirfile = (substr($dirfile, length($dirfile)-1, 1) eq '/') ? $dirfile : $dirfile.'/';
### Process the files...
foreach (sort @files) {
push(@ls, $self->_ls_stat($dirfile.$_, $_));
}
}
}
return @ls;
}
=pod
=head2 stat($file)
Does a normal stat() on a file or directory
=cut
sub stat {
my ($self, $fn) = @_;
$fn =~ s/\s+/ /g;
$fn = $self->_path_from_root($fn);
return CORE::stat($fn);
}
=pod
=head2 test($test,$file)
Perform a perl type test on a file and returns the results.
For example to perform a -d on a directory.
$self->test('d','/testdir');
See filetests in perlfunc (commandline: perldoc perlfunc)
=cut
# -r File is readable by effective uid/gid.
# -w File is writable by effective uid/gid.
# -x File is executable by effective uid/gid.
# -o File is owned by effective uid.
# -R File is readable by real uid/gid.
# -W File is writable by real uid/gid.
# -X File is executable by real uid/gid.
# -O File is owned by real uid.
# -e File exists.
# -z File has zero size.
# -s File has nonzero size (returns size).
# -f File is a plain file.
# -d File is a directory.
# -l File is a symbolic link.
# -p File is a named pipe (FIFO), or Filehandle is a pipe.
# -S File is a socket.
# -b File is a block special file.
# -c File is a character special file.
# -t Filehandle is opened to a tty.
# -u File has setuid bit set.
# -g File has setgid bit set.
# -k File has sticky bit set.
# -T File is a text file.
# -B File is a binary file (opposite of -T).
# -M Age of file in days when script started.
# -A Same for access time.
# -C Same for inode change time.
sub test {
my ($self, $test, $fn) = @_;
$fn = $self->_path_from_root($fn);
$fn =~ s/'/\\'/g;
# NO FUNNY BUSINESS
$test =~ s/^(.)/$1/;
my $ret = eval("-$test '$fn'");
return ($@) ? undef : $ret;
}
=pod
=head2 open_read($file,[params])
Opens a file with L<IO::File>. Params are passed to open() of IO::File.
It returns the file handle on success or undef on failure. This could
be technically be used for any sort of open operation. See L<IO::File>'s
open method.
=cut
sub open_read {
my ($self, $fin, @opts) = @_;
$fin =~ s/\s+/ /g;
$self->{file_path} = $fin = $self->_path_from_root($fin);
return IO::File->new($fin,@opts);
}
=pod
=head2 close_read($fh)
Performs a $fh->close()
=cut
sub close_read {
my ($self, $fh) = @_;
return $fh->close();
}
=pod
=head2 open_write($fh, $append)
Performs an $fh->open(">$file") or $fh->open(">>$file") if $append is defined.
Returns the filehandle on success or undef on failure.
=cut
sub open_write {
my ($self, $fin, $append) = @_;
$fin =~ s/\s+/ /g;
$self->{file_path} = $fin = $self->_path_from_root($fin);
my $o = (defined($append)) ? '>>' : '>';
return IO::File->new($o.$fin);
}
=pod
=head2 close_write($fh)
Performs a $fh->close()
=cut
sub close_write {
my ($self, $fh) = @_;
$fh->close();
return 1;
}
=pod
=head2 seek($fh, $pos, $wence)
Performs a $fh->seek($pos, $wence). See L<IO::Seekable>.
=cut
sub seek {
my ($self, $fh, $first, $second) = @_;
return $fh->seek($first, $second);
}
=pod
=head2 utime($atime, $mtime, @files)
Performs a utime() on the file(s). It changes the access time and mod time of
those files.
=cut
sub utime {
my ($self, $atime, $mtime, @fn) = @_;
foreach my $i ( 0 .. $#fn ) {
$fn[$i] = $self->_path_from_root($fn[$i]);
}
return CORE::utime($atime, $mtime, @fn);
}
### Internal methods
# Restrict the path to beneath root path
sub _path_from_root {
my ($self, $path) = @_;
return $self->root_path().$self->_resolve_path($path);
}
# Resolve a path from the current path
sub _resolve_path {
my $self = shift;
my $path = shift || '';
my $cwd = $self->cwd();
my $path_out = '';
if ($path eq '') {
$path_out = $cwd;
} elsif ($path eq '/') {
$path_out = '/';
} else {
my @real_ele = split(/\//, $cwd);
if ($path =~ m/^\//) {
undef @real_ele;
}
foreach (split(/\//, $path)) {
if ($_ eq '..') {
pop(@real_ele) if ($#real_ele);
} elsif ($_ eq '.') {
next;
} elsif ($_ eq '~') {
@real_ele = split(/\//, $self->home_path());
} else {
push(@real_ele, $_);
}
}
$path_out = join('/', @real_ele);
}
$path_out = (substr($path_out, 0, 1) eq '/') ? $path_out : '/'.$path_out;
return $path_out;
}
# Given a file's full path and name, produce a full ls line
sub _ls_stat {
my ($self, $full_fn, $fn) = @_;
my @modes = ("---------", "rwxrwxrwx");
# Determine the current year, for time comparisons
my $curr_year = (localtime())[5]+1900;
# Perform stat() on current file.
my ($mode,$nlink,$uid,$gid,$size,$mtime) = (CORE::stat($full_fn))[2 .. 5,7,9];
#my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
# $atime,$mtime,$ctime,$blksize,$blocks) = CORE::stat($full_fn);
# Format the mod datestamp into the ls format
my ($day, $mm, $dd, $time, $yr) = (localtime($mtime) =~ m/(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/);
# Get a string of 0's and 1's for the binary file mode/type
my $bin_str = substr(unpack("B32", pack("N", $mode)), -16);
# Produce a permissions map from the file mode
my $mode_bin = substr($bin_str, -9);
my $mode_str = '';
for (my $i=0; $i<9; $i++) {
$mode_str .= substr($modes[substr($mode_bin, $i, 1)], $i, 1);
}
# Determine what type of file this is from the file type
my $type_bin = substr($bin_str, -16, 7);
my $type_str = '-';
$type_str = 'd' if ($type_bin =~ m/^01/);
# Assemble and return the line
return sprintf("%1s%9s %4s %-8s %-8s %8s %3s %2s %5s %s",
$type_str, $mode_str, $nlink,
$self->_user($uid), $self->_group($gid), $size, $mm, $dd,
($curr_year eq $yr) ? substr($time,0,5) : $yr, $fn);
}
# Lookup user name by uid
{
my %user;
sub _user {
my ($self, $uid) = @_;
if (!exists($user{$uid})) {
if (defined($uid)) {
my $obj = getpwuid($uid);
if ($obj) {
$user{$uid} = $obj->name;
} else {
$user{$uid} = "#$uid";
}
} else {
return '#?';
}
}
return $user{$uid};
}
}
# Lookup group name by gid
{
my %group;
sub _group {
my ($self, $gid) = @_;
if (!exists($group{$gid})) {
if (defined($gid)) {
my $obj = getgrgid($gid);
if ($obj) {
$group{$gid} = $obj->name;
} else {
$group{$gid} = "#$gid";
}
} else {
return '#?';
}
}
return $group{$gid};
}
}
1;
__END__
=head1 AUTHOR
David Davis, E<lt>xantus@cpan.orgE<gt>, http://teknikill.net/
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
perl(1), L<Filesys::Virtual>, L<Filesys::Virtual::SSH>,
L<Filesys::Virtual::DAAP>, L<POE::Component::Server::FTP>,
L<Net::DAV::Server>, L<HTTP::Daemon>,
http://perladvent.org/2004/20th/
=cut
|