home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Attributes.pm < prev    next >
Encoding:
Perl POD Document  |  2003-12-03  |  4.3 KB  |  176 lines

  1. # $Id: Attributes.pm,v 1.5 2001/05/16 01:38:01 btrott Exp $
  2.  
  3. package Net::SFTP::Attributes;
  4. use strict;
  5.  
  6. use Net::SFTP::Constants qw( :att );
  7. use Net::SFTP::Buffer;
  8.  
  9. use vars qw( @FIELDS );
  10. @FIELDS = qw( flags size uid gid perm atime mtime );
  11.  
  12. for my $f (@FIELDS) {
  13.     no strict 'refs';
  14.     *$f = sub {
  15.         my $a = shift;
  16.         $a->{$f} = shift if @_;
  17.         $a->{$f};
  18.     };
  19. }
  20.  
  21. sub new {
  22.     my $class = shift;
  23.     my $a = bless { }, $class;
  24.     $a->init(@_);
  25. }
  26.  
  27. sub init {
  28.     my $a = shift;
  29.     my %param = @_;
  30.     for my $f (@FIELDS) {
  31.         $a->{$f} = 0;
  32.     }
  33.     if (my $stat = $param{Stat}) {
  34.         $a->{flags} |= SSH2_FILEXFER_ATTR_SIZE;
  35.         $a->{size} = $stat->[7];
  36.         $a->{flags} |= SSH2_FILEXFER_ATTR_UIDGID;
  37.         $a->{uid} = $stat->[4];
  38.         $a->{gid} = $stat->[5];
  39.         $a->{flags} |= SSH2_FILEXFER_ATTR_PERMISSIONS;
  40.         $a->{perm} = $stat->[2];
  41.         $a->{flags} |= SSH2_FILEXFER_ATTR_ACMODTIME;
  42.         $a->{atime} = $stat->[8];
  43.         $a->{mtime} = $stat->[9];
  44.     }
  45.     elsif (my $buf = $param{Buffer}) {
  46.         $a->{flags} = $buf->get_int32;
  47.         if ($a->{flags} & SSH2_FILEXFER_ATTR_SIZE) {
  48.             $a->{size} = $buf->get_int64;
  49.         }
  50.         if ($a->{flags} & SSH2_FILEXFER_ATTR_UIDGID) {
  51.             $a->{uid} = $buf->get_int32;
  52.             $a->{gid} = $buf->get_int32;
  53.         }
  54.         if ($a->{flags} & SSH2_FILEXFER_ATTR_PERMISSIONS) {
  55.             $a->{perm} = $buf->get_int32;
  56.         }
  57.         if ($a->{flags} & SSH2_FILEXFER_ATTR_ACMODTIME) {
  58.             $a->{atime} = $buf->get_int32;
  59.             $a->{mtime} = $buf->get_int32;
  60.         }
  61.     }
  62.     $a;
  63. }
  64.  
  65. sub as_buffer {
  66.     my $a = shift;
  67.     my $buf = Net::SFTP::Buffer->new;
  68.     $buf->put_int32($a->{flags});
  69.     if ($a->{flags} & SSH2_FILEXFER_ATTR_SIZE) {
  70.         $buf->put_int64(int $a->{size});
  71.     }
  72.     if ($a->{flags} & SSH2_FILEXFER_ATTR_UIDGID) {
  73.         $buf->put_int32($a->{uid});
  74.         $buf->put_int32($a->{gid});
  75.     }
  76.     if ($a->{flags} & SSH2_FILEXFER_ATTR_PERMISSIONS) {
  77.         $buf->put_int32($a->{perm});
  78.     }
  79.     if ($a->{flags} & SSH2_FILEXFER_ATTR_ACMODTIME) {
  80.         $buf->put_int32($a->{atime});
  81.         $buf->put_int32($a->{mtime});
  82.     }
  83.     $buf;
  84. }
  85.  
  86. 1;
  87. __END__
  88.  
  89. =head1 NAME
  90.  
  91. Net::SFTP::Attributes - File/directory attribute container
  92.  
  93. =head1 SYNOPSIS
  94.  
  95.     use Net::SFTP::Attributes;
  96.     my $attrs = Net::SFTP::Attributes->new(Stat => [ stat "foo" ]);
  97.     my $size = $attrs->size;
  98.  
  99. =head1 DESCRIPTION
  100.  
  101. I<Net::SFTP::Attributes> encapsulates file/directory attributes
  102. for I<Net::SFTP>. It also provides serialization and
  103. deserialization methods to encode/decode attributes into
  104. I<Net::SFTP::Buffer> objects.
  105.  
  106. =head1 USAGE
  107.  
  108. =head2 Net::SFTP::Attributes->new( [ %args ] )
  109.  
  110. Constructs a new I<Net::SFTP::Attributes> object and returns
  111. that object.
  112.  
  113. I<%args> is optional; if not provided the object will be
  114. initialized with the default values. If provided, I<%args>
  115. can contain:
  116.  
  117. =over 4
  118.  
  119. =item * Stat
  120.  
  121. A reference to the return value of the built-in I<stat>
  122. function. The values in the I<Net::SFTP::Attributes> object
  123. will be initialized from the values in the I<stat> array,
  124. and the flags will be set appropriately.
  125.  
  126. =item * Buffer
  127.  
  128. A I<Net::SFTP::Buffer> object containing a serialized
  129. attribute object. The I<Net::SFTP::Attributes> object will
  130. be initialized from the values in the serialized string,
  131. and flags will be set appropriately.
  132.  
  133. =back
  134.  
  135. =head2 $attrs->as_buffer
  136.  
  137. Serializes the I<Attributes> object I<$attrs> into string
  138. form, using the flags in the object to determine what fields
  139. get placed in the buffer. Returns a I<Net::SFTP::Buffer>
  140. object.
  141.  
  142. =head2 $attrs->flags( [ $value ] )
  143.  
  144. Get/set the value of the flags in I<$attrs>.
  145.  
  146. =head2 $attrs->size( [ $value ] )
  147.  
  148. Get/set the value of the file size (in bytes) in I<$attrs>.
  149.  
  150. =head2 $attrs->uid( [ $value ] )
  151.  
  152. Get/set the value of the UID in I<$attrs>.
  153.  
  154. =head2 $attrs->gid( [ $value ] )
  155.  
  156. Get/set the value of the GID in I<$attrs>.
  157.  
  158. =head2 $attrs->perm( [ $value ] )
  159.  
  160. Get/set the value of the permissions in I<$attrs>.
  161.  
  162. =head2 $attrs->atime( [ $value ] )
  163.  
  164. Get/set the value of the last access time (atime) in I<$attrs>.
  165.  
  166. =head2 $attrs->mtime( [ $value ] )
  167.  
  168. Get/set the value of the last modified time (mtime) in I<$attrs>.
  169.  
  170. =head1 AUTHOR & COPYRIGHTS
  171.  
  172. Please see the Net::SFTP manpage for author, copyright, and
  173. license information.
  174.  
  175. =cut
  176.