| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # -*- perl -*- | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # Net::FTPServer A Perl FTP Server | 
| 4 |  |  |  |  |  |  | # Copyright (C) 2000 Bibliotech Ltd., Unit 2-3, 50 Carnwath Road, | 
| 5 |  |  |  |  |  |  | # London, SW6 3EG, United Kingdom. | 
| 6 |  |  |  |  |  |  | # | 
| 7 |  |  |  |  |  |  | # This program is free software; you can redistribute it and/or modify | 
| 8 |  |  |  |  |  |  | # it under the terms of the GNU General Public License as published by | 
| 9 |  |  |  |  |  |  | # the Free Software Foundation; either version 2 of the License, or | 
| 10 |  |  |  |  |  |  | # (at your option) any later version. | 
| 11 |  |  |  |  |  |  | # | 
| 12 |  |  |  |  |  |  | # This program is distributed in the hope that it will be useful, | 
| 13 |  |  |  |  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 14 |  |  |  |  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 15 |  |  |  |  |  |  | # GNU General Public License for more details. | 
| 16 |  |  |  |  |  |  | # | 
| 17 |  |  |  |  |  |  | # You should have received a copy of the GNU General Public License | 
| 18 |  |  |  |  |  |  | # along with this program; if not, write to the Free Software | 
| 19 |  |  |  |  |  |  | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =pod | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =head1 NAME | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | Net::FTPServer::Full::FileHandle - The full FTP server personality | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | use Net::FTPServer::Full::FileHandle; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head1 METHODS | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =cut | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | package Net::FTPServer::Full::FileHandle; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 1 |  |  | 1 |  | 4 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 53 |  | 
| 40 |  |  |  |  |  |  | ( $VERSION ) = '$Revision: 1.2 $ ' =~ /\$Revision:\s+([^\s]+)/; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  | 1 |  | 5 | use Net::FTPServer::FileHandle; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 16 |  | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  | 1 |  | 4 | use vars qw(@ISA); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 363 |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | @ISA = qw(Net::FTPServer::FileHandle); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =pod | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =over 4 | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =item $dirh = $fileh->dir; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Return the directory which contains this file. | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub dir | 
| 59 |  |  |  |  |  |  | { | 
| 60 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  |  | my $dirname = $self->{_pathname}; | 
| 63 | 0 |  |  |  |  |  | $dirname =~ s,[^/]+$,,; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  |  | return Net::FTPServer::Full::DirHandle->new ($self->{ftps}, $dirname); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =pod | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =item $fh = $fileh->open (["r"|"w"|"a"]); | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | Open a file handle (derived from C, see | 
| 73 |  |  |  |  |  |  | C) in either read or write mode. | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =cut | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub open | 
| 78 |  |  |  |  |  |  | { | 
| 79 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 80 | 0 |  |  |  |  |  | my $mode = shift; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  |  | return new IO::File $self->{_pathname}, $mode; | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =pod | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =item ($mode, $perms, $nlink, $user, $group, $size, $time) = $handle->status; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | Return the file or directory status. The fields returned are: | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | $mode     Mode        'd' = directory, | 
| 92 |  |  |  |  |  |  | 'f' = file, | 
| 93 |  |  |  |  |  |  | and others as with | 
| 94 |  |  |  |  |  |  | the find(1) -type option. | 
| 95 |  |  |  |  |  |  | $perms    Permissions Permissions in normal octal numeric format. | 
| 96 |  |  |  |  |  |  | $nlink    Link count | 
| 97 |  |  |  |  |  |  | $user     Username    In printable format. | 
| 98 |  |  |  |  |  |  | $group    Group name  In printable format. | 
| 99 |  |  |  |  |  |  | $size     Size        File size in bytes. | 
| 100 |  |  |  |  |  |  | $time     Time        Time (usually mtime) in Unix time_t format. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | In derived classes, some of this status information may well be | 
| 103 |  |  |  |  |  |  | synthesized, since virtual filesystems will often not contain | 
| 104 |  |  |  |  |  |  | information in a Unix-like format. | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =cut | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub status | 
| 109 |  |  |  |  |  |  | { | 
| 110 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, | 
| 113 |  |  |  |  |  |  | $atime, $mtime, $ctime, $blksize, $blocks) | 
| 114 | 0 |  |  |  |  |  | = lstat $self->{_pathname}; | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | # If the file has been removed since we created this | 
| 117 |  |  |  |  |  |  | # handle, then $dev will be undefined. Return dummy status | 
| 118 |  |  |  |  |  |  | # information. | 
| 119 | 0 | 0 |  |  |  |  | return ("f", 0000, 1, "-", "-", 0, 0) unless defined $dev; | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # Generate printable user/group. | 
| 122 | 0 |  | 0 |  |  |  | my $user = getpwuid ($uid) || "-"; | 
| 123 | 0 |  | 0 |  |  |  | my $group = getgrgid ($gid) || "-"; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # Permissions from mode. | 
| 126 | 0 |  |  |  |  |  | my $perms = $mode & 0777; | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | # Work out the mode using special "_" operator which causes Perl | 
| 129 |  |  |  |  |  |  | # to use the result of the previous stat call. | 
| 130 | 0 | 0 |  |  |  |  | $mode | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | = (-f _ ? 'f' : | 
| 132 |  |  |  |  |  |  | (-d _ ? 'd' : | 
| 133 |  |  |  |  |  |  | (-l _ ? 'l' : | 
| 134 |  |  |  |  |  |  | (-p _ ? 'p' : | 
| 135 |  |  |  |  |  |  | (-S _ ? 's' : | 
| 136 |  |  |  |  |  |  | (-b _ ? 'b' : | 
| 137 |  |  |  |  |  |  | (-c _ ? 'c' : '?'))))))); | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 0 |  |  |  |  |  | return ($mode, $perms, $nlink, $user, $group, $size, $mtime); | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =pod | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =item $rv = $handle->move ($dirh, $filename); | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Move the current file (or directory) into directory C<$dirh> and | 
| 147 |  |  |  |  |  |  | call it C<$filename>. If the operation is successful, return 0, | 
| 148 |  |  |  |  |  |  | else return -1. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | Underlying filesystems may impose limitations on moves: for example, | 
| 151 |  |  |  |  |  |  | it may not be possible to move a directory; it may not be possible | 
| 152 |  |  |  |  |  |  | to move a file to another directory; it may not be possible to | 
| 153 |  |  |  |  |  |  | move a file across filesystems. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =cut | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | sub move | 
| 158 |  |  |  |  |  |  | { | 
| 159 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 160 | 0 |  |  |  |  |  | my $dirh = shift; | 
| 161 | 0 |  |  |  |  |  | my $filename = shift; | 
| 162 |  |  |  |  |  |  |  | 
| 163 | 0 | 0 |  |  |  |  | die if $filename =~ /\//;	# Should never happen. | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 0 |  |  |  |  |  | my $new_name = $dirh->{_pathname} . $filename; | 
| 166 |  |  |  |  |  |  |  | 
| 167 | 0 | 0 |  |  |  |  | rename $self->{_pathname}, $new_name or return -1; | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 0 |  |  |  |  |  | $self->{_pathname} = $new_name; | 
| 170 | 0 |  |  |  |  |  | return 0; | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =pod | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =item $rv = $fileh->delete; | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | Delete the current file. If the delete command was | 
| 178 |  |  |  |  |  |  | successful, then return 0, else if there was an error return -1. | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =cut | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | sub delete | 
| 183 |  |  |  |  |  |  | { | 
| 184 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 185 |  |  |  |  |  |  |  | 
| 186 | 0 | 0 |  |  |  |  | unlink $self->{_pathname} or return -1; | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 0 |  |  |  |  |  | return 0; | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =item $link = $fileh->readlink; | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | If the current file is really a symbolic link, read the contents | 
| 194 |  |  |  |  |  |  | of the link and return it. | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =cut | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | sub readlink | 
| 199 |  |  |  |  |  |  | { | 
| 200 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 201 |  |  |  |  |  |  |  | 
| 202 | 0 |  |  |  |  |  | return readlink $self->{_pathname}; | 
| 203 |  |  |  |  |  |  | } | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | 1 # So that the require or use succeeds. | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | __END__ |