File Management-hard links to files

Source: Internet
Author: User
Tags symlink

1. Create a hard link

 

# Include <unistd. h>
Define the function int Link (const char * oldpath, const char * newpath );
Function Description Link () uses the name specified by the newpath parameter to create a new connection (hard connection) to the existing file specified by the oldpath parameter. If the newpath parameter is specified as an existing file, no connection is established.
If the return value is successful, 0 is returned. If the return value is failed,-1 is returned. The cause of the error is errno.
Note that the hard connection established by Link () cannot span different file systems. Use symlink () if necessary ().
Error Code
The exdev parameter oldpath and newpath are not created in the same file system.
The eperm parameter oldpath and the file system specified by newpath do not support hard connection.
Erofs files exist in the read-only File System
The efault parameter oldpath or newpath pointer exceeds the accessible memory space.
The oldpath or newpath parameter of enametollong is too long.
Insufficient enomem core memory
The file name specified by the eexist parameter newpath already exists.
The emlink parameter oldpath indicates that the maximum number of connections has been reached.
The pathname of the eloop parameter has too many symbolic connections.
The enospc file system has insufficient space.
Eio I/O access error.

 

 

# Include <unistd. h>
Define the function int symlink (const char * oldpath, const char * newpath );
Function Description symlink () uses the name specified by newpath to create a new connection (symbolic connection) to the existing file specified by oldpath. The file specified by the oldpath parameter does not necessarily exist. If the newpath parameter is named as an existing file, no connection is established.
If the return value is successful, 0 is returned. If the return value is failed,-1 is returned. The cause of the error is errno.
The error code eperm parameter oldpath does not support symbolic connections to the file system specified by newpath.
The file to be tested with write permission by erofs exists in the read-only file system.
The efault parameter oldpath or newpath pointer exceeds the accessible memory space.
The oldpath or newpath parameter is too long.
Insufficient enomem core memory
The file name specified by the eexist parameter newpath already exists.
The emlink parameter oldpath indicates that the maximum number of connections has been reached.
The pathname of the eloop parameter has too many symbolic connections.
The enospc file system has insufficient space.
Eio I/O Access Error

 

 

 

2. delete a hard link

 

# Include <unistd. h>
Define the function int unlink (const char * pathname );
Function Description
Unlink () deletes the file specified by the pathname parameter. If the file name is the last connection point, but other processes open the file, it will be deleted after all the file descriptions about the file are closed. If the pathname parameter is a symbolic connection, the connection is deleted.
If the return value is successful, 0 is returned. If the return value is failed,-1 is returned. The cause of the error is errno.
Error Code: The erofs file exists in the read-only file system.
The pathname pointer of the efault parameter exceeds the accessible memory space.
The pathname parameter of enametoolong is too long.
Insufficient enomem core memory
The pathname of the eloop parameter has too many symbolic connections.
Eio I/O Access Error

 

 

3. Example

 

// Hardlink. c

# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <string. h>

/* Buffer size */
# Deprecision Max 1024

Int main (void)
{
Int FD;
Struct stat statbuf;
Char Buf [Max];
Int N;

If (STAT ("test.txt", & statbuf) =-1) {/* Get the status of the target file */
Perror ("fail to get status ");
Exit (1 );
}

/* Number of links to the printed file */
Printf ("test.txt, the number of links is: % d/N", statbuf. st_nlink );

/* Create a file named test2.txtin the current directory, and the file shares an I node with test.txt */
If (Link ("test.txt", "test2.txt") =-1 ){
Perror ("fail to link ");
Exit (1 );
}

/* Obtain the status of the test.txt file again, and the number of links has been updated */
If (STAT ("test.txt", & statbuf) =-1 ){
Perror ("fail to get status ");
Exit (1 );
}

Printf ("test.txt, the number of links is: % d/N", statbuf. st_nlink );

/* The test2.txtfile state is obtained. The structure of this state is test.txt */
If (STAT ("test2.txt", & statbuf) =-1 ){
Perror ("fail to get status ");
Exit (1 );
}

Printf ("test2.txt, the number of links is: % d/N", statbuf. st_nlink );
Printf ("/N ");

If (FD = open ("test.txt", o_rdwr) =-1) {/* open the file test.txt */
Perror ("fail to open ");
Exit (1 );
}

Strcpy (BUF, "Hello World");/* Copy string */

If (n = write (FD, Buf, strlen (BUF) =-1) {/* output the string "Hello World "*/
Perror ("fail to write ");
Exit (1 );
}

Close (FD);/* close the file and write the output string to the disk file */

If (FD = open ("test2.txt", o_rdwr) =-1) {/* Open the test2.txt file */
Perror ("fail to open ");
Exit (1 );
}

If (n = read (FD, Buf, n) =-1) {/* read the content of the file */
Perror ("fail to read ");
Exit (1 );
}
Buf [N] = '/0';/* Add the string end sign to facilitate printing */

Printf ("content of test2.txt is: % s/n", Buf);/* output content in the test2.txt file */

Close (FD );

/* Delete the directory item test2.txt, but its disk file is not affected,
* You can still use another link test.txt to reference this file.
*/
If (unlink ("test2.txt") =-1 ){
Perror ("fail to unlink ");
Exit (1 );
}

If (STAT ("test.txt", & statbuf) =-1) {/* get to the status of the test.txt file */
Perror ("fail to get status ");
Exit (1 );
}

Printf ("test.txt, the number of links is: % d/N", statbuf. st_nlink);/* print the File Link count */

/* Open the test.txt file to prevent the file from being deleted by the system */
If (FD = open ("test.txt", o_rdwr) =-1 ){
Perror ("fail to open ");
Exit (1 );
}

If (unlink ("test.txt") =-1) {/* Now the reference count of this file is 0 */
Perror ("fail to unlink ");
Exit (1 );
}

If (fstat (FD, & statbuf) =-1) {/* get the file status */
Perror ("fail to get status ");
Exit (1 );
}

Printf ("test.txt, the number of links is: % d/N", statbuf. st_nlink );
Printf ("/N ");

/* Because the file is still open, all the files can still be applied.
*/
If (n = read (FD, Buf, n) =-1 ){
Perror ("fail to read ");
Exit (1 );
}
Buf [N] = '/0 ';

Printf ("content of test.txt is: % s/n", Buf);/* output result */

Close (FD);/* close the file */

Return 0;
}

 

This example also shows that even if the number of links to a file is already 0, the program can still reference the file as long as the file is opened. This includes reading and writing files, but the read and write operations are actually performed on the buffer zone in the memory. When the file is closed, the buffer in the memory disappears and the file disappears.

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.