2012-05-21 55 views
4

可能重複:
Get file's owner and group using boost如何使用boost文件系統確定文件或目錄的所有者?

我想使用boost ::文件系統,以確定哪些文件和目錄/文件夾由特定用戶所擁有。

我需要在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 
    DWORD dwRtnCode = 0; 
    PSID pSidOwner = NULL; 
    BOOL bRtnBool = TRUE; 
    LPTSTR AcctName = NULL; 
    LPTSTR DomainName = NULL; 
    DWORD dwAcctName = 1, dwDomainName = 1; 
    SID_NAME_USE eUse = SidTypeUnknown; 
    HANDLE hFile; 
    PSECURITY_DESCRIPTOR pSD = NULL; 


    // Get the handle of the file object. 
    hFile = CreateFile(
     TEXT(Filename.c_str()), 
     GENERIC_READ, 
     FILE_SHARE_READ, 
     NULL, 
     OPEN_EXISTING, 
     FILE_ATTRIBUTE_NORMAL, 
     NULL); 

    // Check GetLastError for CreateFile error code. 
    if (hFile == INVALID_HANDLE_VALUE) 
    { 
     DWORD dwErrorCode = 0; 

     dwErrorCode = GetLastError(); 
     _tprintf(TEXT("CreateFile error = %d\n"), dwErrorCode); 
     return false; 
    } 

    // Get the owner SID of the file. 
    dwRtnCode = GetSecurityInfo(
     hFile, 
     SE_FILE_OBJECT, 
     OWNER_SECURITY_INFORMATION, 
     &pSidOwner, 
     NULL, 
     NULL, 
     NULL, 
     &pSD); 

    // Check GetLastError for GetSecurityInfo error condition. 
    if (dwRtnCode != ERROR_SUCCESS) { 
     DWORD dwErrorCode = 0; 

     dwErrorCode = GetLastError(); 
     _tprintf(TEXT("GetSecurityInfo error = %d\n"), dwErrorCode); 
     return false; 
    } 

    // First call to LookupAccountSid to get the buffer sizes. 
    bRtnBool = LookupAccountSid(
     NULL,   // local computer 
     pSidOwner, 
     AcctName, 
     (LPDWORD)&dwAcctName, 
     DomainName, 
     (LPDWORD)&dwDomainName, 
     &eUse); 

    // Reallocate memory for the buffers. 
    AcctName = (LPTSTR)GlobalAlloc(
     GMEM_FIXED, 
     dwAcctName); 

    // Check GetLastError for GlobalAlloc error condition. 
    if (AcctName == NULL) { 
     DWORD dwErrorCode = 0; 

     dwErrorCode = GetLastError(); 
     _tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode); 
     return false; 
    } 

    DomainName = (LPTSTR)GlobalAlloc(
     GMEM_FIXED, 
     dwDomainName); 

    // Check GetLastError for GlobalAlloc error condition. 
    if (DomainName == NULL) { 
     DWORD dwErrorCode = 0; 

     dwErrorCode = GetLastError(); 
     _tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode); 
     return false; 

    } 

    // Second call to LookupAccountSid to get the account name. 
    bRtnBool = LookupAccountSid(
     NULL,     // name of local or remote computer 
     pSidOwner,    // security identifier 
     AcctName,    // account name buffer 
     (LPDWORD)&dwAcctName, // size of account name buffer 
     DomainName,    // domain name 
     (LPDWORD)&dwDomainName, // size of domain name buffer 
     &eUse);     // SID type 

    // Check GetLastError for LookupAccountSid error condition. 
    if (bRtnBool == FALSE) 
    { 
     DWORD dwErrorCode = 0; 

     dwErrorCode = GetLastError(); 

     if (dwErrorCode == ERROR_NONE_MAPPED) 
     { 
      _tprintf(TEXT("Account owner not found for specified SID.\n")); 
     } 
     else 
     { 
      _tprintf(TEXT("Error in LookupAccountSid.\n")); 
     } 
     return false; 

    } 
    else if (bRtnBool == TRUE) 
    { 

     // Print the account name. 
     _tprintf(TEXT("Account owner = %s\n"), AcctName); 
     Owner = AcctName; 
     return true; 
    } 
#endif 
    return false; 
} 

int main(int argc, char *argv[]) 
{ 
    std::string Filename = argv[0]; 
    if (argc > 1) 
    { 
     Filename = argv[1]; 
    } 
    std::cout << "File " << Filename << " is owned by: "; 
    // 
    std::string Owner; 
    bool OK = getOwner(Filename, Owner); 
    if (OK) 
    { 
     std::cout << Owner << "." << std::endl; 
    } 
    else 
    { 
     std::cout << "Unknown." << std::endl; 
    } 
    return 0; 
} 

回答

2

恐怕你正在努力實現未覆蓋了boost ::文件系統庫的內容。有可能檢索file permissions,但對於所有權,您將不得不求助於其他人或編寫自己的東西。

這裏是一個解釋如何做到這一點WindowsLinux

+1

這可以解釋爲什麼我在參考文獻中找不到它。感謝您的鏈接,我會用它們來提供這方面的功能,但仍然可以使用boost :: filesystem進行一般的文件系統工作。 – pugdogfan

相關問題