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

Similar documents
csci-e28 lecture 4samples page 1

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

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

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

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

Operating System Labs. Yuanbin Wu

OPERATING SYSTEMS: Lesson 12: Directories

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

structs as arguments

CS240: Programming in C

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

Operating System Labs. Yuanbin Wu

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSE 333 SECTION 3. POSIX I/O Functions

The UNIX File System

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

System Programming. Introduction to Unix

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

CSC 271 Software I: Utilities and Internals

File and Directories. Advanced Programming in the UNIX Environment

The UNIX File System

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

Chapter 1. Introduction

Lecture 23: System-Level I/O

CSCE 313 Introduction to Computer Systems

Homework 4 Answers. Due Date: Monday, May 27, 2002, at 11:59PM Points: 100. /* * macros */ #define SZBUFFER 1024 /* max length of input buffer */

CSE 333 SECTION 3. POSIX I/O Functions

CSci 4061 Introduction to Operating Systems. File Systems: Basics

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

Files and Directories

CS167 Programming Assignment 1: Shell

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

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

CS , Spring Sample Exam 3

File System User API

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

Lecture 21 Systems Programming in C

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

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

CSCI-E28 LECTURE 2 SAMPLES PAGE 1

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

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

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

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

INTRODUCTION TO UNIX FILE SYSTEM:

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

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

Matt Ramsay CS 375 EXAM 2 Part 1

Systems Programming. 09. Filesystem in USErspace (FUSE) Alexander Holupirek

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

UNIX System Calls. Sys Calls versus Library Func

Remote Procedure Calls, TLI, STREAMS

Week 2 Lecture 3. Unix

File Management 1/34

CS240: Programming in C. Lecture 14: Errors

Files and Directories Filesystems from a user s perspective

12: Filesystems: The User View

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

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

All the scoring jobs will be done by script

File IO and command line input CSE 2451

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

All the scoring jobs will be done by script

UNIX System Programming

Operating Systems Lab

Exploring the file system. Johan Montelius HT2016

PRACTICAL NO : 1. AIM: To study various file management system calls in UNIX.

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

CITS2002 Systems Programming. Identifying related data. 1 next CITS2002 CITS2002 schedule. Let's consider the st project for CITS1002.

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

Files and Directories

Introduction: The Unix shell and C programming

Slide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Naked C Lecture 6. File Operations and System Calls

UNIX System Programming. Overview. 1. A UNIX System. 2. Processes (review) 2.1. Context. Pipes/FIFOs

File Access. FILE * fopen(const char *name, const char * mode);

File System (FS) Highlights

Pintos Project 4 File Systems. November 14, 2016

CSCI 2132 Software Development. Lecture 5: File Permissions

Computer Programming: Skills & Concepts (CP1) Files in C. 18th November, 2010

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

CSC209S Midterm (L0101) Spring 1999 University of Toronto Department of Computer Science

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

Table of contents. Our goal. Notes. Notes. Notes. Summer June 29, Our goal is to see how we can use Unix as a tool for developing programs

Thesis, antithesis, synthesis

Files and the Filesystems. Linux Files

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

CSC 1600 Unix Processes. Goals of This Lecture

M.CS201 Programming language

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Chapter 4. File Systems. Part 1

POSIX Semaphores. Operations on semaphores (taken from the Linux man page)

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

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

CSci 4061 Introduction to Operating Systems. IPC: Basics, Pipes

Introduction to Linux. Fundamentals of Computer Science

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

Transcription:

CSCI-E28 Lecture 4 Outline Topics: Approach: Internal Structure of Files, Directories, File Systems Work from user view tosystem view, write pwd Featured Commands: mkdir, rmdir, rm, ln, mv, pwd Main Ideas: Users see a file system as a tree of directories, files, and info Afile system is a sequence of disk blocks Afile is a struct of info (an inode) and a list of data blocks Adirectory is a list of inode numbers and names Agenda Intro What does it mean to be in a directory? Write pwd. One tree - multiple disks How doseveral disks appear as a single tree of directories? errno and perror() system calls return -1 on error; what s the problem? User View offile System directory tree, files, info, moving around, moving files mkdir abuse (don t try this at home) Face Reality Adisk is a stack of platters, tracks, sectors, just blocks The Unix File System Three (well, four) parts Inodes and Device ID Identifying each item in a tree Looking at Operations in terms of a Unix File System Creating a file Building a Tree File operations: rm, ln, mv, mkdir, rmdir Writing pwd inodes and names Symbolic Links Definition, Examples, Directories, cross-system

csci-e28 lecture 4samples page 1 :::::::::::::: perror_demo.c :::::::::::::: #include <errno.h> #include <string.h> #include <fcntl.h> program perror_demo.c * purpose show how errno is set by syscalls * and how perror prints the associated message * usage perror_demo * action tries to open() a file for a given mode * then, if error, prints errno and message int main() char name[100]; filename int mode; second arg to open int rv = 0; program status reprt printf("open what file or dir? "); fgets(name, 100, stdin); name[strlen(name)-1] = \0 ; printf(" Open %s for 0,1, or 2? ", name); scanf("%d", &mode); if ( open( name, mode ) == -1 ) printf("oh No!, errno %d just occurred\n", errno); perror( name ); print message rv = 1; else printf("%s opened ok\n", name ); return rv; :::::::::::::: burrow :::::::::::::: #!/bin/sh # # script burrow # purpose show how to dig deep holes in a file system # method mkdir x; cd x; repeat (like on shampoo bottles) # warning good way to antagonize system managers while true do mkdir tedwilliams cd tedwilliams done :::::::::::::: rm1.c :::::::::::::: example of using unlink to delete a file int main(int ac, char *av[]) int rv = 0; while(--ac) if ( unlink( *++av ) == -1 ) perror(*av); rv = 1; else printf("%s is gone\n", *av); return rv;

csci-e28 lecture 4samples page 2 :::::::::::::: rm2.c :::::::::::::: * rm2 - interactive version of rm using unlink(2) int main() char name[100]; char confirm[100]; int rv = 0; return value from main while ( printf("delete what file? "), fgets(name,100,stdin) ) printf("about to unlink %s, ok? ", name ); fgets(confirm,100,stdin); if ( *confirm == y ) if( unlink(name) == -1 ) perror(name); rv = 1; else printf("%s is gone\n", name); return rv; :::::::::::::: mv1.c :::::::::::::: #include <errno.h> #include <stdlib.h> * program mv1.c * purpose show how to use link and unlink to rename a file * notes checks errno to make it more adaptable * note This version could be replaced by rename() but it * works differently when the target exists int main(int ac, char *av[]) if ( link( av[1], av[2])!= -1 ) unlink( av[1] ); else if ( errno == EEXIST ) name2 already in use if (unlink( av[2] ) == -1 ) not any more perror(av[2]); return main(ac,av); and try again return 0;

csci-e28 lecture 4samples page 3 :::::::::::::: mv2.c :::::::::::::: #include <limits.h> #include <stdlib.h> #include <string.h> * ** mv28 version 1 ** ** if last arg is a dir, then move all files into there ** if two args and last is a file, then move first to ** second. will clobber second if exists. ** Uses isadir. ** ** problems: 1) what if an arg is a dir? ** 2) what about paths in source files? * void mv(char *,char *); void mv_to_newdir(int, char **, char *); int isadir(char *); int main( int ac, char *av[] ) if ( ac < 3 ) fprintf( stderr, "too few args\n"); * if last arg is a dir, then move args to there if ( isadir( av[ac-1] ) ) mv_to_newdir(ac-1,av,av[ac-1]); * last arg is not a dir, so must be two args only else if ( ac!= 3 ) fprintf( stderr, "too many args\n"); else mv(av[1], av[2]); return 0; void mv_to_newdir( int ac, char *av[], char *newdir ) * move av[1], av[2],... av[ac-1] into newdir char newpath[path_max]; int pos ; for( pos = 1 ; pos<ac; pos++ ) if ( strlen(newdir)+strlen(av[pos])+2 <= PATH_MAX ) sprintf( newpath, "%s/%s", newdir, av[pos]); mv( av[pos], newpath ); void mv( char *oldname, char *newname ) if ( link(oldname, newname) == -1 unlink(oldname) == -1 ) fprintf(stderr,"error mv ing %s to %s\n", oldname, newname);

csci-e28 lecture 4samples page 4 :::::::::::::: pwd28.c :::::::::::::: #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #include <stdlib.h> #include <string.h> * ** a simplified version of pwd ** ** Starts in current directory and recursively ** climbs up to root of filesystem, prints top part ** then prints current part ** Uses readdir() to get info about each thing * void get_info(char*, dev_t *, ino_t *); void printpathto(dev_t, ino_t); void inum_to_name(dev_t, ino_t, char *); int main() ino_t myinode ; dev_t mydev ; get_info(".", &mydev, &myinode); printpathto( mydev, myinode ); print path to here putchar( \n ); then add newline return 0; void printpathto( dev_t this_dev, ino_t this_inode ) * prints path leading down to an object with this dev,inode ino_t myinode, par_inode; dev_t mydev, par_dev; char its_name[bufsiz]; chdir( ".." ); up one dir inum_to_name(this_dev,this_inode,its_name ); get its name get_info(".",&mydev,&myinode); print head get_info("..",&par_dev,&par_inode); if ( myinode!= par_inode mydev!= par_dev ) printpathto( mydev, myinode ); recursively printf("/%s", its_name ); now print name of this * gets inode and dev for the specified path, uses lstat void get_info(char *path, dev_t *dp, ino_t *inp) struct stat info; if ( lstat(path, &info) == -1 ) perror(path); exit(2); *inp = info.st_ino; *dp = info.st_dev;

csci-e28 lecture 4samples page 5 void inum_to_name(dev_t dev_to_find, ino_t inode_to_find, char *namebuf) * looks through current directory for a file with this inode * number and copies its name into namebuf DIR *dir_ptr; the directory struct dirent *direntp; each entry ino_t cur_inum; dev_t cur_dev; dir_ptr = opendir( "." ); if ( dir_ptr == NULL ) fprintf(stderr, "cannot open a directory\n"); * search directory for a file with specified inum and device while ( ( direntp = readdir( dir_ptr ) )!= NULL ) get_info(direntp->d_name, &cur_dev, &cur_inum); if ( cur_inum == inode_to_find && cur_dev == dev_to_find ) strcpy( namebuf, direntp->d_name ); closedir( dir_ptr ); return ; fprintf(stderr, "error looking for inum %d\n", (int) inode_to_find);

A Unix Directory Tree Can Consist of Several Disks Notes: Each disk can be a regular hard disk, a USB flash drive, a camera memor y card,... any block device The root directory of each mounted file system obscures the directory on which it is mounted The disks can be connected directly to the computer or may be connected over a networ k. An nfs disk is attached using a networ k file system connection.