Special file-Symbolic Link operation

Source: Internet
Author: User
Tags lstat symlink

1. Create a symbolic link

 

# 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 have to exist.
(If it does not exist, the symbolic link can also be created, but it is rough when the symbolic link is used to read or write files ), 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.
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. Read and Write the target file of the symbolic link

 

You can operate on a linked file like a normal file. In this case, the actual operation is the file pointed to by the symbolic link.

 

3. Read and Write the target file with multiple Symbolic Links

 

When a symbolic connection is created, the Read and Write Functions keep track of the symbolic link until the actual target file is encountered. Therefore, even if multiple symbolic links exist, you can perform any operation on the link chain to read and write the actual files.

 

Example:

// Mul_link.c

# Include <stdio. h>
# Include <unistd. h>
# Include <stdlib. h>
# Include <string. h>
# Include <fcntl. h>

# Deprecision Max 1024

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

If (symlink ("test.txt", "SL") =-1) {/* Create a symbolic link, pointing to the test.txt file */
Perror ("fail to create symbol link ");
Exit (1 );
}

/* Create a symbolic link again to point to SL. This forms a symbolic link chain: sl2-> sl-> test.txt
* The test.txt file is the destination file and the key of the symbolic link chain.
*/
If (symlink ("SL", "sl2") =-1 ){
Perror ("fail to create symbol link ");
Exit (1 );
}

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

Printf ("already create symbol-links/N ");

If (n = read (FD, Buf, max) =-1) {/* Read File Content */
Perror ("fail to read ");
Exit (1 );
}
Buf [N] = '/0 ';

Printf ("file content is: % s/n", Buf);/* output result */

Strcat (BUF, ", admin ");

If (write (FD, Buf, strlen (BUF) =-1) {/* write content to this file */
Perror ("fail to write ");
Exit (1 );
}

Printf ("done/N");/* output prompt information */

Close (FD );

Return 0;
}

 

 

4. read/write Symbolic Links

A symbolic link is a type of file, and such files are saved by themselves. The symbolic link stores the path of the target file, which is a string.

To read the content of a symbolic link, you can use the following functions:

 

# Include <unistd. h>
Define the function int readlink (const char * path, char * Buf, size_t bufsiz );
Function Description readlink () stores the symbolic connection content of the path parameter in the memory space specified by the Buf parameter. The returned content does not end with null, but returns the number of characters of the string. If the bufsiz parameter is smaller than the content length of the symbolic connection, the content that is too long will be truncated.
If the return value is successful, the path string of the file indicated by the symbolic connection is passed. If the return value fails,-1 is returned. The error code is stored in errno.
The error code eaccess is rejected when obtaining the file, and the permission is insufficient.
The value of the einval parameter bufsiz is negative.
Eio I/O access error.
The file to be opened by eloop has too many symbolic connections.
The path name of the enametoolong parameter is too long.
The file specified by the enoent parameter path does not exist.
Insufficient enomem core memory
The directory in the path of the enotdir parameter exists but is not a real directory.

 

The string in the Buf buffer does not end with '/0'. If necessary, add it by yourself.
.

 

 

Example:

// Readlink. c

# Include <stdio. h>

# Include <unistd. h>

# Include <stdlib. h>

 

# Deprecision Max 1024

 

Int main ()

{

Char Buf [Max];

Int FD;

 

If (syslink ("root/Alei/Linux/code/test.txt", "S1") =-1 ){

Perror ("fail to create symbol-link ");

Exit (1 );

}

 

If (syslink ("S1", "S2") =-1 ){

Perror ("fail to create symbol-link ");

Exit (1 );

}

 

If (readlink ("S1", Buf, max) =-1 ){

Perror ("fail to read link ");

Exit (1 );

}

Printf ("% s/n", Buf );

 

If (readlink ("S1", Buf, max) =-1 ){

Perror ("fail to read link ");

Exit (1 );

}

Printf ("% s/n", Buf );

 

Return 0;

}

 

If a nonexistent file is used as the target file of the symbolic link, the readlink function can also read the content of the symbolic link, because the readlink function is fond of reading content in the database of the symbolic link, the content is a path name, but the file on the path endpoint does not exist.


5. Get the symbolic link status

A symbolic link is a special file that also has its own State.

 

// Sys_stat.c

# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/STAT. h>

Int main (void)
{
Struct stat Buf;/* store File status information */

If (STAT ("SL", & BUF) =-1 ){
Perror ("fail to stat ");
Exit (1 );
}

Printf ("permission: % d/N", Buf. st_mode);/* print the File Permission word */
Printf ("inode number: % d/N", Buf. st_ino);/* print the I node number */
Printf ("device number: % d/N", Buf. st_dev);/* print the file system device Number */
Printf ("R-device number: % d/N", Buf. st_rdev);/* print the hardware device Number */
Printf ("link: % d/N", Buf. st_nlink);/* print the number of hard links */
Printf ("uid: % d/N", Buf. st_uid);/* print the owner user ID */
Printf ("GID: % d/N", Buf. st_gid);/* print the owner group ID */
Printf ("file size: % d/N", Buf. st_size);/* print the file size */
Printf ("Access time: % d/N", Buf. st_atime);/* print the last access time */
Printf ("motify time: % d/N", Buf. st_mtime);/* print the last time the file was modified */
Printf ("Change Time: % d/N", Buf. st_ctime);/* print the last time the file attribute was modified */
Printf ("Buf size: % d/N", Buf. st_blksize);/* print the optimal buffer size */
Printf ("Block Size: % d/N", Buf. st_blocks);/* print the number of disk blocks occupied by the file on the external storage */

If (STAT ("test.txt", & BUF) =-1 ){
Perror ("fail to stat ");
Exit (1 );
}

Printf ("permission: % d/N", Buf. st_mode);/* print the File Permission word */
Printf ("inode number: % d/N", Buf. st_ino);/* print the I node number */
Printf ("device number: % d/N", Buf. st_dev);/* print the file system device Number */
Printf ("R-device number: % d/N", Buf. st_rdev);/* print the hardware device Number */
Printf ("link: % d/N", Buf. st_nlink);/* print the number of hard links */
Printf ("uid: % d/N", Buf. st_uid);/* print the owner user ID */
Printf ("GID: % d/N", Buf. st_gid);/* print the owner group ID */
Printf ("file size: % d/N", Buf. st_size);/* print the file size */
Printf ("Access time: % d/N", Buf. st_atime);/* print the last access time */
Printf ("motify time: % d/N", Buf. st_mtime);/* print the last time the file was modified */
Printf ("Change Time: % d/N", Buf. st_ctime);/* print the last time the file attribute was modified */
Printf ("Buf size: % d/N", Buf. st_blksize);/* print the optimal buffer size */
Printf ("Block Size: % d/N", Buf. st_blocks);/* print the number of disk blocks occupied by the file on the external storage */

Return 0;
}

 

The two outputs are the same, because when the stat function is used, the actual status information of the target file is obtained.

 

 

If you want to know the file status information of the symbolic link file. Available:

 

# Include <sys/STAT. h>
# Include <unistd. h>
Define the int lstat (const char * file_name.struct stat * BUF) function );
Function Description: lstat () and Stat () Act exactly the same way and get the file status indicated by the file_name parameter. The difference is that when the file is a symbolic connection, lstat () returns the status of the link. For more information, see Stat ().
If the return value is successfully executed, 0 is returned. If the return value is failed,-1 is returned. The error code is stored in errno.

 

Example:

// Sym_lstat.c

 

# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/STAT. h>

