File and Directories. Advanced Programming in the UNIX Environment

Similar documents
Files and Directories

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

Files and Directories E. Im

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

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

Files and Directories

Files and directories. Updated by: Dr. Safwan Qasem Spring 2010 Original version created by: Dr. Mohamed El Bachir Menai

Operating System Labs. Yuanbin Wu

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

Operating System Labs. Yuanbin Wu

CSE 333 SECTION 3. POSIX I/O Functions

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

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

OPERATING SYSTEMS: Lesson 12: Directories

OPERATING SYSTEMS: Lesson 2: Operating System Services

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

The UNIX File System. File Systems and Directories UNIX inodes Accessing directories Understanding links in directories.

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

CSE 333 SECTION 3. POSIX I/O Functions

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

CSC 271 Software I: Utilities and Internals

UNIX System Calls. Sys Calls versus Library Func

Lecture 21 Systems Programming in C

CSCE 313 Introduction to Computer Systems

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

FILE SYSTEMS. Jo, Heeseung

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CSci 4061 Introduction to Operating Systems. File Systems: Basics

Files. Eric McCreath

File Systems Overview. Jin-Soo Kim ( Computer Systems Laboratory Sungkyunkwan University

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

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

CS , Spring Sample Exam 3

CS631 - Advanced Programming in the UNIX Environment

ADVANCED OPERATING SYSTEMS UNIT 2 FILE AND DIRECTORY I/O BY MR.PRASAD SAWANT

Unix File and I/O. Outline. Storing Information. File Systems. (USP Chapters 4 and 5) Instructor: Dr. Tongping Liu

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Important Dates. October 27 th Homework 2 Due. October 29 th Midterm

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

structs as arguments

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

Operating Systems Lab

CS240: Programming in C

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

Lecture 23: System-Level I/O

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

UNIT I INTRODUCTION TO UNIX & FILE SYSTEM

File Types in Unix. Regular files which include text files (formatted) and binary (unformatted)

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

Files and Directories Filesystems from a user s perspective

CSE 410: Systems Programming

The course that gives CMU its Zip! I/O Nov 15, 2001

Preview. lseek System Calls. A File Copy 9/18/2017. An opened file offset can be explicitly positioned by calling lseek system call.

File I/O. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

File Descriptors and Piping

Thesis, antithesis, synthesis

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

Homework 5. Due Date: Friday, June 7, 2002, at 11:59PM; no late assignments accepted Points: 100

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

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

File I/O. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions

System- Level I/O. Andrew Case. Slides adapted from Jinyang Li, Randy Bryant and Dave O Hallaron

The bigger picture. File systems. User space operations. What s a file. A file system is the user space implementation of persistent storage.

Files and Directories Filesystems from a user s perspective

All the scoring jobs will be done by script

File Systems. Today. Next. Files and directories File & directory implementation Sharing and protection. File system management & examples

UNIX System Programming

Parents and Children

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

Contents. Programming Assignment 0 review & NOTICE. File IO & File IO exercise. What will be next project?

ELEC-C7310 Sovellusohjelmointi Lecture 3: Filesystem

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

0UNIT-III UNIX FILE SYSTEM

everything is a file main.c a.out /dev/sda1 /dev/tty2 /proc/cpuinfo file descriptor int

CPSC 410/611: File Management. What is a File?

ECE 650 Systems Programming & Engineering. Spring 2018

Programiranje za UNIX. Datoteke i direktoriji

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

All the scoring jobs will be done by script

Operating systems. Lecture 7

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

12: Filesystems: The User View

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

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Preview. Interprocess Communication with Pipe. Pipe from the Parent to the child Pipe from the child to the parent FIFO popen() with r Popen() with w

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Process Creation in UNIX

The UNIX File System

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

Contents. NOTICE & Programming Assignment 0 review. What will be next project? File IO & File IO exercise

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

The UNIX File System

System-Level I/O. Topics Unix I/O Robust reading and writing Reading file metadata Sharing files I/O redirection Standard I/O

Transcription:

File and Directories Advanced Programming in the UNIX Environment

stat Function #include <sys/stat.h> int stat(const char *restrict pathname, struct stat *restrict buf ); int fstat(int fd, struct stat *buf); int lstat(const char *restrict pathname, struct stat *restrict buf ); int fstatat(int fd, const char *restrict pathname, struct stat *restrict buf, int flag); All four return: 0 if OK, 1 on error the stat function returns a structure of information about the named file.

stat Structure

File Types Regular file. The most common type of file, which contains data of some form. Directory file. A file that contains the names of other files and pointers to information on these files. Block special file. A type of file providing buffered I/O access in fixed-size units to devices such as disk drives. Character special file. A type of file providing unbuffered I/O access in variable-sized units to devices. FIFO. A type of file used for communication between processes. It s sometimes called a named pipe. Socket. A type of file used for network communication between processes. Symbolic link. A type of file that points to another file.

File type macros

Set-User-Id and Set-Group-Id

Set-User-Id and Set-Group-Id When we execute a program file, the effective user ID of the process is usually the real user ID, and the effective group ID is usually the real group ID. However, we can also set a special flag in the file s mode word (st_mode) that says, When this file is executed, set the effective user ID of the process to be the owner of the file (st_uid). Similarly, we can set another bit in the file s mode word that causes the effective group ID to be the group owner of the file (st_gid). These two bits in the file s mode word are called the set-user-id bit and the set-group-id bit.

File Access Permissions The st_mode value also encodes the access permission bits for the file.

Read vs Execute for a Directory Read permission for a directory and execute permission for a directory mean different things. Read permission lets us read the directory, obtaining a list of all the filenames in the directory. Execute permission lets us pass through the directory when it is a component of a pathname that we are trying to access.

access and faccessat Functions Used when a process wants to test accessibility based on the real user and group IDs. When set-user- ID or the set-group-id feature it might want to verify he real user can access a given file. #include <unistd.h> int access(const char *pathname, int mode); int faccessat(int fd, const char *pathname, int mode, int flag); Both return: 0 if OK, 1 on error

umask Function The umask function sets the file mode creation mask for the process and returns the previous value. #include <sys/stat.h> mode_t umask(mode_t cmask); Returns: previous file mode creation mask

umask Function Users can set the umask value to control the default permissions on the files they create. This value is expressed in octal, with one bit representing one permission to be masked off.

chmod #include <sys/stat.h> int chmod(const char *pathname, mode_t mode); int fchmod(int fd, mode_t mode); int fchmodat(int fd, const char *pathname, mode_t mode, int flag); All three return: 0 if OK, 1 on error The chmod function operates on the specified file, whereas the fchmod function operates on a file that has already been opened.

chmod

Sticky Bit If it was set for an executable program file, then the first time the program was executed, a copy of the program s text was saved in the swap area when the process terminated. On contemporary systems, the use of the sticky bit has been extended. The Single UNIX Specification allows the sticky bit to be set for a directory. If the bit is set for a directory, a file in the directory can be removed or renamed only if the user has write permission for the directory and meets one of the following criteria: Owns the file Owns the directory Is the superuser

File Systems

link, unlink, remove, and rename Functions #include <unistd.h> int link(const char *existingpath, const char *newpath); Returns: 0 if OK, 1 on error #include <unistd.h> int unlink(const char *pathname); Returns: 0 if OK, 1 on error

#include <stdio.h> int remove(const char *pathname); Returns 0 if OK, 1 on error #include <stdio.h> int rename(const char *oldname, const char *newname); Returns: 0 if OK, 1 on error

symlink and readlink Functions #include <unistd.h> int symlink(const char *actualpath, const char *sympath); Returns: 0 if OK, 1 on error #include <unistd.h> ssize_t readlink(const char* restrict pathname, char *restrict buf, size_t bufsize); Returns: number of bytes read if OK, 1 on error

utime Function #include <utime.h> int utime(const char *pathname, const struct utimbuf *times); Returns: 0 if OK, 1 on error struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ }

mkdir and rmdir Functions #include <sys/stat.h> int mkdir(const char *pathname, mode_t mode); Returns: 0 if OK, 1 on error #include <unistd.h> int rmdir(const char *pathname); Returns: 0 if OK, 1 on error

Reading Directories #include <dirent.h> DIR *opendir(const char *pathname); Returns: pointer if OK, NULL on error struct dirent *readdir(dir *dp); Returns: pointer if OK, NULL at end of directory or error void rewinddir(dir *dp); int closedir(dir *dp); Returns: 0 if OK, 1 on error long telldir(dir *dp); Returns: current location in directory associated with dp void seekdir(dir *dp, long loc);

struct dirent { ino_t d_ino; /* i-node number */ char d_name[name_max + 1]; /* null-terminated filename */ }

Programs Write a program check file types #include "apue.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); }

Utime program #include "apue.h" #include <fcntl.h> #include <utime.h> int main(int argc, char *argv[]) { int i, fd; struct stat statbuf; struct utimbuf timebuf; for (i = 1; i < argc; i++) { if (stat(argv[i], &statbuf) < 0) { /* fetch current times */ err_ret("%s: stat error", argv[i]); continue; } if ((fd = open(argv[i], O_RDWR O_TRUNC)) < 0) { /* truncate */ err_ret("%s: open error", argv[i]); continue; } close(fd); timebuf.actime = statbuf.st_atime; timebuf.modtime = statbuf.st_mtime; if (utime(argv[i], &timebuf) < 0) { /* reset times */ err_ret("%s: utime error", argv[i]); continue; } } exit(0); }

Programs WAP to check whether the file exist or not using access function WAP to mask permission bits WAP to change the mode using chmod WAP to create hard link and soft link WAP to copy one time to another time Emulate ls command Emulate ls li