Linux system programming: Open file, read file and write file
This is my first article in what I’m hoping will be a series of articles on system programming for POSIX compliant operating systems with focus on Linux. Actually I’ve touched this topic a while ago when I wrote three articles about library programming on Linux (static libraries, dynamic libraries and dynamic libraries using POSIX API). In this series my goal is to go trough basics of Linux system programming from the easiest topics like open file, read file and file write to a bit more complicated things like Berkeley sockets network programming. So lets get started with environment setup and an example of program that copies source file into destination file using POSIX API system calls to demonstrate open(), read() and write() system calls on Linux operating system.
Configuring your environment
I’ll use my trustworthy Ubuntu Linux operating system but you can actually use any POSIX compliant operating system, the only difference will probably be that you will need to configure your environment differently. What we need to begin with Linux system programming is gcc compiler with related packages and POSIX related man pages. So here’s how to install this packages on Ubuntu based operating system:
Basically that’s all you need to create serious system tools for Linux operating system. Later we will probably need some more libraries but we will install them when necessary.
open(), read() and write() system calls
If you have named this code file sp_linux_copy.c and if you want to name executable file sp_linux_copy to compile this program you would probably use something like this:
Then if your source file is named source_file.txt and if you want to name the destination file destination_file.txt you would run this program like this:
Now lets go trough the code and explain tricky parts. First thing we must do is to include necessary header files. Man page of every system call tells you what header files you need to include to be able to use this system call. Second we will define constant we will use to define size of our buffer in bytes. Smaller buffer size will make our copy process longer but it will save memory. Next we open source and destination file descriptors, source with O_RDONLY to make it read only, destination with O_WRONLY | O_CREAT to make it writable and to create destination file with 0644 file system permission flags. In case of error we use perror() man 3 perror to print relatively user friendly error message.
Now we are ready to start copy process. We run read() and write() inside loop (because source file might be bigger than our buffer) to copy from one file into another. Important to notice is that write() is using number of bytes read from source file returned by read() so it would know how much to write into destination file. If number of bytes read (ret_in) and number of bytes written (ret_out) differ this indicates error so once again we use perror() to print out error description. At the end if all went well we do cleanup by closing both file descriptors and returning 0 (EXIT_SUCCESS) to indicate that program ended without errors.
That’s it for this introductory article on Linux system programming topic. In my next article I will show you few more examples on POSIX input/output and then move on to memory management related system calls.
Источник
Unix / Linux — File System Basics
A file system is a logical collection of files on a partition or disk. A partition is a container for information and can span an entire hard drive if desired.
Your hard drive can have various partitions which usually contain only one file system, such as one file system housing the /file system or another containing the /home file system.
One file system per partition allows for the logical maintenance and management of differing file systems.
Everything in Unix is considered to be a file, including physical devices such as DVD-ROMs, USB devices, and floppy drives.
Directory Structure
Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at the base of the file system and all other directories spreading from there.
A Unix filesystem is a collection of files and directories that has the following properties −
It has a root directory (/) that contains other files and directories.
Each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier, typically called an inode.
By convention, the root directory has an inode number of 2 and the lost+found directory has an inode number of 3. Inode numbers 0 and 1 are not used. File inode numbers can be seen by specifying the -i option to ls command.
It is self-contained. There are no dependencies between one filesystem and another.
The directories have specific purposes and generally hold the same types of information for easily locating files. Following are the directories that exist on the major versions of Unix −
Sr.No.
Directory & Description
1
This is the root directory which should contain only the directories needed at the top level of the file structure
This is where the executable files are located. These files are available to all users
These are device drivers
Supervisor directory commands, configuration files, disk configuration files, valid user lists, groups, ethernet, hosts, where to send critical messages
Contains shared library files and sometimes other kernel-related files
Contains files for booting the system
Contains the home directory for users and other accounts
Used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive and floppy diskette drive, respectively
Contains all processes marked as a file by process number or other information that is dynamic to the system
Holds temporary files used between system boots
Used for miscellaneous purposes, and can be used by many users. Includes administrative commands, shared files, library files, and others
Typically contains variable-length files such as log and print files and any other type of file that may contain a variable amount of data
Contains binary (executable) files, usually for system administration. For example, fdisk and ifconfig utlities
Contains kernel files
Navigating the File System
Now that you understand the basics of the file system, you can begin navigating to the files you need. The following commands are used to navigate the system −
Sr.No.
Command & Description
1
Displays a filename
Moves you to the identified directory
Copies one file/directory to the specified location
Identifies the file type (binary, text, etc)
find filename dir
Finds a file/directory
Shows the beginning of a file
Browses through a file from the end or the beginning
Shows the contents of the directory specified
Creates the specified directory
Browses through a file from the beginning to the end
Moves the location of, or renames a file/directory
Shows the current directory the user is in
Removes a directory
Shows the end of a file
Creates a blank file or modifies an existing file or its attributes
Shows the location of a file
Shows the location of a file if it is in your PATH
You can use Manpage Help to check complete syntax for each command mentioned here.
The df Command
The first way to manage your partition space is with the df (disk free) command. The command df -k (disk free) displays the disk space usage in kilobytes, as shown below −
Some of the directories, such as /devices, shows 0 in the kbytes, used, and avail columns as well as 0% for capacity. These are special (or virtual) file systems, and although they reside on the disk under /, by themselves they do not consume disk space.
The df -k output is generally the same on all Unix systems. Here’s what it usually includes −
Sr.No.
Column & Description
1
The physical file system name
Total kilobytes of space available on the storage medium
Total kilobytes of space used (by files)
Total kilobytes available for use
Percentage of total space used by files
What the file system is mounted on
You can use the -h (human readable) option to display the output in a format that shows the size in easier-to-understand notation.
The du Command
The du (disk usage) command enables you to specify directories to show disk space usage on a particular directory.
This command is helpful if you want to determine how much space a particular directory is taking. The following command displays number of blocks consumed by each directory. A single block may take either 512 Bytes or 1 Kilo Byte depending on your system.
The -h option makes the output easier to comprehend −
Mounting the File System
A file system must be mounted in order to be usable by the system. To see what is currently mounted (available for use) on your system, use the following command −
The /mnt directory, by the Unix convention, is where temporary mounts (such as CDROM drives, remote network drives, and floppy drives) are located. If you need to mount a file system, you can use the mount command with the following syntax −
For example, if you want to mount a CD-ROM to the directory /mnt/cdrom, you can type −
This assumes that your CD-ROM device is called /dev/cdrom and that you want to mount it to /mnt/cdrom. Refer to the mount man page for more specific information or type mount -h at the command line for help information.
After mounting, you can use the cd command to navigate the newly available file system through the mount point you just made.
Unmounting the File System
To unmount (remove) the file system from your system, use the umount command by identifying the mount point or device.
For example, to unmount cdrom, use the following command −
The mount command enables you to access your file systems, but on most modern Unix systems, the automount function makes this process invisible to the user and requires no intervention.
User and Group Quotas
The user and group quotas provide the mechanisms by which the amount of space used by a single user or all users within a specific group can be limited to a value defined by the administrator.
Quotas operate around two limits that allow the user to take some action if the amount of space or number of disk blocks start to exceed the administrator defined limits −
Soft Limit − If the user exceeds the limit defined, there is a grace period that allows the user to free up some space.
Hard Limit − When the hard limit is reached, regardless of the grace period, no further files or blocks can be allocated.
There are a number of commands to administer quotas −
Sr.No.
Command & Description
1
Displays disk usage and limits for a user of group
This is a quota editor. Users or Groups quota can be edited using this command
Scans a filesystem for disk usage, creates, checks and repairs quota files
This is a command line quota editor
This announces to the system that disk quotas should be enabled on one or more filesystems
This announces to the system that disk quotas should be disabled for one or more filesystems
This prints a summary of the disc usage and quotas for the specified file systems
You can use Manpage Help to check complete syntax for each command mentioned here.
Источник
Linux file system programming
man 2 open
Open’s system call
Open calls must specify one of the file access modes as shown below:
Open calls can include combinations of the following optional modes in the Flags parameters:
O_Append: Each time the write data is added to the end of the file.
O_Trunc: When the property is turned on, if this file is originally content, and it is only 0 for only the only or only write success.
O_CREAT: If necessary, create a file by the access mode given in the parameter mode.
O_EXCL: Calls with O_CREAT to ensure that the caller creates a file. Using this mode prevents two programs from creating a file at the same time, if the file already exists, the Open call will fail.
For other Flags values that may have, please see the Open’s call manual.
Mode: When using the Open of the O_CREAT flag to create a file, we must use Open calls in three parameter formats. The third parameter Mode is obtained after a bit of the flag. they are:
S_Irusr: Reading Permissions, the file owner.
S_IWUSR: Write permissions, file owner.
S_ IXUSR: Execute Permissions, the file owner.
S_IRGRP: Reading permissions, groups of files.
S_IWGRP: Write permissions, group of files.
Mainly for the use of O_EXCL, O_EXCL: Calls with O_CREAT to ensure that the caller creates a file. Using this mode prevents two programs from creating a file at the same time, if the file already exists, the Open call will fail.
O_Append: Each time the write data is added to the end of the file. It means that in the original content File1 (132123132123123132) increases the content you want (Ganboss Is Very Handsome)
O_trunc: When the property is turned on, if there is content in this file, and for only the only or only write success, it is cut off from 0. All the content cleans up to Test!
File writing operation
man 2 write
Write system call
CLOSE system call
The Close system call is used to «Close» a file, and the Close call Terminates a file descriptor Fildes to associate between its files. The file descriptor is released and can be reused.
Close successfully returns 1. Error returns -1.
#Include
int close(int fildes);
READ system call
The result of the operation is:
Re-open method: This is to open the Open folder and then turn off the close folder and open the Open to let the cursor to go to the front to read the previous content. If it is not like this, the cursor is behind the string. By the change of the sparkle position of the cursor
File cursor mobile operation
Solve the above method: to rewind the cursor
Write the file to write the pointer relative to when WHENCE mobile OFFSET bytes (Offset’s offset value)
The result of the operation is:
The result of the operation is:
Creat system call
The result of the operation is:Let A.out become -RWXR-XR-X