可能重复:
使用boost获取文件的所有者和组
我想使用boost :: filesystem来确定特定用户拥有哪些文件和目录/文件夹.
我需要在Linux(ext3)和Windows(NTFS)文件系统上执行此操作.
如何使用boost :: filesystem获取指定路径的用户标识符?
提前致谢.
编辑:从所选答案派生的代码:
#include <iostream>
#include <string>
#ifdef LINUX
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
#else
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include "accctrl.h"
#include "aclapi.h"
#pragma comment(lib, "advapi32.lib")
#endif
bool getOwner(const std::string &Filename, std::string &Owner)
{
#ifdef LINUX
struct stat FileInfo;
stat(Filename.c_str(), &FileInfo);
struct passwd *UserDatabaseEntry = getpwuid(FileInfo.st_uid);
struct group *GroupDatabaseEntry = getgrgid(FileInfo.st_gid);
//
if (UserDatabaseEntry != 0)
{
Owner = UserDatabaseEntry->pw_name;
return true;
}
else
{
return false;
}
#else …Run Code Online (Sandbox Code Playgroud)