File system programming (i)

Source: Internet
Author: User

★ What is File system

A file system is the organization of files stored on a storage device such as a disk. The implementation of Linux file system adopts layered architecture, including the file user interface layer, file system, device driver, etc.

★ What knowledge is included in file system programming

◇ The basic operation of the file. including creation and opening of files, reading and writing of files, closing of files

◇ properties of the file. Including the acquisition and setting of file attributes

◇ operation of the catalog file. Include Create, delete, open, close, read, get or change current working directory, etc.

★ Basic operation of the file◇ Opening and creation of files
1. Opening and creation of files you can use an open function, which has two forms:

#include <fcntl.h>

#include <sys/stat.h>

#include <sys/types.h>

int open (const char *filename, int flags);

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

2. Introduction of Open function parameters

◇filename: The name of the file to be created. If there is only a file name, then the program is in the same directory as this file name.

Routines:

Compile and run the program to get:

You can get the compiled executable file creat_file with the test created under the same directory. What if you want to create a file under the specified directory?

Just add the specified directory to the file! Here's what the above routine modifies:

The above three files are placed under the/home/system_program directory:

Compile the modified program to run the resulting:

◇Flags: The way to open a file

1. O_rdonly: Open in read-only mode

2, O_wronly: Open in a write-only way

3. O_RDWR: Open in Read and write mode

4. O_append: Open file in Append mode

5, O_creat: If the file does not exist, then create a file and use the parameter mode to set permissions

6. O_exec : If O_creat is used, but the file already exists, return error message

7, O_noblock: Open the file in a non-blocking way

8. O_trunc: If the file already exists, delete its original data before writing the data

◇mode: Setting access permissions for a file

1, S_IRUSR: The document belongs to the main readable

2, S_IWUSR: The document belongs to the master can write

3, S_IXUSR: The file is the main executable

4, S_irwxu: The document is the master can read, write, execute

5. S_IRGRP: The document belongs to the group readable

6, S_iwgrp: Documents belong to the group can be written

7, S_ixgrp: File group executable

8, S_irwxg: Documents belong to the group can read, write, execute

9, S_iroth: Other users can read

10, S_iwoth: Other users can write

11, S_irwxo: Other users can read, write, execute

12, S_isuid: Set the file owner's execution ID

13. S_isgid: Set the execution ID of the file group

3. Relationship between Umask value and file permissions

or This program, when we compile and run it to get Test1 file, from the above can be seen test1 This file permissions are read, write, executable. However, when viewing file permissions

you can see that test1 's permissions are not the permission settings you want in the open function, what is the cause of this?

In fact, this is related to the value of umask. Umask represents some of the access bits that a file or directory needs to be masked when it is created. When a process creates a file, the actual access permission for the file is determined by the mode& (~umask) formula.

0022 This value indicates that the created file will automatically block write permissions for the group and write permissions for other groups. Because in Linux file permissions 4-readable, 2-writable, 1-executable.

So what do you do to modify this value?

After you re-create the file:

File system programming (i)

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.