File Types in Unix regular file - can be text, binary, can be executable directory file - "folder type" file FIFO file - special pipe device file, allows.

Slides:



Advertisements
Similar presentations
Linux device-driver issues
Advertisements

The UNIX File System Harry Chen Department of CSEE University of MD Baltimore County.
More on File Management
CSCI 1730 April 1 st, Materials Class notes slides & some “plain old” html & source code examples linked from course calendar board notes & diagrams.
File and I/O system calls int open(const char* path, int flags, mode_t modes) int creat(const char *path, mode_t mode) ssize_t read(int fd, void *buf,
Chapter 4 : File Systems What is a file system?
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 2)
Operating system services Program execution I/O operations File-system manipulation Communications Error detection Resource allocation Accounting Protection.
January 13, Csci 2111: Data and File Structures Week1, Lecture 2 Basic File Processing Operations.
CS 311 – Lecture 09 Outline Introduction to Systems programming – System calls – Categories of system calls Error Management System calls File Handling.
Files. System Calls for File System Accessing files –Open, read, write, lseek, close Creating files –Create, mknod.
1 Advanced programming in UNIX 1 File I/O Hua LiSystems ProgrammingCS2690File I/O.
Linux+ Guide to Linux Certification, Second Edition
Design and Implementation of the Log-based File I/O Library for Sandboxing CSE 542 Operating Systems Fall 2005 Qi Liao and Xuwen Yu.
Ceng Operating Systems
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
Linux Linux File System.
Lecture 17 FS APIs and vsfs. File and File Name What is a File? Array of bytes. Ranges of bytes can be read/written. File system consists of many files,
1 Course Outline Processes & Threads CPU Scheduling Synchronization & Deadlock Memory Management File Systems & I/O Networks, Protection and Security.
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 311 – Lecture 12 Outline File management system calls Stat() Directory Information  Opendir()  Readdir()  Closedir() Truncate() and remove() Lecture.
CSCI 1730 April 2 nd, C review – 4 data types /* A review of the basic data types in C. */ #include int main() { intx,y; chara; floatf,e; doubled;
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
Chapter 39 Virtsualization of Storage: File and Directory Chien-Chung Shen CIS, UD
Adv. UNIX: lowIO/161 Advanced UNIX v Objectives –look at low-level operations for handling files Special Topics in Comp. Eng. 2 Semester.
CSC 322 Operating Systems Concepts Lecture - 4: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Linux+ Guide to Linux Certification, Second Edition
SIMULATED UNIX FILE SYSTEM Implementation in C Tarek Youssef Bipanjit Sihra.
1 UNIX System Programming v Objectives –look at how to program with directories –briefly describe the UNIX file system Directories and File System.
Chapter 4. INTERNAL REPRESENTATION OF FILES
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
Operating Systems Recitation 1, March th, 2002.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
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.
UNIX Files File organization and a few primitives.
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
Linux+ Guide to Linux Certification, Third Edition
Manage Directories and Files in Linux Part 2. 2 Identify File Types in the Linux System The file types in Linux referred to as normal files and directories.
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?
Linux Commands C151 Multi-User Operating Systems.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
Laface 2007 File system 2.1 Operating System Design Filesystem system calls buffer allocation algorithms getblk brelse bread breada bwrite iget iput bmap.
NCHU System & Network Lab Lab 14 File and Directory.
Lecture 19 Linux/Unix – File System
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Directories.
File I/O open close lseek read and write – unbuffered I/O dup and dup2.
File Systems - Part I CS Introduction to Operating Systems.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
OS interface: file and I/O system calls File operations in C/C++? –fopen(), fread(), fwrite(), fclose(), fseek() in C f.open(…), f.close(…) in C++ I/O.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 39 File and Directory Chien-Chung Shen CIS/UD
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.
Linux Filesystem Management
Jonathan Walpole Computer Science Portland State University
Today topics: File System Implementation
Chapter 4: System calls for the file system
CIT 383: Administrative Scripting
An overview of the kernel structure
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
File Structure Related system calls
Department of School of Computing and Engineering
Advanced UNIX progamming
Internal Representation of Files
Unix Directories unix etc home pro dev motd snt unix ... slide1 slide2
Presentation transcript:

