[Apue] Files and directories (medium)

Source: Internet
Author: User
Tags symlink

Link, unlink, remove and rename
    1. A file can have multiple directory entries pointing to its I node. Use the link function to create a link to an existing file
#include <unistd.h>int link(const char *existingpath, const char *newpath);返回值:成功为0,出错为-1

The function creates a new catalog entry NewPath, points to the existing file Existingpath, and returns an error if NewPath already exists.

    1. In order to delete an existing directory entry, you can call the Unlnk function.
#include <unistd.h>int unlink(const char *pathname);返回值:成功为0,出错为-1

This function deletes the catalog entry and pathname the connection count of the file referenced by-1. The contents of the file can only be deleted if the connection count reaches 0, and its contents cannot be deleted if a process has opened the file. When you close a file, the kernel first checks the process count that causes the file to open, if the count is 0 and then checks the connection count, and if 0 then deletes the file.

    1. You can use the Remove function to disassociate a file or directory, and remove has the same functionality as unlink for a file. For directories, remove has the same functionality as rmdir.
#include <stdio.h>int remove(const char *pathname);
    1. File or directory renamed with rename function
#include <stdio.h>int rename(const char *oldname, const char *newname);

There are two scenarios depending on whether oldname is a directory or a file:

(1) If Oldname is a file, rename the file. In this case, if newname already exists, it cannot refer to a directory. If newname already exists and is not a directory, then the directory entry (this is not a directory!) Delete and rename oldname to NewName. For directories that contain oldname and newname, the calling process must have write permissions because the two directories will be changed.

(2) If oldname describes a directory, rename the directory. If newname already exists, it must refer to a directory, and the directory should be an empty directory. If newname exists and is an empty directory, delete it first, and then rename Oldname to NewName. /usr/foo cannot be renamed to/usr/foo/dir because Oldname/usr/foo is a newname/usr/foo/dir prefix and cannot be deleted. You can rename the/tmp/orlion/b/(if there is a C file under the folder) to/tmp/orlion_1/, and you can see a C file under the Orlion_1 folder after the modification is complete.

(3) If Oldname and newname refer to the same file, the success is returned without any modification. If the newname already exists, the process must have write access to it (as if it were deleted). In addition, the process will delete the Oldname directory entry and possibly create a newname directory entry, so it needs to have write and execute rights to the directory containing the Oldname and newname.

Second, symbolic connection

A symbolic link is an indirect pointer to a file. The RmDir function returns an error if path is a symbolic link, so the following table does not list the processing of symbolic connections for each function as follows:

  

Three, symlink and Readlink functions

The Symlink function creates a symbolic connection.

#include <unistd.h>int symlink(const char *actualpath, const char *sympath);

Actualpath is not required to exist when creating symbolic connections. Actualpath and Sympath are not required to be in the same file system as the hard connect requirement. Because the open function follows a symbolic connection (that is, a file that actually opens a symbolic connection), a way to open the connection itself is required, and Readlink provides this functionality.

#include <unistd.h>int readlink(const char *pathname, char *buf, int bufsize);

This function combines all the operations of Open,read and close. Returns the number of bytes read into BUF if successful (1 if it fails). The contents of the file link returned in BUF are not terminated with a null character.

Iv. Time of the document

Each file has three time periods, such as:

  

The following table is the function of the various functions on these three times. A directory is a file that contains a catalog item (file name and associated I node), and adding, deleting, or modifying a catalog item affects the three time associated with the directory in which it resides. For example, creating a new file affects the directory that contains the file (because the directory is essentially the file that contains the file) and the I node of the new file, but the read or write affects only the I node of the file, not the parent directory.

Five, Utime function

The access and modification times of a file can be changed with the Utime function.

#include <sys/types.h>int utime(const char *pathname, const struct utimbuf *times);返回值:成功0,失败-1.

The structure used by this function is:

struct utimbuf {  time_t actime; /* access time */  time_t modtime; /* modification time */}

Two times in a struct are UNIX timestamps.

The operation of this function and the priority required to execute it are canceled by the Times parameter if it is null.

(1) If the Times is a null pointer, both the access time and the modification time are set to the current time. In order to perform a secondary operation, one of the two conditions must be met (a) the valid user ID of the process must be equal to the owner ID of the file, and (b) the process must have write permission on the file.

(2) If the Times is a non-null pointer, the access time and modification time are set to the value in the struct to which times is pointing. At this point, the valid user ID of the process must be equal to the owner of the file or the process is a root process. It is not sufficient to have write access to a file.

The file's status time St_ctime cannot be modified, and when Utime is called, this field is automatically updated.

[Apue] Files and directories (medium)

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.