• Aucun résultat trouvé

FILES AND DIRECTORIES

Dans le document PRO/VENIXTM for (Page 61-70)

2.7.1 File Attributes - Stat Call

VENIX maintains for each file a header block known as an "inode", holding various pieces of information about the file, such as its length and read/write/

execute permissions. Using the stat system call, the contents of an i-node can be examined at any time. stat returns a buffer with the following structure:

struct stat

dev_t st_dev;

ino_t st~no;

unsigned short st-"1ode;

short st_nlink;

short st_uid;

short st_gid;

dev_t st_rdev;

off_t st_size;

time_t st_atime;

time_t st_mtime;

time_t st_ctime;

1 ;

This structure is defined in the include file

<

sys/ stat.h >. The type definitions

"dev_t", "off_t" and "time_t" are all given in < sys/types.h>. (Since these types may vary with different implementations of UNIX, they are given special definitions in order to make the code using them more portable.)

VENIX PROGRAMMING numbers uniquely identify each disk partition; their actual meaning is described in SETTING UP VENIX in the Installation and System Manager's Guide.

st~no holds the i-node number (or "i-number") of this file. Each file has an i-node number unique to its file system. Since each file system corresponds with exactly one disk partition, st~no in combination with st_dev uniquely identifies the file for the entire system.

st-"lode holds (bit-encoded) information on the read/write/execute permission of the file, and a few other items. The encoding is outlined in the

VENIX PROGRAMMING

«mode & S~XEC) !

=

0)

is used. The high-order bits masked by S~FMT indicate the type of file: directory, character device, block device or regular file. For example, the expression:

«mode & S~FMT) S~FDIR)

is true if the file is a directory.

The permissions for the owner's group are given in the bits masked by 070; those for "others" are given in 07. Other bits indicate that the file is "set-UID", "set-GID", and "sticky". The meaning of these conditions is described elsewhere.

st_nlink is the number of links to the file, that is, the number of names by which the file is known. When a file is first created, the link count is one. It is possible to create further links to the file with the link system call or the In command. Having multiple links to a file is convenient if different users or programs prefer to call one file by different names.

When a file is removed, its name is said to be "unlinked" (the sys-tem call used is unUnkO). Unlinking a file name breaks the connec-tion between that name and the file; only the name is removed from the file system. The file's link count is decremented by one. The file itself, however, remains around, accessible by all remaining links, until the last link is undone. At that point, the file disappears and its disk space deallocated.

holds the owner's user ID number, corresponding to the user's name in the /ete/passwd file.

holds the owner's group ID number, corresponding to the user's group name in the jete/group file. Although the user and group ID are given here as type short integer, they are internally stored as char's, and thus are limited to values 0-255.

VENIX PROGRAMMING st_rdev holds major and minor device numbers for files that are block or character devices. These numbers are of the same type that st_dev is, but indicate the device these special files "point" to, not the device they reside on.

sL---.Size is the size of the file, measured in bytes.

st_atime is the time the file was last accessed.

st_mtime is the time the file was last modified. Time is measured in seconds since 0:00 January 1, 1970.

st_ctime is identical to st_atime (In some UNIX implementations, it holds the time the file was created, but this is not currently done in VENIX.)

With all this in mind, you can write a crude version of the Is command. This program, which may be called "list", gives a status report on all files passed it on the command line. The program is written with a main routine which pro-cesses each command argument, does a stat of it, and passes the information to a routine called list which prints out the information in readable form. list uses the standard library routines ctime(3) to convert the time into English, and getpwuid(3) to map the owner's user ID number to his name in the /etc/passwd file.

/* list - list status information on files * /

# include

<

stdio.h

>

# include

<

pwd.h

>

# include < sys/types.h >

# include < sys/ stat.h

>

/* standard 110 * / /* password header * /

/* system type defs * / /* stat structure * /

char def_perm£]

=

"rwxrwxrwx"; /* full permission setting */

char perm[9]; /* permission for each file * / struct stat sbuf; /* stat buf * /

VENIX PROGRAMMING

VENIX PROGRAMMING

printf("OJocOJos 0J0 -3dOJo -7s% -7D%.24s % -14s\n", type,perm,lcount,uname,size,time,name);

2.7.2 Directory Structure

A directory is a special type of file which contains a list of file names and cor-responding inode-numbers. Directories may only be written into by VENIX itself; this is done of course whenever a new file is created or removed from the directory. However, it is entirely possible for a user program to read a direc-tory, and the contents can be easily interpreted.

VENIX PROGRAMMING

The format of a directory entry is very simple and is defined in the include file

< sys/dir.h > :

#define DIRSIZ 14

struct direct /* structure of directory entry * / ino_t d-.-ino; /* inode number * / char d-"ame[DIRSIZ); /* file name * / }j

The type definition "ino_t" is defined in <sys/types.h>; on PDP-II's, it is an integer. d-.-ino is the inode number, and d_name is the file name. The size of each file entry then is 2 bytes (inode number) + 14 bytes (name)

=

16 bytes total. However, to keep your programs portable, it is always best to refer to the length of each entry as

sizeof(struct direct)

which evaluates to the right number.

When a file is created, it is assigned a new entry in its directory. When it is removed, the i-node number is zeroed, though the file name itself may remain.

Therefore when reading a directory a program must always check that a file has a non-zero i-number and thus really exists. Future entries will overwrite removed ones, but no attempt is ever made to condense a directory to recover removed entries; directories grow as needed, but never shrink.

With this in mind, you can improve the listing program given earlier. In the earlier version, the specifications of each file argument were listed; if a directory name was given as argument, the directory itself was listed, not the files it con-tained. Since it is frequently convenient to have the contents of each directory argument listed, you can modify the list program to check if each argument is a directory, and if so, to extract and list all its entries, that is, all the files the directory contains.

Since the program was written in a modular fashion, only the main need be modified. The list routine remains the same, and is not duplicated here.

VENIX PROGRAMMING /* list program # 2 examines contents of directory * /

# include < stdio.h

>

/* standard 110 * /

# include

<

pwd.h > /* password header * /

# include < sys/types.h

>

/* system type defs * /

# include < sys/ dir.h > /* directory structure * /

# include < sys/stat.h

>

/* stat structure * /

char def_perm[] "rwxrwxrwx If; /* full permission setting * / char perm(9); /* permission for each file * /

char name(200);

struct direct dbuf;

struct stat sbuf; /* stat buf * / main(argc,argv)

int argc;

char *argv[];

register char *pl, *p2, *ql;

int i, fd;

if (argc = = l){

fprintf(stderr, ''Usage: list file ..• \n '');

exit(l);

while (- - argc) { + +argv;

if (stat(*argv,&sbuf)

<

O){

fprintf(stderr, "can't stat OJos\n", *argv);

continue;

VENIX PROGRAMMING

fprintf(stderr, "can't open directory OJos\n", *argv);

exit(l);

VENIX PROGRAMMING name itself so that a stat can be done on it. The list routine is then called to print out the inode information.

To repeat, this is quite a simple list program. It doesn't attempt to sort its entries, and when listing a directory's contents it doesn't suppress listing of the mandatory"." and" .. " entries (referring to the directory itself and its parent).

Dans le document PRO/VENIXTM for (Page 61-70)