OPERATING SYSTEMS: Lesson 12: Directories

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

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

Operating System Labs. Yuanbin Wu

CSCE 313 Introduction to Computer Systems

FILE SYSTEMS. Tanzir Ahmed CSCE 313 Fall 2018

Operating System Labs. Yuanbin Wu

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

File and Directories. Advanced Programming in the UNIX Environment

OPERATING SYSTEMS: Lesson 11: Files

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

OPERATING SYSTEMS: Lesson 13: File Systems

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

CSE 333 SECTION 3. POSIX I/O Functions

OPERATING SYSTEMS: Lesson 2: Operating System Services

System Programming. Introduction to Unix

CSE 333 SECTION 3. POSIX I/O Functions

The UNIX File System

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

Lecture 21 Systems Programming in C

File Systems. File system interface (logical view) File system implementation (physical view)

The UNIX File System

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

CSC 271 Software I: Utilities and Internals

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

File Systems. What do we need to know?

Files and Directories

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

Chapter 4. File Systems. Part 1

Long-term Information Storage Must store large amounts of data Information stored must survive the termination of the process using it Multiple proces

CS240: Programming in C

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

File Management 1/34

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

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

CS , Spring Sample Exam 3

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

File Systems. Information Server 1. Content. Motivation. Motivation. File Systems. File Systems. Files

FILE SYSTEMS. Jo, Heeseung

CS333 Intro to Operating Systems. Jonathan Walpole

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

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

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

This document gives a general overview of the work done by an operating system and gives specific examples from UNIX.

structs as arguments

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

CS 4284 Systems Capstone

Input & Output 1: File systems

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

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

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

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

Files and File System

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

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

csci-e28 lecture 4samples page 1

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

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

File Management. Ezio Bartocci.

CMSC421: Principles of Operating Systems

Chapter 11: File-System Interface. File Concept. File Structure

OPERATING SYSTEMS: Lesson 1: Introduction to Operating Systems

File System: Interface and Implmentation

Files and Directories

Typical File Extensions File Structure

Unix Filesystem. January 26 th, 2004 Class Meeting 2

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

CS4500/5500 Operating Systems File Systems and Implementations

Chapter 1. Introduction

Chapter 7 File Operations

Chapter 11: File-System Interface

OPERATING SYSTEMS CS136

F 4. Both the directory structure and the files reside on disk Backups of these two structures are kept on tapes

File System (FS) Highlights

Project 3: An Introduction to File Systems. COP4610 Florida State University

File Systems: Naming

Chapter 11: File-System Interface. Long-term Information Storage. File Structure. File Structure. File Concept. File Attributes

Chapter 10: File-System Interface

Exploring the file system. Johan Montelius HT2016

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

CS720 - Operating Systems

UNIT V SECONDARY STORAGE MANAGEMENT

Chapter 10: File-System Interface

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

CMSC421: Principles of Operating Systems

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

Files. Eric McCreath

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

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

CSE325 Principles of Operating Systems. File Systems. David P. Duggan. March 21, 2013

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Project 3: An Introduction to File Systems. COP 4610 / CGS 5765 Principles of Operating Systems

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

Week 10 Project 3: An Introduction to File Systems. Classes COP4610 / CGS5765 Florida State University

Chapter 6: File Systems

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

CS3600 SYSTEMS AND NETWORKS

A file system is a clearly-defined method that the computer's operating system uses to store, catalog, and retrieve files.

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

Introduction to OS. File Management. MOS Ch. 4. Mahmoud El-Gayyar. Mahmoud El-Gayyar / Introduction to OS 1

Transcription:

OPERATING SYSTEMS: Lesson 12: Directories Jesús Carretero Pérez David Expósito Singh José Daniel García Sánchez Francisco Javier García Blas Florin Isaila 1

Goals To know the concepts of file and directory and their characteristics. To use file and directory management services offered by de operating system. To understand a file system structure. To understand the mechanisms supporting a file server and to apply them to simple exercises. 2

Content Directories Structure alternatives Name interpretation Directory handling. 3

File organization A file system may store a great number of files. Mechanism needed to organize and locate files. Extensions: Organization by file type. Directory: abstraction of a file container. 4

Concept Directory: Object uniquely relating a user file name with an internal file descriptor. Organizes and provides information about the file system structure. A directory has an entry per stored file. Entry information: Internal file descriptor. In some cases, some file attributes. 5

Example: Windows explorer 6

Directories: logical view Hierarchical approach. When a file is opened the OS looks for the name in the directory structure. Operations on directories: Create and erase directories. Open and close directories. Rename directory. Read directory entries. Directory hierarchical organization: Simplifies file naming (unique names). Provides distribution management => group files logically (same user, same application, same task, ) 7

Content Directories Structure alternatives Name interpretation Directory handling. 8

Structure alternatives Single level directory. Two-levels directory. Tree structure directory. DAG (directed acyclic graph) structure directory. Generalized graph structure directory. 9

Single level directory A single directory for all users. Problems with file naming. High probability of name coincidence. letter.doc list.xls notes. photo.jpg 10

Two-levels directory A directory per user. Automated or manual path. Same file name for multiple users is valid. Efficient lookup, but grouping problems. daniel carlos letter.doc listing.xls notes.txt photo.jpg photo.jpg photo2.jpg 11

Tree structure directory Efficient lookup. Relative and absolute naming -> working directory. tmp home usr carlos daniel maria bin include data letter.doc stdio.h 12

Tree structure directory Absolute names contain the full path. Relative names start from working (or current) directory. Directory change: cd /spell/mail/prog cd prog Erase file: rm <filename> Create directory: mkdir <dir-name> Example: cd /spell/mail mkdir count ls /spell/mail/count Erase directory: rm -r mail 13

Acyclic graph structure directory Has shared files and directories. Concept not visible to Windows users. tmp home usr carlos daniel maria bin include data letter.doc stdio.h cab.h io.h 14

Acyclic graph directory link: A file with multiple names -> link control A single file with link count in descriptor (physical link). New file type with target name in file content (symbolic link). Link removal: A. Decrement counter; if 0 erase file. B. Traverse links and erase all. C. Erase only link and leave the rest. Problem: close loops traversing the tree. Solutions: Allow only links to files, but not for directories. Loop detection algorithm when link is created. UNIX implementation limitation: physical links only within same file system. 15

Directory structure Directory structure and files stored in disks. Implementation alternatives for directories: Use special block with directory information. Use file whose content is the directory. Information in directory: name, type, address, max and current length, access and modification time, owner, In case of using a file, most of the contents are file metadata. 16

Directory structure: alternatives Directories for contiguous files. Assume all files are stored with contiguous allocation. Directories for linked files. Assume all files are stored with non-contiguous allocation and blocks are represented as a linked list. Directories for indexed files. Assume all files are stored with non-contiguous allocation and blocks or extents are represented through an indexed structure. 17

Directories for contiguous files Directory entry: File attributes in directory entry. File first block identifier. File size. Example: ISO-9660 format for CD-ROM Location Date Name Length Size 18

Directory for linked files Directory entry: File attributes. First block number. File size. Example: FAT Type Time Date Size Name Attributes First block number 19

Directory for indexed files Most popular alternative Directory entry: Name. Metada identifier for file (i-node, MFT entry, ) i-node id Name 20

Directories for indexed files. Advantages: No need to modify directory to change file attributes. No need to modify directory when file changes its length. An i-node may represent a directory or a file. Simplified construction of hierarchical systems. Name length is not prefixed. Easy creation of name synonyms (links) for a file name. 21

Directory organization Efficiency: fast file lookup. Naming: Convenient and easy for users. Two users may have the same name for different files. Same file may have different names. Variable length names. Grouping: Logical grouping for files according to properties (e.g. c++ programs, games, ) Structuring: Access operations clearly defined and structure hiding. Simplification: Directory entry must be as simple as possible. 22

Hierarchical naming Absolute name: Path from root directory (/ in GNU/Linux, \ in Windows). Relative name Path from directory different from root. Example: (you are in /users/) daniel/keys Relative to the working directory (pwd) Special directories:. Working directory. Example: cp /users/daniel/keys... Parent directory. Example: ls.. HOME: Base directory for a given user. 23

Content Directories Structure alternatives Name interpretation Directory handling. 24

Name interpretation in UNIX Each directory is stored as a file with pairs <i-node number, file-name>. Initially in memory directory for /. How many disk blocks does a directory need for storage? Depends on the number of files in directory and the length of names. Lookup in directory is sequential. 25

Name interpretation in UNIX Find i-node for file /users/daniel/notes.txt.. 2.. 2 tmp 25 users 80 bin 37 Directory traversing many lead to more than one read on disk. 80.. 2 carlos 88 daniel 121. 121.. 80 proy 21 notes.tx t 33 Daniel 10.0 read i-node 2 i-node 80 i-node 121 i-node 33 26

Directory hierarchy Single directory tree? Per logical device in Windows (c:\users\carlos\keys, j:\joe\tmp,...) System wide in UNIX (/users/carlos/keys, /joe/tmp, ) Services for building hierarchy are needed: mount and umount. mount /dev/hda /users umount /users Advantages: Single system image and hiding device type. Drawbacks: More complex name translation. Problems with physical file links. 27

