Appendix B. Low-Level I/O

image

C PROGRAMMERS ON GNU/LINUX HAVE TWO SETS OF INPUT/OUTPUT functions at their disposal. The standard C library provides I/O functions: printf, fopen, and so on.[1] The Linux kernel itself provides another set of I/O operations that operate at a lower level than the C library functions.

[1] The C++ standard library provides iostreams with similar functionality. The standard C library is also available in the C++ language.

Because this book is for people who already know the C language, we’ll assume that you have encountered and know how to use the C library I/O functions.

Often there are good reasons to use Linux’s low-level I/O functions. Many of these are kernel system calls[2] and provide the most direct access to underlying system capabilities that is available to application programs. In fact, the standard C library I/O routines are implemented on top of the Linux low-level I/O system calls. Using the latter is usually the most efficient way to perform input and output operations—and is sometimes more convenient, too.

[2] See Chapter 8, “Linux System Calls,” for an explanation of the difference between a system call and an ordinary function call.

Throughout this book, we assume that you’re familiar with the calls described in this appendix. You may already be familiar with them because they’re nearly the same as those provided on other UNIX and UNIX-like operating systems (and on the Win32 platform as well). If you’re not familiar with them, however, read on; you’ll find the rest of the book much easier to understand if you familiarize yourself with this material first.

B.1 Reading and Writing Data

The first I/O function you likely encountered when you first learned the C language was printf. This formats a text string and then prints it to standard output. The generalized version, fprintf, can print the text to a stream other than standard output. A stream is represented by a FILE* pointer. You obtain a FILE* pointer by opening a file with fopen. When you’re done, you can close it with fclose. In addition to fprintf, you can use such functions as fputc, fputs, and fwrite to write data to the stream, or fscanf, fgetc, fgets, and fread to read data.

With the Linux low-level I/O operations, you use a handle called a file descriptor instead of a FILE* pointer. A file descriptor is an integer value that refers to a particular instance of an open file in a single process. It can be open for reading, for writing, or for both reading and writing. A file descriptor doesn’t have to refer to an open file; it can represent a connection with another system component that is capable of sending or receiving data. For example, a connection to a hardware device is represented by a file descriptor (see Chapter 6, “Devices”), as is an open socket (see Chapter 5, “Interprocess Communication,” Section 5.5, “Sockets”) or one end of a pipe (see Section 5.4, “Pipes”).

Include the header files <fcntl.h>, <sys/types.h>, <sys/stat.h>, and <unistd.h> if you use any of the low-level I/O functions described here.

B.1.1 Opening a File

To open a file and produce a file descriptor that can access that file, use the open call. It takes as arguments the path name of the file to open, as a character string, and flags specifying how to open it. You can use open to create a new file; if you do, pass a third argument that specifies the access permissions to set for the new file.

If the second argument is O_RDONLY, the file is opened for reading only; an error will result if you subsequently try to write to the resulting file descriptor. Similarly, O_WRONLY causes the file descriptor to be write-only. Specifying O_RDWR produces a file descriptor that can be used both for reading and for writing. Note that not all files may be opened in all three modes. For instance, the permissions on a file might forbid a particular process from opening it for reading or for writing; a file on a read-only device such as a CD-ROM drive may not be opened for writing.

You can specify additional options by using the bitwise or of this value with one or more flags. These are the most commonly used values:

• Specify O_TRUNC to truncate the opened file, if it previously existed. Data written to the file descriptor will replace previous contents of the file.

• Specify O_APPEND to append to an existing file. Data written to the file descriptor will be added to the end of the file.

• Specify O_CREAT to create a new file. If the filename that you provide to open does not exist, a new file will be created, provided that the directory containing it exists and that the process has permission to create files in that directory. If the file already exists, it is opened instead.

• Specify O_EXCL with O_CREAT to force creation of a new file. If the file already exists, the open call will fail.

If you call open with O_CREAT, provide an additional third argument specifying the permissions for the new file. See Chapter 10, “Security,” Section 10.3, “File System Permissions,” for a description of permission bits and how to use them.

For example, the program in Listing B.1 creates a new file with the filename specified on the command line. It uses the O_EXCL flag with open, so if the file already exists, an error occurs. The new file is given read and write permissions for the owner and owning group, and read permissions only for others. (If your umask is set to a nonzero value, the actual permissions may be more restrictive.)

Listing B.1. (create-file.c) Create a New File

image

Here’s the program in action:

image

Note that the length of the new file is 0 because the program didn’t write any data to it.

B.1.2 Closing File Descriptors

When you’re done with a file descriptor, close it with close. In some cases, such as the program in Listing B.1, it’s not necessary to call close explicitly because Linux closes all open file descriptors when a process terminates (that is, when the program ends). Of course, once you close a file descriptor, you should no longer use it.

