1 / 9

CS 311 – Lecture 12 Outline

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:

royal
Download Presentation

CS 311 – Lecture 12 Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  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. CS 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 CS 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/xsh/time.h.html CS 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<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

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

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

More Related