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

📄 ch13_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>Links and Files (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch13_02.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch13_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">13.3. Links and Files</h2><p><a name="INDEX-890" />Tounderstand more about what's going on with files anddirectories, it helps to understand the<a name="INDEX-891" />Unix model of files anddirectories, even if your non-Unix system doesn't work inexactly this way. As usual, there's more to the story thanwe're able to explain here, so check any good book on Unixinternal details if you need the full story.</p><p>A <em class="firstterm">mounted volume</em><a name="INDEX-892" /> is a hard disk drive (or something elsethat works more-or-less like that, such as a disk partition, a floppydisk, a CD-ROM, or a DVD-ROM). It may contain any number of files anddirectories. Each file is stored in a numbered<em class="firstterm">inode</em><a name="INDEX-893" />,which we can think of as a particular piece of disk real estate. Onefile might be stored in inode 613, while another is in inode 7033.</p><p>To locate a particular file, though, we'll have to look it upin a directory. A<a name="INDEX-894" />directory is aspecial kind of file, maintained by the system. Essentially, it is atable of filenames and their inode numbers.<a href="#FOOTNOTE-286">[286]</a> Along with the other things in thedirectory, there are always two special directory entries. One is<tt class="literal">.</tt> (called "<a name="INDEX-895" />dot"), which is the name ofthat very directory; and the other is<tt class="literal">..</tt><a name="INDEX-896" /> <a name="INDEX-897" />("dot-dot"), which is the directory one step higher inthe hierarchy (i.e., the directory's parentdirectory).<a href="#FOOTNOTE-287">[287]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-286" /><p>[286]On Unixsystems (others don't generally have inodes, hard links, andsuch), you can use the <i class="command">ls </i>command's<em class="emphasis">-i</em> option to see files' inode numbers. Trya command like <i class="command">ls -ail</i>. When two or more inodenumbers are the same for multiple items on a given filesystem,there's really just one file involved, one piece of thedisk.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-287" /><p>[287]The Unix system <em class="emphasis">root</em>directory has no parent. In that directory, <tt class="literal">..</tt> isthe same directory as <tt class="literal">.</tt>, which is the system<em class="emphasis">root</em> directory itself. </p> </blockquote><p><a href="ch13_03.htm#lperl3-CHP-13-FIG-1">Figure 13-1</a> provides an illustration of two inodes.One is for a file called <em class="filename">chicken</em>, and the otheris Barney's directory of poems,<em class="filename">/home/barney/poems</em>, which contains that file.The file is stored in inode 613, while the directory is stored ininode 919. (The directory's own name,<em class="filename">poems</em>, doesn't appear in the illustration,because that's stored in another directory.) The directorycontains entries for three files (including<em class="filename">chicken</em>) and two directories (one of which isthe reference back to the directory itself, in inode 919), along witheach item's inode number.</p><a name="lperl3-CHP-13-FIG-1" /><div class="figure"><img height="134" alt="Figure 13-1" src="figs/lrnp_1301.gif" width="342" /></div><h4 class="objtitle">Figure 13-1. The chicken before the egg</h4><p>When it's time to make a new file in a given directory, thesystem adds an entry with the file's name and the number of anew inode. How can the system tell that a particular inode isavailable, though? Each inode holds a number called its<em class="firstterm">link count</em><a name="INDEX-898" />. The link count is always zero if theinode isn't listed in any directory, so any inode with a linkcount of zero is available for new file storage. When the inode isadded to a directory, the link count is incremented; when the listingis removed, the link count is decremented. For the file<em class="filename">chicken</em> as illustrated above, the inode count of1 is shown in the box above the inode's data.</p><p>But some inodes have more than one listing. For example, we'vealready seen that each directory entry includes <tt class="literal">.</tt>,which points back to that directory's own inode. So the linkcount for a directory should always be at least two: its listing inits parent directory and its listing in itself. In addition, if ithas subdirectories, each of those will add a link, since each willcontain <tt class="literal">..</tt>.<a href="#FOOTNOTE-288">[288]</a> In <a href="ch13_03.htm#lperl3-CHP-13-FIG-1">Figure 13-1</a>, the directory's inode count of<tt class="literal">2</tt> is shown in the box above its data. A link countis the number of true names for the inode.<a href="#FOOTNOTE-289">[289]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-288" /><p>[288]This implies that thelink count of a directory is always equal to two plus the number ofdirectories it contains. On some systems that's true, in fact,but some other systems work differently. </p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-289" /><p>[289]In thetraditional output of <i class="command">ls -l</i>, the number of hardlinks to the item appears just to the right of the permission flags(like "<tt class="literal">-rwxr-xr-x</tt>"). Now you know whythis number is more than one for directories and nearly always<tt class="literal">1</tt> for ordinary files.</p> </blockquote><p>Could an ordinary file inode have more than one listing in thedirectory? It certainly could. Suppose that, working in the directoryshown above, Barney uses the Perl's<tt class="literal">link</tt><a name="INDEX-899" /> function to create a new link:</p><blockquote><pre class="code">link "chicken", "egg"  or warn "can't link chicken to egg: $!";</pre></blockquote><p>This is similar to typing "<tt class="literal">ln chickenegg</tt>" at the Unix shell prompt. If<tt class="literal">link</tt> succeeds, it returns true. If it fails, itreturns false and sets <tt class="literal">$!</tt>, which Barney ischecking in the error message. After this runs, the name<em class="filename">egg</em> is another name for the file<em class="filename">chicken</em>, and vice versa; neither name is"more real" than the other, and (as you may have guessed)it would take some detective work to find out which came first. <a href="ch13_03.htm#lperl3-CHP-13-FIG-2">Figure 13-2</a> shows a picture of the new situation, wherethere are two links to inode 613.</p><a name="lperl3-CHP-13-FIG-2" /><div class="figure"><img height="134" alt="Figure 13-2" src="figs/lrnp_1302.gif" width="342" /></div><h4 class="objtitle">Figure 13-2. The egg is linked to the chicken</h4><p>These two filenames are thus talking about the same place on thedisk. If the file <em class="filename">chicken</em> holds 200 bytes ofdata, <em class="filename">egg</em> holds the same 200 bytes, for a totalof 200 bytes (since it's really just one file with two names).If Barney appends a new line of text to file<em class="filename">egg</em>, that line will also appear at the end of<em class="filename">chicken.</em><a href="#FOOTNOTE-290">[290]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-290" /><p>[290]If you experiment withmaking links and changing text files, be aware that most text editorsdon't edit the file "in place" but instead save amodified copy. If Barney were to edit <em class="filename">egg </em>with atext editor, he'd most likely end up with a new file called<em class="filename">egg </em>and the old file called<em class="filename">chicken</em> -- two separate files, rather thantwo links to the same file.</p> </blockquote><p>Now, if Barney were to accidentally (or intentionally) delete<em class="filename">chicken</em>, that data will not belost -- it's still available under the name<em class="filename">egg</em>. And vice versa: if he were to delete<em class="filename">egg</em>, he'd still have<em class="filename">chicken</em>. Of course, if he deletes both of them,the data will be lost.<a href="#FOOTNOTE-291">[291]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-291" /><p>[291]Although the systemwon't necessarily overwrite this inode right away,there's no easy way in general to get the data back once thelink count has gone to zero. Have you made a backup recently?</p></blockquote>

⌨️ 快捷键说明

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