Apue (4)---files and directories (3)

Source: Internet
Author: User

13. Function Rename and Renameat

#include <stdio.h>int rename (constcharconstChar *newname); int renameat (intconstcharintconstChar * newname);

The file or directory can be renamed with the Rename function or the Renameat function. There are several situations that need to be explained:

1. If oldname refers to a file rather than a directory, rename the file or symbolic link. In this case, if newname already exists, it cannot refer to a directory. If newname already exists and is not a directory, delete the catalog entry and rename the Oldname to NewName. For directories containing oldname and directories containing newname, the calling process must have write permissions.

2, if oldname refers to a directory, then rename the directory. If newname already exists, it must refer to a directory, and the directory should be an empty directory.

3. If oldname or newname refers to a symbolic link, the symbolic link itself is processed instead of the file it refers to.

4. Not to. and. Renamed.

14. Symbolic links and their creation and reading

A symbolic connection is an indirect pointer to a file that differs from a hard link, and a hard link directly points to the I node of the file. The reason for introducing symbolic links is to avoid some of the limitations of hard Links: 1, hard links usually require that the connection and files reside in the same file system; 2. Only Superuser can create hard links to directories. When you use a function that references a file by name, you should know whether the function handles symbolic links, that is, whether the function follows a symbolic connection to the file it is linked to. If the function has a function to handle symbolic joins, its pathname parameter refers to the file that is pointed to by the symbolic connection. Otherwise, a pathname parameter refers to the link itself, not to the file that the connection points to.

#include <unistd.h>intSymlinkConst Char*actualpath,Const Char*sympath);intSymlinkat (Const Char*actualpath,intFdConst Char*sympath); ssize_t Readlink (Const Char*restrict Pathname,Char*restrict buf, size_t bufsize); ssize_t Readlinkat (intFdConst Char*restrict pathname.Char*restrict buf, size_t bufsize);

When creating a symbolic connection, Actualpath is not required to exist, and Actualpath and sympath do not need to reside on the same file system.

XV, the time of the document and its modification

UNIX maintains three time fields for each file:

St_atim: Last access time for file data read-u

St_mtim: Last modified time of file data write default

St_ctim:i The last change time of the state of the node chmod, chown-c

Because all the information in the I node and the actual contents of the file are stored separately. The modification time is the last time the file content has been modified. The status time is the time that the I node of the file was modified once, and we explained many operations that affected the I node, such as changing access to files, changing user IDs, changing the number of links, but they did not modify the actual contents of the file. The system does not maintain the time of the last access to an I node, so access and the stat function do not change any of these 3 times.

The access and modification times of a file can be changed with the following functions:

#include <sys/stat.h>int futimens (intconststruct timespec times[2 ]); int utimensat (intconstcharconststruct timespec times[ 2 int flag);

16. Functions mkdir, Mkdirat and RmDir

#include <sys/stat.h>int mkdir (constChar *Pathnaem, mode_t mode); int mkdirat (intconstChar *<unistd.h>int rmdir (  Constchar *pathname);

Creating an empty directory creates it yourself. And ...; A common mistake is to create the same mode as the file (read and write only), but usually at least one execution permission bit is set for the directory to allow access to the file names in that directory.

17. Read the Catalogue

Any user with access to a directory can read the directory, but in order to prevent file system confusion, only the kernel can write directories. Its related main functions are:

#include <dirent.h>*opendir (constChar **fdopendir (int  FD); struct dirent * READDIR (DIR *dp); void Rewinddir (DIR *dp); int closedir (DIR *dp); long telldir (DIR *dp); void long Loc);

I want to add ....

18. Functions ChDir, Fchdir and GETCWD

Each process has a current working directory, which is the starting point for searching for all relative pathname, the process can call chdir and fchdir to change the current directory, and it is important to note that the current working directory is a property of the process, so it affects only the gold city itself that calls ChDir, and does not affect other processes.

#include <unistd.h>int chdir (constChar *pathname); int fchdir (int  FD); Char * GETCWD (char *buf, size_t size);

#include"apue.h"intMainvoid){    Charptr[1024x768];    size_t size; if(ChDir ("/usr/local/lib") <0) {Err_sys ("chdir Error"); }    if(GETCWD (PTR,1024x768) ==NULL) {Err_sys ("GETCWD Error"); } printf ("CWD =%s\n", PTR); Exit (0);}

4-23-24 ChDir and GETCWD instances

Apue (4)---files and directories (3)

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.