Changeset 1939


Ignore:
Timestamp:
05/12/2007 22:40:01 (4 years ago)
Author:
chris
Message:

Close process token in EnableBackupRights?(), thanks Charles!

Improve error messages in EnableBackupRights?() when failing to
enable the backup privilege.

Use file size returned by GetFileInformationByHandle? in emu_fstat
instead of calling GetFileSizeEx?(), thanks Charles!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/chris/general/lib/win32/emu.cpp

    r1814 r1939  
    165165struct passwd gTempPasswd; 
    166166 
    167 bool EnableBackupRights( void ) 
     167bool EnableBackupRights() 
    168168{ 
    169169        HANDLE hToken; 
     
    171171 
    172172        //open current process to adjust privileges 
    173         if( !OpenProcessToken( GetCurrentProcess( ),  
    174                 TOKEN_ADJUST_PRIVILEGES,  
    175                 &hToken )) 
    176         { 
    177                 printf( "Cannot open process token: error %d\n",  
    178                         (int)GetLastError() ); 
     173        if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES,  
     174                &hToken)) 
     175        { 
     176                ::syslog(LOG_ERR, "Failed to open process token: %s", 
     177                        GetErrorMessage(GetLastError()).c_str()); 
    179178                return false; 
    180179        } 
     
    183182        //first, look up the LUID for the backup privilege 
    184183 
    185         if( !LookupPrivilegeValue( NULL, //this system 
     184        if (!LookupPrivilegeValue( 
     185                NULL, //this system 
    186186                SE_BACKUP_NAME, //the name of the privilege 
    187                 &( token_priv.Privileges[0].Luid )) ) //result 
    188         { 
    189                 printf( "Cannot lookup backup privilege: error %d\n",  
    190                         (int)GetLastError( ) ); 
     187                &( token_priv.Privileges[0].Luid ))) //result 
     188        { 
     189                ::syslog(LOG_ERR, "Failed to lookup backup privilege: %s", 
     190                        GetErrorMessage(GetLastError()).c_str()); 
     191                CloseHandle(hToken); 
    191192                return false; 
    192193        } 
     
    199200        // any need to save current state 
    200201 
    201         if( !AdjustTokenPrivileges( hToken, //our process token 
     202        if (!AdjustTokenPrivileges( 
     203                hToken, //our process token 
    202204                false,  //we're not disabling everything 
    203205                &token_priv, //address of structure 
    204                 sizeof( token_priv ), //size of structure 
    205                 NULL, NULL )) //don't save current state 
     206                sizeof(token_priv), //size of structure 
     207                NULL, NULL)) //don't save current state 
    206208        { 
    207209                //this function is a little tricky - if we were adjusting 
    208210                //more than one privilege, it could return success but not 
    209211                //adjust them all - in the general case, you need to trap this 
    210                 printf( "Could not enable backup privileges: error %d\n",  
    211                         (int)GetLastError( ) ); 
     212                ::syslog(LOG_ERR, "Failed to enable backup privilege: %s", 
     213                        GetErrorMessage(GetLastError()).c_str()); 
     214                CloseHandle(hToken); 
    212215                return false; 
    213216 
    214217        } 
    215         else 
    216         { 
    217                 return true; 
    218         } 
     218 
     219        CloseHandle(hToken); 
     220        return true; 
    219221} 
    220222 
     
    703705        ULARGE_INTEGER conv; 
    704706        conv.HighPart = fi.nFileIndexHigh; 
    705         conv.LowPart = fi.nFileIndexLow; 
     707        conv.LowPart  = fi.nFileIndexLow; 
    706708        st->st_ino = (_ino_t)conv.QuadPart; 
    707709 
     
    717719        else 
    718720        { 
    719                 // size of the file 
    720                 LARGE_INTEGER st_size; 
    721                 memset(&st_size, 0, sizeof(st_size)); 
    722  
    723                 if (!GetFileSizeEx(hdir, &st_size)) 
    724                 { 
    725                         ::syslog(LOG_WARNING, "Failed to get file size: " 
    726                                 "%s", GetErrorMessage(GetLastError()).c_str()); 
    727                         errno = EACCES; 
    728                         return -1; 
    729                 } 
    730  
    731                 conv.HighPart = st_size.HighPart; 
    732                 conv.LowPart = st_size.LowPart; 
     721                conv.HighPart = fi.nFileSizeHigh; 
     722                conv.LowPart  = fi.nFileSizeLow; 
    733723                st->st_size = (_off_t)conv.QuadPart; 
    734724        } 
     
    10791069 
    10801070        pDir->name = ConvertUtf8ToWideString(dirName.c_str()); 
    1081         // We are responsible for freeing dir->name 
     1071        // We are responsible for freeing dir->name with delete[] 
    10821072         
    10831073        if (pDir->name == NULL) 
Note: See TracChangeset for help on using the changeset viewer.