计算目录大小 [英] Calculating a directories size

查看:64
本文介绍了计算目录大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法使用C库函数来获取目录及其内容的大小(不需要使用shell命令)?


==================================

海报的IP地址: 193.123.19.116

通过 http://nodevice.com 发布/>
Linux程序员网站


Is there any way using C library functions to get the size of a directory and its contents (without resorting to using a shell command)?

==================================
Poster''s IP address: 193.123.19.116
Posted via http://nodevice.com
Linux Programmer''s Site

推荐答案

193.123.19.116 [达兹]写道:
193.123.19.116 [Daz] writes:
有没有办法使用C库函数来获取
目录及其内容的大小(不需要使用shell
命令)?
Is there any way using C library functions to get the size of a
directory and its contents (without resorting to using a shell
command)?



No.

-

给我们所有人的教训:即使在琐事中也有陷阱。

--Eric Sosman



No.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman


193.123.19.116 [Daz]在留言新闻中写道:< f4d49c98ee2e411a4b22a4cdeefe59a4 @TeraNews> ..
193.123.19.116 [Daz] wrote in message news:<f4d49c98ee2e411a4b22a4cdeefe59a4@TeraNews>.. .
是以任何方式使用C库函数来获取目录及其内容的大小(不需要使用shell命令)?

=============== ===================
海报的IP地址:193.123.19.116
发表于 http://nodevice.com
Linux程序员网站
Is there any way using C library functions to get the size of a directory and its contents (without resorting to using a shell command)?

==================================
Poster''s IP address: 193.123.19.116
Posted via http://nodevice.com
Linux Programmer''s Site




当然!您可以计算每个文件的大小,然后累积它们!


引用来自busybox:


long du(char * filename)

{

struct stat statbuf;

long sum;


if((lstat(filename,&) ; statbuf))!= 0){

perror_msg("%s",filename);

返回0;

}

if(du_depth == 0)

dir_dev = statbuf.st_dev;

else if(one_file_system&& dir_dev!= statbuf.st_dev)

返回0;


du_depth ++;

sum =(statbuf.st_blocks>> 1);


/ *不要添加符号链接指向的东西* /

if(S_ISLNK(statbuf.st_mode)){

sum = 0L;

if(du_depth == 1){

}

}

if(S_ISDIR( statbuf.st_mode)){

DIR * dir;

struct dirent * entry;

char * newfile;


dir = opendir(filename);

if(!dir){

du_dep th - ;

返回0;

}


newfile = last_char_is(filename,''/'');

if(newfile)

* newfile =''\ 0'';


while((entry = readdir(dir) ))){

char * name = entry-> d_name;


if((strcmp(name," ..")== 0)

|| (strcmp(name,"。")== 0)){

继续;

}

newfile = concat_path_file(filename,姓名);

总和+ = du(新文件);

免费(新文件);

}

closedir (dir);

print(sum,filename);

}

else if(statbuf.st_nlink> 1&&! count_hardlinks){

/ *只添加一次硬链接的文件* /

if(is_in_ino_dev_hashtable(& statbuf,NULL)){

sum = 0L;

if(du_depth == 1)

print(sum,filename);

}

else {

add_to_ino_dev_hashtable(& statbuf,NULL);

}

}

du_depth--;

返还金额;

}



Sure! You can calculate the size of each file, then accumulate them!

Quote from busybox:

long du(char *filename)
{
struct stat statbuf;
long sum;

if ((lstat(filename, &statbuf)) != 0) {
perror_msg("%s", filename);
return 0;
}
if (du_depth == 0)
dir_dev = statbuf.st_dev;
else if (one_file_system && dir_dev != statbuf.st_dev)
return 0;

du_depth++;
sum = (statbuf.st_blocks >> 1);

/* Don''t add in stuff pointed to by symbolic links */
if (S_ISLNK(statbuf.st_mode)) {
sum = 0L;
if (du_depth == 1) {
}
}
if (S_ISDIR(statbuf.st_mode)) {
DIR *dir;
struct dirent *entry;
char *newfile;

dir = opendir(filename);
if (!dir) {
du_depth--;
return 0;
}

newfile = last_char_is(filename, ''/'');
if (newfile)
*newfile = ''\0'';

while ((entry = readdir(dir))) {
char *name = entry->d_name;

if ((strcmp(name, "..") == 0)
|| (strcmp(name, ".") == 0)) {
continue;
}
newfile = concat_path_file(filename, name);
sum += du(newfile);
free(newfile);
}
closedir(dir);
print(sum, filename);
}
else if (statbuf.st_nlink > 1 && !count_hardlinks) {
/* Add files with hard links only once */
if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
sum = 0L;
if (du_depth == 1)
print(sum, filename);
}
else {
add_to_ino_dev_hashtable(&statbuf, NULL);
}
}
du_depth--;
return sum;
}


2003年7月24日19:18:15 -0700,
Kevin Wan< jf *** @ vip.sina.com>写道:
On 24 Jul 2003 19:18:15 -0700,
Kevin Wan <jf***@vip.sina.com> wrote:
193.123.19.116 [Daz]在消息新闻中写道:< f4d49c98ee2e411a4b22a4cdeefe59a4 @TeraNews> ...。
193.123.19.116 [Daz] wrote in message news:<f4d49c98ee2e411a4b22a4cdeefe59a4@TeraNews>.. .
有没有办法使用C库函数来获取大小一个
目录及其内容(不使用shell
命令)?
当然!你可以计算每个文件的大小,然后积累它们!

来自busybox的引用:

long du(char * filename)

struct stat statbuf;
Is there any way using C library functions to get the size of a
directory and its contents (without resorting to using a shell
command)?
Sure! You can calculate the size of each file, then accumulate them!

Quote from busybox:

long du(char *filename)
{
struct stat statbuf;




这不是C.

长款;

if((lstat(filename, & statbuf))!= 0){
perror_msg("%s",filename);



This is not C.
long sum;

if ((lstat(filename, &statbuf)) != 0) {
perror_msg("%s", filename);




这些都不是。


你把C与POSIX或其他与Unix相关的
标准混淆了。 OP没有声明他们是在一个实现POSIX标准的系统上。


到OP:ANSI C没有目录的概念,并且不,没有

直接获取文件大小的方法。你可以打开文件,并计算字符数,但你可能正在寻找一些特定于系统的实现,比如上一篇文章中提到的那些。你需要在一个谈论你的平台的小组中提出这些问题,因为你最有可能在那里得到最好的答案。


Martien

-

|

Martien Verbruggen |

Trading Post Australia |戴夫在这里,根密码是什么?

|



And these aren''t either.

You''re confusing C with POSIX, or one of the other Unix-related
standards. The OP did not state they were on a system that implements
the POSIX standard.

To the OP: ANSI C has no concept of a directory, and no, there is no
direct way to get the size of a file. You can open the file, and count
the characters, but you''re probably looking for some system specific
implementations, like the ones mentioned in the previous post. You
should ask these questions in a group that talks about your platform,
because you are most likely to get the best answer there.

Martien
--
|
Martien Verbruggen |
Trading Post Australia | Hi, Dave here, what''s the root password?
|


这篇关于计算目录大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