FILE_FLAG_BACKUP_SEMANTICS

N

Nick Tucker

Hi everyone

I have a data backup program which I'm trying to modify to allow backing up
of files in use.

The documentation implies this can be done if I grant myself the
SE_BACKUP_NAME privilege, then use CreateFile() specifying flag
FILE_FLAG_BACKUP_SEMANTICS. Unfortunately, this is not working for me.

The test rig code I'm using is as follows:

HRESULT ModifyPrivilege( IN LPCTSTR szPrivilege,
IN BOOL fEnable)
{
HRESULT hr = S_OK;
TOKEN_PRIVILEGES NewState;
LUID luid;
HANDLE hToken = NULL;

// Open the process token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES |
TOKEN_QUERY,
&hToken ))
{
printf("Failed OpenProcessToken\n");
return ERROR_FUNCTION_FAILED;
}

// Get the local unique ID for the privilege.
if ( !LookupPrivilegeValue( NULL,
szPrivilege,
&luid ))
{
CloseHandle( hToken );
printf("Failed LookupPrivilegeValue\n");
return ERROR_FUNCTION_FAILED;
}

// Assign values to the TOKEN_PRIVILEGE structure.
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes =
(fEnable ? SE_PRIVILEGE_ENABLED : 0);

// Adjust the token privilege.
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
0,
NULL,
NULL))
{
printf("Failed AdjustTokenPrivileges\n");
hr = ERROR_FUNCTION_FAILED;
}

// Get the local unique ID for the privilege.
if ( !LookupPrivilegeValue(NULL,
szPrivilege,
&luid ))
{
CloseHandle( hToken );
printf("Failed LookupPrivilegeValue\n");
return ERROR_FUNCTION_FAILED;
}

// Close the handle.
CloseHandle(hToken);

return hr;
}

int main(int argc, const char *argv[])
{
HANDLE h;
HRESULT hr;

hr = ModifyPrivilege(SE_BACKUP_NAME, TRUE);

if (!SUCCEEDED(hr))
printf("\nFailed to modify privilege.\n");
else
printf("\nSuccessfully modified privilege.\n");

h=CreateFile("C:\\test.xls",
READ_CONTROL,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (h!=NULL)
{
CloseHandle(h);
printf("\nSuccessfully opened file");
}
else
printf("\nFailed to open file");
}

Unfortunately, when this code is run, 0xffffffff is always returned by
CreateFile() if the file is open by Excel, with GetLastError() returning
error 32 'The process cannot access the file because it is in use by another
process'. I have tried this on a variety of operating systems and using
Administrative login accounts who are members of the 'Backup Operators'.

Why?

Surely, the whole point of having the FILE_FLAG_BACKUP_SEMANTICS
would be to allow this.

Thanks in advance for any input

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top