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

📄 ch09_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 3 页
字号:
><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a symbolic link</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-p</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">mode</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a named pipe (FIFO)</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-S</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">mode</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a socket</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-b</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">rdev</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a block special file</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-c</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">rdev</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a character special file</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-t</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">rdev</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Filehandle is opened to a tty</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-u</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">mode</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File has setuid bit set</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-g</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">mode</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File has setgid bit set</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-k</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">mode</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File has sticky bit set</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-T</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">N/A</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a text file</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-B</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">N/A</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">File is a binary file (opposite of <CODECLASS="literal">-T</CODE>)</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"></P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-M</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">mtime</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Age of file in days when script started</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-A</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">atime</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Same for access time</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para"><CODECLASS="literal">-C</CODE></P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">ctime</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Same for inode change time (not creation)</P></TD></TR></TBODY></TABLE><PCLASS="para">The <CODECLASS="literal">stat</CODE> and the <CODECLASS="literal">-X</CODE> operators cache the values that the <EMCLASS="emphasis">stat </EM>(2) system call returned. If you then call <CODECLASS="literal">stat</CODE> or a <CODECLASS="literal">-X</CODE> operator with the special filehandle <CODECLASS="literal">_</CODE> (a single underscore), it won't call <CODECLASS="literal">stat</CODE> again but will instead return information from its cache. This lets you test many properties of a single file without calling <EMCLASS="emphasis">stat </EM>(2) many times or introducing a race condition:</P><PRECLASS="programlisting">open( F, &quot;&lt; $filename&quot; )    or die &quot;Opening $filename: $!\n&quot;;unless (-s F &amp;&amp; -T _) {    die &quot;$filename doesn't have text in it.\n&quot;;}</PRE><PCLASS="para">The <CODECLASS="literal">stat</CODE> call just returns the information in one inode, though. How do we get a list of the contents of a directory? For that, Perl provides <CODECLASS="literal">opendir</CODE><ACLASS="indexterm"NAME="ch09-idx-1000004346-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004346-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004346-2"></A>, <CODECLASS="literal">readdir</CODE>, and <CODECLASS="literal">closedir</CODE>:</P><PRECLASS="programlisting">opendir(DIRHANDLE, &quot;/usr/bin&quot;) or die &quot;couldn't open /usr/bin : $!&quot;;while ( defined ($filename = readdir(DIRHANDLE)) ) {    print &quot;Inside /usr/bin is something called $filename\n&quot;;}closedir(DIRHANDLE);<ACLASS="indexterm"NAME="ch09-idx-1000004345-0"></A></PRE><PCLASS="para">These directory reading functions are designed to look like the file open and close functions. Where <CODECLASS="literal">open</CODE> takes a filehandle, though, <CODECLASS="literal">opendir</CODE> takes a directory handle. They look the same (a bare word) but they are different: you can <CODECLASS="literal">open(BIN,</CODE> <CODECLASS="literal">&quot;/a/file&quot;)</CODE> and <CODECLASS="literal">opendir(BIN,</CODE> <CODECLASS="literal">&quot;/a/dir&quot;)</CODE> and Perl won't get confused. You might, but Perl won't. Because filehandles and directory handles are different, you can't use the &lt; &gt; operator to read from a directory handle.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch09-idx-1000004347-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004347-1"></A><ACLASS="indexterm"NAME="ch09-idx-1000004347-2"></A>The filenames in a directory aren't necessarily stored alphabetically. If you want to get an alphabetical list of files, you'll have to read all the entries and sort them yourself.</P><PCLASS="para">The separation of directory information from inode information can create some odd situations. Operations that change directory only require write permission on the directory, not on the file. Most operations that change information in the file's data require write permission to the file. Operations that alter the permissions of the file require that the caller be the file's owner or the superuser. This can lead to the interesting situation of being able to delete a file you can't read, or write to a file you can't remove.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch09-idx-1000004348-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004348-1"></A>Although these situations make the filesystem structure seem odd at first, they're actually the source of much of Unix's power. Links, two filenames that refer to the same file, are now extremely simple. The two directory entries just list the same inode number. The inode structure includes a count of the number of directory entries referring to the file (<CODECLASS="literal">nlink</CODE> in the values returned by <CODECLASS="literal">stat</CODE>), but it lets the operating system store and maintain only one copy of the modification times, size, and other file attributes. When one directory entry is <CODECLASS="literal">unlink</CODE>ed, data blocks are only deleted if the directory entry was the last one that referred to the file's inode &nbsp;-  and no processes still have the file open. You can <CODECLASS="literal">unlink</CODE> an open file, but its disk space won't be released until the last close.</P><PCLASS="para">Links come in two forms. The kind described above, where two directory entries list the same inode number (like <EMCLASS="emphasis">vi</EM> and <EMCLASS="emphasis">nvi</EM> in the earlier table), are called <EMCLASS="emphasis">hard links</EM><ACLASS="indexterm"NAME="ch09-idx-1000004349-0"></A>. The operating system cannot tell the first directory entry of a file (the one created when the file was created) from any subsequent hard links to it. The other kind, <EMCLASS="emphasis">soft</EM> or <EMCLASS="emphasis">symbolic links</EM><ACLASS="indexterm"NAME="ch09-idx-1000004350-0"></A><ACLASS="indexterm"NAME="ch09-idx-1000004350-1"></A>, are very different. A soft link is a special type of file whose data block stores the filename the file is linked to. Soft links have a different <CODECLASS="literal">mode</CODE> value, indicating they're not regular files. The operating system, when asked to <CODECLASS="literal">open</CODE> a soft link, instead opens the filename contained in the data block.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch09-chap09_executive_0">Executive Summary</A></H3><PCLASS="para">Filenames are kept in a directory, separate from the size, protections, and other metadata kept in an inode.</P><PCLASS="para">The <CODECLASS="literal">stat</CODE> function returns the inode information (metadata).</P><PCLASS="para"><CODECLASS="literal">opendir</CODE>, <CODECLASS="literal">readdir</CODE>, and friends provide access to filenames in a directory through a <EMCLASS="emphasis">directory handle</EM>.</P><PCLASS="para">Directory handles look like filehandles, but they are not the same. In particular, you can't use &lt; &gt; on directory handles.</P><PCLASS="para">The permissions on a directory determine whether you can read and write the list of filenames. The permissions on a file determine whether you can change the file's metadata or contents.</P><PCLASS="para">Three different times are stored in an inode. None of them is the file's creation time.</P></DIV></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_21.htm"TITLE="8.20. Program: laston"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 8.20. Program: laston"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch09_02.htm"TITLE="9.1. Getting and Setting Timestamps"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 9.1. Getting and Setting Timestamps"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">8.20. Program: laston</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">9.1. Getting and Setting Timestamps</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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