CSCE 510 - Systems Programming Lecture 03 - The Stat System Call CSCE 510 Jan 23, 2013.

Slides:



Advertisements
Similar presentations
© Original by Donald Acton; Changes by George Tsiknis Unix I/O Related Text Sections: 2nd Ed: and st Ed: and
Advertisements

1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
Today’s topic: –File operations –I/O redirection –Inter-process communication through pipes.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
Peripheral Device Controlled with system calls. Frequently do I/O. Pseudo-device drivers frequently simulate devices such as terminals UNIX simplifies.
Exec function Exec function: - replaces the current process (its code, data, stack & heap segments) with a new program - the new program starts executing.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
I/O April 16, 2002 Topics Files Unix I/O Standard I/O Reading: (Beta) or (New) Problems: 12.9 (Beta) or 12.4 (New) class24.ppt
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
1 Unix File System API Operating System Hebrew University Spring 2007.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Chapter 4 UNIX I/O Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Input and Output Topics I/O hardware Unix file abstraction Robust I/O File sharing io.ppt CS 105 “Tour of the Black Holes of Computing”
1 System-Level I/O Andrew Case Slides adapted from Jinyang Li, Randy Bryant and Dave O’Hallaron.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem  Low level file I/O.
Lecture 2 Linux Basic Commands,Shell and Make September 8, 2015 Kyu Ho Park.
Operating Systems Recitation 1, March th, 2002.
Pipes A pipe is a simple, synchronized way of passing information between processes A pipe is a special file/buffer that stores a limited amount of data.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
1 System Programming Chapter 3 File I/O. 2 Announcement The first exercise is due today. We will NOT accept late assignment this time. –Submission site.
UNIX Files File organization and a few primitives.
Files and Directories File types stat functions for file information
Week 12 - Wednesday.  What did we talk about last time?  File I/O  Binary trees  Lab 11.
Navigating Directories
Lecture 3 Introduction to Unix Systems Programming: Unix File I/O System Calls.
Today’s topic Access and manipulate meta data for files –File type, ownership, access permissions, access time, etc How to determine if a file is not there?
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
NCHU System & Network Lab Lab 14 File and Directory.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
CSCE Systems Programming Lecture 02 - System Calls CSCE 510 Jan 16, 2013.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Tarek Abdelzaher Vikram Adve CS241 Systems Programming System Calls and I/O.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
Library Functions The UNIX system provides a large number of C functions as libraries. Some of these implement frequently used operations, while others.
1 System-Level I/O. 2 Outline Unix I/O Reading File Metadata Sharing Files & I/O redirection Robust I/O Standard I/O Suggested Reading –10.1~10.3, 10.5~10.7,
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Virtual Filesystem.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
File Subsystem in Linux by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSC 271 – Software I: Utilities and Internals An Introduction to File I/O in Linux Credited to Dr. Robert Siegfried and Beginning Linux Programming by.
CSCE Systems Programming Lecture 07 – Make, Stdio, I/O Buffering CSCE 510 Jan 23, 2013.
Week 12 - Friday.  What did we talk about last time?  Exam 2 post mortem.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Week 12 - Thursday CS222.
Lecture 2 Linux Basic Commands,Shell and Make
Systems Programming.
Week 12 - Wednesday CS222.
LINUX programming Unit-3 PPT Slides
Recitation 9: Tshlab + VM
Operating System Hebrew University Spring 2004
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
“The course that gives CMU its Zip!”
CSE 333 – Section 3 POSIX I/O Functions.
File Structure Related system calls
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
CSE 333 – Section 3 POSIX I/O Functions.
Persistence: File System API
CSE 333 – Section 3 POSIX I/O Functions.
Chien-Chung Shen CIS, UD
FILE I/O File Descriptors I/O Efficiency File Sharing
Operating System Hebrew University Spring 2009
Week 12 - Wednesday CS222.
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

CSCE Systems Programming Lecture 03 - The Stat System Call CSCE 510 Jan 23, 2013

Overview Last Time  System Calls Readings for today  Text: 18.8, 15.1, 4  /class/csce /Code/TLPI Prologue  Directories  Stat system call for information on files Interlude - Course Pragmatics, CS man of the day, Quiz Epilogue  File I/0

Shell revisited : Redirecting stderr CSCE Slide - 3 System Calls  3.3 Saving stderr to a file  gcc prog.c 2> compile-errors.txt  3.5 Saving stderr to stdout  gcc prog.c 2> &1  3.5 Saving stderr and stdout to a file  rm -f $(find / -name core) &> /dev/null

/proc CSCE 510 Jan 16, Slide - 4 System Calls  “an interface to kernel data structures in a form that looks like files and directories”  /proc/pid - directory of info about running process  /proc/1 – information on init  /proc/cpuinfo

18.8 Reading Directories CSCE 510 Jan 16, Slide - 5 System Calls  opendir(), readdir() #include DIR *fdopendir( int fd); struct dirent *readdir( DIR *dirp); struct dirent { ino_t d_ino; /* File i-node number */ char d_name[]; /* name of file */ };

CSCE 510 Jan 16, Slide - 6 System Calls errno = 0; direntp = readdir( dirp); if (direntp = = NULL) { if (errno != 0) { /* Handle error */ } else { /* We reached end-of-directory */ }

/class/csce /Code/TLPI - CSCE Slide - 7 System Calls  tar xvfz tlpi dist.tar.gz // the code from text  hermes> ls tlpi dist.tar.gz tlpi-dist  cd tl  Ls hermes> ls acl getopt pmsg svmsg … daemons Makefile.inc.MacOSX pshm tty dirs_links Makefile.inc.Solaris pty users_groups … files pgsjc sockets filesys pipes svipc

TLPI/dirs_links - CSCE Slide - 8 System Calls  cd dir_links  ls bad_symlink.c list_files_readdir_r.c t_dirbasename.c file_type_stats.c Makefile t_unlink.c list_files.c nftw_dir_tree.c view_symlink.c

more list_files_readdir_r.c - CSCE Slide - 9 System Calls #include #include "tlpi_hdr.h" static void /* List all files in directory 'dirpath' */ listFiles(const char *dirpath) { DIR *dirp; struct dirent *dp; Boolean isCurrent; /* True if 'dirpath' is "." */ isCurrent = strcmp(dirpath, ".") == 0; dirp = opendir(dirpath); if (dirp == NULL) { errMsg("opendir failed on '%s'", dirpath); return; } … for (;;) { errno = 0; /* To distinguish error from end-of- directory */ dp = readdir(dirp); if (dp == NULL) break; if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; /* Skip. and.. */ if (!isCurrent) printf("%s/", dirpath); printf("%s\n", dp->d_name); isCurrent = strcmp(dirpath, ".") == 0; dirp = opendir(dirpath); if (dirp == NULL) { errMsg("opendir failed on '%s'", dirpath); return; }

Example Scanning a directory CSCE 510 Jan 16, Slide - 10 System Calls readdir_r - rentrant version of readdir

So where does ls get all that information on files? - CSCE Slide - 11 System Calls The inode table contains information on files: permissions, times, etc. To allow programmers to get at this information Unix provides the stat system call

man –s2 stat - CSCE Slide - 12 System Calls STAT(2) Linux Programmer's Manual STAT(2) NAME stat, fstat, lstat - get file status SYNOPSIS #include int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf);

Stat structure CSCE 510 Jan 16, Slide - 13 System Calls struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };

st_mode macros CSCE 510 Jan 16, Slide - 14 System Calls S_ISREG(m) is it a regular file? S_ISDIR(m) directory? S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link? (Not in POSIX ) S_ISSOCK(m) socket? (Not in POSIX )

st_mode Flags CSCE 510 Jan 16, Slide - 15 System Calls  ls /usr/include/sys …  less /usr/include/sys/stat.h … pages of stuff #define S_IRUSR __S_IREAD /* Read by owner. */ #define S_IWUSR __S_IWRITE /* Write by owner. */ #define S_IXUSR __S_IEXEC /* Execute by owner. */ … #define S_IRGRP (S_IRUSR >> 3) /* Read by group. */

