Determining the size of a directory Part 3
Determining the size of a directory Part 3
- Subject: Determining the size of a directory Part 3
- From: Dominik Freyer <email@hidden>
- Date: Sun, 31 Oct 2004 14:12:58 +0100
Hi there,
I'm still trying to determine directory sizes for big filesystem trees. I've been running about a problem I couldn't get rid of. My function that uses the du tool in a NSTask to get the size of a file or dir runs most of the time very well. If the file that has to be size-determined is a symbolic link the size will be 4 (which is correct) and gets returned, but after returning to the calling method all variables of this method are empty and I get an EXC_BAD_ACCESS. I know, that I get this, because I'm trying to access some values of the no more available variables. This behaviour doesn't appear, if a directory or every other file is determined.
I got rid of this problem by testing the file-type and ignoring symbolic links, but I found the same problem with a file in my ~/Library/Application Support/Adress Book/images Folder called 6D35A9EE-063A-11D8-9905-0030657AB734. It is a 4 KB file (the same size of a symbolic link) but not shown as a link in Finder or Terminal. Here the both important methods:
This method uses du as a NSTask and returns the determined size to the calling method
- (NSNumber*)getSizeOfDirectory:(NSString*)path
{
NSNumber *sizeField;
if([[[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:NO]fileType]!=NSFileTypeSymbolicLink ||
[[[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:NO]fileType]!=NSFileTypeUnknown)
{
NSArray *args;
NSPipe *fromPipe;
NSFileHandle *fromDu;
NSData *duOutput;
NSString *size;
NSArray *stringComponents;
unsigned char aBuffer[70];
args = [NSArray arrayWithObjects:nil];
fromPipe=[NSPipe pipe];
fromDu=[fromPipe fileHandleForWriting];
NSTask *duTool=[[NSTask alloc]init];
[duTool setLaunchPath:@"/usr/bin/du"];
[duTool setStandardOutput:fromDu];
[duTool setArguments:args];
[duTool launch];
duOutput=[[fromPipe fileHandleForReading] availableData];
[duOutput getBytes:aBuffer];
size=[NSString stringWithCString:aBuffer];
stringComponents=[size pathComponents];
size=[stringComponents objectAtIndex:0];
size=[size substringToIndex:[size length]-1];
return sizeField =[NSNumber numberWithUnsignedLongLong:(unsigned long long)[size doubleValue]];
}
else return sizeField=[NSNumber numberWithUnsignedLongLong:(unsigned long 0];
}
This is the calling method which calls getSizeOfDirectory.
- (int)x y:(int)y xdelta:(int)xdelta ydelta:(int)ydelta givenPath:(NSString*)givenPath sizeOfParent:(unsigned long long)sizeOfParent
{
if([[NSFileManager defaultManager] fileExistsAtPath:givenPath])
{
NSArray *pathContents = [[NSFileManager defaultManager] directoryContentsAtPath:givenPath];
NSString *dirContent,*completePath;
NSNumber *sizeOfChosenObject;
int i,xvalue,ratio;
xvalue=x;
0;i<[pathContents count];i++)
{
dirContent = [pathContents objectAtIndex:i];
completePath = [givenPath stringByAppendingString:dirContent];
sizeOfChosenObject = [self getSizeOfDirectory:completePath];
ratio = [sizeOfChosenObject unsignedLongLongValue] * xdelta / sizeOfParent;
NSLog(completePath);
[self lockFocus];
[NSBezierPath strokeRect:NSMakeRect(xvalue,0,ratio,xdelta)];
[self unlockFocus];
xvalue=xvalue+ratio;
if([[[NSFileManager defaultManager] fileAttributesAtPath:[[givenPath stringByAppendingString:dirContent] stringByAppendingString:@"/"] traverseLink:NO]fileType]==NSFileTypeDirectory)
[self drawSequoiaField:xvalue
y:0
xdelta:0
ydelta:0
givenPath:[[givenPath stringByAppendingString:dirContent] stringByAppendingString:@"/"]
sizeOfParent:[sizeOfChosenObject unsignedLongLongValue]];
}
}
}
Another smaller problem I have is a small memory leak I have in getSizeOfDirectory or drawSequoiaField. I found this with
MallocDebug, but couldn't get the problem. Does anybody see it?
Is it right that every class that begins with a NS (like NSString, NSNumber or NSArray) is already autoreleased?
Thanks for your help in advance
Dominik _______________________________________________
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