Navigating Directories

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

Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
1 Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
Linux+ Guide to Linux Certification, Second Edition
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Files and Directories Hua LiSystems ProgrammingCS2690Files and Directories Spring 2003Page 1 of 60.
Unix & Windows Processes 1 CS502 Spring 2006 Unix/Windows Processes.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
Unix System Overview Programmer’s Perspective Chien-Chung Shen CIS, UD
1 UNIX Systems Programming Interprocess communication.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Slide 1 Interprocess communication. Slide 2 1. Pipes  A form of interprocess communication Between processes that have a common ancestor  Typical use:
Chapter 5 Files and Directories Source: Robbins and Robbins, UNIX Systems Programming, Prentice Hall, 2003.
Linux+ Guide to Linux Certification, Second Edition
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
OPERATING SYSTEMS 12 - FILES PIETER HARTEL 1. Files  Properties  Long term existence of data  Sharable between processes  Access control  Operations.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
File Handling Spring 2013Programming and Data Structure1.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
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.
Shell (Addendum). Example r What if we want to support something like this: m ps –le | sort r One process should execute ps –le and another should execute.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
Files and Directories File types stat functions for file information
Linux+ Guide to Linux Certification, Third Edition
Operating Systems Process Creation
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?
Copyright ©: Nahrstedt, Angrave, Abdelzaher, Caccamo1 Files and file allocation.
Chapter 10: File-System Interface Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Jan 1, 2005 File-System Interface.
NCHU System & Network Lab Lab 14 File and Directory.
CSCI 330 UNIX and Network Programming
PHP-5- Working with Files and Directories. Reading Files PHP’s file manipulation API is extremely flexible: it lets you read files into a string or into.
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.
S -1 Library Functions. S -2 Standard Libraries Any system call is not part of the C language definition Such system calls are defined in libraries, identified.
Unix System Overview ( Programmer’s Perspective) Chien-Chung Shen CIS/UD
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.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
S ALVATORE DI G IROLAMO (TA) Networks and Operating Systems: Exercise Session 3.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
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.
LINKED LISTS.
Distributed Computing Systems
A bit of C programming Lecture 3 Uli Raich.
Command Line Arguments
Command-Line Arguments
Computer Science 210 Computer Organization
CS111 Computer Programming
Directory Directory is a file containing list of entries,where each entry represents inode number and name of the file stored in that directory.
Processes in Unix, Linux, and Windows
Computer Science 210 Computer Organization
CSE 333 – Section 3 POSIX I/O Functions.
INTRODUCTION TO UNIX: The File System
Command Line Parameters
CSE 333 – Section 3 POSIX I/O Functions.
Persistence: File System API
CSE 333 – Section 3 POSIX I/O Functions.
EE 312 Exam I Review.
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Exam #1 February 23rd (Next Friday)
File I/O & UNIX System Interface
Chapter 12: File-System Implementation CSS503 Systems Programming
EE 312 Exam I Review.
Files Chapter 8.
EE 312 Exam I Review.
Presentation transcript:

Navigating Directories The Linux File System Navigating Directories

The Linux File System Everything is a file directories are files sockets are files stdin and stdout are files soft links to files are files processes are not files It is all one big directory tree drives are mounted onto the tree

The Linux Drive Structure partition partition partition boot blocks i-list data block data block directory block data block directory block i-node number 1267 2300 2549 i-node number Name 1267 . 555 .. 2549 testdir 2300 testfile i-node number Name 2549 . 1267 ..

Contents of an I-Node

Directory Functions DIR *opendir (char *pathname) parameter is a string with a directory name (relative or absolute) returns a pointer to a directory block returns NULL if no such directory or permission denied struct dirent *readdir (DIR *dp) parameter is a pointer to a directory block returns a pointer to a two-part struct for the next directory entry d_ino --> i-node number d_name --> name returns NULL when no more entries void rewind (DIR *dp) int closedir (DIR *dp)

Example - myls1 #include <sys/types.h> #include <dirent.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { DIR *dp; // ptr to directory block struct dirent *entry; // ptr to next entry /***** check the command line *****/ if (argc != 2) printf ("Error: must provide a directory name\n\n"); exit (1); } /***** can we open that directory *****/ if ( ( dp = opendir(argv[1]) ) == NULL) printf ("cannot open %s\n",argv[1]); /***** loop through all entries *****/ while ( ( entry = readdir(dp) ) != NULL) printf ( "%s\n", entry->d_name); closedir (dp);

Inquiring about directory entries Status Information for files and directories struct stat { st_mode file type st_ino i-node number st_uid user id of owner st_size size in bytes st_atime time of last access st_mtime time of last modification yadda yadda Retrieving that status info for a file or directory stat (char *pathname, struct stat *buffer); Is it a file or directory S_ISREG( mode ) S_ISDIR( mode )

2nd Example - myls2 Prints the name, then i-node number, and if a directory struct stat stat_info; /***** process all directory entries *****/ while ( ( dirp = readdir(dp)) != NULL) { printf ("%20s %d",dirp->d_name,dirp->d_ino); stat (dirp->d_name, &stat_info); if (S_ISDIR(stat_info.st_mode)) printf (" directory\n"); else printf ("\n"); }

Example Run > ls myls1 myls1.c myls2 myls2.c testdir > myls1 . myls1 myls2 . myls2.c testdir .. myls1.c > myls2 . myls1 5113261 myls2 5113264 . 5113260 directory myls2.c 5113266 testdir 5113263 directory .. 5112686 directory myls1.c 5113265

Your Assignment Create a program to print a directory tree of variable depth. Eat this elephant slowly: compile myls2.c put the traversal loop into a function have the traversal function call itself use depth as the stop condition for recursion add the indenting