Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just 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

  2. #2
    Just Joined! Gustav's Avatar
    Join Date
    May 2007
    Posts
    13
    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...

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You can easily get the free space for a mounted file system from your C/C++ program. Do this at the command line:
    Code:
    man statvfs
    and all will be revealed.

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    5
    Quote Originally Posted by wje_lf View Post
    You can easily get the free space for a mounted file system from your C/C++ program. Do this at the command line:
    Code:
    man statvfs
    and all will be revealed.
    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:

    Code:
    mount /dev/sdb1 /media/my_thumb_drive
    before running my application. I would like to not have to do this and have the mount command somehow within my application.

    Cheers

    Peter

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    man 3 system
    will help your C code execute any arbitrary shell command line.

    You might even want to do another one at the end of your program to (gasp!) unmount the drive.

    Hope this helps.

  6. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    There is also a mount(), its prototype in sys/mount.h.

  7. #7
    Just 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).

  8. #8
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    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.

  9. #9
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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. #10
    Just Joined!
    Join Date
    Oct 2007
    Posts
    5
    Quote Originally Posted by wje_lf View Post
    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?
    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.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...