• 沒有找到結果。

Files and Directories

N/A
N/A
Protected

Academic year: 2022

Share "Files and Directories"

Copied!
19
0
0

加載中.... (立即查看全文)

全文

(1)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Contents

1. Preface/Introduction

2. Standardization and Implementation 3. File I/O

4. Standard I/O Library 5. Files and Directories

6. System Data Files and Information 7. Environment of a Unix Process 8. Process Control

9. Signals

10.Inter-process Communication

Files and Directories

ƒ Objectives

ƒ Additional Features of the File System

ƒ Properties of a File.

ƒ Three major functions:

#include <sys/types.h>

#include <sys/stat.h>

int stat(const char *pathname, struct stat *buf);

int fstat(int filedes, struct stat *buf);

int lstat(const char *pathname, struct stat *buf);

(2)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Files and Directories

ƒ Differences on stat(), fstat(), lstat():

ƒ lstat() returns info regarding the symbolic link, instead of the referenced file, if it happens.

struct stat {

mode_t st_mode; /* type & mode */

ino_t st_ino; /* i-node number */

dev_t st_dev; /* device no (filesystem) */

dev_t st_rdev; /* device no for special file */

nlink_t st_nlink; /* # of links */

uid_t st_uid; gid_t st_gid;

off_t st_size; /* sizes in byes */

time_t st_atime; /* last access time */

time_t st_mtime; /* last modification time */

time_t st_ctime; /* time for last status change */

long st_blk_size; /* best I/O block size */

long st_blocks; /* number of 512-byte blocks allocated */

};

File Types

ƒ Regular Files: text, binary, etc.

ƒ Directory Files: Only Kernel can update these files – { (filename, pointer) }.

ƒ Character Special Files, e.g., tty, audio, etc.

ƒ Block Special Files, e.g., disks, etc.

ƒ FIFO – named pipes

ƒ Sockets – not POSIX.1 or SVR4

ƒ SVR4 uses library of socket functions, instead. 4.3+BSD has a file type of socket.

ƒ Symbolic Links – not POSIX.1 or SVR4

(3)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

File Types

ƒ Program 4.1 – Page 76

ƒ lstat() and Figure 4.1

ƒ <sys/stat.h>

#define S_IFMT 0xF000 /* type of file */

#define S_IFDIR 0x4000 /* directory */

#define S_ISDIR(mode) (((mode)&0xF000)

== 0x4000)

ƒ st_mode

ƒ Percentage of Files in a Medium-Sized System – Figure 4.2

Access Permissions & UID/GID

ƒ All files have access permissions

ƒ st_mode mask – owner, group, other

#define S_IRWXU 00700 /* read, write, execute: owner */

#define S_IRUSR 00400 /* read permission: owner */

#define S_IWUSR 00200 /* write permission: owner */

#define S_IXUSR 00100 /* execute permission: owner */

#define S_IRWXG 00070 /* read, write, execute: group */

#define S_IRGRP 00040 /* read permission: group */

#define S_IWGRP 00020 /* write permission: group */

#define S_IXGRP 00010 /* execute permission: group */

#define S_IRWXO 00007 /* read, write, execute: other */

#define S_IROTH 00004 /* read permission: other */

#define S_IWOTH 00002 /* write permission: other */

#define S_IXOTH 00001 /* execute permission: other */

(4)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Access Permissions & UID/GID

ƒ Operations vs Permissions

ƒ Directory

ƒ X – pass through the dir (search bit), e.g., /usr/dict/words and PATH env var.

ƒ R – list of files under the dir.

ƒ W – update the dir, e.g., delete or create a file.

ƒ File

ƒ X – execute a file (which must be a regular file)

ƒ R – O_RDONLY or O_RDWR

ƒ W – O_WRONLY, O_RDWR, or O_TRUNC

Access Permissions & UID/GID

ƒ Files

ƒ st_uid, st_gid in stat

ƒ S_ISUID, S_ISGID – set st_uid/gid bit in st_mode

ƒ E.g., Command “passwd” updates /etc/passwd or /etc/shadow

ƒ Process UID/GID – optional with POSIX.1

ƒ sysconf(_SC_SAVED_IDS)

ƒ Real User/Group ID (from /etc/passwd)

ƒ Effective User/Group ID, Supplementary GID’s

ƒ Check for file access permissions

ƒ Saved Set-User/Group-ID – saved by exec()

(5)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Access Permissions & UID/GID

