Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 4.12 File Size 111


4.12 File Siz e


Thest_sizemember of thestatstructurecontains the size of the file in bytes. This
field is meaningful only for regular files, directories, and symbolic links.

FreeBSD 8.0, Mac OS X 10.6.8, and Solaris 10 also define the file size for a pipe as the number of
bytes that areavailable for reading from the pipe.We’ll discuss pipes in Section 15.2.

For a regular file, a file size of 0 is allowed.We’ll get an end-of-file indication on the
first read of the file. For a directory,the file size is usually a multiple of a number,such
as 16 or 512.We talk about reading directories in Section 4.22.
For a symbolic link, the file size is the number of bytes in the filename. For
example, in the following case, the file size of 7 is the length of the pathnameusr/lib:
lrwxrwxrwx 1 root 7 Sep 25 07:14 lib -> usr/lib
(Note that symbolic links do not contain the normal C null byte at the end of the name,
as the length is always specified byst_size.)
Most contemporary UNIX systems provide the fields st_blksize and
st_blocks.The first is the preferred block size for I/O for the file, and the latter is the
actual number of 512-byte blocks that areallocated. Recall from Section 3.9 that we
encountered the minimum amount of time required to read a file when we used
st_blksizefor thereadoperations. The standardI/O library,which we describe in
Chapter 5, also tries to read or writest_blksizebytes at a time, for efficiency.

Be awarethat different versions of the UNIX System use units other than 512-byte blocks for
st_blocks.Use of this value is nonportable.

Holes in a File


In Section 3.6, we mentioned that a regular file can contain ‘‘holes.’’Weshowed an
example of this in Figure3.2. Holes arecreated by seeking past the current end of file
and writing some data. As an example, consider the following:
$ls -l core
-rw-r--r-- 1 sar 8483248 Nov 18 12:18 core
$du -s core
272 core
The size of the filecoreis slightly morethan 8 MB, yet theducommand reports that
the amount of disk space used by the file is 272 512-byte blocks (139,264 bytes).
Obviously,this file has many holes.

Theducommand on many BSD-derived systems reports the number of 1,024-byte blocks.
Solaris reports the number of 512-byte blocks. On Linux, the units reported depend on the
whether thePOSIXLY_CORRECTenvironment is set. When it is set, theducommand reports
1,024-byte block units; when it is not set, the command reports 512-byte block units.

As we mentioned in Section 3.6, thereadfunction returns data bytes of 0 for any
byte positions that have not been written. If we execute the following command, we
can see that the normal I/O operations read up through the size of the file:
Free download pdf