Apue (4)---files and directories (2)

Source: Internet
Author: User

The Umask function creates a masking word for the process settings file mode and returns the previous value, which is one of the few return functions that have no errors. Where Cmask is composed of several bitwise "or" in 9 constants (S_IR/W/XUSR, S_ir/w/xgrp, S_ir/w/xoth).

#include <sys/stat.h>mode_t umask (mode_t cmask); // return Value: The previous file mode creates a mask word
#include <apue.h>#include<fcntl.h>#defineRWRWRW (s_irusr| s_iwusr| s_irgrp| s_iwgrp| s_iroth| S_iwoth)intMainvoid) {umask (0); if(Creat ("Foo", RWRWRW) <0) {Err_sys ("Create error for Foo"); } umask (S_irgrp| s_iwgrp| s_iroth|S_iwoth); if(Creat ("Bar", RWRWRW) <0) {Err_sys ("Create error for Bar"); } exit (0);}

4-9 Umask Function Instance

Most users of UNIX systems never process their umask values, usually at logon time, set by the shell's startup file, and then change again. Users can set Umask values to control the default permissions for the files they create. Common values for Umask: 000 (any user can read and write files), 002 (prevents other users from writing to your files), 022 (prevents members of the same group and other members from writing to your files), 027 (prevents members of the same group from writing your files and other users reading, writing, or executing your files).

Viii. functions chmod, Fchmod and Fchmodat

#include <sys/stat.h>int chmod (constChar *pathname, mode_t mask); int fchmode (int  FD, mode_t mask); int fchmodeat (intChar *pathname, mode_t mask); // If successful, returns 0; If an error occurs, return-1

These three functions allow us to change the access rights of an existing file, in order to change the permission bit of a file, the process's valid user ID must be equal to the owner ID of the file, or the process must have superuser privileges. It is also important to note that the Chmode function updates the last time that the knowledge I node was changed. By default, ls-l lists the last time the file content was modified

#include <apue.h>intMainvoid){    structstat statbuf; if(Stat ("Foo", &statbuf) <0) {Err_sys ("stat error for Foo"); }    if(Chmod ("Foo", Statbuf.st_mode & ~s_ixgrp| S_isgid) <0) {Err_sys ("chmode error for Foo"); }    if(Chmod ("Bar", s_irusr| s_iwusr| s_irgrp| S_iroth) <0) {Err_sys ("chmode error for Bar"); } exit (0);}

4-12 Chmode Function Instance

X. Functions chown, Fchown, Fchownat and Lchown

 #include <unistd.h>int  chown (const  char  *pathname, uid_t owner, gid_t Group) ; int  Fchown (int   FD, uid_t owner, gid_t Group);  int  Fchownat (int  fd, const  char  *pathname, uid_t owner, gid_t Group, int   flag);  int  Lchown (const  char  *pathname, uid_t owner, gid_t Group); 

These four functions are used to change the user ID and group ID of the file, and if the two parameters ower and any of the group are-1, the corresponding ID will be the same. Lchown and Fchownat (set At_synlink_nofollow) Change the symbolic connection itself instead of changing the owner of the file that the symbolic connection points to.

Xi. file length and file truncation

The stat struct member st_size represents the length of a file in bytes, and this field is only meaningful for normal files, directory files, and symbolic connections. For normal files, the length can be 0; for symbolic connections, the file length is the actual number of bytes in the filename. Most modern UNIX systems today provide fields St_blksize and st_blocks. The first is the fast length that is appropriate for file I/O, and the second is the number of actual 512-byte block blocks allocated. We mention that ordinary files can contain voids, and the actual number of allocated blocks can be viewed using du-s.

Truncating a file to 0 can be opened with the O_TRUNC flag, in order for the stage file to call functions truncate and ftruncate. Truncated if length is less than the length of the current file, or 0 if it is greater than the length of the current file.

#include <unistd.h>int truncate (constChar *pathname, off_t length); int ftruncate (int  FD, off_t length); // If successful, returns 0; if failed, returns-1

12. File System

4-13 disks, partitions, and file systems

We can divide a disk into one or more partitions. Each partition can contain a single file system. The I node is a fixed-length record entry that contains most of the information.

4-14 i nodes and data blocks for more detailed cylinder groups

1. On the way there are two entries pointing to the same ijiedian. Each i node has a connection count whose value is the number of directory entries that point to the I node, which can only be deleted if the connection calculation is reduced to 0 o'clock. This is why lifting a connection to a file does not always mean releasing the disk block that the file occupies. This is why the function of deleting a directory entry is called unlink rather than delete.

2. Another type of connection is called symbolic connection (symbolic link). The actual content of the symbolic connection file (in the data block) contains the name of the file that the regal link points to.

The 3.I node contains all the information about the file: The file type, file access permissions, the length of the file, and a pointer to only the file data block. However, there are two important data that are stored in the catalog entry: The file name and the I node number.

The 4.I node number is pointing to the corresponding I node in the same file system, and one directory entry cannot just think of the I node of another filesystem, which is why the LB command cannot span the file system.

5. When renaming a file without updating the file system, the actual contents of the file are not moved, only a new directory entry that points to the existing I node is constructed, and the old directory entry is deleted, and the connection technology does not change.

13. Function link, Linkat, Unlin, Unlinkat, and remove

 #include <unistd.h>int  link (const  char  *existingpath, const  char  *newpath);  int  Linkat (int  EFD, const  char  *existingpath, int  NFD, const  int   flag);  //  

Creating a connection method that only wants an existing file is to use the link and Linkat functions. When an existing file is a symbolic connection, there is a falg parameter to control whether the Linkat function creates a connection to an existing symbolic connection or creates a connection to the file pointed to by an existing symbolic connection. If the At_symlink_follow flag is set in the flag parameter, a connection to the symbolic connection target is created. Creating a new catalog entry and increasing the connection count should be an atomic operation.

#include <unistd.h>int unlink (constChar *pathname); int unlinkat (intconstcharint flag);

The above two is used to access the connection to the file, and its permission requirements: to own the file, to own the directory, or to have superuser privileges.

#include <apue.h>#include<fcntl.h>intMainvoid){    if(Open ("Tempfile", O_RDWR) <0) {Err_sys ("Open Error"); }    if(Unlink ("Tempfile") <0) {Err_sys ("Unlink Error"); } printf ("file unlinked\n"); Sleep ( the); printf ("done\n"); Exit (0);}

4-16 open a file and then unlink

This feature of unlink is often used by programs to ensure that even when a program crashes, the temporary files it creates are not left behind. The process creates a file with open or creat and then immediately calls unlink because the file is still open, so its contents are not deleted. The contents of the file are deleted only when the process is closed or terminated. If pathname is a symbolic connection, unlink deletes the symbolic connection instead of deleting the file referenced by the connection.

Apue (4)---files and directories (2)

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.