一道面试题:Linux目录

runnerterry 2007-11-16 11:48:50
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脚本的程序,谢谢!
...全文
606 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xi2008wang 2007-12-30
  • 打赏
  • 举报
回复
标准UNIX系统工具是怎么处理这种长路径的?
find + Xargs
asxiao999 2007-12-30
  • 打赏
  • 举报
回复
有linux使用手册么
mfcmaster 2007-12-30
  • 打赏
  • 举报
回复
大哥,给点分啊。。。。。
mfcmaster 2007-12-30
  • 打赏
  • 举报
回复
Linux下的PATH_MAX指的是目录长度,为256个字符(好像是),Linux下对目录深度好像没有限制。跟传统的Unix怎么样我不清楚,不过跟Solaris里边的是有区别别,Solaris里边的长度要求不一样(好像是)。你可以去查《UNIX环境高级编程》里边有。
littlefirebug 2007-12-29
  • 打赏
  • 举报
回复
内核对目录的深度没有内在的限制,但是如果路径名的长度超出了PATH_MAX,则有许多命令会失败.


你这到题是Unix环境高级编程上的一道课后习题.有答案.如果你没有找到的话我再把代码给你贴上去.
wangox 2007-12-29
  • 打赏
  • 举报
回复
sprintf, strcat的风险在于不能控制输出的内容的长度,有可能越界,strncat则可以指定输出的字符串的长度.

cpio也是一个备份工具,具体的可以通过man cpio来获取帮助.

int chdir(const char *path);
就是用于改变工作目录的函数.
runnerterry 2007-11-16
  • 打赏
  • 举报
回复
顺便问下,有没有进入某个目录的函数?
即与getcwd函数相反的函数?
例如:
(控制台)
#mkdir /1/2/3/4
#cd /1/2/3/4
类似与cd功能的函数?
runnerterry 2007-11-16
  • 打赏
  • 举报
回复
PATH_MAX=4096,为路径字符串长度。

但可以在控制台命令行环境下进入到最底层,建立下级目录,此时目录绝对路径字符串长度已经超过PATH_MAX。
runnerterry 2007-11-16
  • 打赏
  • 举报
回复
啊,谢谢cceczjxy

终于看到shell的程序了,我先学习一下。


希望能得到其他的问题的答案。
下周来散分。
cceczjxy 2007-11-16
  • 打赏
  • 举报
回复
378,刚才写错了.
cceczjxy 2007-11-16
  • 打赏
  • 举报
回复
path=./
count=1
while [ 1 ]
do
path=$path/1234567890
count=` expr $count + 1 `
echo $count
mkdir $path
if [ ! $? -eq 0 ]
then
break
fi

count是间目录的深度,1234567890是目录名,一层一层往下建,我机器上可以建278层.

PATH_MAX 是限制的目录总长度.
还有一个宏连限制文件名长度,具体叫什么忘啦.

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