C language to establish and delete file connection related functions

  • 2020-04-02 03:22:05
  • OfStack

C language link() function: establish a file connection
The header file:


#include <unistd.h>

Definition function:


int link (const char * oldpath, const char * newpath);

Link () creates a new connection (hard connection) to an existing file specified by oldpath with the name specified by newpath. No connection is made if the name specified by newpath is an existing file.

Return value: 0 on success, -1 on failure, error in errno.

Note: the hard connection established by link() cannot span different file systems. Use symlink() instead.

Error code:
1. The EXDEV parameter oldpath and newpath are not on the same file system.
2. The file systems indicated by EPERM parameters oldpath and newpath do not support hard connection
3. The EROFS file exists in the read-only file system
4. The EFAULT parameter oldpath or newpath pointer exceeds the accessible memory space.
5. ENAMETOLLONG parameter oldpath or newpath is too long
6. ENOMEM is out of core memory
7. The file name referred to by the EEXIST parameter newpath already exists.
8. The file to which the EMLINK parameter oldpath refers has reached the maximum number of connections.
9. The ELOOP parameter pathname has too many symbolic connection problems
10. Insufficient space for ENOSPC file system.
EIO I/O access error.

Example:


/*  To establish /etc/passwd  Is hardwired as pass */
#include <unistd.h>
main()
{
  link("/etc/passwd", "pass");
}

C language unlink() function: delete files
The header file:


#include <unistd.h>

Definition function:


int unlink(const char * pathname);

Unlink () deletes the file specified by the pathname parameter. If the file is named the last join point, but is opened by another process, the file will not be deleted until all file descriptors for the file are closed. If the pathname is a symbolic connection, the connection will be deleted.

Return value: 0 on success, -1 on failure, error in errno

Error code:
1. The EROFS file exists in the read-only file system.
2. The pathname pointer of EFAULT parameter exceeds the accessible memory space.
3. The ENAMETOOLONG parameter pathname is too long.
4. ENOMEM is out of core memory.
5. The ELOOP parameter pathname has too many symbolic connection problems.
EIO I/O access error.


Related articles: