CSC320 chapter5 - Files and directories Original version...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Files and directories 1 Original version created by: Dr. Mohamed El Bachir Menai Updated by: Dr. Safwan Qasem – Spring 2010
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Files and Directories Objectives • Additional Features of the File System • Properties of a File. Three major functions that return file information: #include <sys/types.h> #include <sys/stat.h> int stat(const char *pathname, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *pathname, struct stat *buf); Return: 0 if OK, -1 on error
Background image of page 2
3 Files and Directories UNIX kernel maintains considerable details about every file system object whether it is: •File •Directory •Special device •Named pipe (FIFO) •stat(), fstat(), and lstat() return information about file system objects in a structure named stat.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Files and Directories Differences on stat(), fstat(), lstat(). lstat() similar to stat(), except that, when file is a symbolic link , it returns info regarding the symbolic link itself, not the referenced file. File info is returned in buf defined as the following structure: struct stat { mode_t st_mode; /* type & mode */ ino_t st_ino; /* i-node number */ dev_t st_dev; /* device no (filesystem) */ dev_t st_rdev; /* device no for special file */ nlink_t st_nlink; /* # of links */ uid_t st_uid; gid_t st_gid; off_t st_size; /* sizes in bytes */ time_t st_atime; /* last access time */ time_t st_mtime; /* last modification time */ time_t st_ctime; /* time for last status change */ long st_blksize; /* best I/O block size */ long st_blocks; /* number of 512-byte blocks allocated */ };
Background image of page 4
5 File Types Regular Files: text, binary, etc. Directory Files: Only Kernel can update these files – { (filename, i-node pointer) }. Character Special Files, e.g., tty, audio, etc. Block Special Files, e.g., disks, etc. FIFO – named pipes Sockets Symbolic Links – not POSIX.1 or SVR4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Macro Type of File S_ISREG() Regular file S_ISDIR() Directory S_ISCHR() Character special file S_ISBLK() Block special file S_ISFIFO() FIFO or PIPE S_ISLNK() Symbolic link S_ISSOCK() Socket File Types File types can be determined using following macros ; argument of macro is st_mode (which encodes file type) member from the stat structure.
Background image of page 6
7 # include <sys/types.h> #include <sys/stat.h> int main(int argc, char *argv[]) { int i; struct stat buf; char *ptr; for (i = 1; i < argc; i++) { printf("%s: ", argv[i]); if (lstat(argv[i], &buf) < 0) { err_ret(“lstat error"); continue; } if (S_ISREG(buf.st_mode)) ptr = "regular"; else if (S_ISDIR(buf.st_mode)) ptr = "directory"; else if (S_ISCHR(buf.st_mode)) ptr = "character special"; else if (S_ISBLK(buf.st_mode)) ptr = "block special"; else if (S_ISFIFO(buf.st_mode)) ptr = "fifo"; else if (S_ISLNK(buf.st_mode)) ptr = "symbolic link"; else if (S_ISSOCK(buf.st_mode)) ptr = "socket"; else ptr = "** unknown mode **"; printf("%s\n", ptr); } exit(0); }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 File Types Example of execution : $ a.out /vmunix /etc /dev/ttya /dev/hd0a gives: /vmunix: regular file /etc: directory /dev/ttya: character special device /dev/hd0a: block special device $ ./filetype_stat /etc/passwd /etc /dev/log /dev/cdrom /dev/tty /dev/sr0 . ./toto $ ./filetype_lstat /etc/passwd /etc /dev/log /dev/cdrom /dev/tty /dev/sr0 . ./toto
Background image of page 8
9 Set-User-ID and Set-Group-ID Every process has 6 or more IDs associated with it.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

CSC320 chapter5 - Files and directories Original version...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online