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?

Slides:



Advertisements
Similar presentations
Stat, mmap, process syncing and file system interface Nezer J. Zaidenberg.
Advertisements

1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
System-Level I/O Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Linux+ Guide to Linux Certification, Second Edition
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
Kernel Directory Interface. DirectoriesDirectories Recall that in Unix your hard drive is organized into a hierarchical system of directories and files.
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
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.
1 UNIX Systems Programming Interprocess communication.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
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.
Chapter 5 Files and Directories Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
CSCE Systems Programming Lecture 03 - The Stat System Call CSCE 510 Jan 23, 2013.
Linux+ Guide to Linux Certification, Second Edition
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
1 UNIX System Programming Files and Directories. 2 File Pointer Both read() and write() will change the file pointer. The pointer will be incremented.
Adv. UNIX: dirs/181 Advanced UNIX v Objectives –look at how to program with directories –briefly describe the UNIX file system Special.
Directory structure. Slide 2 Directory Structure  A directory ‘file’ is a sequence of lines; each line holds an i-node number and a file name.  The.
UNIX Files File organization and a few primitives.
Files and Directories File types stat functions for file information
Project 6 Unix File System. Administrative No Design Review – A design document instead 2-3 pages max No collaboration with peers – Piazza is for clarifications.
Navigating Directories
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Advanced Programming in the Unix Environment
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
Programming with UNIX File Systems (Chap 3, 4. in the book “Advanced Programming in the UNIX Environment”) Acknowledgement : Prof. Y. Moon at Kangwon Nat’l.
1 Contents 1. Preface/Introduction 2. Standardization and Implementation 3. File I/O 4. Standard I/O Library  5. Files and Directories 6. System Data.
NCHU System & Network Lab Lab 14 File and Directory.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
File System Security ls -l. First Columm d = directory l = symbolic link b = block special file c = character special file p = fifo (or named pipe) special.
January 7, 2003Serguei Mokhov, 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004.
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 Programming Chapter 4 Files and Directories.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
1 PART II System Programming. 2 Chapter 10 The File and I/O System.
Adv. UNIX: FileAgain/171 Advanced UNIX v Objectives –to look at the low-level manipulation of files and their properties (e.g. permissions)
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.
Chapter 39 File and Directory Chien-Chung Shen CIS/UD
CHAPTER 4. FILES AND DIRECTORIES System Programming 本份投影片大量參考熊博安教授的系統程式投影片 羅習五 國立中正大學資訊工程學系 EA-001 (05)
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.
Linux Filesystem Management
Week 12 - Thursday CS222.
Unix Programming Environment
Unix Files Course code: 10CS62 Prepared by Department of CSE
Systems Programming.
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
LINUX programming Unit-3 PPT Slides
UNIX FILES
Operating System Hebrew University Spring 2004
An overview of the kernel structure
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
File Structure Related system calls
“The course that gives CMU its Zip!”
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Persistence: File System API
CSE 333 – Section 3 POSIX I/O Functions.
Programming with UNIX File Systems (Chap 3, 4
Chien-Chung Shen CIS, UD
Operating System Hebrew University Spring 2009
Presentation transcript:

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? How to find out the access permissions of a file? How to have the full control of permissions of the files you created in your program? Operations on directories

How to determine if a file is not there? –Try ‘ls –l’, ‘ls aa’ –Get the meta data of a file: stat #include int stat (const char *pathname, struct stat *buf) int fstat(int filedes, struct stat *buf) int lstat(const char *filename, struct stat *buf) –This function fails when the file is not there There are some other ways for it to fail, –man –a stat –When the function succeeds, buf stores the meta data for the file.

Struct stat { mode_t st_mode; /* file type & mode & permissions */ ino_t st_ino; /* file inode number */ dev_t st_dev; /* device number (file system) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlinks; /* number of links */ uid_t st_uid; /* owner user ID */ gid_t st_gid; /* owner group ID */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of the last access */ time_t st_mtime; /* time of the last modification */ time_t st_ctime; /* time of the last status change */ long st_blksize; /* best I/O block size */ long st_blocks; /* number of 512 byte blocks */ }; You can see most of the fields in the stat data structure with ls.

Files types (st_mode): –Regular (S_ISREG(buf.st_mode)) –Directory (S_ISDIR(..)) –Character special file (S_ISCHR(..)) –Block special file (S_ISBLK(..)) –FIFO (S_ISFIFO(..)) –Symbolic link (S_ISLNK(..)) –Socket (S_ISSOCK(..)) –See example1.c

Set-user-ID and set-group-ID (st_mode): –For executable files only. Relate to what permission should the system gives to a program executing? –What permission should a process get when a program is executed? The same permission as whoever runs it? Not sufficient sometimes. Example? –Change effective user ID and group ID when executed. The main use: allowing regular users to have root access. –Checking these bits (example1a.c) S_ISUID & buf.st_mode, S_ISGID & buf.st_mode

File access permissions (st_mode) –user-read (S_IRUSR) –user-write (S_IWUSR) –user-execute(S_IXUSR) –group-read (S_IRGRP) –group-write (S_IWGRP) –group-execute(S_IXGRP) –other-read (S_IROTH) –other-write(S_IWOTH) –other-execute(S_IXOTH) See example2.c

Creating file with customized permissions. –Can specify permission in the creat and open calls. –See example3.c –There is a default file mode creation mask. Try ‘umask’ The bits that are 1’s are turned-off. –The mask can be manipulated in the program, see example4.c. #include Mode_t umask(mode_t cmask);

Change mode and change owner –chmod and chown, both as command lines and as system calls. –Remove a file without write permission, see example5a.c –Read a file without the read permission, see example5.c (as long as you are the owner). Remove and rename files: #include int remove(const char *path); /* C function, same as unlink */ int rename(const char *oldname, const char *newname) /* also a C function */

Hard link and symbolic link of a file: –Hard link of a file is to create a new directory entry with the same inode number. –Hard link is not allowed for a directory (to avoid loops in the directory’s tree structure). –In struct stat, the st_nlinks field records the number of directory entries pointing to the inode for the file. –Link/unlink are both command line and system calls #include int link(const char *existingpath, const char *newpath) /* create a new entry in the directory with the same inode number */ /* similar to copy, what is the difference?*/ int unlink(const char *path);

Symbolic link –The link function creates a “hard link”: (newname, inode number) in a directory If not done carefully, can be a problem (destroy the tree structure of the directories) –Not allow to create a hard link on directories –Symbolic link, which is an indirect pointer to a file, gets around the problem need to be careful whether a function follow the symbolic link –Unlink do not follow symbolic link (only the symbolic link is removed), open follow the symbolic link by default.

Symbolic link –Try the following ‘ln –s /no/such/file myfile’ ‘ls myfile’ ‘cat myfile’ ‘ls –l myfile’ –System calls: #include int symlink(const char *actualpath, const char *sympath); int readlink(const char *pathname, char *buf, int bufsize);

File times –st_atime: last access time –st_mtime: last modification time –st_ctime: last status change time –Try: ‘ls –u -l’, ‘ls –c -l’ –The access time and modification time can be changed with the utime function. See example6.c.

Random access files –Current file offset can be manipulated with lseek calls, see example7.c. Try ‘more hole001’ and ‘od –c hole001’. #include off_t lssek(int filedes, off_t offset, int whence) whence = SEEK_SET, SEEK_CUR, SEEK_END

Operating on directories Only root can write, all other can only read (or write implicitly through system calls). #include DIR *opendir(const char *name) struct dirent *readdir(DIR *dp); void rewinddir (DIR *dp); int closedir(DIR *dp)

Operating on directories #include struct dirent *readdir(DIR *dp); struct dirent { ino_t d_ino; char *d_name[NAME_MAX+1]; } –Return NULL when reaching the end of the directory. –Example: implementing the ‘find’ command, see example8.c

Current Working Directory Can be changed using –int chdir(const char *pathname); –int fchdir(int filedes); Note that they only affect the current process’ working directory Obtaining current working directory –char *getcwd(char *buf, size_t size);

Review What is the most reliable way to check if a file exists? What other information are stored in the struct stat variable returned from stat()? What is the purpose of set-user-id and set-group-id in the file meta-data? Which system call sets the file permission? Give 2 functions that delete a file. What is the difference between hard link and symbolic link? How to create/delete a hard link? How to create/delete a symbolic link? Is a directory a file? Can you edit directory? What does chdir do?