0

enter image description hereI have just started exploring 'Boost' library. My main motive is to find the space details of a directory. I know this can be done using 'boost:: filesystem'. I am trying to write platform independent code. For windows, it is working fine. And gives proper output for given inputs. But, for a Linux(ubuntu) I can see weird outputs. As I am passing input through the command line, for every input it is showing the same output. I don't know what's wrong here. Your help will be appreciated. Thanks in advance. Also look into image.

code i have written:

int main(int argc, char*argv[])
{
#ifdef BOOST_WINDOWS_API
cout<<"\n Running on windows";
#else
cout<<"\n Running on linux";
#endif

if(argc !=2 )
{
    cout<<"\n Invalid inputs";
    return 0;
}

fs::path p(argv[1]);

fs::space_info sp = space(p);

cout<<"\n directory capacity is: "<<sp.capacity;
cout<<"\n directory free is: "<<((sp.free;
cout<<"\n directory available is: "<<sp.available;'

cout<<"\n directory capacity is: "<<((sp.capacity/1024)/1024);
cout<<"\n directory free is: "<<((sp.free)/1024);
cout<<"\n directory available is: "<<((sp.available/1024)/1024);
}
sonu gupta
  • 473
  • 8
  • 21

1 Answers1

0

For every input it is showing the same output as it should be, if the paths you do supply are on the same file system (same device and partition).

  • capacity -- total size of the filesystem, in bytes
  • free -- free space on the filesystem, in bytes
  • available -- free space available to a non-privileged process (may be equal or less than free)

If you had it different on Windows, then you either used several partitions or pointed at restricted folders.

On POSIX systems all directories on partition by default share space on that partition . If you want find how much space is used by files in that directory, you have to iterate all files in directory and summarize sizes (that's what windows' dir command or linux ls utility does).

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
  • Oh ok. But i want to find directory space. How i can find using 'Boost' library. Is there any provision or not. I dont have much experience on 'Boost Library'. Thanks for your reply. – sonu gupta Apr 17 '17 at 06:49
  • @sonu gupta WHAT is directory space? all directories on partition by default share space on that partition unless some non-posix file system drivers are used. If you want find how much space is used by files in that directory, you have to iterate all files in directory and summarize sizes (that's what window's dir command or linux ls does) – Swift - Friday Pie Apr 17 '17 at 06:53
  • @sonu shell script is better option to achieve this goal – PapaDiHatti Apr 17 '17 at 06:54
  • @Kapil not portable at all – Swift - Friday Pie Apr 17 '17 at 06:55
  • 1
    @Sonu check this to find directory space http://stackoverflow.com/questions/10015341/size-of-a-directory – PapaDiHatti Apr 17 '17 at 06:58
  • But is there any function in boost that can iterate files in directories and return its size? Also i want my code to be platform independent. Scrips are not good option. I want to achieve this by making function in cpp only. – sonu gupta Apr 17 '17 at 06:58
  • @sonu gupta Kapil just gave a link, boost do contain directory_iterators and it can be used with std:: since C++17. – Swift - Friday Pie Apr 17 '17 at 07:01
  • @Swift and Kapil Thank you for your quick efforts. Really appreciate it. I will go through that link. Once done, I will share my thoughts. – sonu gupta Apr 17 '17 at 07:08
  • @Kapil and swift it worked for me. So as far as now is concern, my understanding is 'Space_info' gives details about 'Filesystem'. But to get directory's size, we need to iterate through each file and do calculations. – sonu gupta Apr 18 '17 at 06:36