Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to read/process locked files?

How to read/process locked files?

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasesysadminwindows-adminhelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZystemsK
    wrote on last edited by
    #1

    Hi all, I'm working on a backup project in C++ for Windows XP and Server 2003. I want to be able to backup locked files like the registry and database files. I've looked into Volume Shadow Copy and it seems like the right direction to head, but I can't understand how to implement it in C++. Here is our current method for backing up a file. 1. FindFirstFile/FindNextFile 2. Send path to file to compression function 3. Compression function simply calls zip.exe with the proper arguments (path to file, path to zip, etc) 3a. zip.exe is Info-ZIP.org's zip program. My major issue with shadow volumes is that I want to directly access the files and not copy them. Since I'm trying to backup database files, it doesn't make sense to copy them (they are several GB in size sometimes). I'm curious to know if there are other methods outside of VSS to read/process locked files. The majority of enterprise backup solutions can do it without any issue or delay, so I know it's possible, I just don't know how. I've looked at Hobocopy and it seems to be a valid option, but I don't think it'll work with my current compression method. I tried creating a shadow copy and using the \\?\GLOBALROOT\... path with zip.exe, but it failed, of course. Thanks.

    D P 2 Replies Last reply
    0
    • Z ZystemsK

      Hi all, I'm working on a backup project in C++ for Windows XP and Server 2003. I want to be able to backup locked files like the registry and database files. I've looked into Volume Shadow Copy and it seems like the right direction to head, but I can't understand how to implement it in C++. Here is our current method for backing up a file. 1. FindFirstFile/FindNextFile 2. Send path to file to compression function 3. Compression function simply calls zip.exe with the proper arguments (path to file, path to zip, etc) 3a. zip.exe is Info-ZIP.org's zip program. My major issue with shadow volumes is that I want to directly access the files and not copy them. Since I'm trying to backup database files, it doesn't make sense to copy them (they are several GB in size sometimes). I'm curious to know if there are other methods outside of VSS to read/process locked files. The majority of enterprise backup solutions can do it without any issue or delay, so I know it's possible, I just don't know how. I've looked at Hobocopy and it seems to be a valid option, but I don't think it'll work with my current compression method. I tried creating a shadow copy and using the \\?\GLOBALROOT\... path with zip.exe, but it failed, of course. Thanks.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      What if you granted your code the SE_BACKUP_NAME privilege and then used CreateFile() with FILE_FLAG_BACKUP_SEMANTICS?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      Z 1 Reply Last reply
      0
      • D David Crow

        What if you granted your code the SE_BACKUP_NAME privilege and then used CreateFile() with FILE_FLAG_BACKUP_SEMANTICS?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        Z Offline
        Z Offline
        ZystemsK
        wrote on last edited by
        #3

        Here's my sample code and output:

        #include <windows.h>
        #include <iostream>
        #include <time.h>
        #include <tchar.h>

        using namespace std;
        BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
        HANDLE hFile;

        void main() {

        BOOL ret = SetPrivilege(SE\_BACKUP\_NAME,TRUE);
        cout << "Ret: " << ret << endl;
        
        DWORD dwErr=0;
        hFile = CreateFile(L"c:\\\\windows\\\\system32\\\\config\\\\system", GENERIC\_READ, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE, NULL, OPEN\_EXISTING, FILE\_FLAG\_BACKUP\_SEMANTICS, 0);
        cout << "Handle: " << hFile << endl;
        if (INVALID\_HANDLE\_VALUE == hFile) {
        	dwErr = GetLastError();
        	CloseHandle(hFile);
        	cout << "Could not open file: " << dwErr << endl;
        }
        
        CloseHandle(hFile);
        SetPrivilege(SE\_BACKUP\_NAME,FALSE);
        

        }

        BOOL SetPrivilege(LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
        {
        TOKEN_PRIVILEGES tp;
        LUID luid;
        HANDLE hToken;

        OpenProcessToken(GetCurrentProcess(), TOKEN\_ADJUST\_PRIVILEGES | TOKEN\_QUERY, &hToken);
        if ( !LookupPrivilegeValue(NULL, lpszPrivilege, &luid) )    
        	return FALSE; 
        
        tp.PrivilegeCount = 1;
        tp.Privileges\[0\].Luid = luid;
        
        if (bEnablePrivilege)
        	tp.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
        else
            tp.Privileges\[0\].Attributes = 0;
        
        AdjustTokenPrivileges(hToken, FALSE, &tp, 0, (PTOKEN\_PRIVILEGES) NULL, 0); 
        
        return ( (GetLastError()!=ERROR\_SUCCESS)?FALSE:TRUE);
        

        }

        Ret: 1 Handle: FFFFFFFF Could not open file: 32 As you can see, it gave an Error 32, which is: ERROR_SHARING_VIOLATION 32 (0x20) The process cannot access the file because it is being used by another process. I think I'm doing it properly... :mad:

        1 Reply Last reply
        0
        • Z ZystemsK

          Hi all, I'm working on a backup project in C++ for Windows XP and Server 2003. I want to be able to backup locked files like the registry and database files. I've looked into Volume Shadow Copy and it seems like the right direction to head, but I can't understand how to implement it in C++. Here is our current method for backing up a file. 1. FindFirstFile/FindNextFile 2. Send path to file to compression function 3. Compression function simply calls zip.exe with the proper arguments (path to file, path to zip, etc) 3a. zip.exe is Info-ZIP.org's zip program. My major issue with shadow volumes is that I want to directly access the files and not copy them. Since I'm trying to backup database files, it doesn't make sense to copy them (they are several GB in size sometimes). I'm curious to know if there are other methods outside of VSS to read/process locked files. The majority of enterprise backup solutions can do it without any issue or delay, so I know it's possible, I just don't know how. I've looked at Hobocopy and it seems to be a valid option, but I don't think it'll work with my current compression method. I tried creating a shadow copy and using the \\?\GLOBALROOT\... path with zip.exe, but it failed, of course. Thanks.

          P Offline
          P Offline
          Peter Weyzen
          wrote on last edited by
          #4

          You'll need to update your code (based on hobocopy) to use this API -- ExposeSnapshot() -- will cause the snapshot to be mounted to a drive letter. Letting you use a "normal path". http://msdn.microsoft.com/en-us/library/aa382662(VS.85).aspx[^]

          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

          Z 1 Reply Last reply
          0
          • P Peter Weyzen

            You'll need to update your code (based on hobocopy) to use this API -- ExposeSnapshot() -- will cause the snapshot to be mounted to a drive letter. Letting you use a "normal path". http://msdn.microsoft.com/en-us/library/aa382662(VS.85).aspx[^]

            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)

            Z Offline
            Z Offline
            ZystemsK
            wrote on last edited by
            #5

            Thanks for the hint, Peter, but when I added the following, the program crashes. I assume I'm missing something.

            IVssBackupComponents *pReq;
            VSS_ID snapID;
            PWSTR wszExposed;

            CLSIDFromString(L"{3c8b7455-4f04-4ead-b666-578ddb608138}", &snapID);
            hres = pReq->ExposeSnapshot(snapID, NULL, VSS\_VOLSNAP\_ATTR\_EXPOSED\_LOCALLY, L"x:", &wszExposed);
            

            I am using vshadow -wait c: on a test machine to generate the snapshot, so I'm not sure if that will cause any conflicts. I'll go ahead and tinker with HoboCopy and toss it in there to see what happens. Thanks.

            Z 1 Reply Last reply
            0
            • Z ZystemsK

              Thanks for the hint, Peter, but when I added the following, the program crashes. I assume I'm missing something.

              IVssBackupComponents *pReq;
              VSS_ID snapID;
              PWSTR wszExposed;

              CLSIDFromString(L"{3c8b7455-4f04-4ead-b666-578ddb608138}", &snapID);
              hres = pReq->ExposeSnapshot(snapID, NULL, VSS\_VOLSNAP\_ATTR\_EXPOSED\_LOCALLY, L"x:", &wszExposed);
              

              I am using vshadow -wait c: on a test machine to generate the snapshot, so I'm not sure if that will cause any conflicts. I'll go ahead and tinker with HoboCopy and toss it in there to see what happens. Thanks.

              Z Offline
              Z Offline
              ZystemsK
              wrote on last edited by
              #6

              Doing more research on ExposeSnapshot(), aside from my above implementation being incorrect (don't need the {}'s), I don't think it works on XP. The snapshot needs to be created with a specific context property, either VSS_CTX_APP_ROLLBACK or VSS_CTX_NAS_ROLLBACK. Sadly, XP does not support setting a different context other than the default, VSS_CTX_BACKUP. So, it seems that using ExposeSnapshot is not a viable option as it needs to work on XP and Server 2003. Thanks, though. I learned quite a bit more about VSS by looking through this.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups