CptS 360 (System Programming) Unit 6: Files and Directories

Similar documents
Files and Directories

Files (review) and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

File and Directories. Advanced Programming in the UNIX Environment

Advanced Programming in the UNIX Environment W. Richard Stevens

The UNIX File System

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

Files and Directories

Fuse Extension. version Erick Gallesio Université de Nice - Sophia Antipolis 930 route des Colles, BP 145 F Sophia Antipolis, Cedex France

The UNIX File System

Building blocks for Unix power tools

1 / 23. CS 137: File Systems. General Filesystem Design

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Lecture files in /home/hwang/cs375/lecture05 on csserver.

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CS631 - Advanced Programming in the UNIX Environment

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Overview. Over the next four weeks, we will look at these topics: Building Blocks. Advanced Authentication Issues.

1 / 22. CS 135: File Systems. General Filesystem Design

CSC 271 Software I: Utilities and Internals

OPERATING SYSTEMS: Lesson 12: Directories

RTEMS Filesystem Design Guide

UNIX File Hierarchy: Structure and Commands

Chp1 Introduction. Introduction. Objective. Logging In. Shell. Briefly describe services provided by various versions of the UNIX operating system.

CS , Spring Sample Exam 3

Lecture 21 Systems Programming in C

Virtual File System. Don Porter CSE 506

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Filesystem Hierarchy and Permissions

Filesystem Hierarchy and Permissions

Inode. Local filesystems. The operations defined for local filesystems are divided in two parts:

Virtual File System. Don Porter CSE 306

Unix Basics. UNIX Introduction. Lecture 14

Data Security and Privacy. Unix Discretionary Access Control

Chapter 8: Filesystem Implementation

(32 KB) 216 * 215 = 231 = 2GB

CIS Operating Systems File Systems Security. Professor Qiang Zeng Fall 2017

rfs Remote File System Softwarepraktikum für Fortgeschrittene

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

5/8/2012. Creating and Changing Directories Chapter 7

412 Notes: Filesystem

CSE 333 SECTION 3. POSIX I/O Functions

Files and Directories Filesystems from a user s perspective

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

Motivation. Operating Systems. File Systems. Outline. Files: The User s Point of View. File System Concepts. Solution? Files!

Operating system security models

UNIX FILESYSTEM STRUCTURE BASICS By Mark E. Donaldson

File Systems. What do we need to know?

Case study: ext2 FS 1

Secure Software Programming and Vulnerability Analysis

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

CST8207: GNU/Linux Operating Systems I Lab Six Linux File System Permissions. Linux File System Permissions (modes) - Part 1

Unix Filesystem. January 26 th, 2004 Class Meeting 2

RCU. ò Dozens of supported file systems. ò Independent layer from backing storage. ò And, of course, networked file system support

Input & Output 1: File systems

File System User API

Case study: ext2 FS 1

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Operating System Labs. Yuanbin Wu

Exploring the file system. Johan Montelius HT2016

CSCI-E28 Lecture 4 Outline. Internal Structure of Files, Directories, File Systems. Work from user view tosystem view, write pwd

File Management 1/34

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

Permission and Ownership

Files and the Filesystems. Linux Files

which maintain a name to inode mapping which is convenient for people to use. All le objects are

CS 1550 Project 3: File Systems Directories Due: Sunday, July 22, 2012, 11:59pm Completed Due: Sunday, July 29, 2012, 11:59pm

Files and Directories Filesystems from a user s perspective

Chapter 1 - Introduction. September 8, 2016

Unix File System. Class Meeting 2. * Notes adapted by Joy Mukherjee from previous work by other members of the CS faculty at Virginia Tech

lsx [ls_options ] [names]

CSE 333 SECTION 3. POSIX I/O Functions

Project 5 File System Protection

Processes are subjects.

File Management. Ezio Bartocci.

CS/CIS 249 SP18 - Intro to Information Security

3/26/2014. Contents. Concepts (1) Disk: Device that stores information (files) Many files x many users: OS management

CS 201. Files and I/O. Gerson Robboy Portland State University

rpaths Documentation Release 0.13 Remi Rampin

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CptS 360 (System Programming) Unit 2: Introduction to UNIX and Linux

ECE 598 Advanced Operating Systems Lecture 18

File Systems. q Files and directories q Sharing and protection q File & directory implementation

File handling is an important part of any web application. You often need to open and process a file for different tasks.

Portably Preventing File Race Attacks with User-Mode Path Resolution

Files and Directories Objectives Additional Features of the File System Properties of a File. Three major functions that return file information:

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

Linux C C man mkdir( ) mkdir Linux man mkdir mkdir( ) mkdir mkdir( )

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

Project 4: File System Implementation 1

Operating systems. Lecture 7 File Systems. Narcis ILISEI, oct 2014

Files and File Systems

UNIX File System. UNIX File System. The UNIX file system has a hierarchical tree structure with the top in root.

Welcome to getting started with Ubuntu Server. This System Administrator Manual. guide to be simple to follow, with step by step instructions

Implementation should be efficient. Provide an abstraction to the user. Abstraction should be useful. Ownership and permissions.

File Systems Ch 4. 1 CS 422 T W Bennet Mississippi College

Transcription:

CptS 360 (System Programming) Bob Lewis School of Engineering and Applied Sciences Washington State University Spring, 2019

Motivation Need to know your way around a filesystem. A properly organized filesystem is an aid to computing. Symbolic links can be very useful critters.

References Stevens & Rago Ch. 4

How Do I Find File Metadata? What is metadata? stat(2), lstat(2), and fstat(2) all return the same information, just different ways of requesting it Consider this code snippet: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>... struct stat *statbuf; int st = stat("/home/bobl", statbuf); Will this compile? Will it run? Always? If not, how could you make it better?

File Types regular file directory character device block device FIFO socket symbolic link There are macros to identify all of these. (See stat(2).) They act on the stat.st_mode field.

Finding Unusual Files try $ ls -l and $ ls --color remember file(1) Typical places for such files: /tmp /dev but these are just conventions.

What ID s are Attached to Files and to Processes? Files (and other filesystem objects) have one UID and one GID attached to them. Processes have at least six ID s associated with them: real UID and GID established at login rarely changed effective UID, GID, and supplementary GIDs initially == real may be reset by SUID programs saved UID and GID (discussed later)

File Access Permissions I Permission bits: (We already spoke of these.) rwxrwxrwx The only justification I know of for learning about octal numbers. To open any type of file by name requires execute (a.k.a. search) bit access of every directory (including., even implicitly) contained in the (full) path. This is also necessary for seeking executables in $PATH. Read permission on a directory lets us read the directory (e.g. for an ls ), execute/search lets us open a specific file. Example: Setting permissions on your home directory.

File Access Permissions II Read and write permissions affect open flags O_RDONLY, O_RDWR, and O_WRONLY. The O_TRUNC flag requires (only) write permission. Logical, right? To create a new file in a directory requires write and execute permission. To delete a file, we need write and execute permission in the directory containing the file. We don t need read or write permission on the file itself. Execute permission is needed to execute the file, which must be a regular file.

How UIDs/GIDs Affect File Access If the EUID (of the process) is 0, anything goes. If the EUID is the file s owner, look at user id bit for appropriate access mode Else if the EGID (or a supplementary GID) matches the GID of the file, look at the group bit for appropriate access mode. Else, look at the other bit for the appropriate access mode.

Ownership of New Files (and Directories and...) UID of file = EUID of process GID of file = EGID of process, unless the set-gid bit of the containing directory is set, in which case it s the GID of the directory. This was intended to allow groups to share directory contents. Kind of anachronistic now.

What Can Your Process Do to a File? access(2) Handy function to check accessibility. Note that it uses real UID and GID. Watch out for possible race condition with chmod(2) or chown(2).

How Can a Process Change Its Umask? umask(2) (previously discussed)

How Can a Process Change a File s Protections? chmod(2) and fchmod(2) This includes setting sticky, set-uid, and set-gid bits. sticky bit semantics: On a(n executable) file: vestigial and ignored. On a directory: A file contained within can be renamed or deleted only by the owner of the file, the owner of the directory, and (of course) root. Best sticky bit example: the /tmp directory: Everybody can write files to it, but not everybody should be able to rename or delete files from it.

Can I Change the Owner of a File? chown(2), fchown(2), and lchown(2) Can change owner and group, but -1 arguments leave that id unchanged. Only root can change owner and group of a file arbitrarily. Figures, doesn t it? A file s owner can change its group to any other group he/she belongs to.

How Big is This File? Use one of the stat(2) calls. stat.st_size is the length of the file in bytes. = 1 + offset of the last byte in the file You might use this to read a whole file into a buffer. There s a possible race condition. See it? How can you overcome it? stat.st_blocks is the number of blocks allocated for the file. stat.st_size may be less than, equal to, or greater than stat.st_blocks * stat.st_blksize. Why? block allocation holes

How Do I Shorten a File? truncate(2) and ftruncate(2) (see man page)

What is a Filesystem? Structure imposed by OS on any block device: hard disk (most often) CD-ROM or DVD-ROM USB stick floppy drive (remember those?) What are inodes? (discuss) Q: Where are filenames kept in the filesystem? Q: Where are paths kept in the filesystem?

How Do I Change the Name of a File? rename(2) obvious purpose atomic This only works if oldpath and newpath are on the same filesystem. (unlike mv(1))

Can a File Have More Than One Name? Yes, in two ways: links (a.k.a. hard links) A UNIX filesystem is really a DAG. link(2) and unlink(2) link counts restrictions (Hard links don t work everywhere.) symbolic links symlink(2) and readlink(2) internal representation of symlinks stat.st_size of a symlink == strlen(link_name) + 1 + 1 is for null-termination

What Timestamps are on a File? Kept with the inode: atime (a.k.a. actime) last time file was accessed (but not modified) set by (e.g.) read(2) (of > 0 bytes) execve(2) (later) pipe(2) (also later) mtime (a.k.a. modtime) last time file was modified (but not accessed) set by (e.g.) write(2) (of > 0 bytes) mknod(2) truncate(2) ctime last time file status was changed set when inode info is modified (owner, group, etc.)

How Do I Change Timestamps? utime(2) and utimes(2) allow modification of atime and mtime fields utime(2) resolution is 1 sec. utimes(2) resolution is 1 usec (!). utimensat(2) resolution is 1 nsec (!!).

How Do I Create and Remove Directories? mkdir(2) and rmdir(2) just like shell commands rmdir() won t remove a non-empty directory remove(3) general purpose clobber anything unlink()s a file rmdir()s a directory (still won t remove a non-empty directory)

How Can I Read Data About Directory Contents? opendir(3), readdir(3), scandir(3), seekdir(3), telldir(3), rewinddir(3), and closedir(3) struct dirent readdir(2) vs. readdir(3) This is not the function you are interested in. Don t use low-level (i.e. (2)) directory access functions. (There are synonyms. Be sure to include the right header.)

What s My Current Directory and How Do I Change It? 3 getcwd(2), chdir(2), and fchdir(2) (see man pages) This is actually an attribute of your process, not the filesystem.

Special Device Files device numbers major minor use macros to extract them stat.st_dev device number of a device containing a filesystem device number denotes the filesystem (i.e. its driver) stat.st_rdev device number of a character or block device file device number denotes the device itself

stat.st dev vs. stat.st rdev compare $ stat /home/bobl and $ stat /dev/sda2 with $ stat -f /dev/sda2 The former tells you about the file (stat.st_dev). The latter tells you about the filesystem. mounted on the raw device (stat.st_rdev).

How Can I Make Sure Stuff Has Been Written to Disk? sync(2) and fsync(2) No special permission required. (Why not?) Use with discretion: potentially time-consuming.