一道面试题:Linux目录
the original question:
Does the UNIX System have a fundamental limitation on the depth of a directory tree? To find
out, write a program that creates a directory and then changes to that directory, in a loop.
Make certain that the length of the absolute pathname of the leaf of this directory is
greater than your system's PATH_MAX limit. Can you call getcwd to fetch the directory's
pathname? How do the standard UNIX System tools deal with this long pathname? Can you
archive the directory using either tar or cpio?
translate:
UNIX系统的目录树有没有深度上的限制?在一个循环中创建一个目录并且进入到那个目录,写这样的程序
来验证。确认目录分支的绝对路径长度大于系统定义的PATH_MAX限制。你能否调用getcwd去获取此目录的
路径?标准UNIX系统工具是怎么处理这种长路径的?你能否使用tar或cpio访问此目录?
doubt:
1.系统的PATH_MAX限制是多少?PATH_MAX是指目录深度,还是指路径字符串长度?
2.处理长路径的工具有哪些?
3.cpio是什么命令?
testing program:
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
// (part 1.)
// if the PATH_MAX is the depth of the directory tree.
#define PATH_TEST (PATH_MAX+1)
int mian(void)
{
int depth = 0;
char pathname[PATH_MAX*4];
char temp[PATH_MAX*4];
printf("the PATH_MAX's value is %d.\n", PATH_MAX);
while(depth++ < PATH_TEST)
{
sprintf(temp, "//%d", depth);
strcat(pathname, temp);
ret = mkdir(pathname, 777);
if (ret == -1)
{
printf("the max depth is %d.\n", depth);
printf("the failed pathname is \"%s\".\n", pathname);
break;
}
}
return 1;
}
// (part 2.)
// if the PATH_MAX is the length of the directory pathname.
#define PATH_TEST (PATH_MAX*2)
int main(void)
{
int length = 0;
int depth = 0;
char pathname[PATH_TEST];
char temp[PATH_TEST];
printf("the PATH_MAX's value is %d.\n", PATH_MAX);
while(length < PATH_TEST)
{
sprintf(temp, "//%d", depth++);
strcat(pathname, temp);
length = strlen(pathname);
ret = mkdir(pathname, 777);
if (ret == -1)
{
printf("the max depth is %d.\n", depth);
printf("the max length is %d.\n", length);
printf("the failed pathname is \"%s\".\n", pathname);
break;
}
}
return 1;
}
additional requirement:
1. 上面是我自己写的测试程序,还没有真正编译运行过,肯定有不严谨的地方,存在一定的风险,比如
用了不定参数的sprintf和strcat(听某些高人说最好用strncat,不知道为什么),所以请大家多多赐教
,谢谢!
2. 希望有热心人给我一个shell脚本的程序,谢谢!