I'm using NSFileManager to get the size of some files in our application.
Each file is examined by a separate thread and the result for all the files is the same wrong value. The following code is ran for each file in a separate thread:
NSFileManager *fileManager= [[NSFileManager alloc] init];
NSUInteger completedFileSize = [[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];
NSLog(@"Part %lu: %@ => size:%lu actual size:%lu", myPart.SequenceNumber, fileName, completedFileSize, myPart.Size);
The output is:
Part 1: /Users/David/TEMP/245E0BF53ECA/part1 => size:36864 actual size:37187
Part 5: /Users/David/TEMP/245E0BF53ECA/part5 => size:36864 actual size:37187
Part 2: /Users/David/TEMP/245E0BF53ECA/part2 => size:36864 actual size:37187
Part 4: /Users/David/TEMP/245E0BF53ECA/part4 => size:36864 actual size:37187
Part 3: /Users/David/TEMP/245E0BF53ECA/part3 => size:36864 actual size:37187
Part 6: /Users/David/TEMP/245E0BF53ECA/part6 => size:36864 actual size:37187
Part 7: /Users/David/TEMP/245E0BF53ECA/part7 => size:36864 actual size:37186
Part 0: /Users/David/TEMP/245E0BF53ECA/part0 => size:36864 actual size:37189
Also output of ls -l command on terminal is identical to the correct file sizes previously saved in myPart.Size
-rw-r--r-- 1 David staff 37189 Nov 25 11:25 part0
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part1
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part2
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part3
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part4
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part5
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part6
-rw-r--r-- 1 David staff 37186 Nov 25 11:25 part7
I'm using OS X 10.8 and Xcode 4.5.
Any help would be highly appreciated.
I Found the actual problem!
The file is not closed before the:
[[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];
so I closed the file handler associated with fileName variable for path and the problem is solved.
fclose(myFile); //this is the handler for fileName
//now the value returned from the below line is correct.
[[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];
Thanks from everyone.