LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2010, 02:30 PM   #1
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Rep: Reputation: 18
Check available space on a directory with c++


Hi,

I'm using c++ and Ubuntu and I need to write/copy some files to a directory chosen by the user of my system (the user can save it on any folder of any disk of the pc or other usb device). Before I do it, I need to know if there's space available on the disk. I cannot only check the result of write() function, the validation must be executed before I start to save or copy.

I was trying to use statvsf structure, but I only have the complete filepath (/home/lobinho/myDocs/), not the disk path. The statvfs() function only works with disk path (i.e. /media/KINGSTON ).

The source of my function:

Code:
int MyClass::availableSpace(string dst, ulong* availableSize) {
    int result = ERROR;
    try {
        struct statvfs devData;
        memset(&devData, 0, sizeof (struct statvfs));
        if ((statvfs(dst.c_str(), &devData)) >= 0) {
            if (availableSize != NULL) {
                //I don't know if it's right, but I'll set availableSize only if the available size doesn't pass the ulong limit.
                if (devData.f_bfree * devData.f_bsize > 4294967295UL) {
                    *availableSize = 4294967295UL;
                } else { 
                    *availableSize = devData.f_bfree * devData.f_bsize;
                }
            }
            result = OK;
        }
    } catch (...) {

    }
    return result;
}
This function works perfectly with the disk path.

Is there any other function that could help me?
I've tried search references on cplusplus, google, but no answer until now... :/

thanks in advance!
 
Old 09-23-2010, 10:58 PM   #2
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
Ahhh.... can't you use an Operating System call to check the used and available space. I forget the exact commands but I know they are there.
 
Old 09-24-2010, 06:41 AM   #3
Lobinho
Member
 
Registered: May 2010
Distribution: Ubuntu
Posts: 72

Original Poster
Rep: Reputation: 18
Thanks for your response gothrog.

There's "du" and "df".
du works showing the size of some file
df shows the available space on the disk (like statvfs())

I think both command can't help me.
 
Old 09-25-2010, 06:52 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Why can't you use du or df? Also, why are you doing this in C++ rather than a shell script?
 
Old 09-25-2010, 10:23 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It seems to me, you need the functionality of df. You can use it directly, by launching it as a child process, capturing and parsing its output to determine the amount of free space on the appropriate filesystem. Or, you could acquire the source code for df, and use the methods found in it to build the functionality into your program.
You can read & parse /proc/mounts to divine on which filesystem the target file would be written. There may be other useful data in the /proc virtual filesystem, although I was unable to locate anything after a quick scan.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Check disk space used in a directory cli_man Linux - General 5 03-13-2009 03:28 PM
Not enough space for root directory on a drive with 50g free space??? auoq Linux - Newbie 1 10-13-2004 12:44 PM
How to check space sikandar Linux - Software 12 09-16-2003 10:58 AM
How to check HD space? sikandar Linux - Software 10 09-01-2003 04:09 AM
check space? dsgdevil Linux - Software 1 02-21-2003 12:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration