90 likes | 253 Views
CS 311 – Lecture 12 Outline. File management system calls Stat() Directory Information Opendir() Readdir() Closedir() Truncate() and remove(). stat(). Used to obtain file information The stat structure is defined in / usr /include/sys/ stat.h (#include<sys/ stat.h >) Prototype:
E N D
CS 311 – Lecture 12 Outline • File management system calls • Stat() • Directory Information • Opendir() • Readdir() • Closedir() • Truncate() and remove() CS 311 - Operating Systems 1
stat() • Used to obtain file information • The stat structure is defined in /usr/include/sys/stat.h (#include<sys/stat.h>) • Prototype: • int stat() (const char name, struct stat* buf) • intlstat() (const char* name, struct stat* buf) – returns information of any symbolic links to the file • intfstat() (intfd, struct stat* buf) – similar to stat but uses fd as the first argument • The file information can be accessed by “buf” which is a pointer to stat structure. CS 311 - Operating Systems 1
Stat() - Members of the structure • st_dev Device ID of device containing file. • st_ino File serial number. • st_mode Mode of file. • st_nlink Number of hard links to the file. • st_uid User ID of file. • st_gid Group ID of file. • st_rdev Device ID (if file is character or block special). • st_size For regular files, the file size in bytes. • st_atime Time of last access. • st_mtime Time of last data modification. • st_ctime Time of last status change. CS 311 - Operating Systems 1
Stat() modes • S_ISDIR – directory • S_ISCHR – character oriented special device • S_ISBLK – block oriented special device • S_ISREG – regular file • S_ISFIFO - pipe CS 311 - Operating Systems 1
Stat() - time fields • Time fields in the stat structure like the atime, ctime, ftime are in an encoded format and can be decoded using standard C time functions in time.h • asctime(), localtime() subroutines help in decoding the time fields. • More time functions can be found at this URL - http://opengroup.org/onlinepubs/007908799/xsh/time.h.html CS 311 - Operating Systems 1
Directory information • opendir() - opens a directory file for reading and returns a pointer to a stream descriptor. • Prototype: DIR* opendir (char* filename) • The DIR* directory pointer is defined in /usr/include/dirent.h (#include<dirent.h>) • The fields in ‘dirent’ structure • d_ino – inode number • d_off – offset of next directory entry • d_reclen – the length of directory entry structure • d_name – filename • DIR * pointer will be used in other system calls related to directory (DIR* is different structdirent) CS 311 - Operating Systems 1
Directory information • To read directory contents use readdir() system call • Prototype: struct dirent *readdir(DIR *dir) • returns a pointer to dirent structure containing information about the next directory entry. • To close a directory contents use closedir() system call • Prototype: int closedir(DIR *dir) CS 311 - Operating Systems 1
truncate() • Used to decrease a size of a file to the size specified. • Prototype: • int truncate(const char *path, off_t length); • int ftruncate(int fd, off_t length) • Length specifies the resulting size of file after truncation. • Returns 0 on success and -1 on failure. CS 311 - Operating Systems 1
remove() • Used to delete a whole file. • Prototype: int remove ( const char * filename ); • Returns a non-zero value if failure and zero if success. CS 311 - Operating Systems 1