Common system function descriptions for Linux file operations

Source: Internet
Author: User
Tags lstat posix symlink

1. Open File (Man 2 open View)

int open (const char *pathname, int flags); //pathname file name (path); Flags open mode, O_rdonly, O_wronly, O_RDWR

int open (const char *pathname, int flags, mode_t mode);This function is typically used to create new files, flags to add o_creat, such as: o_rdwr| o_creat int creat (const char *pathname, mode_t mode);Create a new file, mode permission description, such as 0644 (octal, inverse and umask do with the actual result of the operation)

Return value: Successfully returned file descriptor FD, failed to return-1.

To illustrate:

int fd = open ("Test.txt", o_wronly); //write-only way to open Test.txt, return file descriptor

Attached: Description of the file descriptor (32-bit machine file descriptor from 0 to 1023): 0 for the standard input, 1 for the output, 2 for the error; Our program opens from 3, always using the smallest one at a time.

2. Read file reads (Man 2 write view)

ssize_t Read (int fd, void *buf, size_t count); //ssize_t signed integer; fd file descriptor; buf outgoing parameter, read content is in BUF; count represents the length of buf

Return value: Greater than 0 indicates the number of bytes read, equal to 0 means that the read is complete, 1 indicates a read failure

3. Write file (man 2 write view)

ssize_t Write (int fd, const void *buf, size_t count); //buf The length of content to write, count needs to write, ssize_t signed integer, size_t unsigned integer

Return value: Greater than the number of bytes written, 0 means no data is written, 1 indicates a write failure

4. Lseek Move file pointer (man 2 Lseek view)

off_t lseek (int fd, off_t offset, int whence); //fd file descriptor, offset shift shift, whence move reference point

Whence three values: Seek_set file start position, Seek_cur indicates the pointer's current position, Seek_end file end position

Return value: greater than or equal to 0 indicates the offset from the beginning of the file, 1 indicates failure

Attached: This function can be used for capacity expansion, such as Lseek (fd,100,seek_end), which means 100 bytes expansion, but the capacity to perform a write operation is not effective.

5. Stat gets a file of information (Man 2 stat view)

int stat (const char *path, struct stat *buf); //path file path; buf outgoing parameter, gets the file information in BUF

int fstat (int fd, struct stat *buf); //fd File descriptor

int Lstat (const char *path, struct stat *buf); The difference between//lstat and stat is only the symbolic link file, lstat read the symbolic link file, stat reads the source file corresponding to the symbolic link

Return value: 0 successfully returned, failure returned-1.

Attached: the composition of the struct stat structure is explained below

struct Stat { dev_t st_dev;/* ID of device containing file device id*/ino_t st_ino;/* inode n Umber inode node * /mode_t St_mode; /* Protection file type and access permissions, etc. (this field is important, see the man document for a specific description of the field)* /

nlink_t St_nlink; * Number of hard links * /uid_t st_uid; */user ID of owner user id*/ gid_t St_gid; /* Group ID of the owner Group id*/dev_t St_rdev; /* Device ID (if special file) If this is a device file, then it is the unit number * /

off_t st_size;/* Total size, in bytes file bytes, which is the file size */blksize_t st_blksize;/* blocksize for file sy The size of the stem I/O block, which is the file system I/o buffer size * /

blkcnt_t st_blocks; /* Number of 512B blocks allocated blocks * /time_t st_atime; /* Time of last access times * /time_t st_mtime; /* Time Modification Last modified * /time_t st_ctime; /* Time of the last status change for the latest changes */};

Structural bodydescription of the st_mode of the struct stat:

The. Check the file type using the St_mode field://macro that uses St_mode to examine the types of files is defined as follows

S_isregNormal file (m) is it a regular file?S_isdirCatalog (m) directory?S_ischrCharacter device (m) character device?S_isblkBlock Devices (m) block device?S_isfifoPipe (m) FIFO (named pipe)?S_islnkSymbolic link (m) Symbolic link? (not in posix.1-1996.)S_issockSocket (m) socket? (not in posix.1-1996.)To illustrate:

Switch (Sb.st_mode & s_ifmt) { //s_ifmt is a file type mask with a value of octal 170000

Case s_ifblk:printf ("Block device\n"); Break

Case s_ifchr:printf ("character device\n"); Break

Case s_ifdir:printf ("directory\n"); Break

Case s_ififo:printf ("fifo/pipe\n"); Break

Case s_iflnk:printf ("symlink\n"); b reak;

Case s_ifreg:printf ("regular file\n"); Break

Case s_ifsock:printf ("socket\n"); Break

default:printf ("unknown?\n"); Break

}

Attached: The S_irwxu file owner permission mask, whose value is the 00700;S_IRWXG group permission mask, whose value is 00070;s_irwxo the other person's permission mask, whose value is 00007; see man Manual for details. The usage is the same, take S_irwxu as an example, as follows:

Switch (Sb.st_mode & S_irwxu) {

CASE:S_IRUSR printf ("owner has read permission\n"); Break

CASE:S_IWUSR printf ("Owner has wirte permission\n"); Break
CASE:S_IXUSR printf ("Owner has execute permission\n"); Break

}

6. Access Check file permissions (man 2 access View)

int access (const char *pathname, int mode); //pathname file name, mode permission

Return value: All permissions are allowed to return 0, as long as a permission is not allowed to return-1, error is also returned-1;

The value of mode indicates:

If it weren't for F_OK: Indicates if the file exists

If it weren't for R_OK | W_OK | X_OK, can be one or more, multiple bitwise with; R_OK readable, W_ok writable, X_OK executable

7. chmod changing file permissions

int chmod (const char *path, mode_t mode); //path file path; mode_t integer, mode permission, must be an octal number, or use a predefined macro to see the man manual

int fchmod (int fd, mode_t mode); //fd File descriptor

Return value: 0 successfully returned, failure returned-1.

8. Chown change the owner of the file (typically requires root privileges to operate successfully)

int chown (const char *path, uid_t owner, gid_t Group); //through symbolic link; file path

int fchown (int fd, uid_t owner, gid_t Group); //Direct file operation; FD file descriptor

int Lchown (const char *path, uid_t owner, gid_t Group); //Do not penetrate symbolic links; file path

Return value: 0 successfully returned, failure returned-1.

9. Truncate change the file to a specified length

int truncate (const char *path, off_t length); //path file path, must have write permission; length file size, number of bytes per unit

int ftruncate (int fd, off_t length);//fd file descriptor, file must be open

Return value: 0 successfully returned, failure returned-1.

Attached: This function can be used for file expansion, do not need to write once to take effect; Lseek function expansion needs to be written once to take effect.

Link to create a hard link

int link (const char *oldpath, const char *newpath); //oldpath source file, NewPath the path of the hard link to be created

Return value: 0 successfully returned, failure returned-1.

Symlink Create a soft connection

int symlink (const char *oldpath, const char *newpath); //oldpath source file path, NewPath soft connection path

Return value: 0 successfully returned, failure returned-1.

Readlink read the file name of the soft connection (not read the contents of the source file)

ssize_t readlink (const char *path, char *buf, size_t buf-siz);Path Soft link, buf outgoing parameter, Buf-siz is the length of the buf, the content is read in buf

Return value: The length of the read content is returned successfully, and the failure returns-1.

Unlink Delete a link

If it is a symbolic link, delete the symbolic link;

If it is a hard link, the number of hard links is reduced by 1, and when it is reduced to 0 o'clock, the data block and Inode are released;

Attached: If the number of hard links is 0, but a process occupies the file, you must wait for the process to close the file before you actually delete the file, you can use this feature to create a temporary file,

First Open or creat create a file, then unlink. When the process closes the file or the process exits, the file is automatically deleted.

int unlink (const char *pathname); //pathname file path

Return value: 0 successfully returned, failure returned-1.

. rename File Rename

int rename (const char *oldpath, const char *newpath); //oldpath Original file, NewPath new file

Return value: 0 successfully returned, failure returned-1.

Fcntl Operation file Descriptor (Memory: File control)

int fcntl (int fd, int cmd, .../* arg */); //fd file descriptor; cmd command; the third is a mutable parameter, optional

Return value: Successful return is greater than or equal to 0, depending on the value of CMD returns a different meaning value; failure returns-1.

To illustrate:

int fcntl (int fd, int cmd);

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

int fcntl (int fd, int cmd, struct flock *lock);

Determine the value of CMD according to functional classification:

1. Copy an existing file descriptor, CMD is F_DUPFD

2. Gets or sets the file descriptor tag, cmd is F_GETFD, F_SETFD

3. Gets or sets the file status tag, cmd is F_GETFL, F_SETFL

4. Get or set the lock, CMD is f_setlk, f_setlkw, f_getlk

5. Get or set asynchronous IO ownership, cmd for F_getown, F_setown, F_getsig, F_setsig

6.lease, File and directory change notification, etc., see the Man manual.

Common system function descriptions for Linux file operations

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.