Unix文件和目录
一. 文件类型的判断
首先介绍 stat 结构体,其结构体中包含以下类型:
这些数据类型配合以下的宏定义用于使用户查看文件的各项信息
文件类型宏:
使用案例部分代码如下(判断是否为目录文件):
int main(int argc, char *argv[])
{
struct stat buf;
char *ptr;
...
if(lstat(argv[1], &buf) < 0) //获取文件信息
err_ret("lstat error");
...
if (S_DIR(buf.st_mode)) //判断是否为目录文件
ptr = "directory";
}
...
二. 文件模式屏蔽函数umask
函数原型:
#include<sys/stat.h>
mode_t umask(mode_t cmask);
此函数用于创建文件时给文件设置权限的屏蔽标志,对应八进制数中的每一位代表一位屏蔽。具体屏蔽位参考《UNIX高级环境编程》图4-10。
三. 其他一些函数
#include<unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group); //用户权限赋予函数
int trucate(const char *pathname, off_t length); //文件截断函数
int link(const char *existingpath, const char *newpath); //创建新链接
int unlink(const char *pathname); //删除目录
int symlink(const char *actualpath, const char *sympath); //创建符号链接
ssize_t readlink(const char *restrict pathname, char *restrict buf, size_t bufsize); //解决open函数不能读取symlink的问题
int futimens(int fd, const struct timespec time[2]); //更改访问时间和修改时间
int redir(const char *pathname); //删除空目录
四. 遍历文件结构程序
1 程序代码
程序来源于《UNIX高级环境编程》
#include "apue.h"
#include <dirent.h>
#include <limits.h>
/* function type that is called for each filename */
typedef int Myfunc(const char *, const struct stat *, int); //定义函数类型Myfunc
static Myfunc myfunc;
static int myftw(char *, Myfunc *);
static int dopath(Myfunc *);
static long nreg, ndir, nblk, nchr, nfifo, nslink, nsock, ntot;
int
main(int argc, char *argv[])
{
int ret;
if (argc != 2)
err_quit("usage: ftw <starting-pathname>");
ret = myftw(argv[1], myfunc); /* myfunc函数作为参数,myfunc参数由下面代码指定 */
ntot = nreg + ndir + nblk + nchr + nfifo + nslink + nsock;
if (ntot == 0)
ntot = 1; /* avoid divide by 0; print 0 for all counts */
printf("regular files = %7ld, %5.2f %%\n", nreg,
nreg*100.0/ntot);
printf("directories = %7ld, %5.2f %%\n", ndir,
ndir*100.0/ntot);
printf("block special = %7ld, %5.2f %%\n", nblk,
nblk*100.0/ntot);
printf("char special = %7ld, %5.2f %%\n", nchr,
nchr*100.0/ntot);
printf("FIFOs = %7ld, %5.2f %%\n", nfifo,
nfifo*100.0/ntot);
printf("symbolic links = %7ld, %5.2f %%\n", nslink,
nslink*100.0/ntot);
printf("sockets = %7ld, %5.2f %%\n", nsock,
nsock*100.0/ntot);
exit(ret);
}
/*
* Descend through the hierarchy, starting at "pathname".
* The caller's func() is called for every file.
*/
#define FTW_F 1 /* file other than directory */
#define FTW_D 2 /* directory */
#define FTW_DNR 3 /* directory that can't be read */
#define FTW_NS 4 /* file that we can't stat */
static char *fullpath; /* contains full pathname for every file */
static size_t pathlen;
static int /* we return whatever func() returns */
myftw(char *pathname, Myfunc *func)
{
fullpath = path_alloc(&pathlen); /* malloc PATH_MAX+1 bytes */
/* ({Prog pathalloc}) */
if (pathlen <= strlen(pathname)) {
pathlen = strlen(pathname) * 2;
if ((fullpath = realloc(fullpath, pathlen)) == NULL)
err_sys("realloc failed");
}
strcpy(fullpath, pathname);
return(dopath(func));
}
/*
* Descend through the hierarchy, starting at "fullpath".
* If "fullpath" is anything other than a directory, we lstat() it,
* call func(), and return. For a directory, we call ourself
* recursively for each name in the directory.
*/
static int /* we return whatever func() returns */
dopath(Myfunc* func)
{
struct stat statbuf;
struct dirent *dirp;
DIR *dp;
int ret, n;
if (lstat(fullpath, &statbuf) < 0) /* stat error */
return(func(fullpath, &statbuf, FTW_NS));
if (S_ISDIR(statbuf.st_mode) == 0) /* not a directory */
return(func(fullpath, &statbuf, FTW_F));
/*
* It's a directory. First call func() for the directory,
* then process each filename in the directory.
*/
if ((ret = func(fullpath, &statbuf, FTW_D)) != 0)
return(ret);
n = strlen(fullpath);
if (n + NAME_MAX + 2 > pathlen) { /* expand path buffer */
pathlen *= 2;
if ((fullpath = realloc(fullpath, pathlen)) == NULL)
err_sys("realloc failed");
}
fullpath[n++] = '/'; //加上‘/’号
fullpath[n] = 0; //删除后面目录
if ((dp = opendir(fullpath)) == NULL) /* can't read directory */
return(func(fullpath, &statbuf, FTW_DNR));
while ((dirp = readdir(dp)) != NULL) { //目录中文件读完终止本轮读,跳出while循环
if (strcmp(dirp->d_name, ".") == 0 ||
strcmp(dirp->d_name, "..") == 0)
continue; /* ignore dot and dot-dot */
strcpy(&fullpath[n], dirp->d_name); /* append name after "/" */
if ((ret = dopath(func)) != 0) /* recursive */
break; /* time to leave */
}
fullpath[n-1] = 0; /* erase everything from slash onward */
if (closedir(dp) < 0)
err_ret("can't close directory %s", fullpath);
return(ret);
}
static int
myfunc(const char *pathname, const struct stat *statptr, int type)
{
switch (type) {
case FTW_F:
switch (statptr->st_mode & S_IFMT) {
case S_IFREG: nreg++; break;
case S_IFBLK: nblk++; break;
case S_IFCHR: nchr++; break;
case S_IFIFO: nfifo++; break;
case S_IFLNK: nslink++; break;
case S_IFSOCK: nsock++; break;
case S_IFDIR: /* directories should have type = FTW_D */
err_dump("for S_IFDIR for %s", pathname);
}
break;
case FTW_D:
ndir++;
break;
case FTW_DNR:
err_ret("can't read directory %s", pathname);
break;
case FTW_NS:
err_ret("stat error for %s", pathname);
break;
default:
err_dump("unknown type %d for pathname %s", type, pathname);
}
return(0);
}
2 执行思路
- 首先在main函数中读入开始目录,并将其传递给pathname参数,同时将myfunc函数作为参数传递给myftw函数
- 在myftw函数中给fullpath分配空间并接收pathname参数的值,再调用dopath函数正式开始计数
- 在dopath函数中递归调用自身并遍历目录中的每个文件,同时调用myfunc函数判断文件种类并计算
- 最后在主函数中打印各类文件数目和占比