Int main (void)
{
Struct stat Buf;/* store File status information */

If (lstat ("SL", & BUF) =-1 ){
Perror ("fail to stat ");
Exit (1 );
}

Printf ("permission: % d/N", Buf. st_mode);/* print the File Permission word */
Printf ("inode number: % d/N", Buf. st_ino);/* print the I node number */
Printf ("device number: % d/N", Buf. st_dev);/* print the file system device Number */
Printf ("R-device number: % d/N", Buf. st_rdev);/* print the hardware device Number */
Printf ("link: % d/N", Buf. st_nlink);/* print the number of hard links */
Printf ("uid: % d/N", Buf. st_uid);/* print the owner user ID */
Printf ("GID: % d/N", Buf. st_gid);/* print the owner group ID */
Printf ("file size: % d/N", Buf. st_size);/* print the file size */
Printf ("Access time: % d/N", Buf. st_atime);/* print the last access time */
Printf ("motify time: % d/N", Buf. st_mtime);/* print the last time the file was modified */
Printf ("Change Time: % d/N", Buf. st_ctime);/* print the last time the file attribute was modified */
Printf ("Buf size: % d/N", Buf. st_blksize);/* print the optimal buffer size */
Printf ("Block Size: % d/N", Buf. st_blocks);/* print the number of disk blocks occupied by the file on the external storage */

If (STAT ("test.txt", & BUF) =-1 ){
Perror ("fail to stat ");
Exit (1 );
}

Printf ("permission: % d/N", Buf. st_mode);/* print the File Permission word */
Printf ("inode number: % d/N", Buf. st_ino);/* print the I node number */
Printf ("device number: % d/N", Buf. st_dev);/* print the file system device Number */
Printf ("R-device number: % d/N", Buf. st_rdev);/* print the hardware device Number */
Printf ("link: % d/N", Buf. st_nlink);/* print the number of hard links */
Printf ("uid: % d/N", Buf. st_uid);/* print the owner user ID */
Printf ("GID: % d/N", Buf. st_gid);/* print the owner group ID */
Printf ("file size: % d/N", Buf. st_size);/* print the file size */
Printf ("Access time: % d/N", Buf. st_atime);/* print the last access time */
Printf ("motify time: % d/N", Buf. st_mtime);/* print the last time the file was modified */
Printf ("Change Time: % d/N", Buf. st_ctime);/* print the last time the file attribute was modified */
Printf ("Buf size: % d/N", Buf. st_blksize);/* print the optimal buffer size */
Printf ("Block Size: % d/N", Buf. st_blocks);/* print the number of disk blocks occupied by the file on the external storage */

Return 0;
}
The output content is different this time.

 

6. Change the owner of the symbolic link

When the chown function is used for symbolic links, it will change the user ID of the owner of the file to which the symbolic link points, rather than the user ID of the linked file.

 

To change the owner ID of the symbolic link, use the following function:

 

# Include <unistd. h>

Int lchown (const char * pathname, uid_t owner, gid_t group );

 

When the owner value is-1, the owner ID of the symbolic link does not change. Similarly, the group ID is the same.

 

// Sym_chown.c

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

Int main (void)
{
Struct stat statbuf;
Char Buf [1024];

Printf ("before changing/n");/* output prompt information */

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

Printf ("the owner of test.txt is: % d/N", (unsigned INT) statbuf. st_uid );
Printf ("the group of test.txt is: % d/N", (unsigned INT) statbuf. st_gid );

If (lstat ("SL", & statbuf) =-1) {/* use the lstat function to obtain the status information of the symbolic link */
Perror ("fail to get status ");
Exit (1 );
}

Printf ("the owner of SL is: % d/N", (unsigned INT) statbuf. st_uid );
Printf ("the group of SL is: % d/N", (unsigned INT) statbuf. st_gid );

If (chown ("SL", 0,-1) =-1) {/* change the owner of the target file */
Perror ("fail to change owner ");
Exit (1 );
}

Printf ("after changing/N");/* output prompt information */

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

Printf ("the owner of test.txt is: % d/N", (unsigned INT) statbuf. st_uid );
Printf ("the group of test.txt is: % d/N", (unsigned INT) statbuf. st_gid );

If (lstat ("SL", & statbuf) =-1) {/* use the lstat function to obtain the File status information of the symbolic link */
Perror ("fail to get status ");
Exit (1 );
}

Printf ("the owner of SL is: % d/N", (unsigned INT) statbuf. st_uid );
Printf ("the group of SL is: % d/N", (unsigned INT) statbuf. st_gid );

Return 0;
}

 

 

 

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.