none
GetFileAttributes returns Error: (19, 'GetFileAttributes', 'The media is write protected.') for onedrive folder after Win10-1803 update

    Question

  • In below CPP code, I am calling GetFileAttributes  API to fetch file information. 

    I have created VSS snapshot via <g class="gr_ gr_16 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="16" id="16">wmic</g> <g class="gr_ gr_15 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="15" id="15">shadowcopy</g> call create Volume='C:\

    It works fine when I am running it as process But fails when running it as windows service.

    It fails only for one-drive folder and works fine for rest of file/folders on the system.

    It failing after Windows10-1803 update.

    #include <windows.h>
    #include <stdio.h>
    int main(){

        char *path = "\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy195\\Users\\Win10\\OneDrive";
        FILE *fp;
        fp=fopen("C:\\trunk\\err_19_log.txt", "w");
        int res1 = 0;
        res1 = GetFileAttributes(path);
        fprintf (fp, "\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy4\\Users\\Win10\\OneDrive%d\n",res1);
        fprintf (fp, "%d", GetLastError());
        printf("%d %d", res1,GetLastError());
        fclose(fp);
        return 0;
    }
    Monday, June 11, 2018 9:23 AM

All replies

  • Hi,

    thanks for posting here.

    >>It works fine when I am running it as process But fails when running it as windows service.

    It fails only for one-drive folder and works fine for rest of file/folders on the system.

    Could you please provide more information about your issue? Have you tried to use GetLastError to get the error code?

    I have tested the method with Win10 1803, it works fine with my test folder. The return value is 16 which means FILE_ATTRIBUTE_DIRECTORY.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 12, 2018 6:15 AM
  • Thank you, Baron, for your response.

    Error code from GetLastError is 19.

    To reproduce the issue following condition must be met.

    1) Create VSS snapshot on C:\. Let us say snapshot path  for C:\ is "\\\\\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy195

    2)  If username is Win10 then one drive folder is located at C:\\Users\\Win10\\OneDrive, So snapshot path will be "\\\\?\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy195\\Users\\Win10\\OneDrive"

    3) If there folder name "New" in one-drive then its snaphot path is "\\\\\\GLOBALROOT\\Device\\HarddiskVolumeShadowCopy195\\Users\\Win10\\OneDrive\\New" 

    4) Now, pass above snaphpot path in GetFileAttributes API.

    5)  GetFileAttributes on above path return error code 19, when running as window service.

    PS :

    1) It was working prior to Windows 1803 release.

    2) GetFileAttributes fails only for VSS snapshot path when "running as window service." 

    3) It fails only for the one-drive folder.







    • Edited by cmits2016 Wednesday, June 13, 2018 11:01 AM
    Wednesday, June 13, 2018 10:45 AM
  • Hi,

    >>1) It was working prior to Windows 1803 release.
    2) GetFileAttributes fails only for VSS snapshot path when "running as window service."
    3) It fails only for the one-drive folder.

    This error code means ERROR_WRITE_PROTECT. Have you checked the permission of this folder?

    You could try to use an admin account or call LogonUser before you call GetFileAttributes.

    If it still couldn't work, you could post this issue to Microsoft on this forum below.

    https://developercommunity.visualstudio.com/spaces/8/index.html

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 17, 2018 8:57 AM
  • Baron Bi,

    OneDrive backups no longer work from Windows Services, they only work via a Console APP when the "Files-on-demand" option is enabled in OneDrive.

    This is a serious problem that affects customers requiring their backup solution to work for their OneDrive files.

    The reason is that GetFileAttributes for OneDrive no longer works in Windows Service but works via Console app.

    The steps to reproduce are similar as  cmits2016 mentioned.

    You need to test using a Windows Service. The GetAttributes API works when called from a Console, for the same file!

    Steps:

    • Use a Windows Service for your tests. The windows service is configured to run as SYSTEM, as usually.
    • Run on Windows 10 Version 1803 (latest at the time of writing)
    • OneDrive configured with the Files-on-Demand option enabled
    • take a VSS snapshot of the OneDrive volume
    • call GetFileAttributes on the OneDrive root folder
    • call fails with "The media is write protected." (System Error 19).
    • call GetFileAttributes api on a OneDrive file available locally from that volume. The files that are not available locally are not expected to work, so that is fine.
    • call fails with "The media is write protected." (System Error 19).
    • You should see that the same calls works fine from a console app. This is proven by several of our developers. The console app runs as administrator (no permissions specially added). It would seem that any app other than Windows Service has no issues calling that method.
    Not being able to backup OneDrive files via VSS, through a Windows service is a recurring pattern for many backup apps (just search for it). Unfortunately, this post won't allow me to submit links "until my account is verified", as the message from the website says.

    • Edited by Sorin D Wednesday, July 25, 2018 12:46 PM Adding more details on how to reproduce
    Wednesday, July 25, 2018 11:46 AM
  • Thanks, Baron.

    1) It was working fine prior 1803 release.

    2) It also works fine in admin account or LogonUser context.

    3) The issue occurs for windows service only.


    • Edited by cmits2016 Monday, August 13, 2018 5:10 AM
    Monday, August 13, 2018 4:59 AM