ƒ File Access Test – each time a process creates/opens/deletes a file

ƒ If the effective UID == 0 Æ superuser!

ƒ If the effective UID == UID of the file

ƒCheck appropriate access permissions!

ƒ If the effective GID == GID of the file

ƒCheck appropriate access permissions!

ƒ Check appropriate access permissions for others!

ƒ Related Commands: chmod & umask

Ownership of a New File

ƒ Rules:

ƒ UID of a file = the effective UID of the creating process

ƒ GID of a file – options under POSIX 1. GID of the file = the effective GID of the

process

2. GID of the file = the GID of the residing dir

ƒ 4.3BSD and FIPS 151-1 always do it.

ƒ SVR4 needs to set the set-group-ID bit of the residing dir (mkdir)!

(6)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Function – access

#include <unistd.h>

int access(const char *pathname, int mode);

ƒ Check the real UID/GID!

ƒ R_OK, W_OK, X_OK, F_OK

ƒ Program 4.2 – Page 83

ƒ access function

Function – umask

#include <sys/types.h>

#include <sys/stat.h>

mode_t umask(mode_t cmask);

ƒ Turn off the file mode

ƒ cmask = bitwise-OR S_I[RWX]USR, etc (Figure 4.4).

ƒ The mask goes with the process only.

ƒ Inheritance from the parent!

ƒ Program 4.3 – Page 85

ƒ umask

(7)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Function – chmod & fchmod

#include <sys/types.h>

#include <sys/stat.h>

int chmod(const char *pathname, mode_t mode);

int fchmod(int filedes, mode_t mode);

ƒ fchmod() is not in POSIX.1, but in SVR4/4.3+BSD

ƒ Callers must be a superuser or effective UID = file UID.

ƒ Mode = bitwise-OR S_I[RWX]USR, S_ISVTX (sticky bit), S_IS[UG]ID, etc (Fig 4.6).

Function – chmod & fchmod

ƒ Program 4.4 – Page 87

ƒ chmod – updates on i-nodes

ƒ set-group-ID

ƒ If the GID of a newly created file is not equal to the effective GID of the creating process (or one of the

supplementary GID’s), or the process is not a superuser, clear the set- group-ID bit!

ƒ Clear up set-user/group-ID bits if a non-superuser process writes to a set-uid/gid file.

(8)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Function – chmod & fchmod

ƒ Sticky Bit (S_ISVTX) – saved-text bit

ƒ Not POSIX.1 – by SVR4 & 4.3+BSD

ƒ Only superusers can set it!

ƒ S_ISVTX executable file

ƒ Used to save a copy of a S_ISVTX executable in the swap area to speed up the execution next time.

ƒ S_ISVTX directory file, e.g., /tmp

ƒ Remove/rename its file only if w permission of the dir is set, and the process is belonging to

superusers/owner of the file/dir

Function – chown, fchown, lchown

#include <sys/types.h>

#include <unistd.h>

int chown(const char *pathname, uid_t owner, gid_t, grp);

int fchown(int filedes, uid_t owner, gid_t, grp);

int lchown(const char *pathname, uid_t owner, gid_t, grp);

ƒ lchown() is unique to SVR4. Under non-SVR4 systems, if the pathname to chown() is a

symbolic link, only the ownership of the symbolic link is changed.

ƒ -1 for owner or grp if no change is wanted.

(9)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Function – chown, fchown, lchown

ƒ _POSIX_CHOWN_RESTRICTED is in effect (check pathconf())

ƒSuperuserÆ the UID of the file can be changed!

ƒThe GID of the file can be changed if

ƒthe process owns the file, and

ƒParameter owner = UID of the file

& Parameter grp = the process GID or is in supplementary GID’s

ƒ set-user/group-ID bits would be cleared if chown is called by non-super users.

File Size

ƒ File Sizes – st_size

ƒ Regular files – 0~max (off_t)

ƒ Directory files – multiples of 16/512

ƒ Symbolic links – pathname length

ƒ /* a pipe’s file size for SVR4 */

ƒ File Holes

ƒ st_blocks vs st_size (st_blksize)

ƒ Commands:

ƒ “ls –l file.hole” == “wc –c file.hole”

ƒ du –s file.hole Æ actual size

ƒ cat file.hole > file.hole.copy

(10)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Functions – truncate &

ftruncate

#include <sys/types.h>

#include <unistd.h>

int truncate(const char *pathname, off_t length);

