Section 3.26. File Attributes


[Page 68]

3.26. File Attributes

Now that I've introduced you to some of the common file-oriented utilities, it's time to look at the various file attributes. I used ls to obtain a long listing of "heart.final," and got the following output:

$ ls -lsF heart.final 1 -rw-r--r--   1   glass  cs   213   Jan 31 00:12   heart.final $ _ 


Each field is the value of a file attribute, described by the Figure 3-33.

Figure 3-33. File attributes.

Field #

Field value

Meaning

1

1

The number of blocks of physical storage occupied by the file.

2

-rw-r--r--

The type and permission mode of the filewhich indicates who can read, write, and execute the file.

3

1

The hard link count (discussed in Chapter 4, "GNU Utilities for Power Users").

4

glass

The username of the owner of the file.

5

cs

The group name of the file.

6

213

The size of the file, in bytes.

7

Jan 31 00:12

The date and time that the file was last modified.

8

heart.final

The name of the file.


The next few sections describe the meaning of the individual fields, in increasing order of difficulty.

3.26.1. File Storage

The number of blocks of physical storage is shown in field 1 and is useful if you want to know how much actual disk space a file is using. It's possible to create sparse files that seem to be very large in terms of field 6 but actually take up very little physical storage. Sparse files are discussed in detail in Chapter 12, "Systems Programming."

3.26.2. Filenames

The name of the file is shown in field 8. A Linux filename may be up to 255 characters in length. You may use any printable[2] characters you want in a filename except the slash (/), although I recommend that you avoid the use of any character that is special to a shell (like <, >, *, ?, or the tab) as these can confuse both the user and the shell. Unlike some operating systems, there's no requirement that a file end in an extension such as ".c" and ".h," although many GNU utilities, such as the C compiler, will only accept files that end with a particular suffix. Thus, the filenames "heart" and "heart.final" are both perfectly legal. The only filenames that you definitely can't choose are "." and "..", as these are predefined filenames that correspond to your current working directory and its parent directory, respectively.

[2] Some nonprintable characters are valid in filenames, but can result in unexpected behavior when displayed or used, so their use is discouraged.


[Page 69]

3.26.3. File Modification Time

Field 7 shows the time that the file was last modified, and is used by several utilities. For example, the make utility, described in Chapter 11, "C Programming Tools," uses the last modification time of files to control its dependency checker. The find utility, described in Chapter 4, "GNU Utilities for Power Users," may be used to find files based on their last modification time.

3.26.4. File Owner

Field 4 tells you the owner of the file. Every Linux process has an owner, which is typically the same as the username of the person who started it. For example, my login shell is owned by "glass," which is my username. Whenever a process creates a file, the file's owner is set to the process's owner. This means that every file that I create from my shell is owned by "glass," the owner of the shell itself. Chapter 12, "Systems Programming," contains more information on processes and ownership.

Note that while the text string known as the username is typically how we refer to a user, internally Linux represents this as an integer known as the user ID. The username is easier for humans to understand than a numeric ID. Therefore I will refer to the textual name as username while using user ID to refer to the numeric value itself.

3.26.5. File Group

Field 5 shows the file's group. Every Linux user is also a member of a group. This membership is initially assigned by the system administrator, and is used as part of the Linux security mechanism. For example, my group name is "cs." Every Linux process also belongs to a specific group, usually the same as that of the user that started the process. My login shell belongs to the group name "cs." Because a file created by a process is assigned to the same group as that of the creating process, this means that every file that I create from my shell has the group name "cs." Chapter 12, "Systems Programming," contains more information on processes and groups. The use of groups in relation to the Linux security mechanism is described in the next few sections.

As with the user ID, the group is usually referenced by the text string name, but is represented internally as an integer value called the group ID. Therefore I will refer to the textual name as group name while using group ID when referring to the numeric value itself.

3.26.6. File Types

Field 2 describes the file's type and permission settings. In the previous ls example:

1 -rw-r--r--  1  glass  cs  213  Jan 31 00:12 heart.final 


the first character of field 2 indicates the type of the file, which is encoded as shown in Figure 3-34.


[Page 70]

Figure 3-34. File types.

Character

File type

-

regular file

d

directory file

b

buffered (block-oriented) special file (such as a disk drive)

c

unbuffered (character-oriented) special file (such as a terminal)

l

symbolic link

p

pipe

s

socket


