files and directories

32
Files and Directories File types stat functions for file information File permissions suid and sgid Sticky bit Hard link and soft link Directory operations Device special files File system

Upload: field

Post on 06-Jan-2016

61 views

Category:

Documents


3 download

DESCRIPTION

Files and Directories. File types stat functions for file information File permissions suid and sgid Sticky bit Hard link and soft link Directory operations Device special files File system. File Types. Regular file Directory file Character special file (unbuffered I/O access) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Files and Directories

Files and Directories File types stat functions for file information File permissions suid and sgid Sticky bit Hard link and soft link Directory operations Device special files File system

Page 2: Files and Directories

File Types Regular file Directory file Character special file (unbuffered I/O

access) Block special file (buffered I/O) Named Pipe (a type of IPC) - FIFO Socket (a network form of IPC) Symbolic Link (a file that just points to

another file)

Page 3: Files and Directories

stat, fstat and lstat

int stat(const char *path, struct stat *buf);int fstat(int filedes, struct stat *buf);int lstat(const char *path, struct stat *buf); All three return 0 on success or -1 on failure All three set buf to point to a structure of

information stat get info about the file fstat gets info about an already open file lstat same as stat but if given a symbolic link will

get info about the link instead of what the link points to

Page 4: Files and Directories

stat structure Definition on page

88 st_mode st_ino st_dev st_rdev st_nlink st_uid st_gid

st_size st_blocks st_blksize st_atime st_mtime st_ctime

Page 5: Files and Directories

Determining the type of a file

Set of macros defined in <sys/stat.h> can be used.

They all accept the st_mode field of the stat struct as their only parameter

S_ISREG(mode_t mode) S_ISDIR(mode_t mode) S_ISCHR(mode_t mode) S_ISBLK(mode_t mode) S_ISFIFO(mode_t mode) S_ISLNK(mode_t mode) S_ISSOCK(mode_t mode)

Page 6: Files and Directories

Set-User-ID & Set-Group-ID

Every process has 6 or more IDs Real user ID Real group ID Effective user ID Effective group ID Supplementary group IDs Saved set-user-ID Saved set-group-ID

Page 7: Files and Directories

Set-User-ID & Set-Group-ID

Normally, effective IDs are the same as real user IDs

Sometimes, we need to run a process with the permissions of the user or group that owns the file (ex: passwd)

Two bits in the file’s mode word (st_mode) allow this. set-user-ID bit set-group-ID bit

Page 8: Files and Directories

Set-User-ID & Set-Group-ID

When creating a file with the creat function, the new file’s UID is the effective UID of the process, and the new file’s GID is the effective GID of the process

Page 9: Files and Directories

Checking set-user-ID and set-group-ID bits

Can use S_ISUID and S_ISGID masks

Ex:if(S_ISUID & st_mode)if(S_ISGID & st_mode)

Page 10: Files and Directories

Setting SUID and GUID

SUID has a value of 4 GUID has a value of 2chmod 4755 afile -rwsr-xr-xchmod 2755 afile -rwxr-sr-xchmod 6755 afile -rwsr-sr-x

Page 11: Files and Directories

File access permissions

Read section 4.5 on page 92. Note the different permissions required for operations on regular files versus directories

Page 12: Files and Directories

access function Tests to see if the real user ID and real

group ID allow access to a file Useful if a program that may be running as

another user (ex root) wants to be sure that the person that started the program has permissions to access some file

int access(const char *pathname, int mode); Returns 0 if ok, -1 otherwise

Page 13: Files and Directories

access function (cont)

In this context, mode is not the same as in previous functions. Here, mode is the bitwise OR of several constants R_OK - test for read permission W_OK - test for write permission X_OK - test for execute permission F_OK - test for file existence

Page 14: Files and Directories

umask function

mode_t umask(mode_t mask); Sets the “file mode creation mask” “file mode creation mask” is used to

specify the default permissions of newly created files

Since we are dealing with a mask it is the complement of what we want. So we set the bits corresponding to the permissions we don’t want.

Page 15: Files and Directories

umask function

When using open or creat, any bits in mode that are also set in the file creation mask are turned off.

All the UNIX shells also define a shell command called umask that displays the default umask set at login. Each process can change its own mask without interfering with each other

Page 16: Files and Directories

File Size st_size member of stat structure holds

the size of a file Only meaningful for regular files,

directories and symbolic links Regular file – number of bytes in the file Directories – multiple of a number (more on

this later) Symbolic Links – number of chars in

filename

Page 17: Files and Directories

Sticky Bit

Historically Used to tell the system to keep a

copy of the “text” portion of a program in the swap space even after the program exits. (text = instructions, not data)

Swap file is contiguous faster start on next execution.

Page 18: Files and Directories

Sticky Bit Currently

Used on directories, indicates that in order to rename or delete a file in that directory a user must both have write permissions to the directory and one of the following must be true

User owns the file User owns the directory User is the superuser

To set the sticky bit chmod +t mydir

Page 19: Files and Directories

chmod and fchmod Allows us to change the existing

permissions for a fileint chmod(const char *path, mode_t mode);int fchmod(int fildes, mode_t mode); Returns 0 on success or -1 on failure mode is bitwise OR of constants shown

on page 99 fig 4.11 Effective UID of process must be the

same as the UID of the file or the process must have superuser permissions

Page 20: Files and Directories

link, unlink, remove and rename

linkint link(const char *oldpath, const char *newpath); Creates a new hard link within the file

system oldpath is the file to be linked. Newpath

must already exist except for the last portion which will be created

If all of newpath already exists, an error will be returned

Returns 0 if ok, -1 otherwise

Page 21: Files and Directories

link, unlink, remove and rename

unlinkint unlink(const char *pathname); Removes directory entry indicated by

pathname Decrements the link count of the file

Must have write and execute on containing directory

Only when link count reaches 0 and no process has the file open will it be deleted

Page 22: Files and Directories

link, unlink, remove and rename

removeint remove(const char *pathname); Calls unlink for files and rmdir for

directories rename

int rename(const char *oldpath, const char *newpath);

Equivelant to mv shell command

Page 23: Files and Directories

Symbolic Links

Indirect pointer to a file or directory Created to get around limitations of

hard links Hard links must live in the same file

system that the think it links to is in Only superuser can hard link to a

directory

Page 24: Files and Directories

symlink and readlink symlink

int symlink(const char *oldpath, const char *newpath); Creates the symbolic link file newpath that

contains the text contained in oldpath oldpath and newpath may be on different

file systems readlink

ssize_t readlink(const char *path, char *buf, size_t bufsiz); open function follows symbolic links.

readlink operates on the link file itself String from file placed in buf, with number

of bytes in bufsiz. String is not null terminated

Page 25: Files and Directories

File Times 3 times are kept for any file

Last access time Last modified time Last status change time

These are stored in the following fields of the stat struct st_atime st_mtime st_ctime

Page 26: Files and Directories

utimeint utime(const char *filename, const struct utimbuf *buf);struct utimbuf { time_t actime; time_t modtime;}

actime and modtime are calendar times marking seconds since Epoch

buf may be NULL, in which case the time is updated to current system time

Unless using NULL for buf, just having write permissions is not enough. Effective user ID must equal owner ID or process must have superuser privileges

Page 27: Files and Directories

Directory Operations mkdir and rmdir

int mkdir(const char *pathname, mode_t mode);int rmdir(const char *pathname); Create and remove the specified directory For mkdir, the mode parameter is the same

as those used for open. However, recall that interactions with the umask may occur!

When creating directories, remember that we usually want execute permissions so that the files within the directory can be listed

Page 28: Files and Directories

Reading DirectoriesDIR *opendir(const char *name);struct dirent *readdir(DIR *dir);void rewinddir(DIR *dir);int closedir(DIR *dir);off_t telldir(DIR *dir);void seekdir(DIR *dir, off_t offset);

struct dirent { ino_t d_ino; char d_name[NAME_MAX + 1];}

Page 29: Files and Directories

chdir fchdir and getcwd

int chdir(const char *path);int fchdir(int fd);

Change the current working directory of the process

long getcwd(char *buf, unsigned long size); Get the current working directory of

the process

Page 30: Files and Directories

Device Special Files Many devices are represented as

files within /dev directory Each device has major number and

minor number for the Kernel to identify it Major number usually specifies the

driver to use for the device Minor number may be the specific sub

device or options for that device

Page 31: Files and Directories

Device Special Files ls –lL on a device file will show major

and minor numbers instead of size of file

st_dev and st_rdev st_dev for every file is the device number

under the filesystem containing that filename and its associated i-node

st_rdev (only for character and block special files) contains actual device number

Page 32: Files and Directories

Device Special Files

Obtaining the major and minor numbers Use the major and minor macros

major(st_dev) minor(st_dev)

For block and character special devices use st_rdev instead to obtain real device number