Closing a file descriptor may cause Linux to take a particular action, depending on the nature of the file descriptor. For example, when you close a file descriptor for a network socket, Linux closes the network connection between the two computers communicating through the socket.

Linux limits the number of open file descriptors that a process may have open at a time. Open file descriptors use kernel resources, so it’s good to close file descriptors when you’re done with them. A typical limit is 1,024 file descriptors per process. You can adjust this limit with the setrlimit system call; see Section 8.5, “getrlimit and setrlimit: Resource Limits,” for more information.

B.1.3 Writing Data

Write data to a file descriptor using the write call. Provide the file descriptor, a pointer to a buffer of data, and the number of bytes to write. The file descriptor must be open for writing. The data written to the file need not be a character string; write copies arbitrary bytes from the buffer to the file descriptor.

The program in Listing B.2 appends the current time to the file specified on the command line. If the file doesn’t exist, it is created. This program also uses the time, localtime, and asctime functions to obtain and format the current time; see their respective man pages for more information.

Listing B.2. (timestamp.c) Append a Timestamp to a File

image

Here’s how the timestamp program works:

image

Note that the first time we invoke timestamp, it creates the file tsfile, while the second time it appends to it.

The write call returns the number of bytes that were actually written, or -1 if an error occurred. For certain kinds of file descriptors, the number of bytes actually written may be less than the number of bytes requested. In this case, it’s up to you to call write again to write the rest of the data. The function in Listing B.2 demonstrates how you might do this. Note that for some applications, you may have to check for special conditions in the middle of the writing operation. For example, if you’re writing to a network socket, you’ll have to augment this function to detect whether the network connection was closed in the middle of the write operation, and if it has, to react appropriately.

Listing B.3. (write-all.c) Write All of a Buffer of Data

image

B.1.4 Reading Data

The corresponding call for reading data is read. Like write, it takes a file descriptor, a pointer to a buffer, and a count. The count specifies how many bytes are read from the file descriptor into the buffer. The call to read returns -1 on error or the number of bytes actually read. This may be smaller than the number of bytes requested, for example, if there aren’t enough bytes left in the file.

Listing B.4 provides a simple demonstration of read. The program prints a hexadecimal dump of the contents of the file specified on the command line. Each line displays the offset in the file and the next 16 bytes.

Listing B.4. (hexdump.c) Print a Hexadecimal Dump of a File

image

Here’s hexdump in action. It’s shown printing out a dump of its own executable file:

image

Your output may be different, depending on the compiler you used to compile hexdump and the compilation flags you specified.

B.1.5 Moving Around a File

A file descriptor remembers its position in a file. As you read from or write to the file descriptor, its position advances corresponding to the number of bytes you read or write. Sometimes, however, you’ll need to move around a file without reading or writing data. For instance, you might want to write over the middle of a file without modifying the beginning, or you might want to jump back to the beginning of a file and reread it without reopening it.

The lseek call enables you to reposition a file descriptor in a file. Pass it the file descriptor and two additional arguments specifying the new position.

• If the third argument is SEEK_SET, lseek interprets the second argument as a position, in bytes, from the start of the file.

• If the third argument is SEEK_CUR, lseek interprets the second argument as an offset, which may be positive or negative, from the current position.

• If the third argument is SEEK_END, lseek interprets the second argument as an offset from the end of the file. A positive value indicates a position beyond the end of the file.

The call to lseek returns the new position, as an offset from the beginning of the file. The type of the offset is off_t. If an error occurs, lseek returns -1. You can’t use lseek with some types of file descriptors, such as socket file descriptors.

If you want to find the position of a file descriptor in a file without changing it, specify a 0 offset from the current position—for example:


off_t position = lseek (file_descriptor, 0, SEEK_CUR); 

Linux enables you to use lseek to position a file descriptor beyond the end of the file. Normally, if a file descriptor is positioned at the end of a file and you write to the file descriptor, Linux automatically expands the file to make room for the new data. If you position a file descriptor beyond the end of a file and then write to it, Linux first expands the file to accommodate the “gap” that you created with the lseek operation and then writes to the end of it. This gap, however, does not actually occupy space on the disk; instead, Linux just makes a note of how long it is. If you later try to read from the file, it appears to your program that the gap is filled with 0 bytes.

Using this behavior of lseek, it’s possible to create extremely large files that occupy almost no disk space. The program lseek-huge in Listing B.5 does this. It takes as command-line arguments a filename and a target file size, in megabytes. The program opens a new file, advances past the end of the file using lseek, and then writes a single 0 byte before closing the file.

Listing B.5. (lseek-huge.c) Create Large Files with lseek

image

Using lseek-huge, we’ll make a 1GB (1024MB) file. Note the free space on the drive before and after the operation.

image

No appreciable disk space is consumed, despite the enormous size of bigfile. Still, if we open bigfile and read from it, it appears to be filled with 1GB worth of 0s. For instance, we can examine its contents with the hexdump program of Listing B.4.

image

If you run this yourself, you’ll probably want to kill it with Ctrl+C, rather than watching it print out 230 0 bytes.

Note that these magic gaps in files are a special feature of the ext2 file system that’s typically used for GNU/Linux disks. If you try to use lseek-huge to create a file on some other type of file system, such as the fat or vfat file systems used to mount DOS and Windows partitions, you’ll find that the resulting file does actually occupy the full amount of disk space.

Linux does not permit you to rewind before the start of a file with lseek.

B.2 stat

Using open and read, you can extract the contents of a file. But how about other information? For instance, invoking ls -l displays, for the files in the current directory, such information as the file size, the last modification time, permissions, and the owner.

The stat call obtains this information about a file. Call stat with the path to the file you’re interested in and a pointer to a variable of type struct stat. If the call to stat is successful, it returns 0 and fills in the fields of the structure with information about that file; otherwise, it returns -1.

These are the most useful fields in struct stat:

st_mode contains the file’s access permissions. File permissions are explained in Section 10.3, “File System Permissions.”

• In addition to the access permissions, the st_mode field encodes the type of the file in higher-order bits. See the text immediately following this bulleted list for instructions on decoding this information.

st_uid and st_gid contain the IDs of the user and group, respectively, to which the file belongs. User and group IDs are described in Section 10.1, “Users and Groups.”

st_size contains the file size, in bytes.

st_atime contains the time when this file was last accessed (read or written).

st_mtime contains the time when this file was last modified.

These macros check the value of the st_mode field value to figure out what kind of file you’ve invoked stat on. A macro evaluates to true if the file is of that type.

S_ISBLK (mode)  block device

S_ISCHR (mode)  character device

S_ISDIR (mode)  directory

S_ISFIFO (mode)  fifo (named pipe)

S_ISLNK (mode)  symbolic link

S_ISREG (mode)  regular file

S_ISSOCK (mode)  socket

The st_dev field contains the major and minor device number of the hardware device on which this file resides. Device numbers are discussed in Chapter 6. The major device number is shifted left 8 bits; the minor device number occupies the least significant 8 bits. The st_ino field contains the inode number of this file. This locates the file in the file system.

If you call stat on a symbolic link, stat follows the link and you can obtain the information about the file that the link points to, not about the symbolic link itself. This implies that S_ISLNK will never be true for the result of stat. Use the lstat function if you don’t want to follow symbolic links; this function obtains information about the link itself rather than the link’s target. If you call lstat on a file that isn’t a symbolic link, it is equivalent to stat. Calling stat on a broken link (a link that points to a nonexistent or inaccessible target) results in an error, while calling lstat on such a link does not.

If you already have a file open for reading or writing, call fstat instead of stat. This takes a file descriptor as its first argument instead of a path.

Listing B.6 presents a function that allocates a buffer large enough to hold the contents of a file and then reads the file into the buffer. The function uses fstat to determine the size of the buffer that it needs to allocate and also to check that the file is indeed a regular file.

Listing B.6. (read-file.c) Read a File into a Buffer

image

image

B.3 Vector Reads and Writes

The write call takes as arguments a pointer to the start of a buffer of data and the length of that buffer. It writes a contiguous region of memory to the file descriptor. However, a program often will need to write several items of data, each residing at a different part of memory. To use write, the program either will have to copy the items into a single memory region, which obviously makes inefficient use of CPU cycles and memory, or will have to make multiple calls to write.

For some applications, multiple calls to write are inefficient or undesirable. For example, when writing to a network socket, two calls to write may cause two packets to be sent across the network, whereas the same data could be sent in a single packet if a single call to write were possible.

The writev call enables you to write multiple discontiguous regions of memory to a file descriptor in a single operation. This is called a vector write. The cost of using writev is that you must set up a data structure specifying the start and length of each region of memory. This data structure is an array of struct iovec elements. Each element specifies one region of memory to write; the fields iov_base and iov_len specify the address of the start of the region and the length of the region, respectively. If you know ahead of time how many regions you’ll need, you can simply declare a struct iovec array variable; if the number of regions can vary, you must allocate the array dynamically.

Call writev passing a file descriptor to write to, the struct iovec array, and the number of elements in the array. The return value is the total number of bytes written.

The program in Listing B.7 writes its command-line arguments to a file using a single writev call. The first argument is the name of the file; the second and subsequent arguments are written to the file of that name, one on each line. The program allocates an array of struct iovec elements that is twice as long as the number of arguments it is writing—for each argument it writes the text of the argument itself as well as a new line character. Because we don’t know the number of arguments in advance, the array is allocated using malloc.

Listing B.7. (write-args.c) Write the Argument List to a File with writev

image

image

Here’s an example of running write-args.

image

Linux provides a corresponding function readv that reads in a single operation into multiple discontiguous regions of memory. Similar to writev, an array of struct iovec elements specifies the memory regions into which the data will be read from the file descriptor.

B.4 Relation to Standard C Library I/O Functions

We mentioned earlier that the standard C library I/O functions are implemented on top of these low-level I/O functions. Sometimes, though, it’s handy to use standard library functions with file descriptors, or to use low-level I/O functions on a standard library FILE* stream. GNU/Linux enables you to do both.

If you’ve opened a file using fopen, you can obtain the underlying file descriptor using the fileno function. This takes a FILE* argument and returns the file descriptor. For example, to open a file with the standard library fopen call but write to it with writev, you could use this code:


FILE* stream = fopen (filename, "w"); 
int file_descriptor = fileno (stream); 
writev (file_descriptor, vector, vector_length); 

Note that stream and file_descriptor correspond to the same opened file. If you call this line, you may no longer write to file_descriptor:


fclose (stream); 

Similarly, if you call this line, you may no longer write to stream:


close (file_descriptor); 

To go the other way, from a file descriptor to a stream, use the fdopen function. This constructs a FILE* stream pointer corresponding to a file descriptor. The fdopen function takes a file descriptor argument and a string argument specifying the mode in which to create the stream. The syntax of the mode argument is the same as that of the second argument to fopen, and it must be compatible with the file descriptor. For example, specify a mode of r for a read file descriptor or w for a write file descriptor. As with fileno, the stream and file descriptor refer to the same open file, so if you close one, you may not subsequently use the other.

B.5 Other File Operations

A few other operations on files and directories come in handy:

getcwd obtains the current working directory. It takes two arguments, a char buffer and the length of the buffer. It copies the path of the current working directory into the buffer.

chdir changes the current working directory to the path provided as its argument.

mkdir creates a new directory. Its first argument is the path of the new directory. Its second argument is the access permissions to use for the new file. The interpretation of the permissions are the same as that of the third argument to open and are modified by the process’s umask.

rmdir deletes a directory. Its argument is the directory’s path.

unlink deletes a file. Its argument is the path to the file. This call can also be used to delete other file system objects, such as named pipes (see Section 5.4.5, “FIFOs”) or devices (see Chapter 6).

Actually, unlink doesn’t necessarily delete the file’s contents. As its name implies, it unlinks the file from the directory containing it. The file is no longer listed in that directory, but if any process holds an open file descriptor to the file, the file’s contents are not removed from the disk. Only when no process has an open file descriptor are the file’s contents deleted. So, if one process opens a file for reading or writing and then a second process unlinks the file and creates a new file with the same name, the first process sees the old contents of the file rather than the new contents (unless it closes the file and reopens it).

rename renames or moves a file. Its two arguments are the old path and the new path for the file. If the paths are in different directories, rename moves the file, as long as both are on the same file system. You can use rename to move directories or other file system objects as well.

B.6 Reading Directory Contents

GNU/Linux provides functions for reading the contents of directories. Although these aren’t directly related to the low-level I/O functions described in this appendix, we present them here anyway because they’re often useful in application programs.

To read the contents of a directory, follow these steps:

  1. Call opendir, passing the path of the directory that you want to examine. The call to opendir returns a DIR* handle, which you’ll use to access the directory contents. If an error occurs, the call returns NULL.
  2. Call readdir repeatedly, passing the DIR* handle that you obtained from opendir. Each time you call readdir, it returns a pointer to a struct dirent instance corresponding to the next directory entry. When you reach the end of the directory’s contents, readdir returns NULL.

    The struct dirent that you get back from readdir has a field d_name, which contains the name of the directory entry.

  3. Cal closedir, passing the DIR* handle, to end the directory listing operation.

Include <sys/types.h> and <dirent.h> if you use these functions in your program.

Note that if you need the contents of the directory arranged in a particular order, you’ll have to sort them yourself.

The program in Listing B.8 prints out the contents of a directory. The directory may be specified on the command line, but if it is not specified, the program uses the current working directory. For each entry in the directory, it displays the type of the entry and its path. The get_file_type function uses lstat to determine the type of a file system entry.

Listing B.8. (listdir.c) Print a Directory Listing

image

image

image

Here are the first few lines of output from listing the /dev directory. (Your output might differ somewhat.)

image

To verify this, you can use the ls command on the same directory. Specify the -U flag to instruct ls not to sort the entries, and specify the -a flag to cause the current directory ( . ) and the parent directory ( .. ) to be included.

image

The first character of each line in the output of ls indicates the type of the entry.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset