Files and Directories File types stat functions for file information

Slides:



Advertisements
Similar presentations
Concepts about the file system 2. The disk structure 3. Files in disk – The ext2 FS 4. The Virtual File System (c) 2013, Prof. Jordi Garcia.
Advertisements

Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
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.
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.
Filesystem Hierarchy Standard (FHS) –Standard of outlining the location of set files and directories on a Linux system –Gives Linux software developers.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
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.
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 5 Acknowledgements: The syllabus and power point presentations are modified versions.
Linux+ Guide to Linux Certification, Second Edition
Managing Files CSCI N321 – System and Network Administration Copyright © 2000, 2011 by the Trustees of Indiana University except as noted.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
Bash startup files Linux/Unix files stty Todd Kelley CST8207 – Todd Kelley1.
File Permission and Access. Module 6 File Permission and Access ♦ Introduction Linux is a multi-user system where users can assign different access permission.
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
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.
Managing Files. Module 5 Managing Files ♦ Introduction “On a Linux system, everything is a file; if something is not a file, it is a process.” ♦ Topics.
Process Control Process identifiers Process creation fork and vfork wait and waitpid Race conditions exec functions system function.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
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.
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
Advanced Programming in the Unix Environment
Managing Files CSCI N321 – System and Network Administration Copyright © 2000, 2007 by the Trustees of Indiana University except as noted.
Manage Directories and Files in Linux Part 2. 2 Identify File Types in the Linux System The file types in Linux referred to as normal files and directories.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
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.
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.
The Unix File system (UFS) Presented by: Gurpreet Singh Assistant Professor Department of School of Computing and Engineering Galgotias University.
NCHU System & Network Lab Lab 14 File and Directory.
CSCI 330 The UNIX System Unit V Permissions. all access to directories and files is controlled UNIX uses discretionary access control (DAC) model each.
CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.
CS 241 Section Week #8 (10/29/09). Outline MP5 Overview Files & I/O UNIX File Systems inodes Directories Links.
CSCI 330 UNIX and Network Programming Unit VIII: I/O Management II.
Jozef Goetz, expanded by Jozef Goetz, 2008 Credits: Parts of the slides are based on slides created by UNIX textbook authors, Syed M. Sarwar, Robert.
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.
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
Privileges: who can control what
Week 12 - Thursday CS222.
Unix Programming Environment
Unix Files Course code: 10CS62 Prepared by Department of CSE
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
Operating System Hebrew University Spring 2004
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
File Structure Related system calls
File I/O (1) Prof. Ikjun Yeom TA – Hoyoun
Persistence: File System API
CSE 333 – Section 3 POSIX I/O Functions.
Chien-Chung Shen CIS, UD
Presentation transcript:

Files and Directories File types stat functions for file information File permissions suid and sgid Sticky bit Hard link and soft link Directory operations Device special files File system

File Types Regular file Directory file Character special file (unbuffered I/O access) Block special file (buffered I/O) Named Pipe (a type of IPC) - FIFO Socket (a network form of IPC) Symbolic Link (a file that just points to another file)

stat, fstat and lstat int stat(const char *path, struct stat *buf); int fstat(int filedes, struct stat *buf); int lstat(const char *path, struct stat *buf); All three return 0 on success or -1 on failure All three set buf to point to a structure of information stat get info about the file fstat gets info about an already open file lstat same as stat but if given a symbolic link will get info about the link instead of what the link points to

stat structure Definition on page 88 st_mode st_ino st_size st_dev st_rdev st_nlink st_uid st_gid st_size st_blocks st_blksize st_atime st_mtime st_ctime st_mode – file type and mode (permissions) st_ino - i-node number (serial number) st_dev - device number st_rdev - device number for special files st_nlink - number of links st_uid - user id of owner st_gid - group id of owner st_size - number of bytes (for regular files) st_blocks – number of disk blocks allocated st_blksize – best I/O block size st_atime - time of last access st_mtime - time of last modification st_ctime - time of last file status change

Determining the type of a file Set of macros defined in <sys/stat.h> can be used. They all accept the st_mode field of the stat struct as their only parameter S_ISREG(mode_t mode) S_ISDIR(mode_t mode) S_ISCHR(mode_t mode) S_ISBLK(mode_t mode) S_ISFIFO(mode_t mode) S_ISLNK(mode_t mode) S_ISSOCK(mode_t mode) Example code on page 90, fig 4.3 Similar program http://www.cs.uah.edu/~kkeen/CS590/examples/filesanddirs/lstat/lstatexample.c

Set-User-ID & Set-Group-ID Every process has 6 or more IDs Real user ID Real group ID Effective user ID Effective group ID Supplementary group IDs Saved set-user-ID Saved set-group-ID Green – IDs of the account that starts the process. Usually taken from login information such as that defined in /etc/passwd file Blue – used for file access permission checks Red – previous effective UID and GID saved by the exec function

Set-User-ID & Set-Group-ID Normally, effective IDs are the same as real user IDs Sometimes, we need to run a process with the permissions of the user or group that owns the file (ex: passwd) Two bits in the file’s mode word (st_mode) allow this. set-user-ID bit set-group-ID bit Setting the set-user-ID bit will cause the effective UID to be the same as st_uid (owner of the file) Setting the set-group-ID bit will cause the effective GID to be the same as st_gid (owning group of the file)

Set-User-ID & Set-Group-ID When creating a file with the creat function, the new file’s UID is the effective UID of the process, and the new file’s GID is the effective GID of the process

Checking set-user-ID and set-group-ID bits Can use S_ISUID and S_ISGID masks Ex: if(S_ISUID & st_mode) if(S_ISGID & st_mode)

Setting SUID and GUID SUID has a value of 4 GUID has a value of 2 chmod 4755 afile -rwsr-xr-x chmod 2755 afile -rwxr-sr-x chmod 6755 afile -rwsr-sr-x Execute permission must be set for the associated SUID and GUID bits to have any effect.

File access permissions Read section 4.5 on page 92. Note the different permissions required for operations on regular files versus directories

access function Tests to see if the real user ID and real group ID allow access to a file Useful if a program that may be running as another user (ex root) wants to be sure that the person that started the program has permissions to access some file int access(const char *pathname, int mode); Returns 0 if ok, -1 otherwise

access function (cont) In this context, mode is not the same as in previous functions. Here, mode is the bitwise OR of several constants R_OK - test for read permission W_OK - test for write permission X_OK - test for execute permission F_OK - test for file existence Example: http://www.cs.uah.edu/~kkeen/CS590/examples/filesanddirs/access/accessexample.c

umask function mode_t umask(mode_t mask); Sets the “file mode creation mask” “file mode creation mask” is used to specify the default permissions of newly created files Since we are dealing with a mask it is the complement of what we want. So we set the bits corresponding to the permissions we don’t want.

umask function When using open or creat, any bits in mode that are also set in the file creation mask are turned off. All the UNIX shells also define a shell command called umask that displays the default umask set at login. Each process can change its own mask without interfering with each other http://www.cs.uah.edu/~kkeen/CS590/examples/filesanddirs/umask/umaskexample.c

File Size st_size member of stat structure holds the size of a file Only meaningful for regular files, directories and symbolic links Regular file – number of bytes in the file Directories – multiple of a number (more on this later) Symbolic Links – number of chars in filename Symbolic link – no ending null byte on filename because we know the size from st_size

Sticky Bit Historically Used to tell the system to keep a copy of the “text” portion of a program in the swap space even after the program exits. (text = instructions, not data) Swap file is contiguous  faster start on next execution.

Sticky Bit Currently To set the sticky bit Used on directories, indicates that in order to rename or delete a file in that directory a user must both have write permissions to the directory and one of the following must be true User owns the file User owns the directory User is the superuser To set the sticky bit chmod +t mydir This is useful for directories where everyone has permissions to create files, but only the owners of files should be able to delete them.

chmod and fchmod Allows us to change the existing permissions for a file int chmod(const char *path, mode_t mode); int fchmod(int fildes, mode_t mode); Returns 0 on success or -1 on failure mode is bitwise OR of constants shown on page 99 fig 4.11 Effective UID of process must be the same as the UID of the file or the process must have superuser permissions

link, unlink, remove and rename link int link(const char *oldpath, const char *newpath); Creates a new hard link within the file system oldpath is the file to be linked. Newpath must already exist except for the last portion which will be created If all of newpath already exists, an error will be returned Returns 0 if ok, -1 otherwise A hard link looks like another regular file that actually points to the same data. What really happens is that a new entry is put into the directory that points to the same i-node as the original file. Thus, we have two “filenames” that are really one file. Every file keep track of the number of hard links that refer to it. This data is stored in the st_nlink field of the stat struct Ex: link(“/home/bob/data.txt”, ”/home/joe/foo.txt”); /home/joe must already exist, and foo.txt will be created. If foo.txt already exists in that directory, an error will be returned.

