Re: Determining the size of a directory Part 2
Re: Determining the size of a directory Part 2
- Subject: Re: Determining the size of a directory Part 2
- From: stephane sudre <email@hidden>
- Date: Mon, 25 Oct 2004 14:50:12 +0200
On Oct 25, 2004, at 2:42 PM, Dominik Freyer wrote:
Hi everybody,
I'm still trying to get the size of a directory. After some tips from
this List I tried to use the 'du' command line utility which works
very fast. I wanted to use it as an NSTask. Now I've got the problem,
that I don't get any data back from the utility. I think I set the
FileHandle correctly. Somehow, the programm begins to spin when
reaching the line duOutput=[fromDu readDataOfLength:10]; I checked in
the debugger and to me it seems like it just waits.
Here is the complete method:
- (unsigned long long)getSizeOfDirectory:(NSString*)path
{
NSAutoreleasePool *poolForSize=[[NSAutoreleasePool alloc]init];
NSArray *args;
NSPipe *fromPipe;
NSFileHandle *fromDu;
NSData *duOutput;
unsigned char aBuffer[70];
args = [NSArray arrayWithObjects:@"-ks",path,nil];
fromPipe=[NSPipe pipe];
fromDu=[[NSFileHandle alloc]init];
fromDu=[fromPipe fileHandleForReading];
NSTask *duTool=[[NSTask alloc]init];
[duTool setLaunchPath:@"/usr/bin/du"];
[duTool setStandardOutput:fromDu];
[duTool setArguments:args];
[duTool launch];
duOutput=[fromDu readDataOfLength:10];
[duOutput getBytes:aBuffer];
unsigned long long output=123;
return (unsigned long long)output;
[duTool release];
[poolForSize release];
}
Any ideas?
Use the POSIX API?
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <stdio.h>
unsigned long long ComputeFolderSize(const char * inFolderPath)
{
FTS *fts;
long totalblocks=0;
char * const argv[2]={inFolderPath,NULL};
unsigned long long folderSize=0;
if ((fts = fts_open(argv, FTS_PHYSICAL, NULL)) != NULL)
{
int rval;
FTSENT *p;
for (rval = 0; (p = fts_read(fts)) != NULL;)
{
switch (p->fts_info)
{
case FTS_D: /* Ignore. */
break;
case FTS_DP:
totalblocks += p->fts_statp->st_blocks;
break;
case FTS_DC: /* Ignore. */
break;
case FTS_DNR: /* Warn, continue. */
case FTS_ERR:
case FTS_NS:
rval = 1;
break;
default:
if (p->fts_statp->st_nlink > 1)
break;
totalblocks += p->fts_statp->st_blocks;
break;
}
}
if (errno==0)
{
folderSize=howmany(totalblocks, 2);
}
}
return (folderSize*1024);
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden