如何使用C++ Boost库获取文件权限?

7

我正在开展一个项目,以制作我当前目录中文件的数据库。我想了解有关我的文件的详细信息中包括在Ubuntu中使用chmod设置的文件权限(请注意:我还需要组和所有者信息-例如chown,并且如果您可以让我知道boost是否也能检索所有权信息,那就太棒了)。

我正在使用boost文件系统库,并且已经多次查看了文档,但无法找到如何获取权限的方法。

在这个页面上,它显示有一个枚举perms,其中包含文件权限字符串,但这些内容在我的filesystem.hpp上并未显示。(而且我已经确认我已经获得了1.49版本,也从源代码构建了一遍,以确保)。同样在同一页上,它说明可以像这样获得权限:

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教程中修改成递归),如果您能告诉我如何获取文件权限/所有权或建议使用比boost更好的库,我将不胜感激。
编辑:我已按照ethan_liou的建议添加了s.permissions(),但输出结果与预期不符。以下是更新后的代码和输出结果。
//  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个回答

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

谢谢,你碰巧知道如何获取文件的权限吗? - Logan
我按照你建议的方式进行了测试,但是我无法获得777等权限。它们显示为每次执行程序时都会更改的随机数字,或者显示为0。我已经发布了新的源代码和输出结果。 - Logan
我已经测试了你的代码,它运行得非常完美。难道你没有测试过我的程序吗? - Ethan Liou
我尝试过了,但是得到了相同的结果... 我认为我的编译有问题。你是如何编译的?我通常这样做:$ g++ test.cpp -lboost_filesystem -lboost_system - Logan
实际上,让我先问一下,如果您使用指向不存在的文件的参数测试代码会发生什么?例如 $ ./a.out asdasd,您会得到随机的长数字吗? - Logan
显示剩余2条评论

-5

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
这段话的意思是:它没有像 OP 所要求的那样使用 boost 库。而且看起来你只是从某个地方复制了代码,却不知道它是做什么的。 - quantum
4
这个问题特别涉及到在Ubuntu上的boost。我认为Win32代码可能不是特别有用。 - Martin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接