Lecture 05 File Attributes Outline stat struct stat
Lecture 05 File Attributes
Outline • • • stat() struct stat chmod() utime() struct utimbuf Live Coding 1 March 11, 2021
File Metadata • Each file has metadata associated with it: • • i-node number File type File permissions Ownership File size Time … 2 March 11, 2021
stat() System Call • Returns a structure containing many attributes • • Timestamps Ownership Permissions … • (other system calls used to modify these attributes) 3 March 11, 2021
stat() int stat ( const char *pathname, struct stat *statbuf ); • • • #include <sys/stat. h> Returns 0 on success, -1 on error Attempts to retrieve info about file at pathname Fills statbuf struct with file’s attributes stat() DOES NOT require read/write permissions 4 March 11, 2021
lstat() int lstat ( const char *pathname, struct stat *statbuf ); • #include <sys/stat. h> • lstat() returns info about symbolic link, not linked file • Otherwise, same as stat() • • Returns 0 on success, -1 on error Attempts to retrieve info about file at pathname Fills statbuf struct with file’s attributes lstat() DOES NOT require read/write permissions 5 March 11, 2021
fstat() int fstat ( int fd, struct stat *statbuf ); • • • #include <sys/stat. h> Returns 0 on success, -1 on error Retrieves info of file referred to by open file descriptor Fills statbuf struct with file’s attributes fstat() ALWAYS succeeds (as long as fd is valid) 6 March 11, 2021
Outline • • • stat() struct stat chmod() utime() struct utimbuf Live Coding 7 March 11, 2021
struct stat • struct filled by stat(), lstat(), fstat() • Contains file metadata, e. g. , • • • i-node number file type file permissions file size time of last access … (see page 280) 8 March 11, 2021
struct stat Attributes struct stat { mode_t off_t time_t … }; st_mode; st_size; st_atime; • Note: st_mode is of type mode_t… • See page 280 for more 9 March 11, 2021 //file type and permissions //file size (in bytes) //time of last access //much, much more
struct stat • File type and permissions is a bit map (st_mode attribute) • Use bit masks to check file type and permissions • U, G, T- advanced topic. . more later 10 March 11, 2021
struct stat • Macros exist to check file type ( defined in <sys/stat. h> ) • S_ISREG( ) => regular file • S_ISDIR( ) => directory • S_ISLNK( ) => symbolic link • E. g. , : //struct stat sb filled by call to stat() if( S_ISREG( sb. st_mode )) printf(“Regular filen”); 11 March 11, 2021
struct stat • Can check file’s permissions in similar way • Use bit mask to analyze st_mode attribute • See table 15 -4 for permission bit mask constants //struct stat sb filled by call to stat() if( sb. st_mode & S_IRUSR ) printf(“user can readn file”); else printf(“user cannot read filen”); 12 March 11, 2021
Outline • • • stat() struct stat chmod() utime() struct utimbuf Live Coding 13 March 11, 2021
chmod() int chmod( const char *pathname, mode_t mode); • • • #include <sys/stat. h> Set permissions of file at pathname according to mode Returns 0 on success Returns -1 on error Use bit-mask permissions (from table 15 -4) OR’d together 14 March 11, 2021
chmod() • E. g. , //grant read permissions to all (user, group, other) chmod(“file. txt”, S_IRUSR | S_IRGRP | S_IROTH); 15 March 11, 2021
fchmod() int fchmod( int fd, mode_t mode); • • • #include <sys/stat. h> Set permissions of open file descriptor fd according to mode Returns 0 on success Returns -1 on error Use bit-mask permissions (from table 15 -4) OR’d together 16 March 11, 2021
Outline • • • stat() struct stat chmod() utime() struct utimbuf Live Coding 17 March 11, 2021
utime() int utime ( const char *pathname, const struct utimbuf *buf); • • • #include <utime. h> #include <sys/time. h> updates file’s time attributes according to utimbuf struct returns 0 on success returns -1 on error if buf == NULL, set to current time 18 March 11, 2021
Outline • • • stat() struct stat chmod() utime() struct utimbuf Live Coding 19 March 11, 2021
struct utimbuf Attributes struct utimbuf { time_t actime; time_t modtime; }; //access time //modification time • time_t is UNIX time type => number of seconds since 1. 01. 70 20 March 11, 2021
Outline • • • stat() struct stat chmod() utime() struct utimbuf Live Coding 21 March 11, 2021
Live Coding • Recreate ls –l • List each file’s • • • Owner Permissions Size Time accessed Time modified i-node number 22 March 11, 2021
Live Coding • Time cheat. . • Modify a file’s time to be before a deadline… 23 March 11, 2021
- Slides: 24