[Post migration] Linux File Operations

Source: Internet
Author: User
Tags lstat

I. File System

The purpose of a file system is to organize a file into a logical hierarchy that contains directories, connections, and other physical block devices. It does not care about the structure of the underlying physical block device. When file operations are performed, the block device is driven by the block device.ProgramMaps requests to a specific block to the correct device.
The ext3 file system represents the entire file system as a single hierarchy tree that stores files in data blocks of the same size. The smaller the data block, the higher the disk space utilization, but the operation speed decreases as the number of I/O operations required by the kernel increases. The larger the data block, the higher the speed, however, the disk utilization is reduced.
Ext3 uses an inode structure to describe a file. The inode structure contains information such as the file access permission, modification time, and file type. All inode are stored in the inode table. The directory file only stores a linked list pointing to the index number of the inode table item. The first two entries are always..., indicating the current directory and parent directory.

Ii. file descriptor and stream

The file descriptor is a non-negative integer allocated by the Linux kernel when a file is opened or created. It is used to track opened files. Three file descriptors have specific meanings: 0-standard input, 1-standard output, and 2-standard error, the POSIX standard defines three constants to indicate the three file descriptors: stdin_fileno, stdout_fileno, and stderr_fileno. To use these constants, the header file <unistd. h> must be included.
Stream is a concept proposed by the Standard C language I/O library. It hides the file descriptor and replaces it with a more abstract concept. When a file is opened or created using a standard I/O library function, a pointer of the file structure is returned, which contains information such as the file descriptor, buffer address, and buffer size. There are three predefined file descriptors corresponding to three predefined file descriptors: stdin, stdout, and stderr. To use these three streams, the header file must contain <stdio. h>

Iii. File Operations

File operations can be divided into system calls and Standard C library functions. System calling is the calling interface provided by the operating system. Therefore, it is related to the operating system, and the Standard C library is irrelevant to the operating system. This section describes system calls.

1. open/close a file
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Int open (const char * pathname, int flag );
Int open (const char * pathname, int flag, mode_t mode );
Int creat (const char * pathname, mode_t mode );
Int close (int fd );
The flag value is as follows:
O_rdonly: Read-Only
O_wronly: Write-only
O_rdwr: read/write
O_append: append
O_creat: Creates a file if the file does not exist. You need to specify the mode parameter.
O_excl: If o_creat is specified and the file already exists, an error is returned.
O_trunc: if the file exists and is opened in write-only mode, the file content is cleared.
O_ndelay: open in non-blocking mode. The normal file is on the local disk, and the read/write operation time is determined, so it is always blocked. However, when a process operates on some special files, such as FIFO, the data arrival time is uncertain. Therefore, the read operation must return immediately instead of blocking the data.
O_sync: synchronize data in the buffer zone to the disk.
O_nofollow: If pathname is a symbolic connection file, the open () function returns an error
O_directory: If pathname is not a directory, the open () function returns an error.
Mode is required only when the o_creat flag is specified, and the access permission (Mode &~ Umask)
S_irusr: read permission of the owner
S_iwusr: write permission of the owner
S_ixusr: Execution permission of the owner
S_irwxu: Owner's read, write, and execution Permissions
S_irgrp: Group read permission
S_iwgrp: Group Write Permission
S_ixgrp: group execution permission
S_irwxg: Group read, write, and execution Permissions
S_iroth: Read Permission of other users
S_iwoth: Write Permission for other users
S_ixoth: Execution permission of other users
S_irwxo: read, write, and execute permissions for other users

2. read/write files
# Include <unistd. h>
Size_t write (INT Fildes, const void * Buf, size_t nbytes );
Size_t read (INT Fildes, void * Buf, size_t nbytes );

3. File Location
# Include <unistd. h>
# Include <sys/types. h>
Off_t lseek (INT Fildes, off_t offset, int whence );
Whence can have the following three values, which are consistent with the standard C Library:
Seek_set: relative to the beginning of the file
Seek_cur: relative to the current location
Seek_end: relative to the end of the file

