Advanced Programming in UNIX Environment - Chapter 4

4.2 stat, fstat and lstat functions

#include <sys/types.h>
#include <sys/stat.h>

int stat(const char *pathname, struct stat * buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
  • The three functions return 0 for success and -1 for failure
  • The purpose of the three functions is to obtain the information structure related to the file, but stat provides the path, fstat provides the file descriptor, and the path of lstat is the path of a symbolic link

4.3 File Types

  1. normal file
  2. Directory file: Any process with read permission for a directory file can read the contents of the directory, but only the kernel can write the directory file;
  3. character special files
  4. Block special files: This type of file is typically used for disk devices, and all devices in the system are either character special files or block special files;
  5. FIFO: This file is used for construction and construction communication, and is sometimes called a named pipe;
  6. Socket: This file is used for inter-process communication
  7. symbolic link

4.4 Settings-User-ID and Settings-Group-ID

  • A special flag can be set in the file mode word (st_mode) whose definition is "When executing this file, set the effective user ID of the process to the owner of the file (st_uid)"
  • Another bit can be set in the file mode word, which causes the effective group ID of the process executing the file to be set to the group owner of the file

4.5 File Access Permissions

  • st_mode also contains access permission bits for the file

4.6 Ownership of new files and directories

  • The group ID of the new file can be the effective group ID of the process
  • The group ID of the new file can be the group ID of the directory it is in

4.7 access function

  • The access function tests the access permission according to the actual user ID and actual group ID
  • Access function range 0 means success, return -1 means error
#include <unistd.h>

int access(const char *pathname, int mode);
mode illustrate
R_OK test read permission
W_OK test write permission
X_OK Test Execution Permissions
F_OK Test for file existence

4.8 umask function

  • The umask function sets the file mode mask for a process and returns the previous value
  • cmask is composed of 9 constants (S_IRUSR, S_IWUSR, etc.) in the following table bitwise "or"
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t cmask)
st_mode mask significance
S_IRUSR user-read
S_IWUSR user-write
S_IXUSR user-execute
S_IRGRP group-read
S_IWGRP group - write
S_IXGRP group-execution
S_IROTH other - read
S_IWOTH other - write
S_IXOTH other-execution

4.9 chmod and fchmod functions

#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);

4.10 Sticky bits

I don't need it now

4.11 chown, fchown and lchown functions

  • Three functions return: 0 if successful, -1 if error
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);

4.12 File length

  • The member st_size of the stat structure contains the length of the file in bytes. This field is only meaningful for ordinary files, directory files and symbolic links

4.13 File truncation

  • Truncate some data at the end of the file to shorten the file
  • truncate the length to 0, do this with the O_TRUNC flag
  • Both functions return: 0 on success, -1 on error
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *pathname, off_t length);
int ftruncate(int filedes, off_t length);

4.14 File system

insert image description here- Note that in the graph there are two directory entries that only want the same i-node: each i-node has a connection ending, and its value is the number of directory entries that only want the i-node, only when the connection technology is reduced to 0, the This file can be deleted. In the stat structure, the connection count is contained in the st_nlink member, and its basic system data type is nlink_t

  • Another type of connection is called a symbolic link: my understanding of this connection is the shortcut under windows
  • The i node contains all information related to the file: file type, file access permission bit, file length, pointer to the data block occupied by the changed file, etc.; most of the information in the stat structure is taken from the i node;

4.15 link, unlink, remove and rename functions

  • Any file can have multiple directory entries and its i-node. One way to link to an existing file is to use the link function
  • The unlink function can delete an existing directory entry
  • The remove function unlinks a file or directory. For files, the function of remove is the same as unlink. For directories, the function of remove is the same as rmdir.
  • Rename a file or directory with the rename function
#include <unistd.h>
int link(const char *existingpath, const char *newpath);
int unlink(const char *pathname);

#include <stdio.h>
int remove(const char *pathname);
int rename(const char *oldname, const char *newname);

4.17 symlink and readlink functions

  • The symlink function creates a symbolic link

4.18 File time

field illustrate example ls(l) option
st_atime Last access time of file data read -u
st_mtime The last modification time of the file data write default
st_ctime The last modification time of the inode state chmod,chown -c

4.19 utime function

The access and modification time of a file can be changed with the utime function

4.20 mkidr and rmdir functions

Use the mkdir function to create a directory, use the rmdir function to delete a directory

4.21 Reading directory

4.22 chdir, fchdir and getcwd functions

  • The process calls the chdir or fchdir function to modify the current working directory
  • getcwd can get the current directory

Guess you like

Origin blog.csdn.net/u012850592/article/details/103061521