Results 1 to 10 of 12
Hi
I am quite new to linux. I am writing an application in C++ where I would like the application to automatically check the drive space remaining on a USB ...
- 10-01-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 5
Free space check in C application
Hi
I am quite new to linux. I am writing an application in C++ where I would like the application to automatically check the drive space remaining on a USB mass storage device prior to proceeding. My distro is SuSE 9.3.
What I am hopeing for is something like the following.
int main()
{
space_remaining = check_free_space("/media/USBTHUMBDRIVE");
if(space_remaining < 100000){
stderr("Not enough space\n");
return 0;
} else {
//continue program
}
return 0;
}
I don't think it would be this simple but does anyone know how to do this? Or if it is even possible?
dydx
- 10-01-2007 #2
I think you can use the df command through a script to manipulate the output: Linux and UNIX df command help
I can't find C libraries for checking free space. If I find them, I'll post the links...
- 10-01-2007 #3
You can easily get the free space for a mounted file system from your C/C++ program. Do this at the command line:
and all will be revealed.Code:man statvfs
- 10-04-2007 #4Just Joined!
- Join Date
- Oct 2007
- Posts
- 5
Great, statvfs (though I am using <sys/statfs.h>) works a treat for what I want.
One other question though. The statfs() function returns a statfs structure containing all the information you need regarding a file system.
I am trying to use this function to get the free space remaining on a USB flash drive in my application. The part I don't know how to do is to mount the usb flash drive from within my application. currently I must type:
before running my application. I would like to not have to do this and have the mount command somehow within my application.Code:mount /dev/sdb1 /media/my_thumb_drive
Cheers
Peter
- 10-04-2007 #5will help your C code execute any arbitrary shell command line.Code:
man 3 system
You might even want to do another one at the end of your program to (gasp!) unmount the drive.
Hope this helps.
- 10-04-2007 #6
There is also a mount(), its prototype in sys/mount.h.
- 10-05-2007 #7Just Joined!
- Join Date
- Oct 2007
- Posts
- 5
Ok, I've made further progress with this now. Everything works. I can mount the USB drive from within my application and then determine the free space remaining on it.
Here is where the problem lies:
The reason why I had to mount the USB drive is because when it is inserted it is mounted by "submount" as a "subfs". When you use "mount" to mount a USB drive, the drive is mounted as a "vfat".
The problem with subfs is that it invisible to the user (ie subfs mounts are not recorded by the "df" command). subfs is good because you don't need to unmount in order to remove the USB drive, however I can't figure out how to get the drive information.
I have tried mounting the USB drive again using mount and I get two of the same, a drive mounted at /media/USBTHUMBDRIVE both as a vfat and a subfs. This causes all sorts of problems when it comes to disconnecting.
Can anyone help? Ideally I need a way to check the free space on the USB Drive from within my C application without mounting anything (that is to rely on submount to perform mounting and unmounting).
- 10-05-2007 #8
I never heard of subfs, you might be able to disable this behavior in udev, if that's a solution. Otherwise I am at a loss.
- 10-05-2007 #9
Um, petedaws, if subfs allows the USB drive to be accessed without mounting it, that means you can read files on that drive, yes? What happens if you do a statfs() on a file on that drive?
- 10-05-2007 #10Just Joined!
- Join Date
- Oct 2007
- Posts
- 5
Ok, when I use statfs() with the USB drive mounted as a vfat it returns the correct values for:
long f_bsize; /* optimal transfer block size */
long f_blocks; /* total data blocks in file system */
long f_bfree; /* free blocks in fs */
The usb drive is also shown mounted when you call the df command.
When I use statfs() with the USB drive mounted as a subfs, the statfs structure returns (long)0 for the above the parameters. I haven't looked at the return values for the other parameters.
As I mentioned previously when mounted as a subfs, the df command does not display the USB drive as a mounted file system.


Reply With Quote
