temp.html
来自「perl教程」· HTML 代码 · 共 971 行 · 第 1/4 页
HTML
971 行
that are registered for removal to be removed. This happens automatically
when the process exits but can be triggered manually if the caller is sure
that none of the temp files are required. This method can be registered as
an Apache callback.</p>
</dd>
<dd>
<p>On OSes where temp files are automatically removed when the temp file
is closed, calling this function will have no effect other than to remove
temporary directories (which may include temporary files).</p>
</dd>
<dd>
<pre>
<span class="variable">File::Temp::cleanup</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>Not exported by default.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="package_variables">PACKAGE VARIABLES</a></h1>
<p>These functions control the global state of the package.</p>
<dl>
<dt><strong><a name="item_safe_level"><strong>safe_level</strong></a></strong>
<dd>
<p>Controls the lengths to which the module will go to check the safety of the
temporary file or directory before proceeding.
Options are:</p>
</dd>
<dl>
<dt><strong><a name="item_standard">STANDARD</a></strong>
<dd>
<p>Do the basic security measures to ensure the directory exists and
is writable, that the <a href="../../lib/Pod/perlfunc.html#item_umask"><code>umask()</code></a> is fixed before opening of the file,
that temporary files are opened only if they do not already exist, and
that possible race conditions are avoided. Finally the <a href="#item_unlink0">unlink0</a>
function is used to remove files safely.</p>
</dd>
</li>
<dt><strong><a name="item_medium">MEDIUM</a></strong>
<dd>
<p>In addition to the STANDARD security, the output directory is checked
to make sure that it is owned either by root or the user running the
program. If the directory is writable by group or by other, it is then
checked to make sure that the sticky bit is set.</p>
</dd>
<dd>
<p>Will not work on platforms that do not support the <code>-k</code> test
for sticky bit.</p>
</dd>
</li>
<dt><strong><a name="item_high">HIGH</a></strong>
<dd>
<p>In addition to the MEDIUM security checks, also check for the
possibility of ``chown() giveaway'' using the <a href="../../lib/POSIX.html">POSIX</a>
<code>sysconf()</code> function. If this is a possibility, each directory in the
path is checked in turn for safeness, recursively walking back to the
root directory.</p>
</dd>
<dd>
<p>For platforms that do not support the <a href="../../lib/POSIX.html">POSIX</a>
<code>_PC_CHOWN_RESTRICTED</code> symbol (for example, Windows NT) it is
assumed that ``chown() giveaway'' is possible and the recursive test
is performed.</p>
</dd>
</li>
</dl>
<p>The level can be changed as follows:</p>
<pre>
<span class="variable">File::Temp</span><span class="operator">-></span><span class="variable">safe_level</span><span class="operator">(</span> <span class="variable">File::Temp::HIGH</span> <span class="operator">);</span>
</pre>
<p>The level constants are not exported by the module.</p>
<p>Currently, you must be running at least perl v5.6.0 in order to
run with MEDIUM or HIGH security. This is simply because the
safety tests use functions from <a href="../../lib/Fcntl.html">Fcntl</a> that are not
available in older versions of perl. The problem is that the version
number for Fcntl is the same in perl 5.6.0 and in 5.005_03 even though
they are different versions.</p>
<p>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 will
be ignored. The decision to ignore rather than raise an exception
allows portable programs to be written with high security in mind
for the systems that can support this without those programs failing
on systems where the extra tests are irrelevant.</p>
<p>If you really need to see whether the change has been accepted
simply examine the return value of <a href="#item_safe_level"><code>safe_level</code></a>.</p>
<pre>
<span class="variable">$newlevel</span> <span class="operator">=</span> <span class="variable">File::Temp</span><span class="operator">-></span><span class="variable">safe_level</span><span class="operator">(</span> <span class="variable">File::Temp::HIGH</span> <span class="operator">);</span>
<span class="keyword">die</span> <span class="string">"Could not change to high security"</span>
<span class="keyword">if</span> <span class="variable">$newlevel</span> <span class="operator">!=</span> <span class="variable">File::Temp::HIGH</span><span class="operator">;</span>
</pre>
<dt><strong><a name="item_topsystemuid">TopSystemUID</a></strong>
<dd>
<p>This is the highest UID on the current system that refers to a root
UID. This is used to make sure that the temporary directory is
owned by a system UID (<code>root</code>, <code>bin</code>, <code>sys</code> etc) rather than
simply by root.</p>
</dd>
<dd>
<p>This is required since on many unix systems <code>/tmp</code> is not owned
by root.</p>
</dd>
<dd>
<p>Default is to assume that any UID less than or equal to 10 is a root
UID.</p>
</dd>
<dd>
<pre>
<span class="variable">File::Temp</span><span class="operator">-></span><span class="variable">top_system_uid</span><span class="operator">(</span><span class="number">10</span><span class="operator">);</span>
<span class="keyword">my</span> <span class="variable">$topid</span> <span class="operator">=</span> <span class="variable">File::Temp</span><span class="operator">-></span><span class="variable">top_system_uid</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>This value can be adjusted to reduce security checking if required.
The value is only relevant when <a href="#item_safe_level"><code>safe_level</code></a> is set to MEDIUM or higher.</p>
</dd>
</li>
<dt><strong><a name="item__keep_all"><strong>$KEEP_ALL</strong></a></strong>
<dd>
<p>Controls whether temporary files and directories should be retained
regardless of any instructions in the program to remove them
automatically. This is useful for debugging but should not be used in
production code.</p>
</dd>
<dd>
<pre>
<span class="variable">$File::Temp::KEEP_ALL</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Default is for files to be removed as requested by the caller.</p>
</dd>
<dd>
<p>In some cases, files will only be retained if this variable is true
when the file is created. This means that you can not create a temporary
file, set this variable and expect the temp file to still be around
when the program exits.</p>
</dd>
</li>
<dt><strong><a name="item__debug"><strong>$DEBUG</strong></a></strong>
<dd>
<p>Controls whether debugging messages should be enabled.</p>
</dd>
<dd>
<pre>
<span class="variable">$File::Temp::DEBUG</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Default is for debugging mode to be disabled.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="warning">WARNING</a></h1>
<p>For maximum security, endeavour always to avoid ever looking at,
touching, or even imputing the existence of the filename. You do not
know that that filename is connected to the same file as the handle
you have, and attempts to check this can only trigger more race
conditions. It's far more secure to use the filehandle alone and
dispense with the filename altogether.</p>
<p>If you need to pass the handle to something that expects a filename
then, on a unix system, use <a href="../../lib/Pod/perlfunc.html#item_fileno"><code>"/dev/fd/" . fileno($fh)</code></a> for arbitrary
programs, or more generally <a href="../../lib/Pod/perlfunc.html#item_fileno"><code>"+<=&" . fileno($fh)</code></a> for Perl
programs. You will have to clear the close-on-exec bit on that file
descriptor before passing it to another process.</p>
<pre>
<span class="keyword">use</span> <span class="variable">Fcntl</span> <span class="string">qw/F_SETFD F_GETFD/</span><span class="operator">;</span>
<span class="keyword">fcntl</span><span class="operator">(</span><span class="variable">$tmpfh</span><span class="operator">,</span> <span class="variable">F_SETFD</span><span class="operator">,</span> <span class="number">0</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Can't clear close-on-exec flag on temp fh: $!\n"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="temporary_files_and_nfs">Temporary files and NFS</a></h2>
<p>Some problems are associated with using temporary files that reside
on NFS file systems and it is recommended that a local filesystem
is used whenever possible. Some of the security tests will most probably
fail when the temp file is not local. Additionally, be aware that
the performance of I/O operations over NFS will not be as good as for
a local disk.</p>
<p>
</p>
<h2><a name="forking">Forking</a></h2>
<p>In some cases files created by File::Temp are removed from within an
END block. Since END blocks are triggered when a child process exits
(unless <code>POSIX::_exit()</code> is used by the child) File::Temp takes care
to only remove those temp files created by a particular process ID. This
means that a child will not attempt to remove temp files created by the
parent process.</p>
<p>
</p>
<h2><a name="binmode">BINMODE</a></h2>
<p>The file returned by File::Temp will have been opened in binary mode
if such a mode is available. If that is not correct, use the <a href="../../lib/Pod/perlfunc.html#item_binmode"><code>binmode()</code></a>
function to change the mode of the filehandle.</p>
<p>
</p>
<hr />
<h1><a name="history">HISTORY</a></h1>
<p>Originally began life in May 1999 as an XS interface to the system
<a href="#item_mkstemp"><code>mkstemp()</code></a> function. In March 2000, the OpenBSD <a href="#item_mkstemp"><code>mkstemp()</code></a> code was
translated to Perl for total control of the code's
security checking, to ensure the presence of the function regardless of
operating system and to help with portability. The module was shipped
as a standard part of perl from v5.6.1.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/auto/POSIX/tmpnam.html">tmpnam in the POSIX manpage</a>, <a href="../../lib/auto/POSIX/tmpfile.html">tmpfile in the POSIX manpage</a>, <a href="../../lib/File/Spec.html">the File::Spec manpage</a>, <a href="../../lib/File/Path.html">the File::Path manpage</a></p>
<p>See <a href="../../lib/IO/File.html">the IO::File manpage</a> and <a href="../../File/MkTemp.html">the File::MkTemp manpage</a>, <a href="../../Apachae/TempFile.html">the Apachae::TempFile manpage</a> for
different implementations of temporary file handling.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Tim Jenness <<a href="mailto:tjenness@cpan.org">tjenness@cpan.org</a>></p>
<p>Copyright (C) 1999-2005 Tim Jenness and the UK Particle Physics and
Astronomy Research Council. All Rights Reserved. This program is free
software; you can redistribute it and/or modify it under the same
terms as Perl itself.</p>
<p>Original Perl implementation loosely based on the OpenBSD C code for
mkstemp(). Thanks to Tom Christiansen for suggesting that this module
should be written and providing ideas for code improvements and
security enhancements.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?