⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 temp.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 5 页
字号:
   if (wantarray() ) {       return mkstemp($template);   } else {       return mktemp($template);   }}=item B<tmpfile>Returns the filehandle of a temporary file.  $fh = tmpfile();The file is removed when the filehandle is closed or when the programexits. No access to the filename is provided.If the temporary file can not be created undef is returned.Currently this command will probably not work when the temporarydirectory is on an NFS file system.Will croak() if there is an error.=cutsub tmpfile {  # Simply call tmpnam() in a list context  my ($fh, $file) = tmpnam();  # Make sure file is removed when filehandle is closed  # This will fail on NFS  unlink0($fh, $file)    or return undef;  return $fh;}=back=head1 ADDITIONAL FUNCTIONSThese functions are provided for backwards compatibilitywith common tempfile generation C library functions.They are not exported and must be addressed using the full packagename.=over 4=item B<tempnam>Return the name of a temporary file in the specified directoryusing a prefix. The file is guaranteed not to exist at the timethe function was called, but such guarantees are good for oneclock tick only.  Always use the proper form of C<sysopen>with C<O_CREAT | O_EXCL> if you must open such a filename.  $filename = File::Temp::tempnam( $dir, $prefix );Equivalent to running mktemp() with $dir/$prefixXXXXXXXX(using unix file convention as an example)Because this function uses mktemp(), it can suffer from race conditions.Will croak() if there is an error.=cutsub tempnam {  croak 'Usage tempnam($dir, $prefix)' unless scalar(@_) == 2;  my ($dir, $prefix) = @_;  # Add a string to the prefix  $prefix .= 'XXXXXXXX';  # Concatenate the directory to the file  my $template = File::Spec->catfile($dir, $prefix);  return mktemp($template);}=back=head1 UTILITY FUNCTIONSUseful functions for dealing with the filehandle and filename.=over 4=item B<unlink0>Given an open filehandle and the associated filename, make a safeunlink. This is achieved by first checking that the filename andfilehandle initially point to the same file and that the number oflinks to the file is 1 (all fields returned by stat() are compared).Then the filename is unlinked and the filehandle checked once again toverify that the number of links on that file is now 0.  This is theclosest you can come to making sure that the filename unlinked was thesame as the file whose descriptor you hold.  unlink0($fh, $path)     or die "Error unlinking file $path safely";Returns false on error but croaks() if there is a securityanomaly. The filehandle is not closed since on some occasions this isnot required.On some platforms, for example Windows NT, it is not possible tounlink an open file (the file must be closed first). On thoseplatforms, the actual unlinking is deferred until the program ends andgood status is returned. A check is still performed to make sure thatthe filehandle and filename are pointing to the same thing (but not atthe time the end block is executed since the deferred removal may nothave access to the filehandle).Additionally, on Windows NT not all the fields returned by stat() canbe compared. For example, the C<dev> and C<rdev> fields seem to bedifferent.  Also, it seems that the size of the file returned by stat()does not always agree, with C<stat(FH)> being more accurate thanC<stat(filename)>, presumably because of caching issues even whenusing autoflush (this is usually overcome by waiting a while afterwriting to the tempfile before attempting to C<unlink0> it).Finally, on NFS file systems the link count of the file handle doesnot always go to zero immediately after unlinking. Currently, thiscommand is expected to fail on NFS disks.This function is disabled if the global variable $KEEP_ALL is trueand an unlink on open file is supported. If the unlink is to be deferredto the END block, the file is still registered for removal.This function should not be called if you are using the object orientedinterface since the it will interfere with the object destructor deletingthe file.=cutsub unlink0 {  croak 'Usage: unlink0(filehandle, filename)'    unless scalar(@_) == 2;  # Read args  my ($fh, $path) = @_;  cmpstat($fh, $path) or return 0;  # attempt remove the file (does not work on some platforms)  if (_can_unlink_opened_file()) {    # return early (Without unlink) if we have been instructed to retain files.    return 1 if $KEEP_ALL;    # XXX: do *not* call this on a directory; possible race    #      resulting in recursive removal    croak "unlink0: $path has become a directory!" if -d $path;    unlink($path) or return 0;    # Stat the filehandle    my @fh = stat $fh;    print "Link count = $fh[3] \n" if $DEBUG;    # Make sure that the link count is zero    # - Cygwin provides deferred unlinking, however,    #   on Win9x the link count remains 1    # On NFS the link count may still be 1 but we cant know that    # we are on NFS    return ( $fh[3] == 0 or $^O eq 'cygwin' ? 1 : 0);  } else {    _deferred_unlink($fh, $path, 0);    return 1;  }}=item B<cmpstat>Compare C<stat> of filehandle with C<stat> of provided filename.  Thiscan be used to check that the filename and filehandle initially pointto the same file and that the number of links to the file is 1 (allfields returned by stat() are compared).  cmpstat($fh, $path)     or die "Error comparing handle with file";Returns false if the stat information differs or if the link count isgreater than 1. Calls croak if there is a security anomaly.On certain platforms, for example Windows, not all the fields returned by stat()can be compared. For example, the C<dev> and C<rdev> fields seem to bedifferent in Windows.  Also, it seems that the size of the filereturned by stat() does not always agree, with C<stat(FH)> being moreaccurate than C<stat(filename)>, presumably because of caching issueseven when using autoflush (this is usually overcome by waiting a whileafter writing to the tempfile before attempting to C<unlink0> it).Not exported by default.=cutsub cmpstat {  croak 'Usage: cmpstat(filehandle, filename)'    unless scalar(@_) == 2;  # Read args  my ($fh, $path) = @_;  warn "Comparing stat\n"    if $DEBUG;  # Stat the filehandle - which may be closed if someone has manually  # closed the file. Can not turn off warnings without using $^W  # unless we upgrade to 5.006 minimum requirement  my @fh;  {    local ($^W) = 0;    @fh = stat $fh;  }  return unless @fh;  if ($fh[3] > 1 && $^W) {    carp "unlink0: fstat found too many links; SB=@fh" if $^W;  }  # Stat the path  my @path = stat $path;  unless (@path) {    carp "unlink0: $path is gone already" if $^W;    return;  }  # this is no longer a file, but may be a directory, or worse  unless (-f $path) {    confess "panic: $path is no longer a file: SB=@fh";  }  # Do comparison of each member of the array  # On WinNT dev and rdev seem to be different  # depending on whether it is a file or a handle.  # Cannot simply compare all members of the stat return  # Select the ones we can use  my @okstat = (0..$#fh);  # Use all by default  if ($^O eq 'MSWin32') {    @okstat = (1,2,3,4,5,7,8,9,10);  } elsif ($^O eq 'os2') {    @okstat = (0, 2..$#fh);  } elsif ($^O eq 'VMS') { # device and file ID are sufficient    @okstat = (0, 1);  } elsif ($^O eq 'dos') {    @okstat = (0,2..7,11..$#fh);  } elsif ($^O eq 'mpeix') {    @okstat = (0..4,8..10);  }  # Now compare each entry explicitly by number  for (@okstat) {    print "Comparing: $_ : $fh[$_] and $path[$_]\n" if $DEBUG;    # Use eq rather than == since rdev, blksize, and blocks (6, 11,    # and 12) will be '' on platforms that do not support them.  This    # is fine since we are only comparing integers.    unless ($fh[$_] eq $path[$_]) {      warn "Did not match $_ element of stat\n" if $DEBUG;      return 0;    }  }  return 1;}=item B<unlink1>Similar to C<unlink0> except after file comparison using cmpstat, thefilehandle is closed prior to attempting to unlink the file. Thisallows the file to be removed without using an END block, but doesmean that the post-unlink comparison of the filehandle state providedby C<unlink0> is not available.  unlink1($fh, $path)     or die "Error closing and unlinking file";Usually called from the object destructor when using the OO interface.Not exported by default.This function is disabled if the global variable $KEEP_ALL is true.Can call croak() if there is a security anomaly during the stat()comparison.=cutsub unlink1 {  croak 'Usage: unlink1(filehandle, filename)'    unless scalar(@_) == 2;  # Read args  my ($fh, $path) = @_;  cmpstat($fh, $path) or return 0;  # Close the file  close( $fh ) or return 0;  # Make sure the file is writable (for windows)  _force_writable( $path );  # return early (without unlink) if we have been instructed to retain files.  return 1 if $KEEP_ALL;  # remove the file  return unlink($path);}=item B<cleanup>Calling this function will cause any temp files or temp directoriesthat are registered for removal to be removed. This happens automaticallywhen the process exits but can be triggered manually if the caller is surethat none of the temp files are required. This method can be registered asan Apache callback.On OSes where temp files are automatically removed when the temp fileis closed, calling this function will have no effect other than to removetemporary directories (which may include temporary files).  File::Temp::cleanup();Not exported by default.=back=head1 PACKAGE VARIABLESThese functions control the global state of the package.=over 4=item B<safe_level>Controls the lengths to which the module will go to check the safety of thetemporary file or directory before proceeding.Options are:=over 8=item STANDARDDo the basic security measures to ensure the directory exists and iswritable, that temporary files are opened only if they do not alreadyexist, and that possible race conditions are avoided.  Finally theL<unlink0|"unlink0"> function is used to remove files safely.=item MEDIUMIn addition to the STANDARD security, the output directory is checkedto make sure that it is owned either by root or the user running theprogram. If the directory is writable by group or by other, it is thenchecked to make sure that the sticky bit is set.Will not work on platforms that do not support the C<-k> testfor sticky bit.=item HIGHIn addition to the MEDIUM security checks, also check for thepossibility of ``chown() giveaway'' using the L<POSIX|POSIX>sysconf() function. If this is a possibility, each directory in thepath is checked in turn for safeness, recursively walking back to theroot directory.For platforms that do not support the L<POSIX|POSIX>C<_PC_CHOWN_RESTRICTED> symbol (for example, Windows NT) it isassumed that ``chown() giveaway'' is possible and the recursive testis performed.=backThe level can be changed as follows:  File::Temp->safe_level( File::Temp::HIGH );The level constants are not exported by the module.Currently, you must be running at least perl v5.6.0 in order torun with MEDIUM or HIGH security. This is simply because thesafety tests use functions from L<Fcntl|Fcntl> that are notavailable in older versions of perl. The problem is that the versionnumber for Fcntl is the same in perl 5.6.0 and in 5.005_03 even thoughthey are different versions.On systems that do not support the HIGH or MEDIUM safety levels(for example Win NT or OS/2) any attempt to change the level willbe ignored. The decision to ignore rather than raise an exceptionallows portable programs to be written with high security in mindfor the systems that can support this without those programs failingon systems where the extra tests are irrelevant.If you really need to see whether the change has been acceptedsimply examine the return value of C<safe_level>.  $newlevel = File::Temp->safe_level( File::Temp::HIGH );  die "Could not change to high security"      if $newlevel != File::Temp::HIGH;=cut{  # protect from using the variable itself  my $LEVEL = STANDARD;  sub safe_level {    my $self = shift;    if (@_) {      my $level = shift;      if (($level != STANDARD) && ($level != MEDIUM) && ($level != HIGH)) {	carp "safe_level: Specified level ($level) not STANDARD, MEDIUM or HIGH - ignoring\n" if $^W;      } else {	# Dont allow this on perl 5.005 or earlier	if ($] < 5.006 && $level != STANDARD) {	  # Cant do MEDIUM or HIGH checks	  croak "Currently requires perl 5.006 or newer to do the safe checks";	}	# Check that we are allowed to change level	# Silently ignore if we can not.        $LEVEL = $level if _can_do_level($level);      }    }    return $LEVEL;  }}=item TopSystemUIDThis is the highest UID on the current system that refers to a rootUID. This is used to make sure that the temporary directory isowned by a system UID (C<root>, C<bin>, C<sys> etc) rather thansimply by root.This is required since on many unix systems C</tmp> is not o

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -