ABu
ABu

Reputation: 12249

C++/boost: checking process permission

I'm writting a C++ program in order to make some static analyse and modifications over a website. I don't change the project files, but the files are copied, analysed and modified in a new folder.

Is there a way of checking, for example, using boost::filesystem, if I (the program/the user executing it) have permissions enough to read (files), execute (for processing the directory hierarchy) and write (files or create new folders) on a current folder and its files (or at least under unix systems)?

Upvotes: 0

Views: 2698

Answers (2)

Öö Tiib
Öö Tiib

Reputation: 10979

No, you can not check file permissions with boost program options library. File permissions can be queried with boost filesystem library:

#include <boost/filesystem.hpp> 
#include <stdio.h> 

namespace bfs=boost::filesystem;

int main(int argc,char * argv[])
{
    if (argc < 2) 
        return;

    bfs::path p(argv[1]);
    bfs::file_status s = status(p);
    printf("%X\n",s.permissions());
}

The values of permissions flags are as enum perms in boost/filesystem/v3/operations.hpp

Upvotes: 1

Raydel Miranda
Raydel Miranda

Reputation: 14360

IF you're using linux you can use stat function for get info about the file, including the mode permissions, the owner's ID and the owner's group ID of such file.

Then you can use getuid to compare the permissions of the user running the program with those obtained with stat.

Both links (that are actually man pages) has examples on how to use these functions.

Also you can try to do some operation over the selected file, lets say write, and then handle the exception properly. You don't have to access the file, just try to open it for writting, if you get and exception and errno is equal to EACCESS(permission denied) well, you don't have shuch permissions.

Upvotes: 0

Related Questions