The function link, linkat, unlink, Unlinkat, and remove instance programs open a file and then unlink it. The process that executes the program then sleeps for 30 seconds and then terminates.
#include "apue.h"
#include <fcntl.h>
int
main(void)
{
if (open("tempfile", O_RDWR) < 0)
{
err_sys("open error");
}
if (unlink("tempfile") < 0)
{
err_sys("unlink error");
}
printf("file unlinked\n");
sleep(30);
printf("done\n");
exit(0);
}
$
ls-l tempfile
-rw-rw-r--1 fireway fireway 1344111767 October 21:17 tempfile
$
DF /home/
File system 1k-blocks used available% mount point
/dev/sda1 151650468 29081848 114842132 21%/
$
./unlink &
[1] 4456
$ file unlinked
ls-l tempfile
LS: Unable to access tempfile: No file or directory
$
df/home
File system 1K-BL Ocks used available% mount point
/dev/sda1 151650468 29081848 114842132 ; 21%/
$ done
df/home
File system 1k-blocks used Used% mount point
/dev/sda1 151650468 27769232 116154748 20%/
The process creates a file with open or creat, and then calls unlink because the file is still open, so it does not delete its contents. The contents of the file are deleted only if the process closes the file or terminates. The link between hard links and soft links and different files have filenames and data, which are divided into two sections on Linux: User data and metadata (metadata). User data, which is the file block (data block), where the data block is the real content of the file, the metadata is the attached property of the file, such as file size, creation time, owner, pointer to the file data block, etc. in Linux, the inode number (or I-node number) in the metadata is The unique identity of the file, not the filename. The file name is only for the convenience of people's memory and use, the system or program through the inode number to find the correct file data block. Figure 1 shows the process by which a program obtains the contents of a file by its filename. Figure 1. Get file contents by file name view inode numbers in a Linux system use command stat or ls-i
$
Stat CHOWN_RESTRICTED.C
File: "CHOWN_RESTRICTED.C"
Size: 1625 Block: 8 IO block: 4096 normal file
Device: 801h/2049d inode:1707228 hard Link: 1
Permissions: (0777/-RWXRWXRWX) Uid: (1000/fireway) Gid: (1000/fireway)
Last visited: 2016-10-23 15:48:00.106111057 +0800
Last modified: 2016-10-23 15:47:56.638093860 +0800
Last modified: 2016-10-23 15:47:56.686094098 +0800
Creation Time:-
$
MV CHOWN_RESTRICTED.C MYCHOWN_RESTRICTED.C
$
Stat MYCHOWN_RESTRICTED.C
File: "MYCHOWN_RESTRICTED.C"
Size: 1625 Block: 8 IO block: 4096 normal file
Device: 801h/2049d inode:1707228 hard Link: 1
Permissions: (0777/-RWXRWXRWX) Uid: (1000/fireway) Gid: (1000/fireway)
Last visited: 2016-10-23 15:48:00.106111057 +0800
Last modified: 2016-10-23 15:47:56.638093860 +0800
Last modified: 2016-10-23 16:39:02.469296496 +0800
Creation Time:-
Using the command MV to move and rename the file, the result does not affect the file's user data and inode number for the shared use of the solution file, the Linux system introduces two kinds of links: Hard link and soft link (also known as symbolic link, symbolic links). Link for Linux system to solve the shared use of files, but also bring hidden file paths, increase permissions security and save storage and other benefits. Hard links If an inode number corresponds to more than one file name, these files are called hard links. In other words, a hard link is the same file that uses multiple aliases. Hard links can be made by command
LinkOr
LNCreate. as follows, create a hard link to the file oldfile.
Link Oldfile newfile
ln oldfile newfile
Because a hard link is a file with the same inode number with only a different file name, hard links have the following characteristics:
-
- You can only create files that already exist,
-
- Cannot create a directory, only the file can be created;
- Deleting a hard-link file does not affect other files that have the same inode number.
$
Ls-li
Total usage 0
$
link old.file hard.link <-------------------can only Create a hard connection to an existing file
Link: Could not create link "hard.link" to "Old.file": No file or directory
$
echo "This is an original file" > old.fi Le
$
stat old.file
file: "Old.file"
Size: + & nbsp BLOCK: 8 io block: 4096 Normal file
Device: 801h/2049d ino de:2371939 Hard Links: 1
Permissions: (0664/-rw-rw-r--) uid: (1000/fireway) Gid: (1000/fireway)
Recent visits: 2016-10-23 16:52:51.873409289 +0800
Last modified: 2016-10-23 16:52:51.873409289 +0800
Last modified: 2016-10-23 16:52:51.873409289 +0800
Creation Time:-
$
link old.file hard.link | ls-li
Total usage 8
2371939-RW-RW -r--2 Fireway fireway 25 October 16:52 hard.link
2371939-rw-rw-r--2 fireway fireway 25 October all 16:52 old.file
The file old.file has the same inode number as the Hard.link:2371939 and file permissions, the Inode exists as the file exists, so you can create a hard link only if the file exists.
$
ln/dev/input/event5 mylink
LN: Unable to create hard link "mylink" + "/DEV/INPUT/EVENT5": Invalid Cross device connection
$
LN Temp/mylink
ln: "temp/": Do not allow hard links to be directed to the directory
When the inode exists and the link count is not 0 o'clock. The inode number is unique only under each file system, and when Linux mounts multiple file systems, there is a duplication of inode numbers, so hard links cannot be created across file systems. Soft links If the contents of the file user data block is the path name of another file, then the file is a soft link soft links is a normal file, but the data block content is a bit special. Soft links have their own inode numbers and user data blocks. Soft links have the following features:
- soft links have their own file attributes and permissions, etc.
- A soft link can be created on a nonexistent file or directory;
- soft-link cross-file system;
- Soft links can be created on a file or directory;
- When you create a soft link, the link count is not incremented;
- Deleting a soft link does not affect the file being pointed to, but if the original file being pointed to is deleted, then the related soft connection is called a dead link (that is, dangling link, if the point to the path file is re-created, The dead link can revert to a normal soft link).
$
Ls-li
Total usage 0
$
ln-s old.file soft.link
$
ls-lif br> Total dosage 0
2371941 lrwxrwxrwx 1 fireway fireway 8 October 17:13 soft.link-old.file
$
cat soft.link <-------- Because the file being pointed to does not exist, the soft link soft.link is the dead link
Cat:soft.link: There is no file or directory
$
Echo "This was an original file_a" >> old.file
$
Cat soft.link
Original File_a
$
ln-s old.dir soft.link.dir <--------------------Create a soft link to a nonexistent directory
$
ll
Total Dosage 8
Drwxrwxr-x 2 Fireway fireway 4096 October 23 17:18./
Drwxr-xr-x fireway fireway 4096 October 23 17:18.. /
lrwxrwxrwx 1 fireway fireway 7 October 17:18 Soft.link.dir-Old.dir
$
tree.-F--inodes
.
├──[2371945] old.dir/
│└──[2371946] test/
└──[2371945] Soft.link.dir-old.dir/
3 directories, 0 files
Link-related commands view the file system types that are currently suspended in Linux, in addition to the command DF used above, you can also use the command mount or view the file/proc/mounts.
$
Mount
/DEV/SDA1 on/type Ext4 (Rw,errors=remount-ro)
Proc On/proc type proc (rw,noexec,nosuid,nodev)
Sysfs On/sys type SYSFS (Rw,noexec,nosuid,nodev)
.......
.......
Gvfsd-fuse ON/RUN/USER/112/GVFS type Fuse.gvfsd-fuse (RW,NOSUID,NODEV,USER=LIGHTDM)
Command
lsOr
StatHelps us differentiate between soft links and other files and view file Inode numbers, but a better way to use the Find command is not only to find soft links to a file, but also to find all the hard links in the same inode
$
Find ~-lname old.file <-------------find soft links under path ~ File Old.file
/home/fireway/study/temp2/soft.link
$
Find ~-samefile ~/study/temp/old.file <-----------view Path ~ All hard links with the same inode
/home/fireway/study/temp/old.file
/home/fireway/study/temp/hard.link
$
find/home-inum 2371939
/home/fireway/study/temp/old.file
/home/fireway/study/temp/hard.link
$
Find/home/fireway/study/temp2-type l-ls <----list all soft link files under path/home/fireway/study/temp2
2371941 0 lrwxrwxrwx 1 fireway fireway 8 October 17:13/home/fireway/study/temp2/soft.link old.file
Linux VFS Linux has an extremely rich file system, which can be broadly divided into the following categories:
- Network file system, such as NFS, CIFS, etc.;
- Disk file systems, such as EXT4, ext3, etc.;
- Special file systems, such as Proc, SYSFS, Ramfs, TMPFS, etc.
The basis for implementing these file systems and coexistence under Linux is that the Linux VFS (Virtual file System) VFS, as a common file system, abstracts the four basic concepts of file systems: files, directory entries (dentry), index nodes (inode), and Mount point, which provides the interface to the file system of the user space layer in the kernel, 2 figure 2. VFS's architecture in the system VFS implements a system tune such as Open (), read (), and enables user-space programs such as CP to cross file systems. VFS truly implements the above: Everything is a file except for the process in Linux. VFS has four basic objects: The Superblock object, the Index node objects (Inode object), the Catalog item object (Dentry object), and the file object.
- The Super Block object represents an installed file system;
- The Index node object represents a file;
- A Catalog item object represents a catalog item, such as a device file event5 in Path/DEV/INPUT/EVENT5, which has four directory entry objects:/, dev/, input/, and EVENT5.
- A file object represents a file that is opened by a process.
These four objects are related to the process and disk files 3., where D_inode is a hard link. For fast parsing of file paths, Linux VFS Designs Directory Item caches (directory Entry cache, or Dcache). Figure 3. Processing between the objects of the VFS
Refer to understanding hard links and soft links for Linux http://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/#fig2UNIX环境高级编程 (third edition) 4.15 Functions link, Linkat, unlink, Unlinkat, and remove
Functions link, Linkat, unlink, Unlinkat, and remove