link, unlink, remove and rename unlink int unlink(const char *pathname); Removes directory entry indicated by pathname Decrements the link count of the file Must have write and execute on containing directory Only when link count reaches 0 and no process has the file open will it be deleted Also, if sticky bit is set on the directory we must either Own the file Own the directory Have superuser privileges Because a file is not truly removed while a process has it open, this makes unlink very useful for temporary files a process may need to create. The process can create the file, and immediately unlink it so that if the process crashes, the temp file will automatically be removed. If pathname is a symbolic link, the symbolic link will be removed, not the file it points to

link, unlink, remove and rename remove int remove(const char *pathname); Calls unlink for files and rmdir for directories rename int rename(const char *oldpath, const char *newpath); Equivelant to mv shell command superuser can call unlink on a directory, but really should use rmdir instead

Symbolic Links Indirect pointer to a file or directory Created to get around limitations of hard links Hard links must live in the same file system that the think it links to is in Only superuser can hard link to a directory

symlink and readlink symlink int symlink(const char *oldpath, const char *newpath); Creates the symbolic link file newpath that contains the text contained in oldpath oldpath and newpath may be on different file systems readlink ssize_t readlink(const char *path, char *buf, size_t bufsiz); open function follows symbolic links. readlink operates on the link file itself String from file placed in buf, with number of bytes in bufsiz. String is not null terminated Readlink returns number of bytes read if ok, or -1 otherwise

File Times 3 times are kept for any file Last access time Last modified time Last status change time These are stored in the following fields of the stat struct st_atime st_mtime st_ctime The ls shell command can display sorted by any of these three times. The default time display is last modified. ls –lu can display the last access time ls –lc can display the last status change time Examples of operations that would effect the various times: Access time: read Modification time: write Status change time: chmod, chown (basically anything that changes the file’s i-node)

utime int utime(const char *filename, const struct utimbuf *buf); struct utimbuf { time_t actime; time_t modtime; } actime and modtime are calendar times marking seconds since Epoch buf may be NULL, in which case the time is updated to current system time Unless using NULL for buf, just having write permissions is not enough. Effective user ID must equal owner ID or process must have superuser privileges Requires <utime.h> Epoch: 00:00:00 January 1st 1970 UTC (aka GMT) touch uses utime on some implementations Some backup programs use utime when restoring files to set the date of the newly restored file back to the original date http://www.cs.uah.edu/~kkeen/CS590/examples/filesanddirs/utime/utimeexample.c

Directory Operations mkdir and rmdir int mkdir(const char *pathname, mode_t mode); int rmdir(const char *pathname); Create and remove the specified directory For mkdir, the mode parameter is the same as those used for open. However, recall that interactions with the umask may occur! When creating directories, remember that we usually want execute permissions so that the files within the directory can be listed

Reading Directories DIR *opendir(const char *name); struct dirent *readdir(DIR *dir); void rewinddir(DIR *dir); int closedir(DIR *dir); off_t telldir(DIR *dir); void seekdir(DIR *dir, off_t offset); struct dirent { ino_t d_ino; char d_name[NAME_MAX + 1]; } The dirent struct contains information about individual files in that directory. At a minimum it must contain the file name and i-node number of the associated file. These functions require #include <dirent.h>

chdir fchdir and getcwd int chdir(const char *path); int fchdir(int fd); Change the current working directory of the process long getcwd(char *buf, unsigned long size); Get the current working directory of the process http://www.cs.uah.edu/~kkeen/CS590/examples/filesanddirs/getcwd/getcwdexample.c

Device Special Files Many devices are represented as files within /dev directory Each device has major number and minor number for the Kernel to identify it Major number usually specifies the driver to use for the device Minor number may be the specific sub device or options for that device Example: For file systems, the same driver (major number) would be used for a single disk, but the various file systems would have different minor numbers.

Device Special Files ls –lL on a device file will show major and minor numbers instead of size of file st_dev and st_rdev st_dev for every file is the device number under the filesystem containing that filename and its associated i-node st_rdev (only for character and block special files) contains actual device number Capitol L option to ls says to follow symbolic links

Device Special Files Obtaining the major and minor numbers Use the major and minor macros major(st_dev) minor(st_dev) For block and character special devices use st_rdev instead to obtain real device number http://www.cs.uah.edu/~keen/CS590/filesanddirs/device/devexample.c