Mac OS X supports many types of files. This section discuss the following types of files:
This section does not discuss file type codes that identify the contents of ordinary files. Type codes are discussed on page 96.
Ordinary Files, Directories, Links, and Inodes
Ordinary and directory files
An ordinary file stores user data, such as textual information, programs, or an image, such as a jpeg or tiff file. A directory is a standard-format disk file that stores information, including names, about ordinary files and other directory files.
An inode is a data structure (page 929), stored on disk, that defines a file's existence and is identified by an inode number. A directory relates each of the filenames it stores to a specific inode. An inode contains critical information, such as the name of the owner of the file, the file's physical location on the disk, and the number of hard links that point to it. HFS+ filesystems do not use inodes but emulate their behavior.
When you move (mv) a file within a filesystem, you change the filename portion of the directory entry associated with the inode that describes the file. You do not create a new inode. If you move a file to another filesystem, mv first creates a new inode on the destination filesystem and then deletes the original inode. You can also use mv to move a directory recursively, in which case all the files are copied and deleted.
When you make an additional hard link (ln, page 103) to a file, you create another reference (an additional filename) to the inode that describes the file. You do not create a new inode. Although HFS+ implements hard links through a different mechanism, their appearance is the same as on a traditional UNIX filesystem.
When you remove (rm) a file, you delete the directory entry that describes the file. When you remove the last hard link to a file, the operating system puts all the blocks the inode pointed to back in the free list (the list of blocks on the disk that are available for use) and frees the inode to be used again.
Because each filesystem has a separate set of inodes or directory entries, you can create hard links to a file only from within the filesystem that holds that file. To get around this limitation, Mac OS X provides symbolic links, which are files that point to other files. Files that are linked by a symbolic link do not share an inode: You can create a symbolic link to a file from any filesystem. You can also create a symbolic link to a directory, device, or other special file. For more information refer to "Symbolic Links" on page 105.
Special files represent Mac OS X kernel routines that provide access to an operating system feature. Device files communicate with a peripheral device. FIFO (first in, first out) special files allow unrelated programs to exchange information. Sockets allow unrelated processes on the same or different systems to exchange information. One type of socket, the UNIX domain socket, is a special file. Symbolic links are another type of special file.
Device files, which include block and character special files, represent device drivers that let you communicate with peripheral devices, such as terminals, printers, and hard disks, as well as with virtual devices such as the bit bucket, /dev/null (page 444). By convention, device files appear in the /dev directory and its subdirectories. Each device file represents a device: You read from and write to the file to read from and write to the device it represents. The following example shows part of the output that an ls l command produces for the /dev directory:
$ ls -l /dev total 4 crw------- 1 root wheel 23, 0 May 19 16:45 bpf0 crw------- 1 root wheel 23, 1 May 8 16:44 bpf1 crw------- 1 root wheel 23, 2 May 8 16:44 bpf2 crw------- 1 root wheel 23, 3 May 8 16:44 bpf3 crw------- 1 zach zach 0, 0 May 19 20:50 console crw-rw-rw- 1 root wheel 9, 1 May 8 16:45 cu.modem brw-r----- 1 root operator 14, 0 May 8 16:44 disk0 br--r----- 1 root operator 14, 1 May 8 16:44 disk0s1 brw-r----- 1 root operator 14, 2 May 8 16:44 disk0s3 brw-r----- 1 root operator 14, 3 May 8 16:44 disk0s5 brw-r----- 1 root operator 14, 4 May 8 16:44 disk0s6 brw-r----- 1 root operator 14, 5 May 8 16:44 disk0s7 ...
The first character of each line in /dev is either b, c, d, or l, representing a block, character, directory, or symbolic link file, respectively. The next nine characters represent the permissions for the file, followed by the number of hard links and the names of the owner and group. Where the number of bytes would appear for an ordinary or directory file, a device file shows its major and minor device numbers (next) separated by a comma. The rest of the line is the same as any other ls l listing (page 87).
Major and Minor Device Numbers
A major device number represents a class of hardware devices: terminal, printer, hard disk, and so on. In the preceding /dev directory listing, all of the hard disk partitions have a major device number of 14.
A minor device number represents a particular piece of hardware within a class. Although all of the hard disk partitions are grouped together by their major device number, each has a different minor device number (disk0s1 is 1, disk0s2 is 2, and so on). This setup allows one piece of software (the device driver) to service all similar hardware and to be able to distinguish among different physical units.
Block and Character Devices
A block device is a device that handles data in fixed-size chunks, called blocks. An example is a disk partition (a volume). A character device is any device that is not a block device. Examples of a character device include printers, terminals, and modems.
FIFO Special Files (Named Pipes)
A FIFO special file, also called a named pipe, represents a pipe: You write to and read from the file to write to and read from the pipe. The term FIFO stands for first in, first outthe way any pipe works. The first information that you put in one end is the first information that comes out the other end. When you use a pipe on the command line to send the output of a program to the printer, the printer prints the information in the same order that the program produced it and sent it into the pipe.
Unless you are writing sophisticated programs, you will not be working with FIFO special files. However, programs that you run under Mac OS X may use named pipes for interprocess communication. You can create a pipe using mkfifo:
$ mkfifo AA $ ls -l AA prw-r--r-- 1 zach zach 0 Apr 26 13:11 AA
The p at the left end of the output of ls l indicates that the file is a pipe.
UNIX and related systems have had pipes for many generations. Without named pipes, only processes that were children of the same ancestor could use pipes to exchange information. Using named pipes, any two processes on a single system can exchange information. Suppose one program writes to a FIFO special file and another program reads from the same file. The two programs do not have to run at the same time or be aware of each other's activity. The operating system handles all buffering and information storage. The term asynchronous (async) applies to this type of communication because programs on the ends of the pipe do not have to be synchronized.
Like a FIFO special file, a socket allows asynchronous processes that are not children of the same ancestor to exchange information. Sockets are the central mechanism of the interprocess communication that is the basis of the networking facility. When you use networking utilities, pairs of cooperating sockets manage the communication between the processes on the local and remote systems. Sockets form the basis of utilities such as ssh and scp.