In the example, the type of "heart.final" is indicated as a regular file. You'll encounter symbolic links in Chapter 4, "GNU Utilities for Power Users," pipes and sockets in Chapter 12, "Systems Programming," and buffered/unbuffered special files in Chapter 13, "Linux Internals."

A file's type can often be determined by using the file utility (Figure 3-35).

Figure 3-35. Description of the file command.

Utility: file { fileName }+

The file utility attempts[a] to describe the contents of the fileName arguments, including the language that any text is written in. When using file on a symbolic link file, file reports on the file that the link is pointing to, rather than the link itself.


[a] While file is quite useful, it is not 100% accurate and can be fooled by some file formats.

For example, when I ran file on "heart.final," I saw this:

$ file heart.final       ...determine the file type. heart.final: ascii text $ _ 


3.26.7. File Permissions

The next nine characters of field 2 indicate the file's permission settings. In the current example, the permission settings are "rw-r--r--":

1 -rw-r--r--  1  glass  cs  213  Jan 31 00:12 heart.final 


These nine characters should be thought of as being arranged in three groups of three characters, as in Figure 3-36.


[Page 71]

Figure 3-36. File permissions.

User (owner)

Group

Others

rw-

r--

r--

Read permission

Write permission

Execute permission

r

w

x


If a dash occurs instead of a letter, then the permission is denied. The meaning of the read, write, and execute permissions depends on the type of the file (Figure 3-37).

Figure 3-37. Permission meanings for file types.
 

Regular file

Directory file

Special file

read

The process may access the contents.

The process may read the directory (i.e., list the names of the files that it contains).

The process may read from the file using the read () system call.

write

The process may change the contents.

The process may add or remove files to/from the directory.

The process may write to the file using the write () system call.

execute

The process may execute the file (which only makes sense if it's a program).

The process may access files in the directory or any of its subdirectories.

No meaning.


When a process executes, it has four values related to file permissions:

  1. a real user ID

  2. an effective user ID

  3. a real group ID

  4. an effective group ID

When you log in, your login shell process has its real and effective user IDs set to your own user ID, and its real and effective group IDs set to your group ID. When a process runs, the file permissions apply as follows:

  • If the process's effective user ID is the same as the owner of the file, the User permissions apply.

  • If the process's effective user ID is different from the owner of the file, but its effective group ID matches the file's group ID, then the Group permissions apply.

  • If neither the process's effective user ID nor its effective group ID matches, the Others permissions apply.

The permission system is therefore a three-tier arrangement that allows you to protect your files from general users but at the same time allows access by certain groups. Later in this chapter I'll illustrate the use of permission settings to good effect and describe the utilities that are used to alter them.


[Page 72]

Note that only a process's effective user and group IDs affect its permissions, its real user and group IDs are only used for accounting purposes. Note also that a process's access rights depend ordinarily on who executes the process, and not on who owns the executable. There are some occasions where this is undesirable (e.g., in a game that maintains a file of the best scores of previous players). Obviously, the game program itself must have permission to alter this file when it is executing, but the player that executes the game program should not. This seems impossible, based on the permission rules that I just described. To get around this problem, Linux provides two special file permissions called "set user ID" and "set group ID." When an executable with "set user ID" permission is exec'ed, the process's effective user ID becomes that of the executable. Similarly, when an executable with "set group ID" permission is exec'ed, the process's effective group ID is copied from the executable. In both cases, the real user/group ID is unaffected. In the case of our game, the executable and the score file are both owned by a different user, and the program executable has "set user ID" permission. The score file only has write permission for its owner, thus protecting general users from modifying it. When a player executes the game program, the process executes with the effective user ID of the game, and thus is able to modify the score file.

"Set user ID" and "set group ID" permissions are indicated by an "s" instead of an "x" in the user and group clusters, respectively. They may be set using the chmod utility, described shortly, and by the chmod () system call, described in Chapter 12, "Systems Programming."

Here are a few other notes relating to file permissions:

  • When a process creates a file, the default permissions given to that file are modified by a special value called the umask. The umask value is usually set a sensible default, so we will wait to discuss it further in Chapter 5, "The Linux Shells."

  • The super-user automatically has all access rights, regardless of whether they're granted or not.

  • It's perfectly possible, although unusual, for the owner of a file to have fewer permissions than the group or anyone else.

3.26.8. Hard Link Count

Field 3 of the output from the ls command shows the file's hard link count, which indicates how many labels in the hierarchy are pointing to the same physical file. Hard links are rather advanced, and are discussed in conjunction with the ln utility in Chapter 4, "GNU Utilities for Power Users"




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net