int ftruncate(int filedes, off_t length);

ƒ Not POSIX.1

ƒ SVR4 creates a hole if length > fsize.

ƒfcntl with F_FREESP to free any part of a file.

ƒ 4.3+BSD only truncates files.

ƒ Portability?

Filesystems

ƒ A hierarchical arrangement of directories and files – starting in root /

ƒ Several Types, e.g.,SVR4: Unix System V Filesystems (S5), Unified File System (UFS) – Figure 2.6 (Page 39)

ƒ System V:

partition partition partition

Disk drive

i-list dir/data blocks

Filesystems boot blocks

super block

i-node i-node i-node

(11)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Filesystems

ƒ i-node:

ƒ Version 7: 64B, 4.3+BSD:128B, S5:64B, UFS:128B

ƒ File type, access permission, file size, data blocks, etc.

ƒ Link count – hard links

ƒ st_nlink in stat, LINK_MAX in POSIX.1

ƒ Unlink/link a file i-list

i-node i-node

filename

filename

data block

data block

data block dir

block

dir block

i-node

Filesystem – 4.4BSD i-node

* “Operating system concept”, Silberschatz and Galvin, Addison Wesley, pp. 380.

mode owner timestamp size block ref-count

triple indirect double indirect

single indirect

direct blocks

data data data

data data data

data data

data…

• 4KB block size

• 12 direct pointers

• 48KB

• 1 single indirect

• 4-byte block ptr

• 1K * 4KB = 4MB

• >> 4GB for the largest file!

(offset = 32 bits, 4G=232)

(12)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Sharing of Files

ƒ Hard Link

ƒ Each directory entry creates a hard link of a filename to the i- node that describes the file’s contents.

ƒ Symbolic Link (Soft Link)

ƒ It is implemented as a file that contains a pathname.

ƒ Filesize = pathname length

ƒ Example: Shortcut on Windows

foo

bar

/usr/joe

/usr/sue File i-node:

Reference = 2

foo

bar

/usr/sue

File i-node:

Reference = 1

/usr/joe

File i-node:

Reference = 1 data file:

/usr/joe/foo

* Problem – infinite loop in tracing a path name with symbolic links – 4.3BSD, no 8 passings of soft links

* Dangling pointers

Filesystem

ƒ Example

ƒ At i-node 1267, mkdir testdirÆ i-node 2549

ƒ The link counts of testdir is at least 2

ƒ Command mv only modify dir entries!

ƒ No directory entry points at any i-node residing at other filesystems.

i-list

i-node

0 i-node

..

dir block

dir block

i-node 1267

i-node 2549

2549 .

..

1267 .

2549 testdir 1267

2549 (testdir)

1267

(13)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Functions – link, unlink, rename, remove

#include <unistd.h>

int link(const char *existingpath, const char

*newpath);

int unlink(const char *pathname);

ƒ Atomic action for link – hard link

ƒ POSIX.1 allows linking across filesystems

ƒ Only superusers could create a link to a dir

ƒ Error if newpath exists

ƒ Unlink – WX right at the residing dir

ƒ Remove the dir entry & delete the file if link count reaches zero and no one still opens the file (Remark: sticky bit & dir rights).

Functions – link, unlink, rename, remove

ƒ Program 4.5 – Page 97

ƒ Open & unlink a file

ƒ Unlink a file

ƒ Sticky bits set for a residing dir

ƒOwner of the file or the dir, or super users

ƒ If pathname is a symbolic link, unlink references the symbolic link.

(14)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Functions – link, unlink, rename, remove

#include <stdio.h>

int remove(const char *pathname);

int rename(const char *oldname, const char

*newname);

ƒ remove = rmdir if pathname is a dir. (ANSI C)

ƒ Rename – ANSI C

ƒFile: both files, newname is removed first, WX permission for both residing directories

ƒDirectory: both dir, newname must be empty, newname could not contain oldname.

Symbolic Links

ƒ Goal

ƒ Get around the limitations of hard links:

(a) filesystem boundary (b) link to a dir.

ƒ Initially introduced by 4.2BSD

ƒ Example – ftw

ƒ ln –s ../foo testdir

ƒ Figure 4.10 – functions follow slinks

ƒ No functions which take filedes

ƒ Example: unlink(testdir) foo

a testdir

(15)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Symbolic Links

#include <unistd.h>

int symlink(const char *actualpath, const char *sympath);

int readlink(const char *pathname, char

*buf, int bufsize);

