2

I have the following code so that I can get the size of a directory on my system

size_t size = 0;

for(recursive_directory_iterator it("/Library/Caches");
    it!=recursive_directory_iterator();
    ++it)
{
    if(!is_directory(*it))
        size+=file_size(*it);
}

cout << "Size of Directory: " << size << endl;

The problem with this is if boost encounters an error because of file permissions it throws and error. This has been a known bug with boost, from what I've read.

I thought if I could give my program read and write permissions for each item in the directory, that could fix this problem. So I found the following function in boost.

permissions(file_path, add_perms|owner_write|group_write|others_write);

I'm not 100% sure based on the current code above where to use this. I tried passing *it to the permissions function but that seems to throw an error as well.

wondering the best way to recursively change the permissions on a directory and files so that I can then use the recursive_directory_iterator without error in order to get the file size of all the items in a given directory.

mattwallace
  • 4,132
  • 6
  • 43
  • 77
  • This is an OS issue. You can't change file permissions unless you are the owner of the file or you are root. – Richard Hodges Sep 05 '16 at 16:32
  • 1
    ...otherwise permissions wouldn't be permissions would they? They'd be advisories :) – Richard Hodges Sep 05 '16 at 16:32
  • Ok so is there a way to become "root" in other words prompt the user for root password. If successful then I can recursively check that directory. – mattwallace Sep 05 '16 at 16:35
  • a few ways. you can mark the executable with the "sticky" permission flag which allows itself to switch effective user to root. For other ways in code I'd have to read the linux/osx manual... – Richard Hodges Sep 05 '16 at 16:37
  • Ok that sounds interesting. can the executable be marked "sticky" programmatically or is that something at the OS lever that has to be done outside of the program ? I think what I would rather do is just prompt the user for root password when needed. That seems like the safer option. – mattwallace Sep 05 '16 at 16:41
  • lots of fun reading here: http://stackoverflow.com/questions/2483755/how-to-programmatically-gain-root-privileges – Richard Hodges Sep 05 '16 at 16:43

0 Answers0