Lecture 05 File Attributes

advertisement
Lecture 05
File Attributes
Outline
•
•
•
•
•
•
stat()
struct stat
chmod()
utime()
struct utimbuf
Live Coding
1
July 24, 2016
File Metadata
• Each file has metadata associated with it:
•
•
•
•
•
•
•
i-node number
File type
File permissions
Ownership
File size
Time
…
2
July 24, 2016
stat() System Call
• Returns a structure containing many attributes
•
•
•
•
Timestamps
Ownership
Permissions
…
• (other system calls used to modify these attributes)
3
July 24, 2016
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
July 24, 2016
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
July 24, 2016
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
July 24, 2016
Outline
•
•
•
•
•
•
stat()
struct stat
chmod()
utime()
struct utimbuf
Live Coding
7
July 24, 2016
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
July 24, 2016
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
July 24, 2016
//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
July 24, 2016
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 file\n”);
11
July 24, 2016
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 read\n file”);
else
printf(“user cannot read file\n”);
12
July 24, 2016
Outline
•
•
•
•
•
•
stat()
struct stat
chmod()
utime()
struct utimbuf
Live Coding
13
July 24, 2016
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
July 24, 2016
chmod()
• E.g.,
//grant read permissions to all (user, group, other)
chmod(“file.txt”, S_IRUSR | S_IRGRP | S_IROTH);
15
July 24, 2016
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
July 24, 2016
Outline
•
•
•
•
•
•
stat()
struct stat
chmod()
utime()
struct utimbuf
Live Coding
17
July 24, 2016
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
July 24, 2016
Outline
•
•
•
•
•
•
stat()
struct stat
chmod()
utime()
struct utimbuf
Live Coding
19
July 24, 2016
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
July 24, 2016
Outline
•
•
•
•
•
•
stat()
struct stat
chmod()
utime()
struct utimbuf
Live Coding
21
July 24, 2016
Live Coding
• Recreate ls –l
• List each file’s
•
•
•
•
•
•
Owner
Permissions
Size
Time accessed
Time modified
i-node number
22
July 24, 2016
Live Coding
• Time cheat..
• Modify a file’s time to be
before a deadline…
23
July 24, 2016
Download