Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.

Similar presentations


Presentation on theme: "CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture."— Presentation transcript:

1 CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture 121CS 311 - Operating Systems 1

2 stat() Used to obtain file information The stat structure is defined in /usr/include/sys/stat.h (#include ) Prototype: – int stat() (const char name, struct stat* buf) – int lstat() (const char* name, struct stat* buf) – returns information of any symbolic links to the file – int fstat() (int fd, 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. Lecture 122CS 311 - Operating Systems 1

3 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. Lecture 123CS 311 - Operating Systems 1

4 Stat() modes S_ISDIR – directory S_ISCHR – character oriented special device S_ISBLK – block oriented special device S_ISREG – regular file S_ISFIFO - pipe Lecture 124CS 311 - Operating Systems 1

5 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/xs h/time.h.html Lecture 125CS 311 - Operating Systems 1

6 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 ) 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 struct dirent) Lecture 126CS 311 - Operating Systems 1

7 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) Lecture 127CS 311 - Operating Systems 1

8 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. Lecture 12CS 311 - Operating Systems 18

9 remove() Used to delete a whole file. Prototype: int remove ( const char * filename ); Returns a non-zero value if failure and zero if success. Lecture 12CS 311 - Operating Systems 19


Download ppt "CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture."

Similar presentations


Ads by Google