4. Query file status information
# Include <unistd. h>
# Include <sys/STAT. h>
# Include <sys/types. h>
Int fstat (INT Fildes, struct stat * BUF );
Int Stat (const char * path, struct stat * BUF );
Int lstat (const char * path, struct stat * BUF );
The fstat () and Stat () functions are the same, but the parameters are different. When a file is a symbolic link, lstat () returns information about the link, and Stat () returns information about the file to which the link points.
Stat is a struct that is passed as a parameter and contains the following members:
St_mode File Permission and file type information
St_ino file I node Information
Device information of the st_dev File
User ID of the st_uid file owner
Group ID of the st_gid file owner
St_atime last access time
St_ctime last time the permission, owner, group, or content was modified
St_mtime last time the content was modified
Number of hard links st_nlink to this file
The st_mode member contains the File Permission and file type information.
The File Permission is the same as the mode parameter in the open () function.
The file type can be set to the following values:
S_ifblk: an object is a special block device.
S_ifdir: the object is a directory.
S_ifchr: the entity is a special character device.
S_ififo: the object is an ififo (command pipeline)
S_ifreg: an object is a regular file.
S_iflnk: an object is a symbolic link.
To test these labels, you must first obtain them and use the mask (mask ):
S_ifmt: file type
S_irwxu: read, write, and execute permissions
S_irwxg: Group read/write/execute permission
S_irwxo: read, write, and execute permissions of other users
These masks are used to block all other bits, leaving only the flags to be tested.
In addition to getting the file type through the test file type flag, there are some macros that can help us to judge and use it very conveniently. Just pass st_mode as a parameter:
S_isblk (): Testing for Special Block Files
S_ischr (): special character file test
S_isdir (): Directory Test
S_isfifo (): a FIFO Test
S_isreg (): Testing for regular files
S_islnk (): Testing the Symbolic Link
Example:
Struct stat statbuf;
Mode_t modes;
Stat ("FILENAME", & statbuf );
Modes = statbuf. st_mode;
Test with macros:
If (! S_isdir (modes) // determines if it is not a directory
Test with a flag:
If (MODES & s_irwxu) & s_ixusr) // checks whether the user has the execution permission

5. conversion between file descriptors and streams
#include
file * fdopen (INT filedes, const char * mode);
the mode parameter is the same as the fopen () function

6. advanced topic
# include
int fcntl (INT Fildes, int cmd);
int fcntl (INT Fildes, int cmd, long Arg );
the fcntl () function can be used to query and change the attributes of an opened file.
the CMD parameter can be set to the following values:
f_dupfd: copy file descriptor, which has the same function as DUP ()/dup2 ()
f_getfd/f_setfd: query/set the file descriptor flag, usually only the fd_cloexec flag
f_getfl/f_setfl: Query/set the File status flag, with open () the second parameter of the function has the same value
Note: Because the o_rdonly, o_wronly, and o_rdwr labels do not occupy one place (o_rdwr = o_rdonly | o_wronly), to test the file read/write permissions, you must first use the o_accmode and the return value, and then compare them with these three marks.
In addition, the file read and write permissions cannot be changed, only four o_append, o_nonblock, o_sync, and o_async labels can be changed.

# Include <unistd. h>
# Include <sys/IOCTL. h>
Int IOCTL (INT filedes, int request ,...);
I/O operations that cannot be completed by other functions are generally completed by it. The request parameter specifies the operation to be performed on the file. The operation is different, and the third parameter is also different:
Dioxxx: disk I/O
Fioxxx: file I/O
Mtioxxx: tape I/O
Sioxxx: Socket I/O
Tioxxx: terminal I/O

7. Buffering and no Buffering
(1) completely buffered. The operations performed by standard C library functions on files are generally completely buffered. The Kernel performs real I/O operations only when the I/O buffer is filled (write operation) or empty (read operation. The kernel generally calls a malloc () function to allocate a buffer when performing the first I/O operation on the first stream.
(2) Row buffering. When a file is actually a terminal, row buffering is generally used. After row buffering is used, real I/O operations are performed only when a line break is encountered. Note that due to the limited buffer size in the I/O library, I/O operations may have been performed if a row is long and there is no line break.
(3) No buffer. Generally, standard errors are not buffered, and any output information is immediately reflected in the file.
Set the stream buffer type:
# Include <stdio. h>
Void setbuf (File * FP, char * BUF );
Void setvbuf (File * FP, char * Buf, int mode, size_t size );
Set the Buf parameter in the setbuf () function to null, that is, no buffer type. To allow buffering, The Buf must point to a buffer with a length of at least bufsize (defined in <stdio. h>. If the stream is a terminal, it is set as a row buffer; otherwise, it is a full buffer.
Set the mode parameter in setvbuf () to _ ionbf, which is of the no-buffer type. The Buf and size parameters are ignored. When the mode parameter is set to _ iofbf (full buffer) or _ iolbf (row buffer), size specifies the size of the buffer to which the Buf points. If the Buf is null, the I/O library function automatically allocates a buffer according to the st_blksize member in the stat structure. If the system cannot determine the value, if the file is a device or pipeline, the buffer size is bufsize.
Forced output buffer content: int fflush (File * FP); If FP is null, flush all opened streams.

8. handle errors
When an error occurs, set the value of the global variable errno to indicate the cause of the error.
Map the error number to a string
# Include <string. h>
Char * strerror (INT errnum );

Output Error information to the standard error stream, prefixed with "s :"
# Include <stdio. h>
Void perror (const char * s );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.