7

I am working on a project to make a database of the files I have on current directory. And one of the details I want about my files is the file permissions that are set with chmod in ubuntu. (just a note: I will be needing the group and owner info too - like chown- and if you could let me know if boost can retrieve the ownership info too that'd be great.)

I am using boost filesystem library and I have checked the documentation for numerous times but couldn't find how to get the permissions.

In this page it shows that there's enum perms that has the file permission strings which doesn't show up on my own filesystem.hpp. (And I have checked that i've got the 1.49 version, also built from the source just to be sure). Also on the same page here it shows that it can get the permissions like:

perms permissions() const noexcept; 
//Returns: The value of
//permissions() specified by the postconditions of the most recent call
//to a constructor, operator=, or permissions(perms) function.

I haven't been able to find the permissions function nor the place where it stores the perms list.

This is the code I have so far (which is actually from boost tutorials, but I modified it to be recursive), if you could tell me how to get the file permissions/ownerships or suggest another library than boost I would appreciate it.

EDIT: I have added the s.permissions() as ethan_liou suggested however the output was not as expected. Here's the updated code and the output.

//  filesystem tut4.cpp  ---------------------------------------------------------------//

//  Copyright Beman Dawes 2009

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <stdio.h> 
using namespace std;
using namespace boost::filesystem;
int read(path p);

int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        cout << "Usage: tut4 path\n";
        return 1;
    }

    path p (argv[1]);   // p reads clearer than argv[1] in the following code
    read(p);


    return 0;
}
int read(path p) {
    try
    {
        if (exists(p))    // does p actually exist?
        {
            if (is_symlink(p)) {
                cout << p << " is a link\n";

            }
            else if (is_regular_file(p)) {
                // is p a regular file?


                file_status s = status(p);


                cout << p << " size is " << file_size(p) << " perms " << "" ;
                printf("%o\n",s.permissions());
            }

            else if (is_directory(p))      // is p a directory?
            {

                cout << p << " is a directory containing:\n";

                typedef vector<path> vec;             // store paths,
                vec v;                                // so we can sort them later

                copy(directory_iterator(p), directory_iterator(), back_inserter(v));

                sort(v.begin(), v.end());             // sort, since directory iteration
                // is not ordered on some file systems

                for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it)
                {
                    //cout << "   " << *it << '\n';
                    read(*it);
                }
            }
            else
                cout << p << " exists, but is neither a regular file nor a directory\n";
        }
        else
            cout << p << " does not exist\n";
    }

    catch (const filesystem_error& ex)
    {
        cout << ex.what() << '\n';
    }
    return 0;
}

Output:

$ ./a.out ~/Desktop/test
"/home/usr/Desktop/test" is a directory containing:
"/home/usr/Desktop/test/a.out" size is 69446 perms 27746424350
"/home/usr/Desktop/test/la" is a directory containing:
"/home/usr/Desktop/test/la/Untitled Document" size is 0 perms 27746424170
"/home/usr/Desktop/test/la/lala" is a directory containing:
"/home/usr/Desktop/test/la/lala/Link to lalalala" is a link
"/home/usr/Desktop/test/la/lala/Untitled Folder" is a directory containing:
"/home/usr/Desktop/test/la/lala/lalalala" size is 0 perms 0
"/home/usr/Desktop/test/test.cpp" size is 2234 perms 0
"/home/usr/Desktop/test/test.cpp~" size is 2234 perms 0

Note: Those numbers that are like 27746424350 change each time the program is executed.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
Logan
  • 10,649
  • 13
  • 41
  • 54

2 Answers2

10
perms      permissions() const                { return m_perms; } 

defined in boost/filesystem/v3/operations.hpp

Add an easy sample code

#include <boost/filesystem.hpp> 
#include <stdio.h> 
namespace fs=boost::filesystem;
int main(int argc,char * argv[]){
    fs::path p(argv[1]);
    fs::file_status s = status(p);
    printf("%o\n",s.permissions());
}
Ethan Liou
  • 1,622
  • 13
  • 12
  • thank you, do you happen to know how to get the permissions of the file by any chance? – Logan Mar 19 '12 at 19:33
  • i've tested it like you suggested but i've not been able get the permissions as 777 etc. They showed up as random numbers that change each time the program is executed, or they show up as 0. I have posted the new source and the output I get. – Logan Mar 20 '12 at 13:51
  • I've tested your code, it run perfectly. didn't you test my program? – Ethan Liou Mar 20 '12 at 14:06
  • I did but I got the same result... I think I've a problem with compiling. How do you compile it? What I do is this: `$ g++ test.cpp -lboost_filesystem -lboost_system` – Logan Mar 21 '12 at 13:10
  • Me,too what's your platform? I tested on centos 6 – Ethan Liou Mar 21 '12 at 13:58
  • Ubuntu 11.10. That's a bit weird though, I don't understand what those random numbers are... you do get 777 or 770 instead of those long numbers I get right? Do you have any suggestions? – Logan Mar 21 '12 at 14:22
  • actually let me ask this first, what happens when you test your code with an argument that points to a non-existent file? such as `$ ./a.out asdasd` Do you get random long numbers? – Logan Mar 21 '12 at 14:26
-5

File permissions example for windows:

unsigned long attributes = ::GetFileAttributes( filePath.file_string().c_str());

if ( attributes != 0xFFFFFFFF && ( attributes & FILE_ATTRIBUTE_READONLY ))
{
    attributes &= ~FILE_ATTRIBUTE_READONLY;
    ::SetFileAttributes( filePath.file_string().c_str(), attributes );
}
  • 3
    -1 It doesn't use `boost` like OP asked. Also it looks like you just copied the code from somewhere without knowing what it does. – quantum Sep 23 '12 at 00:47
  • 4
    -1, The question is specifically about boost on Ubuntu. I don't think Win32 code is likely to be particularly useful. – Martin Sep 23 '12 at 01:12