Linux File System
Linux supports multiple file systems, such as Ext, ext2, minix, iso9660, msdos, fat, vfat, and NFS. On the upper layer of these specific file systems, Linux provides a Virtual File System (VFS) to unify their behavior, the Virtual File System provides the same interface for communication between different file systems and the kernel.
On the Linux platform, two types of functions can be used for file programming: (1) Linux operating system file API; (2) c language I/O library function. The former depends on Linux calls, and the latter is actually independent from the operating system, because in any operating system, the methods for operating files using C language I/O library functions are the same. This chapter provides examples of these two methods.
1. Linux File API
The file operation API of Linux involves creating, opening, reading, writing, and closing files.
Create
Int creat (const char * filename, mode_t mode );
The mode parameter specifies the access permission for the new file. It determines the final permission of the file (Mode & umask) together with umask. umask indicates some access permissions that need to be removed during file creation.
Umask can be changed by calling umask:
Int umask (INT newmask );
This call sets umask as newmask and returns the old umask, which only affects read, write, and execution permissions.
Open
Int open (const char * pathname, int flags );
Int open (const char * pathname, int flags, mode_t mode );
The open function has two forms, in which pathname is the name of the file we want to open (including the path name, which is considered to be under the current path by default ), flags can be used to combine the following values or values:
Flag
Description
O_rdonly
Open a file in read-only mode
O_wronly
Open a file in write-only mode
O_rdwr
Open a file in read/write mode
O_append
Open an object in append Mode
O_creat
Create a file
O_exec
If o_creat is used and the file already exists, an error occurs.
O_noblock
Open a file in non-blocking mode
O_trunc
If the file already exists, delete the file content.
O_rdonly, o_wronly, and o_rdwr can only use any one of them.
If the o_create flag is used, the following functions are used:
Int open (const char * pathname, int flags, mode_t mode );
In this case, we also need to specify the mode flag to indicate the object access permissions. Mode can be a combination of the following conditions:
Flag
Description
S_irusr
Users can read
S_iwusr
Users can write
S_ixusr
Users can execute
S_irwxu
Users can read, write, and execute
S_irgrp
Group readable
S_iwgrp
Group Writable
S_ixgrp
Group executable
S_irwxg
The Group can be read and written for execution.
S_iroth
Others can read
S_iwoth
Others can write
S_ixoth
Others can execute
S_irwxo
Others can read, write, and execute
S_isuid
Set User execution ID
S_isgid
Set the execution ID of the Group
In addition to the above macro "or" logic to generate a flag, we can also use numbers to represent the various permissions of the file. Linux uses a total of five numbers to represent the permissions of the file: the first digit indicates the user ID, the second digit indicates the group ID, the third digit indicates the user's own permission, the fourth digit indicates the Group permission, and the last digit indicates the permissions of others.
Each number can be 1 (Execution permission), 2 (write permission), 4 (read permission), 0 (none), or the sum of these values. For example, to create a readable, writable, and executable user, but the group has no permissions, other users can read and execute files, and set the user ID. Then, we should use 1 (Set User ID), 0 (do not set group ID), 7 (1 + 2 + 4, read, write, and execute) 0 (no permission), 5 (1 + 4, read and execute), that is, 10705: open ("test", o_creat, 10705 );
The preceding statement is equivalent:
Open ("test", o_creat, s_irwxu | s_iroth | s_ixoth | s_isuid );
If the file is successfully opened, the open function returns a file descriptor. All subsequent operations on the file can be performed by operating on the file descriptor.
Read/write
After the file is opened, we can read and write the file. in Linux, the Read and Write Functions are called by the system:
Int read (int fd, const void * Buf, size_t length );
Int write (int fd, const void * Buf, size_t length );
The parameter Buf is the pointer to the buffer, and the length is the buffer size (in bytes ).
The read () function reads length bytes from the file specified by the file descriptor FD to the buffer zone pointed to by the BUF. The returned value is the actual number of bytes read.
Function write writes the Length byte from the buffer to which the Buf points to the file pointed by the file descriptor FD. the return value is the actual number of bytes written.
Open marked with o_creat actually implements the file creation function. Therefore, the following function is equivalent to the creat () function:
Int open (pathname, o_creat | o_wronly | o_trunc, mode );
Positioning
For random files, we can read and write at random locations and use the following functions to locate them:
Int lseek (int fd, offset_t offset, int whence );
Lseek () moves the file read/write pointer to the offset byte relative to whence. When the operation is successful, the position of the returned file pointer relative to the file header.
The whence parameter can use the following values:
Seek_set: Start of relative file
Seek_cur: Current Position of the relative file read/write pointer
Seek_end: relative to the end of the file
The offset value can be a negative value. For example, the following call can move the file Pointer Forward to the current position by five Bytes: lseek (FD,-5, seek_cur );
Because the return value of the lseek function is the position of the file pointer relative to the file header, the following returned values are the length of the file: lseek (FD, 0, seek_end );
Close
After the operation is complete, we need to close the file. We only need to call close. FD is the file descriptor we want to close:
Int close (int fd );
Routine: compile a program, create a user-readable file named hello.txt in the current directory, write "Hello, software Weekly" in it, and close the file. Open the file again, read the content and output it on the screen.
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <stdio. h>
# Define length 100
Main ()
{
Int FD, Len;
Char STR [length];
FD = open ("hello.txt", o_creat | o_rdwr, s_irusr | s_iwusr);/* Create and open a file */
If (FD)
{
Write (FD, "Hello, software Weekly", strlen ("Hello, software Weekly");/* write the hello, software weekly string */
Close (FD );
}
FD = open ("hello.txt", o_rdwr );
Len = read (FD, STR, length);/* Read File Content */
STR [Len] = '"0 ';
Printf ("% s" N ", STR );
Close (FD );
};
Compile, run, and execute
2. C language library functions
The file operations of C-library functions are actually independent from the specific operating system platform. These functions are used in DOS, windows, Linux, and VxWorks:
Create and open
File * fopen (const char * path, const char * mode );
Fopen () is used to open the specified file filename. The mode is the open mode. The following table lists the open modes supported in C:
Flag
Description
R, Rb
Open in read-only mode
W, WB
Open in write-only mode. If the file does not exist, create the file. Otherwise, the file is truncated.
A, AB
Open in append mode. If the file does not exist, create the file.
R +, R + B, RB +
Open in read/write mode
W +, W + B, Wh +
Open in read/write mode. If the file does not exist, create a new file. Otherwise, the file is truncated.
A +, A + B, AB +
Open in read and append mode. If the file does not exist, create a new file.
B is used to distinguish between binary files and text files, which are differentiated in DOS and Windows systems, but Linux does not distinguish between binary files and text files.
Read/write
The C-database functions support reading and writing files in a certain format in units of characters and strings. These functions are:
Int fgetc (File * stream );
Int fputc (int c, file * stream );
Char * fgets (char * s, int N, file * stream );
Int fputs (const char * s, file * stream );
Int fprintf (File * stream, const char * format ,...);
Int fscanf (File * stream, const char * format ,...);
Size_t fread (void * PTR, size_t size, size_t N, file * stream );
Size_t fwrite (const void * PTR, size_t size, size_t N, file * stream );
Fread () reads n fields from the stream, each of which is a size byte, and puts the read fields into the character array specified by PTR, returns the number of actually read fields. When the number of fields to be read is smaller than num, an error may occur during function calling or the end of the file. Therefore, you must call feof () and ferror () to determine.
Write () writes n fields from the array referred to by the buffer PTR to the stream. Each field is in size and returns the number of actually written fields.
In addition, the C-database functions provide positioning capabilities in the read/write process, including
Int fgetpos (File * stream, fpos_t * POS );
Int fsetpos (File * stream, const fpos_t * POS );
Int fseek (File * stream, long offset, int whence); and so on.
Close
Using the C library function to close a file is still a simple operation:
Int fclose (File * stream );
Routine: Use the C library function to implement the routine in section 2nd.
# Include <stdio. h>
# Define length 100
Main ()
{
File * FD;
Char STR [length];
FD = fopen ("hello.txt", "W +");/* Create and open a file */
If (FD)
{
Fputs ("Hello, software Weekly", FD);/* write the hello, software weekly string */
Fclose (FD );
}
FD = fopen ("hello.txt", "R ");
Fgets (STR, length, FD);/* Read File Content */
Printf ("% s" N ", STR );
Fclose (FD );
}
4. Summary
The Virtual File System provided by Linux provides a unified interface for a variety of file systems. There are two methods for file programming in Linux: Calling Based on Linux systems and C-library functions. These two types of programming involve file operations such as creating, opening, reading, writing, and closing, and locating random files. This chapter provides examples of both programming methods.