/class/csce /Python/stat.py - CSCE Slide - 16 System Calls ##Slightly modified cs.python.org/2/library/stat.html import os, sys from stat import * def visitfile(file, inodeNum): print "visiting file", file, "inode=", inodeNum if __name__ == '__main__': walktree(sys.argv[1], visitfile)

- CSCE Slide - 17 System Calls  def walktree(top, callback):  '''recursively descend the directory tree rooted at top,  calling the callback function for each regular file'''  for f in os.listdir(top):  pathname = os.path.join(top, f)  stat_struct = os.stat(pathname)  mode = stat_struct.st_mode  if S_ISDIR(mode):  # It's a directory, recurse into it  walktree(pathname, callback)  elif S_ISREG(mode):  # It's a file, call the callback function  callback(pathname, stat_struct.st_ino)  else:  # Unknown file type, print a message  print 'Skipping %s' % pathname

Unix for Harvard Grad Applied Math PhD 2. Father worked at Bell Labs also 3. _____ - Innovator and pioneer Steve Jobs – Used car salesman doing consumer electronics. 4. added data types and new syntax to Thompson's B language 5. Unix along with Thompson 6. C 7. Book with Kernighan CSCE 510 Sp 13 - Slide - 18

Finding Foo CSCE 510 Jan 16, Slide - 19 System Calls hermes> man -s 5 ar No manual entry for ar See 'man 7 undocumented'  Finding the command foo try the following options : foo --help, foo -h, foo -? info foo whatis foo, apropos foo dpkg --listfiles foo, dpkg --search foo locate '*foo*' find / -name '*foo*‘ check /usr/share/doc/foo, /usr/lib/foo.

HW -1 – grader1.py excerpt CSCE 510 Jan 16, Slide - 20 System Calls os.system("cp " + quotedSpacesFilename + " args.c") if os.system("gcc args.c -o args")==0: print "simple gcc compiled successfully" elif os.system("gcc -std=c99 args.c -o args")==0: print "-std=c99 compiled successfully" else: print "try g++", os.system("cp args.c args.cpp") if os.system("g++ args.cpp -o args")==0: print "simple g++ compiled successfully" else: print "g++ failed"

HW2 - CSCE Slide - 21 System Calls Modify list_files.c from /class/csce /TLPI/dirs_links To print inode numbers also (don’t make this tough; no stat required) more Makefile HW2 directory Source,

Chapter 4. File I/ O: The Universal I/ O Model - CSCE Slide - 22 System Calls  Unix I/O = files  File descriptors – index into open file table  _iob[_NFILES] struct - per process open file table  standard descriptors  0 = STDIN_FILENO  1 = STDOUT_FILENO  2 = STDERR_FILENO  Remapping - duplicate

Unix I/O system calls - CSCE Slide - 23 System Calls  ssize_t read(int fildes, void *buf, size_t nbyte);  ssize_t write(int fildes, const void *buf, size_t nbyte);  int open(const char *path, int oflag, /* mode_t mode */...);  int close(int fildes);  int creat(const char *path, mode_t mode);  equivalent to open(path, O_WRONLY | O_CREAT | O_TRUNC, mode)  off_t lseek(int fildes, off_t offset, int whence);

TLPI/fileio/copy.c - CSCE Slide - 24 System Calls … int main(int argc, char *argv[]) { int inputFd, outputFd, openFlags; mode_t filePerms; ssize_t numRead; char buf[BUF_SIZE]; if (argc != 3 || strcmp(argv[1], "--help") == 0) usageErr("%s old-file new-file\n", argv[0]); /* Open input and output files */ inputFd = open(argv[1], O_RDONLY); if (inputFd == -1) errExit("opening file %s", argv[1]); openFlags = O_CREAT | O_WRONLY | O_TRUNC; filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; /* rw-rw-rw- */ outputFd = open(argv[2], openFlags, filePerms); if (outputFd == -1) errExit("opening file %s", argv[2]); /* Transfer data until we …end of input or an error */ while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0) if (write(outputFd, buf, numRead) != numRead) fatal("couldn't write whole buffer"); if (numRead == -1) errExit("read"); if (close(inputFd) == -1) errExit("close input"); if (close(outputFd) == -1) errExit("close output"); exit(EXIT_SUCCESS); }