File Types in Unix regular file - can be text, binary, can be executable directory file - "folder type" file FIFO file - special pipe device file, allows unrelated processes to communiate block device file - represents physical device that transmit data a block at a time character device file - represents physical device that doesn't necessarily transmit data a block at a time symbolic link file - contains a pathname that references another file (some versions of UNIX)

creating and removing files directory created using mkdir, removed using rmdir mkdir /usr/tmp/junkdir rmdir /usr/tmp/junkdir device files created using mknod (need to be superuser) mknod /dev/cdsk c mknod /dev/bdsk b

Creating and removing files FIFO created using mkfifo mkfifo /usr/tmp/fifo.mine to make a link, use ln : ln -s /usr/jose/original /usr/mary/slink then, if you type more /usr/mary/slink you get the contents of /usr/jose/original... the link is followed (like a pointer)

File attributes file type access permission - for owner, group, other hard link count uid - user id of the file owner (linked to user via password file) gid - group id of the file owner file size - in bytes last access time last modify time last change time – time file permission, uid, gid, or hard link count changed inode number - sytem inode number of the file file system id major and minor device numbers

File attributes You can see many of the file attributes by typing: ls -l -rw-r--r--. 1 eileen users 199 Apr 2 04:41 arithmetic.c -rw-r--r--. 1 eileen users 142 Apr 2 04:41 ascii.c -rw-r--r--. 1 eileen users 205 Apr 2 04:41 basic_types.c -rw-r--r--. 1 eileen users 91 Apr 2 04:41 bit.c -rw-r--r--. 1 eileen users 253 Apr 2 04:41 bitmask.c -rw-r--r--. 1 eileen users 155 Apr 2 04:41 blocks.c -rw-r--r--. 1 eileen users 6021 Apr 1 08:41 buggy.c -rw-r--r--. 1 eileen users 6178 Apr 1 08:33 commented.c

File attributes used by the kernel in managing files uid and gid are compared against those of a process attempting to access the file to determine which access permissions apply The make utility looks at the last modification time Not all fields make sense for every file: size doesn't apply to device files device number doesn't apply to regular files

Some file attributes cant be changed inode number file type file system id major and minor device number

Changing file attributes -- UNIX command ---- System call ---- Attributes changed -- chmod access permissions last change time chown uid last change time chgrpchown gid last change time chmod access permissions last change time touchutime last access time modification time lnlink increase hard link count rmunlink decrease hard link count

i-nodes and directory structure kernel does NOT use file names to identify files file names are not even one of the attributes that the kernel maintains the kernels file system has an i-node table to keeps track of all the files i-node table consists of i-node records that contains the file's attributes, the physical address where the file is stored i-node number identifies the record within the table unique within a file system

