Section 4.18. File Times

team bbl


4.18. File Times

Three time fields are maintained for each file. Their purpose is summarized in Figure 4.19.

Figure 4.19. The three time values associated with each file

Field

Description

Example

ls(1) option

st_atime

last-access time of file data

read

-u

st_mtime

last-modification time of file data

write

default

st_ctime

last-change time of i-node status

chmod, chown

-c


Note the difference between the modification time (st_mtime) and the changed-status time (st_ctime). The modification time is when the contents of the file were last modified. The changed-status time is when the i-node of the file was last modified. In this chapter, we've described many operations that affect the i-node without changing the actual contents of the file: changing the file access permissions, changing the user ID, changing the number of links, and so on. Because all the information in the i-node is stored separately from the actual contents of the file, we need the changed-status time, in addition to the modification time.

Note that the system does not maintain the last-access time for an i-node. This is why the functions access and stat, for example, don't change any of the three times.

The access time is often used by system administrators to delete files that have not been accessed for a certain amount of time. The classic example is the removal of files named a.out or core that haven't been accessed in the past week. The find(1) command is often used for this type of operation.

The modification time and the changed-status time can be used to archive only those files that have had their contents modified or their i-node modified.

The ls command displays or sorts only on one of the three time values. By default, when invoked with either the -l or the -t option, it uses the modification time of a file. The -u option causes it to use the access time, and the -c option causes it to use the changed-status time.

Figure 4.20 summarizes the effects of the various functions that we've described on these three times. Recall from Section 4.14 that a directory is simply a file containing directory entries: filenames and associated i-node numbers. Adding, deleting, or modifying these directory entries can affect the three times associated with that directory. This is why Figure 4.20 contains one column for the three times associated with the file or directory and another column for the three times associated with the parent directory of the referenced file or directory. For example, creating a new file affects the directory that contains the new file, and it affects the i-node for the new file. Reading or writing a file, however, affects only the i-node of the file and has no effect on the directory. (The mkdir and rmdir functions are covered in Section 4.20. The utime function is covered in the next section. The six exec functions are described in Section 8.10. We describe the mkfifo and pipe functions in Chapter 15.)

Figure 4.20. Effect of various functions on the access, modification, and changed-status times

Function

Referenced file or directory

Parent directory of referenced file or directory

Section

Note

a

m

c

a

m

c

chmod, fchmod

  

   

4.9

 

chown, fchown

  

   

4.11

 

creat

 

3.4

O_CREAT new file

creat

 

   

3.4

O_TRUNC existing file

exec

     

8.10

 

lchown

  

   

4.11

 

link

  

 

4.15

parent of second argument

mkdir

 

4.20

 

mkfifo

 

15.5

 

open

 

3.3

O_CREAT new file

open

 

   

3.3

O_TRUNC existing file

pipe

   

15.2

 

read

     

3.7

 

remove

  

 

4.15

remove file = unlink

remove

    

4.15

remove directory = rmdir

rename

  

 

4.15

for both arguments

rmdir

    

4.20

 

truncate, ftruncate

 

   

4.13

 

unlink

  

 

4.15

 

utime

   

4.19

 

write

 

   

3.8

 


    team bbl



    Advanced Programming in the UNIX Environment
    Advanced Programming in the UNIX Environment, Second Edition (Addison-Wesley Professional Computing Series)
    ISBN: 0321525949
    EAN: 2147483647
    Year: 2005
    Pages: 370

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