Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging statfs?

I am using statfs to retrieve the total space available on my hard drive. It returns the correct values most of the time, but sometimes it just returns an error for no apparent reason. I want to find out why it generates an error at seemingly random times.

Is there a way I could check why it's generating an error? If I print strerror(errno) I simply get the message "No such file or directory". This tells me what the problem is, but not whats causing it. I don't see any reason why it's able successfully to find the directory at a certain moment but then not be able to locate it the next moment. Is there any way I could check what's causing this? I doubt if it's a problem with my code because if that was the case, if wouldn't ever return the correct data.

I am passing in '/'as the directory. I am on OS X Snowleopard using Xcode 3.2.6 using Objective-C/C

  • How could I locate the problem that's causing it to not find the path?
  • What are some general reasons that would cause it to be able to find the drive at one moment and not the next?

Code used to retrieve info:

    if (statfs(&path, &storageStats))
    {               
        NSLog(@"Total storage stats retrieval failed with errno: %s.\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    else
    {
        totalAmount = storageStats.f_blocks * storageStats.f_bsize;
        NSLog(@"Storage: %f\n", totalAmount);
        return self;
    }

I get the "Total storage stats retrieveal failed ..." and the errno message is "No such file or directory"

like image 860
fdh Avatar asked Jun 29 '26 09:06

fdh


1 Answers

The first argument to statfs is a char *. Since you are passing &path, that would imply that path is a char. If that is the case, then you are passing a null terminated string only sometimes. (If path is '/' and the next byte happens to be '\0', then the call will work. If the memory after path is not '\0', then you are passing a very strange path to statfs.) You probably meant to do:

char *path = "/";
statfs( path, ... )
like image 113
William Pursell Avatar answered Jul 01 '26 23:07

William Pursell