Whats in an i-node record? stat, lstat, or fstat will give you most of it stat Apr_02_2014.html File: `Apr_02_2014.html' Size: 693 Blocks: 8 IO Block: 4096 regular file Device: fd0ch/64780dInode: Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1012/eileen) Gid: (100/users) Access: :42: Modify: :42: Change: :42:

i-node records calls to fstat, lstat return a struct stat Whats a struct? a complex data type declaration that defines a physically grouped list of variables placed under one name in a block of memorydata type allows the different variables to be accessed via a single pointer or the struct declared name that returns the same address pointer

struct stat struct stat{ dev_tst_dev;/*file system ID */ ino_tst_ino;/*file inode number */ mode_tst_mode;/*file type &access flags*/ nlink_tst_nlink;/*hard link count */ uid_tst_uid;/*file user ID */ gid_tst_gid;/*file group ID */ dev_tst_rdev;/*major & minor device nums*/ off_tst_size;/*file size in bytes*/ time_tst_atime;/*last access time */ time_tst_mtime;/*last modification time*/ time_tst_ctime;/*last status change time*/ };

Directory structure directories are files contain the names and i-nodes of files in that directory inode numberfile name xyz 346a.out 201xyz_ln1

Links: hard and symbolic "Hard links directory entries that associate a file name with an inode number. "Soft links (symbolic links) files that contain the name (absolute or relative path name) of another file. followed by most commands and have the result of accessing the file named inside the soft link file

process-level and kernel level data structures involved in managing file access

UNIX file access primitives open - open for reading, writing, or create empty file creat - create an empty file close - close a file read - get info from file write - put info in file lseek - move to specific byte in file unlink - remove a file remove - remove a file fcntl - control attributes associated w/ file

open, read, close #include main() { int fd; ssize_t nread; char buf[1024]; /* open file "data" for reading */ fd = open("data", O_RDONLY); /* read in the data */ nread = read(fd, buf, 1024); /* close the file */ close(fd); }

open for reading … /* open file "data" for reading */ fd = open("data", O_RDONLY); /* if fd is negative -- an error (-1) if fd is non-negative, it is a file descriptor that identifies the file and is used to further access it */

man open #include int open(const char *path, int oflag, /* mode_t mode */...); path -- pathname, can be relative or absolute oflag -- mode (defined in fcntl.h) O_RDONLY- read only O_WRONLY- write only O_RDWR- read write return value: -1 indicates error other value - index into file descriptor table (size of table determines number of open files possible) mode -- optional, used only with O_CREAT (more on this later)

read in the data … nread = read(fd, buf, 1024); /* try to read 1024 bytes from the file referred to by fd into the memory named "buf" */ …

man read SYNOPSIS #include ssize_t read(int fildes, void *buf, size_t nbyte); try to read nbyte bytes into buf from file referred to by filedes return -1 or number of bytes read #define BUFSIZE 1024 char buf[BUFSIZE]; int bytes_read; int fd = open("somefile", O_RDONLY); bytes_read = read(fd, buf, BUFSIZE);

close the file … /* close the file */ close(fd); /* releases that fd for future use*/ /* program termination would have same effect */ …

man close SYNOPSIS #include int close(int fd); DESCRIPTION close() closes a file descriptor, so that it no longer refers to any file and may be reused. … If fd is the last file descriptor referring to the underlying open file description the resources associated with the open file description are freed …

open for read & write ##include #include char *workfile="junk"; main(){ int filedes; if ((filedes = open(workfile, O_RDWR)) == -1){ printf("Couldn't open %s\n", workfile); exit(1); /* abnormal (error) exit */ } /* read/write in loop until EOF */ exit(0); /* normal exit */ }

open for write #include #define PERMS 0644 char *filename="newfile"; main(){ int filedes; // note: if filename already exists it will open if ((filedes = open(filename, O_WRONLY|O_CREAT, PERMS)) == -1){ printf("Couldn't open %s\n", filename); exit(1); /* abnormal (error) exit */ } /* read/write in loop until EOF */ exit(0); /* normal exit */ }

open … refinements If instead, we had written: if ((filedes = open(filename, O_WRONLY|O_CREAT|O_EXCL, PERMS)) == -1) then would fail if file already exists. if we had written: if ((filedes = open(filename, O_WRONLY|O_CREAT|O_TRUNC, PERMS)) == -1) if file already exists, would succeed, but would truncate it to size 0.

creat – old way to open a file creat - create a new file or rewrite an existing one SYNOPSIS #include int creat(const char *path, mode_t mode); returns fd or -1 always truncates an existing file always opens for writing only fd = creat("/users/students/you/testfile", 0644); is equivalent to: fd = open("/users/students/you/testfile", O_WRONLY|O_CREAT|O_TRUNC, 0644);

File pointer (R/W pointer) #define BUFSIZE 1024 char buf1[BUFSIZE]; char buf2[BUFSIZE]; int bytes_read; int fd = open("somefile", O_RDONLY); bytes_read = read(fd, buf1, BUFSIZE); bytes_read = read(fd, buf2, BUFSIZE); each read gets the next chunk of bytes system keeps track of location in file with file pointer, a.k.a. the read/write pointer maintains the number of the next byte to be read or written through that file descriptor

File pointer sequential access to files – just keep reading/writing, system updates for you random access to files – programmer explicitly changes the position of the read/write pointer. How? with lseek How do we know when we're finished reading??? read returns a zero Note: read attempt before last may succeed in reading fewer than requested bytes...

Example #include #define BUFSIZE 512 main(){ char buffer[BUFSIZE]; int filedes; ssize_t nread; long total = 0; /* open the test file */ if ((filedes = open("test.txt", O_RDONLY)) == -1){ printf("Couldn't open test.txt \n"); exit(1); } …

Example, continued /* read in loop until EOF */ while ( (nread = read(filedes, buffer, BUFSIZE)) > 0) { printf("Read %d bytes this time \n", nread); total += nread; } printf("Read %d bytes in total \n", total); exit(0); /* normal exit */ } For maximum efficiency, read in chunks same as system block size -- check BUFSIZ in /usr/include/stdio.h to find out.

write SYNOPSIS #include ssize_t write(int fildes, const void *buf, size_t nbyte);... DESCRIPTION The write() function attempts to write nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fildes. returns -1 (error) or number of bytes written.

write int fd; ssize_t w1, w2; char buf1[512], buf2[1024]l... fill buf1 and buf2 if((fd=open("somefile", O_WRONLY | O_CREAT | O_EXCL, 0644))== -1) return (-1) w1 = write(fd, buf1, 512); w2 = write(fd, buf2, 1024); will write 1536 bytes to somefile, overwriting anything there already to avoid over-writing: fd = open("somefile", O_WRONLY | O_APPEND);

Example See copyfile.c Then alter copyfile to accept BUFSIZE as a parameter as an input paramater and time it time copyfile in.tst out.tst 1 time copyfile in.tst out.tst 64 time copyfile in.tst out.tst 128 time copyfile in.tst out.tst 256 …

lseek, random access SYNOPSIS #include off_t lseek(int fildes, off_t offset, int whence); DESCRIPTION The lseek() function sets the file pointer associated with the open file descriptor specified by fildes as follows: If whence is SEEK_SET, the pointer is set to offset bytes. If whence is SEEK_CUR, the pointer is set to its current location plus offset. If whence is SEEK_END, the pointer is set to the size of the file plus offset. Returns resulting offset or error

lseek example Sample use: fd = open (filename, O_RDWR); lseek(fd, (off_t)0, SEEK_END); write(fd, outbuf, OBSIZE);

Deleting files #include int unlink(const char *path); #include int remove(const char *path); return 0 for success -1 for failure

fcntl – making adjustments SYNOPSIS #include int fcntl(int fildes, int cmd, /* arg */...); DESCRIPTION The fcntl() function provides for control over open files. The fildes argument is an open file descriptor. The fcntl() function may take a third argument, arg, whose data type, value and use depend upon the value of cmd. The cmd argument specifies the operation to be performed by fcntl().

fcntl The available values for cmd are defined in the header and include :.. there's a good-sized list of them. Right now, we care about: F_GETFL - get current file status flags F_SETFL - set file status flags

fcntl int arg1; if ((arg1=fcntl(fd, F_GETFL)) == -1) printf("filestatus failed \n");... switch(arg1 & O_ACCMODE){ case O_WRONLY:.. do something break; case O_RDWR:... do something else break; case O_RDONLY:... do something else again break; default: } if (arg1 & O_APPEND)printf(" append flag set "); printf("\n"); return(0);