IO system - file and directory operations

 1. File kernel data structures

Three open a file in the kernel data structures represents:

(1) file descriptor table

File descriptor flags

File table entry pointer

(2) file entry:

File status flags: read, write, append, and other state and non-blocking synchronization mark

The current file offset

i-node table entry pointer

Reference counter

(3) i node

Operation and function pointer file type of the file

The length of the current file

File owner

Device file is located, file access permissions

Pointing to a file on disk data such as location of the pointer position.

2. atomic operation

(1) additional file

Use when opening file O_APPEND flag, the process of the document data and the additional offset adjustment becomes an atomic operation .

 O_APPEND

 write () {// write becomes an atomic operation

        1) read the file from i node length as the current offset

        2) write data to the file

        3) modification of the length of the document node i

    }

Kernel file each time before writing, will offset the current process is set for the end of the file, so no need to adjust the offset lseek.

(2) file creation:

Function of open O_CREAT O_EXCL and used, if the file exists, open will fail, otherwise the file is created, and it is determined that the file exists and the creation of an atomic operation. 

3. I / O handling 

Five models 3.1 I / O processing

Blocking I / O model: If the called I / O functions related functions will not complete the process hangs, will not return until the data arrives, such as: access terminals, network equipment.

Non-blocking model: When requested I / O operation can not be completed, the process is not to be dormant, but returns an error. Such as: open, read, write access.

I / O multiplexer model: If the requested I / O operation blocked, and it's not really blocking I / O, and let them wait for a function, in this period, I / O can also carry out other operations, such as : select function.

Drive signal I / O function: In this mode, the signal processing by installing a program, the system automatically captures the arrival of a particular signal, thereby initiating I / O.

Asynchronous I / O model: In this mode, when a descriptor is ready to be start I / O, the kernel notifies the process, for subsequent processing by the kernel, which now uses less. 

3.2 Non-blocking I / O 

When the low-speed system call, the process may block

Non-blocking I / O operation is determined (read, open, write) does not block, if the operation can not be completed, an error is returned.

Setting a non-blocking manner: 1, using the Open to open a file, set O_NONBLOCK flag; 2, if a file has been opened, the file is used to modify status flags fcntl. 

4. Advanced file operations - File Lock 

When multiple users to share, operating a file, commonly used method is to give Linux file locking to avoid the state of shared resources generate competition.

File locks are divided into functions: 1, shared read lock : open file descriptors must be read; read lock on a file, other processes can also be read on the lock for reading. 2, exclusive write locks : 1, open file descriptors must be written; a write lock on the process, other processes can not read and write lock on the lock for writing.

Divided by type lock file lock is recommended and mandatory locks. Recommend lock locked file requires a process to detect whether there should be latched, and respect the existing lock. Mandatory locks the system and executed by the kernel.

fcntl locks and can not only implement the recommendations can be implemented mandatory locks. 

Note 4.1 point lock and unlock the region 

The region may begin at or beyond the current end of the file beginning at the trailing end thereof, but not before the beginning of the file or beyond the starting position.

Should l_len is 0, the lock area from its starting point (the l_start and l_whence decision) until the maximum possible position. That is, no matter how much data the file to Tianxie, which are in the range locks.

To lock the entire file, the usual method is to l_start set to 0, l_whence set SEEK_SET, l_len set to zero.

4.2 inheritance lock and release

A process terminates, it established the locks have been released;

Close a file descriptor, this process should be all the locks on files are released;

Lock the child does not inherit the parent process;

After performing exec, the new process can choose whether to inherit the implementation process of the original locks. 

The memory map 

Is a memory map to a disk file memory space mapped caches, read and write cache data corresponding to complete reading and writing files.

 

5.1 calling function 

1 #include <sys/types.h>
2 #include <sys/mman.h>
3 void *mmap(void *addr, size_t length, int port, int flags, int fd, off_t offset);

Returns: If successful, compared with the starting address of the mapped area, or -1 if an error

Function: I / O to make a disk file and a cache memory space mapped.

1 int munmap(void *addr, size_t length);

Returns: the successful return 0, -1 error

Function: unmap

mmap function gets the data from the cache, the read is equivalent to the corresponding byte in the file, similar thereto, the data stored in the buffer, the corresponding byte is automatically written to the file, so that you can read and write without the use of the perform the I / O.

Child inherits the parent process of memory mapped area.

parameter:

addr: Starting address of memory mapped area, usually set to 0 , let distribution system.

length: Number of bytes to be mapped

offset: offset in bytes mapping file

prot: PROT_READ mapping area readable, PROT_WRITE mapping area writable, PROT_EXEC perform mapping area, PROT_NONE: mapping area inaccessible

flags:MAP_FIXD返回地址必须等于addr,不推荐使用、MAP_SHARED存储操作立刻修改映射文件内容、MAP_PRIVATE存储操作导致创建映射文件的副本,并对副本读写。

 

6. 文件属性

struct stat是存放文件属性的结构体 

 1 struct stat{
 2     mode_t st_mode;     //file type & permission
 3     ino_t st_ino;        //i-node number
 4     dev_t st_dev;        //device number(file system)
 5     dev_t st_rdev;        //device number for special files
 6     nlink_t st_nlink;    //number of links
 7     uid_t st_uid;        //user ID of owner
 8     gid_t st_gid;        //group ID of owner
 9     off_t st_size;        //size in bytes
10     time_t st_atime;    //time of last access
11     time_t st_mtime;    //time of last modification
12     time_t st_ctime;    //time of last file status change
13     blksize_t st_blksize;    //best I/O block size最佳块大小
14     blkcnt_t st_blocks;        //number of disk blocks allocated
15 };  

6.1 文件属性操作函数 

1 #include <sys/types.h>
2 #include <sys/stat.h>
3 int stat(const char *pathname, struct stat *buf);
4 int fstat(int fd, struct stat *buf);
5 int lstat(const char *pathname, struct stat *buf);

返回:若成功返回0,若出错则为-1

功能:返回一个与pathname或fd执行的文件属性信息,存储在结构体buf中。

参数:pathname:文件路径名称;buf:struct stat结构体指针。

lstat函数类似于stat,但是当命名的文件是一个符号连接时,lstat返回该符号连接的有关信息,而不是由该符号连接引用的文件的信息。 

6.2 文件类型 

文件类型 判别函数

普通文件(regular file) S_ISREG()

目录文件(directory file) S_ISDIR()

块特殊文件(block special file) S_ISBLK()

字符特殊文件(character special file) S_ISCHR()

FIFO(named pipe) S_ISFIFO()

套接字(socket) S_ISSOCK()

符号链接(symbolic link) S_ISLNK() 

6.3 文件权限 

9种文件访问权限:

用户权限:S_IRUSR、S_IWUSR、S_IXUSR

组权限:S_IRGRP、S_IWGRP、S_IXGRP

其它权限:S_IROTH、S_IWOTH、S_IXOTH

文件权限通过按位或方式构造。 

6.4 文件权限相关函数 

(1)access函数 
1 #include <unistd.h>
2 int access(const char *pathname, int mode);

返回:成功执行返回0,若出错为-1

功能:检查是否可以对指定文件进行某种操作

参数:pathname:文件路径;

mode:文件访问权限:

R_OK:判断文件是否有读权限;

W_OK:判断文件是否有写权限;

X_OK:判断文件是否有可执行权限;

F_OK:判断文件是否存在。

(2)umask函数

1 #include <sys/types.h>
2 #include <sys/stat.h>
3 mode_t umask (mode_t mode);

返回:以前的文件模式创建屏蔽字(掩码)

功能:为进程设置文件方式创建屏蔽字,并返回以前的值

参数:mode:文件权限常量(如:S_IRGRP、S_IWGRP等)

被umask设置过的权限不能再使用在创建文件的权限上。

(3)chmod和fchmod函数

1 #include <sys/stat.h>
2 int chmod(const char * pathname, mode_t mode);
3 int fchmod(int fd, mode_t mode);

返回:成功返回0,出错返回-1

功能:更改现存文件的权限。chmod函数在指定的文件上进行操作,而fchmod函数对已打开的文件进行操作。

参数:pathname:文件路径名字

mode:文件权限(按位或操作)

S_ISUID、S_ISGID、S_ISVTX

S_IRWXU、S_IRUSR、S_IWUSR、S_IXUSR

S_IRWXG、S_IRGRP、S_IWGRP、S_IXGRP

S_IRWXO、S_IROTH、S_IWOTH、S_IXOTH

想要改变一个文件的权限位,需满足条件:进程的有效用户ID必须等于文件的所有者ID或者进程具有超级用户权限。

(4)truncate和ftruncate函数

1 #include <sys/types.h>
2 #include <unistd.h>
3 int truncate(const char * pathname, off_t length);
4 int ftruncate(int fd, off_t length);

返回:成功返回0,出错返回-1

功能:文件截断

参数:pathname:文件路径名字;length:文件截断后的长度。

在文件尾端处截去一些数据以缩短文件。

将一个文件的长度截短为0是一个特例,用O_TRUNC标志可以做到这一点。

如果该文件以前的长度大于length,则超过length以外的数据就不再能存取。如果以前的长度短于length,则其后果与系统有关。 

7. Linux文件系统结构 

文件操作相关的最基本元素是:目录元素、索引节点和文件的数据本身。

目录结构(目录项)

索引节点(i节点)

文件的数据

 

(1)link和unlink函数 

1 #include <unistd.h>
2 int link(const char *exitingpath, const char *newpath);

返回:成功返回0,出错返回-1

功能:创建一个指向现存文件连接(硬链接)

1 int unlink(const char *pathname);

返回:成功返回0,出错返回-1

功能:删除pathname指定的硬链接,并将由pathname所引用的文件链接计数-1

硬链接创建条件:针对文件创建链接,必须是同一个分区,只有超级用户才能对目录建立链接。

文件删除条件:链接计数为0,无其它进程打开该文件。

(2)remove和rename函数

1 #include <unistd.h>
2 int remove(const char * pathname);

返回:成功返回0,出错返回-1

功能:解除对一个文件或目录的链接

1 int rename(const char * oldname, const char * newname);

返回:成功返回0,出错返回-1

功能:文件或目录更名

对于文件,remove的功能和unlink相同

对于目录,remove的功能和rmdir相同

(3)symlink和readlink函数

1 #include <unistd.h>
2 int symlink(const char * actualpath, const char * sympath);

返回:成功返回0,出错返回-1

功能:创建一个符号链接(软链接)

1 int readlink(const char * restrict_pathname, char * restrict buf, size_t bufsize);

返回:成功返回读到的字节数,出错返回-1

功能:打开该链接本身,并读该链接中的名字

符号链接创建并不要求actualpath存在,可以跨文件系统建立符号链接,软链接也可以针对目录创建。 

8. 文件时间 

字段

说明

例子

ls选项

st_atime

文件数据最后访问时间

Read

-u

st_mtime

文件数据最后修改时间

Write

默认

st_ctime

i节点最后更改时间

chmod、chown

-c

(1)utime函数

1 #include <sys/types.h>
2 #include <utime.h>
3 int utime(const char * pathname, const struct utimebuf * times);

返回:成功返回0,出错返回-1

功能:更改文件的存取时间和修改时间

1 struct utimebuf{
2     time_t actime; //access time
3     time_t modtime; //modification time
4 };

参数times:

空指针则读取当前时间:进程的有效用户ID必须等于文件的所有者ID,或者进程对该文件具有写权限

非空取times结构体中的时间:进程有效用户ID等于该文件的所有者的ID,或者进程是超级用户进程。

utime操作会自动更新st_ctime值。 

9. 目录函数 

(1)mkdir和rmdir函数
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 int mkdir(const char * pathname, mode_t mode);

返回:成功返回0,出错返回-1

功能:创建目录

1 int rmdir(const char * pathname);

返回:成功返回0,出错返回-1

功能:删除目录

创建目录:创建一个新的空目录,.和..目录项是自动创建的。创建目录时,至少指定一个执行权限位。

目录删除条件:该目录的链接计数为2(只包含.和..),无其它进程打开目录。

(2)opendir、readdir、rewinddir和closedir函数

1 #include <sys/types.h>
2 #include <dirent.h>
3 DIR * opendir(const char * pathname);

返回:成功返回目录指针,出错返回NULL

功能:打开目录

1 struct dirent * readdir(DIR *dp);

返回:成功返回指针,若在目录结尾或者出错返回NULL

功能:读取目录

1 void rewinddir(DIR *dp);

功能:重新定位从头开始读取

1 int closedir(DIR *dp);

返回:成功返回0,出错返回-1

功能:关闭目录

1 struct dirent{
2     ino_t d_ino; //i-node number
3     char d_name[NAME_MAX+1]; //null-terminated filename
4 };
(3)chdir、fchdir和getcwd函数
1 #include <unistd.h>
2 int chdir(const char * pathname);
3 int fchdir(int fd);

返回:成功返回0,出错返回01

功能:分别用pathname或fd来指定新的当前工作目录

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

返回:成功返回buf,出错返回NULL

功能:获取当前工作目录的绝对路径名

当前工作目录是一个进程的属性,所以它只影响调用chdir的进程本身,而不影响其它进程。

问题:改变了路径是否会影响shell的当前路径?

不会影响。

10. 设备特殊文件

每个文件系统所在的存储设备都由主、次设备号表示;

major和minor宏可用来得到主、次设备号;

只有字符特殊文件和块特殊文件才有st_rdev值。

Guess you like

Origin www.cnblogs.com/mrlayfolk/p/12019753.html