《UNIX环境高级编程》读书笔记之文件和目录(1)
2014-09-05 15:36
495 查看
1.函数stat(),fstat(),fstatat(),lstat()。
#include <sys/stat.h>
int stat(const char * pathname,struct stat * buf);
int fstat(int fd,struct stat * buf);
int lstat(const char * pathname,struct stat * buf);
int fstatat(int fd,const char * pathname,struct stat * buf,int flag);
一旦给出文件名,stat()函数将返回与文件有关的信息结构。fstat()函数返回在文件描述符fd上打开的文件的信息结构。lstat()与stat()类似,但是当文件是一个符号链接时,它将返回符号链接的信息结构。fstatat()稍微复杂:当flag等于AT_SYMLINK_NOFOLLOW时,fstatat()不会跟随符号链接,而是返回符号链接本身的信息。否则,在默认的情况下,fstatat()返回符号链接所链接到的文件的信息。如果fd参数值是AT_FDCWD,且pathname是一个相对路径值,则fstatat()会计算pathname相对于当前目录的值。如果pathname是一个绝对路径,则忽略fd。
2.struct stat的结构。
struct stat {
mode_t st_mode;
ino_t st_ino;//i节点编号
dev_t st_dev;
nlink_t st_nlink;//文件i节点的链接数
uid_t st_uid;//文件所属的用户ID
gid_t st_gid;//文件所属的用户组ID
off_t st_size;//文件的大小
struct timespec st_atime;//文件数据最后访问时间
struct timespec st_mtime;//文件数据最后修改时间
struct timespec st_ctime;//i节点状态最后更该时间
blksize_t st_blksize;//文件块大小
blkcnt_t st_blocks;//文件占多少块
}
(1)st_mode变量里保存着文件的类型信息。在<sys/stat.h>中包含如下宏定义:
#include <sys/stat.h>
S_ISREG(); //是否为普通文件
S_ISDIR();//是否为目录文件
S_ISCHR();//是否为特殊字符文件
S_ISBLK();//是否为块特殊文件
S_ISFIFO();//是否为FIFO文件
S_ISLINK();//是否为符号链接
S_ISOCK();//是否为套接字文件
3.函数access()和faccessat()
#include <unistd.h>
int access(const char * pathname,int mode);
int faccessat(int fd,const char *pathname,int mode,int flag);
access(),faccessat()函数用于验证用户能否访问一个文件。这两个函数在两种情况下是相同的:1.pathname是绝对路径。2.fd等于AT_FDCWD。
mode的取值为F_OK(测试文件是否存在),R_OK(文件是否可读),W_OK(文件是否可写),X_OK(文件是否可执行).
4.函数umask。
函数umask为进程创建文件模式屏蔽字
#include <sys/stat.h>
mode_t umask(mode_t cmask);
其中cmask是由S_IRUSR,S_IWUSR,S_IXUSR,S_IRGRP,S_IWGRP,S_IXGRP,S_IROTH,S_IWOTH,S_IXOTH‘或’构成的。
5函数chmod,fchmod,fchmodat,改变文件的访问权限.
#include <sys/stat.h>
int chmod(const char * pathname,mode_t mode);
int fchmod(int fd,mode_t mode);
int fchmodat(int fd,const char * pathname,mode_t mode,int flag);
flag改变fchmodat的行为。当flag设置为AT_SYMLINK_NOFOLLOW时,fchmodat不会跟随符号链接。
为了改变一个文件的权限位,进程的有效用户ID必须等于文件的所有者ID,或者为超级用户。
mode是由一下几个常量按位或组成:S_ISUID,S_ISGID,S_ISVTX,S_IRWXU,S_IRUSR,S_IWUSR,S_IXUSR,S_IRWXG,S_IRGRP,S_IWGRP,S_IXGRP,S_IRWXO,S_IROTH,S_IWOTH,S_IXOTH
6.改变文件用户ID或组ID:chown,fchown,fchownat,lchown
#include <unistd.h>
int chown(const char * pathname,uid_t owner,gid_t group);
int fchown(int fd,uid_t
owner,gid_t group);
int fchownat(int fd,const char * pathname,uid_t
owner,gid_t group,int flag);
int lchown(const char * pathname,uid_t
owner,gid_t group);
lchown和fchownat在设置flag为AT_SYMLINK_NOFOLLOW时,更改符号链接的所有者,而不是符号链接所指向文件的所有者。
7.函数link,linkat,unlink,unlinkat和remove
任何一个文件可以有多个目录项指向其i节点。创建一个指向现有文件链接的方法是使用link函数或linkat函数。
#include <unistd.h>
int link(const char *existingpath,const
char * newpath)
int linkat(int efd,const
char * existingpath,int nfd,const char * new)
对于linkat函数,现有文件路径是通过efd和existingpath共同确定,新文件路径是通过efd和existingpath共同确定。
删除一个现有的目录项:
#include
<unistd.h>
int
unlink(const char * pathname);
int
unlink(int fd,const char * pathname,int flag);
这两个函数删除指定的目录项,并将文件所引用的链接计数减一。只有当文件的链接计数为0且没有进程打开当前文件时,才会将文件的内容删除。
#include
<stdio.h>
int
remove(const char * pathname);
对于普通文件来说,remove函数与unlink函数一样。对于目录文件来说,remove函数和rmdir函数一样。
8.函数rename与renameat
文件或目录可以通过rename函数或者renamaat函数进行重命名。
#include
<stdio.h>
int
rename(const char * oldpath,const * newpath);
int
renameat(int oldfd,const char * oldpath,int newfd,const char * newpath);
9.创建和读取符号链接
创建符号链接:
#include <unistd.h>
int symlink(const char * actualpath,const char * sympath);
int symlinkat(const char * actualpath,int fd,const char * sympath);
函数创建一个指向actualpath的新目录项sympath。在创建此符号链接时,并不要求actualpath已经存在。
symlinkat函数根据fd与sympath来计算新创建的目录项。
10.函数futimens,utimensat,utimes
#include <sys/stat.h>
int futimens(int fd,const struct timespec times[2]);
int utimensat(int fd,const char * path,const struct timespec time[2],int flag);
time数组包含两个时间,一个是访问时间,另个个是修改时间。这两个时间都是日历时间。
#include <sys/time.h>
int utimes(const char * pathname,const struct timeval times[2]);
times数组包含两个时间戳,一个是访问时间,一个是修改时间。两个时间戳都是用秒和微秒来表示的。
struct timeval {
time_t tv_sec;
long tv_usec;
};
11.函数mkdir,mkdirat,rmdir
#include <sys/stat.h>
int mkdir(const char * pathname,mode_t mode)
int mkdirat(int fd,const char * pathname,mode_t mode)
#include <unistd.h>
int rmdir(const char * pathname)
rmdir函数可以删除一个空目录
12.函数chdir,fchdir,getcwd
每一个进程都有它的当前工作目录,chdir跟fchdir可以用来改变进程当前的工作目录。
#include <unistd.h>
int chdir(const char * pathname);
int fchdir(int fd);
#include <unistd.h>
char * getcwd(char * buf,size_t size);
getcwd函数可以得到当前工作目录的绝对路径
#include <sys/stat.h>
int stat(const char * pathname,struct stat * buf);
int fstat(int fd,struct stat * buf);
int lstat(const char * pathname,struct stat * buf);
int fstatat(int fd,const char * pathname,struct stat * buf,int flag);
一旦给出文件名,stat()函数将返回与文件有关的信息结构。fstat()函数返回在文件描述符fd上打开的文件的信息结构。lstat()与stat()类似,但是当文件是一个符号链接时,它将返回符号链接的信息结构。fstatat()稍微复杂:当flag等于AT_SYMLINK_NOFOLLOW时,fstatat()不会跟随符号链接,而是返回符号链接本身的信息。否则,在默认的情况下,fstatat()返回符号链接所链接到的文件的信息。如果fd参数值是AT_FDCWD,且pathname是一个相对路径值,则fstatat()会计算pathname相对于当前目录的值。如果pathname是一个绝对路径,则忽略fd。
2.struct stat的结构。
struct stat {
mode_t st_mode;
ino_t st_ino;//i节点编号
dev_t st_dev;
nlink_t st_nlink;//文件i节点的链接数
uid_t st_uid;//文件所属的用户ID
gid_t st_gid;//文件所属的用户组ID
off_t st_size;//文件的大小
struct timespec st_atime;//文件数据最后访问时间
struct timespec st_mtime;//文件数据最后修改时间
struct timespec st_ctime;//i节点状态最后更该时间
blksize_t st_blksize;//文件块大小
blkcnt_t st_blocks;//文件占多少块
}
(1)st_mode变量里保存着文件的类型信息。在<sys/stat.h>中包含如下宏定义:
#include <sys/stat.h>
S_ISREG(); //是否为普通文件
S_ISDIR();//是否为目录文件
S_ISCHR();//是否为特殊字符文件
S_ISBLK();//是否为块特殊文件
S_ISFIFO();//是否为FIFO文件
S_ISLINK();//是否为符号链接
S_ISOCK();//是否为套接字文件
3.函数access()和faccessat()
#include <unistd.h>
int access(const char * pathname,int mode);
int faccessat(int fd,const char *pathname,int mode,int flag);
access(),faccessat()函数用于验证用户能否访问一个文件。这两个函数在两种情况下是相同的:1.pathname是绝对路径。2.fd等于AT_FDCWD。
mode的取值为F_OK(测试文件是否存在),R_OK(文件是否可读),W_OK(文件是否可写),X_OK(文件是否可执行).
4.函数umask。
函数umask为进程创建文件模式屏蔽字
#include <sys/stat.h>
mode_t umask(mode_t cmask);
其中cmask是由S_IRUSR,S_IWUSR,S_IXUSR,S_IRGRP,S_IWGRP,S_IXGRP,S_IROTH,S_IWOTH,S_IXOTH‘或’构成的。
5函数chmod,fchmod,fchmodat,改变文件的访问权限.
#include <sys/stat.h>
int chmod(const char * pathname,mode_t mode);
int fchmod(int fd,mode_t mode);
int fchmodat(int fd,const char * pathname,mode_t mode,int flag);
flag改变fchmodat的行为。当flag设置为AT_SYMLINK_NOFOLLOW时,fchmodat不会跟随符号链接。
为了改变一个文件的权限位,进程的有效用户ID必须等于文件的所有者ID,或者为超级用户。
mode是由一下几个常量按位或组成:S_ISUID,S_ISGID,S_ISVTX,S_IRWXU,S_IRUSR,S_IWUSR,S_IXUSR,S_IRWXG,S_IRGRP,S_IWGRP,S_IXGRP,S_IRWXO,S_IROTH,S_IWOTH,S_IXOTH
6.改变文件用户ID或组ID:chown,fchown,fchownat,lchown
#include <unistd.h>
int chown(const char * pathname,uid_t owner,gid_t group);
int fchown(int fd,uid_t
owner,gid_t group);
int fchownat(int fd,const char * pathname,uid_t
owner,gid_t group,int flag);
int lchown(const char * pathname,uid_t
owner,gid_t group);
lchown和fchownat在设置flag为AT_SYMLINK_NOFOLLOW时,更改符号链接的所有者,而不是符号链接所指向文件的所有者。
7.函数link,linkat,unlink,unlinkat和remove
任何一个文件可以有多个目录项指向其i节点。创建一个指向现有文件链接的方法是使用link函数或linkat函数。
#include <unistd.h>
int link(const char *existingpath,const
char * newpath)
int linkat(int efd,const
char * existingpath,int nfd,const char * new)
对于linkat函数,现有文件路径是通过efd和existingpath共同确定,新文件路径是通过efd和existingpath共同确定。
删除一个现有的目录项:
#include
<unistd.h>
int
unlink(const char * pathname);
int
unlink(int fd,const char * pathname,int flag);
这两个函数删除指定的目录项,并将文件所引用的链接计数减一。只有当文件的链接计数为0且没有进程打开当前文件时,才会将文件的内容删除。
#include
<stdio.h>
int
remove(const char * pathname);
对于普通文件来说,remove函数与unlink函数一样。对于目录文件来说,remove函数和rmdir函数一样。
8.函数rename与renameat
文件或目录可以通过rename函数或者renamaat函数进行重命名。
#include
<stdio.h>
int
rename(const char * oldpath,const * newpath);
int
renameat(int oldfd,const char * oldpath,int newfd,const char * newpath);
9.创建和读取符号链接
创建符号链接:
#include <unistd.h>
int symlink(const char * actualpath,const char * sympath);
int symlinkat(const char * actualpath,int fd,const char * sympath);
函数创建一个指向actualpath的新目录项sympath。在创建此符号链接时,并不要求actualpath已经存在。
symlinkat函数根据fd与sympath来计算新创建的目录项。
10.函数futimens,utimensat,utimes
#include <sys/stat.h>
int futimens(int fd,const struct timespec times[2]);
int utimensat(int fd,const char * path,const struct timespec time[2],int flag);
time数组包含两个时间,一个是访问时间,另个个是修改时间。这两个时间都是日历时间。
#include <sys/time.h>
int utimes(const char * pathname,const struct timeval times[2]);
times数组包含两个时间戳,一个是访问时间,一个是修改时间。两个时间戳都是用秒和微秒来表示的。
struct timeval {
time_t tv_sec;
long tv_usec;
};
11.函数mkdir,mkdirat,rmdir
#include <sys/stat.h>
int mkdir(const char * pathname,mode_t mode)
int mkdirat(int fd,const char * pathname,mode_t mode)
#include <unistd.h>
int rmdir(const char * pathname)
rmdir函数可以删除一个空目录
12.函数chdir,fchdir,getcwd
每一个进程都有它的当前工作目录,chdir跟fchdir可以用来改变进程当前的工作目录。
#include <unistd.h>
int chdir(const char * pathname);
int fchdir(int fd);
#include <unistd.h>
char * getcwd(char * buf,size_t size);
getcwd函数可以得到当前工作目录的绝对路径
相关文章推荐
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《Unix环境高级编程》读书笔记 第4章-文件和目录
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》读书笔记之文件与目录(2)
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《UNIX环境高级编程》四文件和目录读书笔记
- 《Unix环境高级编程》读书笔记:文件和目录
- 《UNIX环境高级编程》笔记:第4章 文件和目录
- 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)