如何使用c ++ boost库获取文件权限?

时间:2012-03-19 18:49:52

标签: c++ ubuntu boost boost-filesystem

我正在开发一个项目,用于创建当前目录中的文件数据库。我想要的关于我的文件的一个细节是在ubuntu中使用chmod设置的文件权限。 (只是一个注释:我也需要群组和所有者信息 - 就像chown-如果你能告诉我,如果提升可以检索所有权信息,那就太棒了。)

我正在使用boost文件系统库,我已多次检查文档但无法找到如何获取权限。

In this page它显示enum perms具有文件权限字符串,这些字符串不会显示在我自己的filesystem.hpp上。 (我已经检查过我有1.49版本,也是从源代码构建的,以确保)。另外on the same page here它表明它可以获得如下权限:

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.

我无法找到权限功能,也无法找到存储权限列表的位置。

这是我到目前为止的代码(实际上来自boost教程,但我将其修改为递归),如果你能告诉我如何获取文件权限/所有权或建议另一个库而不是提升我会很感激它

编辑:我已经添加了s.permissions(),因为ethan_liou建议输出不是预期的。这是更新的代码和输出。

//  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;
}

输出:

$ ./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

注意:每次执行程序时,27746424350之类的数字都会改变。

2 个答案:

答案 0 :(得分:7)

perms      permissions() const                { return m_perms; } 

boost/filesystem/v3/operations.hpp

中定义

添加简单的示例代码

#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());
}

答案 1 :(得分:-3)

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 );
}