Chapter Fourth Documents and directories

Source: Internet
Author: User
Tags lstat

This chapter mainly introduces the creation, deletion, reading and writing of file directories, setting access rights, and obtaining properties of files or directories.

The only thing that needs to be emphasized is that the directory is also a file

Get the properties of a file directory

Key functions

Stat/fstat/lstat information for obtaining a file, such as the owner, permissions, modification, and access time of the file, etc.

#include <sys/stat.h>intFstat (intFildes,structStat *buf); Get information about a file that has been openedintFstat64 (intFildes,structStat64 *buf); Get information about a file that has been openedintLstat (Const Char*restrict Path,structStat *restrict buf); Get information about a symbolic link file [not a linked file]intLstat64 (Const Char*restrict Path,structStat64 *restrict buf); Get information about a symbolic link file [not a linked file]intStat (Const Char*restrict Path,structStat *restrict buf);//Get information about files by file nameintStat64 (Const Char*restrict Path,structStat64 *restrict buf);//Get related information by file name

Key structural bodies

structStat {dev_t St_dev; /*device Inode resides on*/ino_t St_ino; /*Inode ' s number*/mode_t St_mode; /*inode Protection Mode*/nlink_t St_nlink; /*Number or hard links to the file*/uid_t St_uid; /*User-id of owner*/gid_t St_gid; /*Group-id of owner*/dev_t St_rdev; /*device type, for special file Inode*/         structTimespec St_atimespec;/*Time of last access*/         structTimespec St_mtimespec;/*Time of last data modification*/         structTimespec St_ctimespec;/*Time of last file status change*/off_t st_size; /*file size, in bytes*/quad_t st_blocks;/*blocks allocated for file*/u_long st_blksize;/*optimal file sys I/o ops blocksize*/U_long st_flags; /*User defined flags for file*/U_long St_gen; /*file Generation number*/ }; structStat64 {dev_t st_dev; /*ID of device containing file*/mode_t St_mode; /*Mode of file (see below)*/nlink_t St_nlink; /*Number of hard links*/ino64_t St_ino; /*File Serial number*/uid_t St_uid; /*User ID of the file*/gid_t St_gid; /*Group ID of the file*/dev_t St_rdev; /*Device ID*/         structTimespec St_atimespec;/*Time of last access*/         structTimespec St_mtimespec;/*Time of last data modification*/         structTimespec St_ctimespec;/*Time of last status change*/         structTimespec St_birthtimespec;/*Time of file creation (birth)*/off_t st_size; /*file size, in bytes*/blkcnt_t st_blocks; /*blocks allocated for file*/blksize_t st_blksize; /*optimal blocksize for I/O*/uint32_t st_flags; /*User defined flags for file*/uint32_t St_gen; /*file Generation number*/int32_t St_lspare; /*Reserved:do not use!*/int64_t st_qspare[2];/*Reserved:do not use!*/ }; 

Access

<unistd.h>

int access (const char* path, int mode);

Test file, Directory access permissions

Mode parameter:

Whether the R_OK is readable

Whether W_OK can be written

X_OK is executable

Whether F_OK exists

The function returns 0 if the test file has the corresponding permission, otherwise returns-1

Umask

<sys/types.h>

<sys/stat.h>

mode_t umask (mode_t cmask);

Create a masking word while the program is running, and control permissions for all newly created files

The parameter mode_t is the ' or ' of the following 9 types of permissions

---------------

S_IRUSR User-Read

S_IWUSR User-Write

S_IXUSR User-execution

S_IRGRP Group-Read

S_IWGRP Group-Write

S_IXGRP Group-Execution

S_iroth Other-read

S_iwoth Other-write

S_ixoth Other-execution

-----------------

Chmod/fchmod

Chmod is used to change permissions on files that are not open, and fchmod is used to change permissions on open files;

<sys/types.h>

<sys/stat.h>

int chmod (const char* Path, mode_t mode);

int fchmod (int fileds, mode_t mode);

Permission change returned successfully 0; 1;

Parameters: Mode

--------------------------

S_isuid Execution Time Settings-user-id

S_isgid execution set-group-id

S_isvtx Save Body

S_irwxu User (owner) read, write, and execute

S_IRUSR User (owner) read

S_IWUSR User (Owner) write

S_IXUSR User (owner) execution

S_IRWXG group Read, write and execute

S_IRGRP Group Read

S_IWGRP Group Write

S_IXGRP Group Execution

S_irwxo other read, write and execute

S_iroth other Reading

S_iwoth Other Write

S_ixoth other executions

--------------------------

Description: To successfully change file permissions, a valid user ID for the process must be the owner of the file or have Superuser permissions

Chown/fchown/lchown

Change file Owner

<sys/types.h>

<unistd.h>

int chown (const char* Path, uid_t owner, gid_t Group);

int fchown (int fileds, uid_t owner, gid_t Group);

int Lchown (const char* Path, uid_t owner, gid_t Group);

Description: Lchown changed the owner of the symbolic link itself, not the owner of the file that the symbolic link points to

To successfully change the file owner requires the program execution user to have the appropriate permissions

truncate/ftruncate[file truncated]

<sys/types.h>

<unistd.h>

int truncate (const char* path, off_t length);

int ftruncate (int filedes, off_t length);

function execution successfully returned 0; error returned-1;

If the length of the file is greater than long, then the function executes successfully beyond the length of the unexpected data can not continue to access, if the file length is less than length, the end of the file has no data, forming a hole;

Link/unlink/symlink/readlink/rename/remove

Create links, delete links, create symbolic links, read links, rename, delete

Mkdir/rmdir/opendir/readdir/closedir/rewinddir/chdir/fchdir/getcwd

Directory manipulation functions

utime[get last access time, modified time of file)

Reference

Advanced Programming for UNIX environments

Https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man2/stat.2.html

Chapter Fourth Documents and directories

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.