Universality of I/O - CSCE Slide - 25 System Calls $./copy test test.old -- Copy a regular file $./copy a.txt /dev/tty $./copy /dev/tty b.txt $./copy /dev/pts/16 /dev/tty /dev/null

OPEN - CSCE Slide - 26 System Calls NAME open, creat - open and possibly create a file or device SYNOPSIS #include int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode);

- CSCE Slide - 27 System Calls

Table 4.3 Flags for open - CSCE Slide - 28 System Calls Flag Purpose SUS? O_RDONLY Open for reading only v3 O_WRONLY Open for writing only v3 O_RDWR Open for reading and writing v3 O_CLOEXEC Set the close-on-exec flag (since Linux ) v4 O_CREAT Create file if it doesn’t already exist v3 O_DIRECT File I/ O bypasses buffer cache O_DIRECTORY Fail if pathname is not a directory v4 O_EXCL With O_CREAT: create file exclusively v3

More flags - CSCE Slide - 29 System Calls  O_EXCL With O_CREAT: create file exclusively v3  O_LARGEFILE Used on 32-bit systems to open large files  O_NOATIME Don’t update file last access time on read() (since Linux 2.6.8)  O_NOCTTY Don’t let pathname become the controlling terminal v3  O_NOFOLLOW Don’t dereference symbolic links v4  O_TRUNC Truncate existing file to zero length v3  O_APPEND Writes are always appended to end of file v3  O_ASYNC Generate a signal when I/ O is possible  O_DSYNC Provide synchronized I/ O data integrity (since Linux ) v3  O_NONBLOCK Open in nonblocking mode v3  O_SYNC Make file writes synchronous v3  Kerrisk, Michael ( ). The Linux Programming Interface: A Linux and UNIX System Programming Handbook (Kindle Locations ). O'Reilly Distribution. Kindle Edition.

Listing open file for Reading - CSCE Slide - 30 System Calls

Errors from open() - CSCE Slide - 31 System Calls  Retval = -1  Errno  EACCES  EISDIR  EMFILE  ENFILE  ENOENT – file does not exist and O_CREAT not specified  EROFS – file system read only  ETXTBSY – executable that is currently running

Creat system call - CSCE Slide - 32 System Calls  Creat equivalent to open with certain flags  fd = open( pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);

Open file structures - CSCE Slide - 33 System Calls  per process table  open file table  vtable

The Read system call - CSCE Slide - 34 System Calls NAME read - read from a file descriptor SYNOPSIS #include ssize_t read(int fd, void *buf, size_t count); DESCRIPTION read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf. If count is zero, read() returns zero and has no other results. If count is greater than SSIZE_MAX, the result is unspecified.

Write, Close - CSCE Slide - 35 System Calls #include ssize_t write(int fd, const void *buf, size_t count); DESCRIPTION - write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd. SYNOPSIS int close(int fd); DESCRIPTION - close() closes a file descriptor, so that it no longer refers to any file and may be reused.

Changing the file OFFSET - lseek - CSCE Slide - 36 System Calls NAME lseek - reposition read/write file offset SYNOPSIS #include off_t lseek(int fd, off_t offset, int whence); DESCRIPTION - The lseek() function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the directive whence as follows: SEEK_SET - The offset is set to offset bytes. SEEK_CUR - The offset is set to its current location plus offset bytes. SEEK_END - The offset is set to the size of the file plus offset bytes.

Figure 4.1 illustrating lseek - CSCE Slide - 37 System Calls ,,

Files with holes - CSCE Slide - 38 System Calls

TLPI/fileio/seek_io.c - CSCE Slide - 39 System Calls

ioctl - CSCE Slide - 40 System Calls IOCTL(2) Linux Programmer's Manual IOCTL(2) NAME ioctl - control device SYNOPSIS #include int ioctl(int d, int request,...); DESCRIPTION - The ioctl() function manipulates the underlying device parameters of special files.