[Linux system programming] API methods for deleting files under Linux and differences in file deletion mechanisms

Directory title

  • Function prototype and file deletion mechanism
    • 1. `remove` function
      • 1.1 Function Prototype
      • 1.2 Error code and description
      • 1.3 Function description and return value
    • 2. `unlink` function
      • 2.1 Function prototype
      • 2.2 Function Description
      • 2.3 Special annotations
    • 3. `unlinkat` function
      • 3.1 Function prototype
      • 3.2 Function Description
    • 4. In-depth insights
    • 5. Source code analysis
    • 6. Code example
  • Conclusion

Function prototype and file deletion mechanism

1. remove function

1.1 Function prototype

#include <stdio.h>
int remove(const char *pathname);

Returns 0 on success, -1 on failure, and the error reason is stored in errno.

1.2 Error code and description

Error code Description
EROFS The file to be written is a read-only file
EFAULT The parameter filename pointer exceeded Accessible memory space
ENAMETOOLONG The parameter filename is too long
ENOMEM Insufficient core memory
ELOOP Parameters filename has too many symbolic link problems
EIO I/O access error

1.3 Function description and return value

The remove function is used to delete files or directories. If the parameter pathname is a file, unlink() is called internally for processing; if it is a directory, rmdir() is called for processing.

“As Bjarne Stroustrup said in “The C++ Programming Language”: ‘The most effective debugging tool is still carefully thought, coupled with judiciously placed print statements.'”

2. unlink function

2.1 Function prototype

#include <unistd.h>
int unlink(const char *pathname);

2.2 Function Description

The unlink() function is used to remove a name from the file system. For hard links, this function deletes the directory entry and decrements the inode reference count. For soft links, this function directly deletes the soft link without affecting the file it points to.

2.3 Special Notes

Executing the unlink() function does not necessarily actually delete the file. This function first checks the number of connections for this file in the file system. If the number of connections is not 1, this function only decrements the number of connections to this file by 1. If the number of connections is 1 and no process opens the file, the file will be truly deleted.

3. unlinkat function

3.1 Function prototype

#include <fcntl.h>
#include <unistd.h>
int unlinkat(int dirfd, const char *pathname, int flags);

3.2 Function Description

This function is similar to unlink(), but provides more options, such as finer control through the dirfd and flags parameters.

4. In-depth insights

In the design of the operating system, deleting files is not a simple operation. It involves multiple levels of checking and validation, which reflects the operating system’s rigorous attitude toward resource management. This design philosophy also applies to the human mind and existence, which requires thorough and in-depth consideration before making important decisions.

5. Source code analysis

In Linux systems, the implementation of the unlink function can be found in the fs/namei.c file. This function first checks the number of connections and open status of the file before performing the actual deletion operation.

6. Code example

#include <stdio.h>
#include <unistd.h>

int main() {<!-- -->
    if (unlink("test.txt") == 0) {<!-- -->
        printf("Successfully deleted the file.\\
");
    } else {<!-- -->
        perror("Failed to delete the file");
    }
    return 0;
}

This simple C code example shows how to use the unlink function to delete a file.

Conclusion

In our programming learning journey, understanding is an important step for us to move to a higher level. However, mastering new skills and ideas always requires time and persistence. From a psychological point of view, learning is often accompanied by constant trial and error and adjustment, which is like our brain gradually optimizing its “algorithm” for solving problems.

This is why when we encounter mistakes, we should view them as opportunities to learn and improve, rather than More than just an obsession. By understanding and solving these problems, we can not only fix the current code, but also improve our programming skills and prevent making the same mistakes in future projects.

I encourage everyone to actively participate and continuously improve their programming skills. Whether you are a beginner or an experienced developer, I hope my blog will be helpful on your learning journey. If you find this article useful, you may wish to click to bookmark it, or leave your comments to share your insights and experiences. You are also welcome to make suggestions and questions about the content of my blog. Every like, comment, share and attention is the greatest support for me and the motivation for me to continue sharing and creating.

Read my CSDN homepage and unlock more exciting content: Bubble’s CSDN homepage