title: How can I determine the owner of a file or directory using boost filesystem?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/10289491/get-files-owner-and-group-using-boost">Get file&#39;s owner and group using boost</a>  </p>
  </blockquote>
  
  
  
  <p>I'd like to use boost::filesystem to determine which files and directories/ folders are owned by a particular user.</p>
  
  <p>I need to do this on both Linux (ext3) and Windows (NTFS) filesystems.</p>
  
  <p>How can I get the user identifier for a specified path using boost::filesystem?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Edit: Code derived from selected answer:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #ifdef LINUX
  #include &lt;pwd.h&gt;
  #include &lt;grp.h&gt;
  #include &lt;sys/stat.h&gt;
  #else
  #include &lt;stdio.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;tchar.h&gt;
  #include "accctrl.h"
  #include "aclapi.h"
  #pragma comment(lib, "advapi32.lib")
  #endif
  
  bool getOwner(const std::string &amp;Filename, std::string &amp;Owner)
  {
  #ifdef LINUX
      struct stat FileInfo;
      stat(Filename.c_str(), &amp;FileInfo);
      struct passwd *UserDatabaseEntry  = getpwuid(FileInfo.st_uid);
      struct group  *GroupDatabaseEntry = getgrgid(FileInfo.st_gid);
      //
      if (UserDatabaseEntry != 0)
      {
          Owner = UserDatabaseEntry-&gt;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,
          &amp;pSidOwner,
          NULL,
          NULL,
          NULL,
          &amp;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)&amp;dwAcctName,
          DomainName,
          (LPDWORD)&amp;dwDomainName,
          &amp;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)&amp;dwAcctName,   // size of account name buffer 
          DomainName,             // domain name
          (LPDWORD)&amp;dwDomainName, // size of domain name buffer
          &amp;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 &gt; 1)
      {
          Filename = argv[1];
      }
      std::cout &lt;&lt; "File " &lt;&lt; Filename &lt;&lt; " is owned by: ";
      //
      std::string Owner;
      bool OK = getOwner(Filename, Owner);
      if (OK)
      {
          std::cout &lt;&lt; Owner &lt;&lt; "." &lt;&lt; std::endl;
      }
      else
      {
          std::cout &lt;&lt; "Unknown." &lt;&lt; std::endl;
      }
      return 0;
  }
  </code></pre>