File systems and partitions Volume: Set of coherent metainformation and data. FAT Boot 2 FAT copies Root directory Data and directories UNIX Boot Super block Bit Maps i-nodes Data and directories 28

Mounting partitions Root volume Un-mounted volume Mounted volume Volumen raiz (/dev/hd0) Volumen sin montar (/dev/hd1) Volumen montado / / mount /dev/hd1 /usr / /lib /bin /usr /d1 /d2 /d3 /lib /bin /usr /d3/f1 /d3/f2 /usr/d1 /usr/d3 /usr/d3/f1 /usr/d3/f2 29

Content Directories Structure alternatives Name interpretation Directory handling. 30

Example: directory handling Services for handling files representing directories. How does one know if a name corresponds to a file or a directory? Service: #include <sys/types.h> #include <sys/stat.h> int stat(char *name, struct stat *buf); int fstat(int fd, struct stat *buf);... cond = S_ISDIR(buf.st_mode) /* true for directories*/ 31

Mkdir Create directory Service: #include <sys/types.h> #include <dirent.h> int mkdir(const char *name, mode_t mode); Arguments: name: directory name. mode: protection bits. Returns: Zero or -1 on error. Description: Creates a directory named name. Owner UID = effective UID. Owner GID = effective GID. 32

Service: #include <sys/types.h> int rmdir(const char *name); Arguments: name: Directory name. Returns: Zero or -1 on error. Rmdir Remove directory Decription: Remove directory if it is empty. Otherwise directory is not removed. 33

Service: #include <sys/types.h> #include <dirent.h> DIR *opendir(char * name); Arguments: dirname: Directory name. Opendir Open a directory Returns: A pointor to be used with readdir() or closedir(). NULL on error. Description: Opens a directory as a sequence of entries. Places pointer in first entry. 34

Service: #include <sys/types.h> #include <dirent.h> int closedir(dir *dirp); Arguments: dirp: Pointer returned by opendir(). Returns: Zero or -1 if error. Closedir Cerrar un directorio Description: Closes association between dirp and directory entry sequence. 35

Readdir Read directory entries Service: #include <sys/types.h> #include <dirent.h> struct dirent *readdir(dir *dirp); Arguments: dirp: pointer returned by opendir(). Returns: A pointer to an object of type struct dirent representing directory. NULL on error. Description: Returns next entry in directory associated to dirp and advances pointer. Structure is implementation dependent but you can assume it has a member char* d_name. 36

Rewindir Position directory pointer Service: #include <sys/types.h> #include <dirent.h> void rewindir(dir *dirp); Arguments: dirp: pointer returned by opendir(). Description: Sets directory position pointer to the first entry. 37

Link Create a directory entry Service: #include <unistd.h> int link(const char *existing, const char *new); int symlink(const char *existing, const char *new); Arguments: existing: Name of existing file. new: name of new entry that will be linked to existing file. Returns: Zero or -1 if error. Description: Create a new physical or symbolic link to an existing file. The OS does not record which is the original file and which is the new one. 38

Unlink Remove directory entry Service: #include <sys/types> int unlink(char *name); Arguments: name: File name. Returns: Zero or -1 if error. Description: Removes entry to directory and decrements number of links to file. When number of links equals zero and no process keeps it open, space is freed and file is no longer accessible. 39

Chdir Change current directory Service: int chdir(char *name); Arguments: name: directory name Returns: Zero or 1 if error. Description: Modifies current directory used to form relative paths. 40

Service: #include <unistd.h> int rename(char *old, char *new); Arguments: old: Name of existing file. new: New file name. Returns: Zero or -1 if error. Description: Change name of file old. New name is new. Rename Change file name 41

Getcwd Get name of current directory Service: char *getcwd(char *buf, size_t size); Arguments: buf: pointer to buffer to store name of current directory. size: Length in byts of buffer. Returns: Pointer to buf or NULL if error. Description: Gets name of current directory. 42

Example: Directory listing #include <sys/types.h> #include <dirent.h> #include <stdio.h> #define MAX_BUF 256 void main(int argc, char **argv) { DIR *dirp; struct dirent *dp; char buf[max_buf]; /* print current directory*/ getcwd(buf, MAX_BUF); printf( Current directory: %s\n", buf); 43

Example: Directory listing /* Open directory argument */ dirp = opendir(argv[1]); if (dirp == NULL) { fprintf(stderr, Cannot open %s\n", argv[1]); } else { /* read entry by entry*/ while ( (dp = readdir(dirp))!= NULL) printf("%s\n", dp->d_name); closedir(dirp); } exit(0); } 44

OPERATING SYSTEMS: Lesson 12: Directories Jesús Carretero Pérez David Expósito Singh José Daniel García Sánchez Francisco Javier García Blas Florin Isaila 45