ƒ actualpath does not need to exist!

ƒ They do not need to be in the same file system.

ƒ readlink is an action consisting of open, read, and close – not null terminated.

File Times

ƒ Three Time Fields:

Field Description Example ls-option

st_atime last-access-time read -u

st_mtime last-modification-time write default st_ctime last-i-node-change-time chmod, chown -c

ƒ Figure 4.13 – Effect of functions on times

ƒ Changing the access permissions, user ID, link count, etc, only affects the i-node!

ƒ ctime is modified automatically! (stat, access)

ƒ Example: reading/writing a file only affects the file, instead of the residing dir (Fig4.13).

(16)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

File Times

#include <sys/types.h>

#include <utime.h>

int utime(const char *pathname, const struct utimbuf *times);

ƒ time values are in seconds since the Epoch

ƒ times = null Æ set as the current time

ƒEffective UID = file UID or W right to the file

ƒ times != null Æ set as requested

ƒEffective UID = (file UID or superuser) and W right to the file.

ƒ Program 4.6 – Page 105, utime

struct utimbuf { time_t actime;

time_t modtime;

}

Functions – mkdir and rmdir

#include <sys/types.h>

#include <sys/stat.h>

int mkdir(const char *pathname, mode_t mode);

ƒ umask, UID/GID setup (Sec 4.6)

ƒ From 4.2BSD & SVR3 (SVR4 – inheritance of the S_ISGID bit)

#include <unistd.h>

int rmdir(const char *pathname);

ƒ An empty dir is deleted.

ƒ Link count reaches zero, and no one still opens the dir.

(17)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Functions – opendir, readdir, rewinddir, closedir

#include <sys/types.h>

#include <dirent.h>

DIR *opendir(const char *pathname);

struct dirent *readdir(DIR *dp);

void rewinddir(DIR *dp);

int closedir(DIR *dp);

ƒ Only the kernel can write to a dir!!!

ƒ WX for creating/deleting a file!

ƒ Implementation-dependent!

Functions – opendir, readdir, rewinddir, closedir

ƒ dirent struct is very implementation- dependent, e.g.,

struct dirent {

ino_t d_ino; /* not in POSIX.1 */

char d_name[NAME_MAX+1];

} /* fpathconf() */

ƒ Program 4.7 – Pages 109-111

ƒ ftw/nftw – recursively traversing the filesys

(18)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Functions – chdir, fchdir, getcwd

#include <unistd.h>

int chdir(const char *pathname);

int fchdir(int filedes);

ƒ fchdir – not POSIX.1

ƒ chdir must be built into shells!

ƒ The kernel only maintains the i-node

number and dev ID for the current working directory!

ƒ Per-process attribute – working dir!

ƒ Program 4.8 – Page 113

ƒ chdir

Functions – chdir, fchdir, getcwd

#include <unistd.h>

char *getcwd(char *buf, size_t size);

ƒ The buffer must be large enough, or an error returns!

ƒ chdir follows symbolic links, and getcwd has not idea of symbolic links!

ƒ Program 4.9 – Page 114

ƒ getcwd

(19)

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2003.

Special Device Files

ƒ Device Number – dev_t

ƒ Major and minor numbers: 8 bit each under 4.3+BSD

ƒ macro definition <sysmacros.h> @ntucsa

#define L_BITSMINOR 18 /* # of SVR4 minor device bits */

#define L_MAXMAJ 0x3fff /* SVR4 max major value */

#define major(x) (int)((unsigned)((x)>>O_BITSMINOR) &

O_MAXMAJ)

ƒ Program 4.10 – Page 115

ƒ st_dev vs st_rdev

Functions – sync and fsync

#include <unistd.h>

void sync(void);

int fsync(int filedes);

ƒ sync() queues all kernel modified block buffers for writing and returns.

ƒ fsync() waits for the I/O of a referred file to complete!

ƒfsync vs O_SYNC

ƒ Not POSIX.1, but supported by SVR4 and 4.3+BSD

參考文獻

相關文件

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

Tei-Wei Kuo, Embedded System and Wireless Networking Lab, National Taiwan University.. Real-Time

* All rights reserved, Tei-Wei Kuo, National Taiwan University,

* All rights reserved, Tei-Wei Kuo, National Taiwan University, 2001.. Operating

* All rights reserved, Tei-Wei Kuo, National Taiwan University,

* All rights reserved, Tei-Wei Kuo, National Taiwan University,