Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to call sys_link in linux system

Shulou Source: shulou.com Published: 2022-06-02 09:41:53 09月25日 Update

This article is to share with you about how to call sys_link in the linux system, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Sys_link is a function that creates hard links, and we can see from the code of this function how hard links work. See the previous article for file lookup and manipulation functions. It won't be posted here.

/ / create a hard link

Int sys_link (const char * oldname, const char * newname)

{

Struct dir_entry * de

Struct m_inode * oldinode, * dir

Struct buffer_head * bh

Const char * basename

Int namelen

/ / find the inode node of the file according to the path name

Oldinode=namei (oldname)

If (! oldinode)

Return-ENOENT

/ / hard links cannot be created for directories

If (S_ISDIR (oldinode- > i_mode)) {

/ / No need to use inode, dereference

Iput (oldinode)

Return-EPERM

}

/ / find out the file names in inode and newname of the last level directory of newname

Dir = dir_namei (newname,&namelen,&basename)

/ / the path does not exist

If (! dir) {

Iput (oldinode)

Return-EACCES

}

/ / the path is a directory, so the file name is empty

If (! namelen) {

Iput (oldinode)

Iput (dir)

Return-EPERM

}

/ / hard links cannot be created across file systems

If (dir- > i_dev! = oldinode- > i_dev) {

Iput (dir)

Iput (oldinode)

Return-EXDEV

}

/ / permission verification

If (! permission (dir,MAY_WRITE)) {

Iput (dir)

Iput (oldinode)

Return-EACCES

}

/ / find an item with a file name equal to basename in the directory

Bh = find_entry (& dir,basename,namelen,&de)

/ / if it is found, it means that the file name already exists, then it cannot be created.

If (bh) {

Brelse (bh)

Iput (dir)

Iput (oldinode)

Return-EEXIST

}

/ / if not, a new directory entry is added. De saves the found directory entry.

Bh = add_entry (dir,basename,namelen,&de)

/ / whether the addition is successful

If (! bh) {

Iput (dir)

Iput (oldinode)

Return-ENOSPC

}

/ / the hard-linked inode is the same as the inode number of the old file

De- > inode = oldinode- > i_num

/ / A new item has been added, and the hard disk needs to be written back.

Bh- > b_dirt = 1

Brelse (bh)

Iput (dir)

/ / add 1 to the number of references. Create a hard link and add an index to the inode node, so you can add one to the number of inode references, and only 0 can delete the file.

Oldinode- > iLinklink +

Oldinode- > i_ctime = CURRENT_TIME

/ / the inode information is updated. You need to write back to the hard disk.

Oldinode- > i_dirt = 1

Iput (oldinode)

Return 0

}

The structure is as follows.

The above is how to call sys_link in the linux system. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Files links directories file names systems functions paths articles more knowledge hard drives articles nodes utility success code information principles that is work will Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux Shulou Tech Info Microsoft MySQL