Linux File Operation learning Summary "reprint"

Source: Internet
Author: User
Tags flock lstat

This article was reproduced from: http://blog.csdn.net/xiaoweibeibei/article/details/6556951

file Type: Normal file (text file, binary file), catalog file, link file, device file, pipeline file.    

Permissions for files: Read, write, execute

Information about the file: directory structure, index node, file data

of the index node Stat structure

struct stat{

dev_t the device number used by the st_dev;//file

ino_t st_inl;//Index Node number

mode_t st_mode;//File access rights

nlink_t number of hard links to st_nlink;//files

uid_t st_uid;//User identification number

gid_t st_gid;//Group Identification number

dev_t the device number of the st_rdev;//device file

off_t st_size;//file Capacity in bytes

unsigned long st_blksize;//the size of the disk block that contains the file

Unsigned a long st_blocks;//file occupies a disk block

time_t st_atime;//the last time to access the file

time_t st_mtime;//last time the file was modified

time_t st_ctime;//The last time the file state was changed

} ;

In this structure, St_dev corresponds to each file name, representing the device number of the filesystem containing the file name and the corresponding index node, and St_rdev only the character device and the fast device, which represents the device number of the actual device.

File manipulation related functions

File creation

function : int creat (const char *pathname,mode_t mode);

header file:sys/types.h sys/stat.h fcntl.h

Description

The mode takes the following values:

Mode Take value

Meaning

S_irusr

Read permission for the file owner

S_iwusr

Write permission for file security owner

S_ixusr

Execution rights for file owners

S_irgrp

Read permissions for file owners and groups of users

S_iwgrp

Write permissions for file owners with group users

S_ixgrp

Execute permissions for file owners and groups of users

S_iroth

Read permissions for other users

S_iwoth

Write permissions for other users

S_ixoth

Execution rights for other users

Several permissions combinations are also defined in the Linux system:

S_irwxu = (s_irusr| s_iwusr| S_IXUSR)

S_irwxg = (s_iwgrp| s_iwgrp| S_IXGRP)

S_irwxo= (s_iroth| s_iwoth| S_ixoth)

When the call succeeds, returns the descriptor of the file, which returns 1, when the file is opened as read-only.

File Open

function:(1) int open (const char *pathname,int flags);

(2) int open (const char *pathname,int flags,mode_t mode);

header file:sys/types.h sys/stat.h fcntl.h

Description:flags is used to describe file open parameters, which are added by bitwise logic, where o_rdonly,o_wronly and o_rdwr,3 values must contain one of them. The flags are valued as follows:

Flags Take value

Meaning

O_rdonly

Open as read-only

O_wronly

Open in write-only mode

O_rdwr

Open in read and write mode

O_creat

If the file you opened does not exist, create this file

O_excl

If the o_creat bit is set when the file is opened and the file exists, it causes a failure

O_noctty

If the process does not control the TTY while the TTY is open, the terminal is not controlled

O_trunc

If you open an existing file as write-only or read-write, the file will be up to 0

O_append

Opens as an append, placing the pointer at the end of the file when adding content to the file

O_nonblock

For non-blocking interface I/O, if the operation cannot be completed without delay, it is returned before the operation

O_nodelay

With O_nonblack

O_sync

Returns only after data is written to external memory or other devices

When the call succeeds, returns the descriptor of the file to be opened, the call fails back to 1, and the errno is the corresponding error number.

File Close

function:int close (int fd);

header file:unistd.h

Description

Returns 0 on success, 1 on Failure, collocated errno for EBADF. When you open a file that is a reference count in the file description plus 1, when you close a file, the reference count of the file is reduced by 1, and when the reference count is 0 o'clock, the close call not only releases the descriptor of the file, but also releases the description table entry that the file occupies.

File Write operations

function:ssize_t write (int fd,void *buf,size_t count);

header file:unistd.h

Description: The call succeeded in returning the number of bytes written to the segment, failure returns-1, and the corresponding errno is set.

File read operations

Function: ssize_t read (int fd, void *buf,size_t count);

header file:unistd.h

Description: The call succeeded in returning the number of bytes read, the failure returned-1, and the corresponding errno was set.

File location Operations

Function: off_t lseek (int fildes.off_t offset,int whence);

header file:unistd.h

Description: The call succeeded in returning the actual offset relative to the beginning of the file, and failed to return 1, and set the corresponding errno. The whence values are as follows:

whence Take value

Meaning

Seek_set

Calculates the offset from the beginning of the file;

Seek_cur

Calculates the offset from the current position;

Seek_end

Calculates the offset from the end of the file

File Property Modification

function: (1) int chown (const char *pathname,uid_t owner,gid_t Group);

(2) int fchown (int fd,uid_t owner,gid_t Group);

header files:sys/types.h unistd.h

Description: Change the owner of the file, due to permissions issues, only the root user can call Chown and fchown to arbitrarily change the owner of a file and the group, ordinary users do not have this permission, ordinary users can only modify their own all the files of the group identification number, Can only be selected in the group to which they belong.

function: (1) int chmod (char *pathname,mode_t mode);

(2) int fchmod (int fd,mode_t mode;

header files:sys/types.h unistd.h

Description: Change the access permissions of the file, about the permissions of the settings and the chmod command similar, the call successfully returned 0, failed to return 1, and set errno.

File Rename

function:int rename (const char *oldname,const char *newname);

header file:stdio.h

Description: when NewName and Oldname point to the same, the rename call does nothing to return successfully.

oldname Pointing

newname point to file does not exist

newname point to normal file

newname pointing to the directory file

Oldname point to Normal file

File is renamed

File Rename

Error return

Oldname pointing to the directory file

File Rename

Error

NewName The directory file pointed to is an empty directory, the directory is deleted, Oldname is renamed, or an error is returned

DUP and the dup2 called

function: (1) int dup (int fd); (2) int dup2 (int fd,int fd2);

header file:unistd.h

Note: The call will copy the file descriptor, and the two invocations will return the new file descriptor when executed successfully, the difference being that (2) the file descriptor Fd2 can be predetermined, and if FD2 is being used, close fd2 first, if FD2 and FD are the same, Does not close the file normally returns;

Stat , Fstat and the Lstat called

function: (1) int stat (const char *pathname,struct stat *sbuf);

(2) int fstat (int fd,struct stat *sbuf);

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

header file:sys/types.h sys/stat.h unistd.h

Note: for Lstat, when accessing a symbolic link, lstat only returns information about the link itself, and stat and Fstat trace to the end of the link file, which is the attached file.

Fsync called

function:int fsync (int fd);

header file:unistd.h

Note: All data stored in the buffer to be written to the file descriptor FD is flushed to the file to be written, and when the call returns 0 successfully, the call fails back to 1, and the errno is set.

Flock called

function:int flock (int fd,int operation);

header file:sys/types.h

Note: to lock or unlock files corresponding to file descriptors, operation is used to indicate different locking or unlocking methods, with the following values:

Lock_sh: Shared lock LOCK_EX: Exclusive lock Lock_un: Unlocking

A process can have only one exclusive lock on a file, but there may be multiple shared locks, which cannot be accessed if a process does not attempt to lock a file that has been locked. Returns 0 when the call succeeds and returns-1 if it fails.

Fcntl called

function: (1) int fcntl (int fd,int cmd);

(2) int fcntl (int fd,int cmd,long arg);

header files:unistd.h fcntl.h

Description: The parameter arg is an optional parameter, corresponding to some of the desirable values of CMD, corresponding to perform some special operations, return 0 on successful invocation, failure to return 1, and set errno, in addition, most of the operations that the FCNTL call can accomplish can be done with other calls. The values of CMD are as follows:

cmd Take value

Appropriate action

F_dupfd

Copy file descriptor

F_getfd

Get file Descriptor

F_setfd

Setting the CLOSE-ON-EXEC flag

F_getfl

Gets the flags for the open call setting

F_setfl

Set the flags for the open call setting

F_getlk

Get a discrete file lock

F_setlk

Set to get a discrete file lock without waiting for

F_setlkw

Set to get a discrete file lock and wait if necessary

F_getown

Retrieves the Sigio and Sigurg semaphore process ID or process group number that will be received

F_setown

Set process ID or process group number

Special File operations

Create a Directory

function:int mkdir (const char *pathname,mde_t mode);

header files:sys/types.h sys/stat.h

Description:The value of mode represents its permission, and the value of mode is determined by the current Umask value and the set mode phase, and the owner of the directory is the valid user identification number of the process that called mkdir to create it. The call returns 0 successfully, the failure returns-1, and the errno is set.

Delete Directory

function:int rmdir (const char *pathname);

header file:unistd.h

Description: When the directory is empty, call this function to delete it, or the deletion will fail. The call returns 0 successfully, the failure returns-1, and the errno is set.

Open Directory

function: DIR * OPENDIR (const char *pathname);

header files: sys/types.h dirent.h

Description: The call return value is the Dir type, which is used to point to the structure pointer to the directory file. The call successfully returned a directory pointer, and the call failed to return null.

Close Directory

function:int closedir (DIR *DP);

header files:sys/types.h dirent.h

Description: The parameter is the directory file pointer to close, the pointer has Opendir returned, the call returns 0 successfully, the failure returns 1, and the errno is set.

Read Directory

function:struct dirent * Readdir (DIP *DP);

header files:sys/types.h dirent.h

Description: The directory file used to access the directory pointer DP, whose return value is the dirent struct pointer, the dirent structure is as follows:

struct drent{

ino_t d_ino;//Directory Node number

off_t d_off;//Node Offset

Unsigned short d_reclen;//record length

unsigned char d_type;//file type

Char d_name[256];//the file name of the directory link

}

When there are no more links in the directory, 0 is returned.

Create a hard link

function:int link (char *pathname1,char *pathname2);

header file:unistd.h

Description:pathname1 for existing files, pathname2 for the link to be established, pathname1 and pathname2 the west of the path must be in the same file system, only Superuser can create a new connection file to the directory file. The call returns 0 successfully, and the failure returns-1.

Remove a hard link

function:int unlink (char *pathname);

header file:unistd.h

Note: to remove a link to a file, you must have write permission and execute permission on the directory, and when unlink is called, the index node count of the directory will be reduced by 1, and when the link calculator is 0 o'clock, the index node and the file data block will be freed.

Creating Symbolic Links

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

header file:unistd.h

Description:Acturalpath is a real-world file, Sympath for the newly created symbolic link to Acturalpath, the call successfully returned 0, failed to return 1, and set errno.

Open link

function:int readlink (const char *pathname, char *buf,int bufsize);

header file:unistd.h

Description: When the call succeeds, the return value is the number of bytes written to the buffer, the return value is 0 when the call fails, and the errno is set.

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.