Search code examples
winapionedrive

How to detect OneDrive online-only files


Starting from Windows 10 Fall Creators Update (version 16299.15) and OneDrive build 17.3.7064.1005 the On-Demand Files are available for users (https://support.office.com/en-us/article/learn-about-onedrive-files-on-demand-0e6860d3-d9f3-4971-b321-7092438fb38e)

Any OneDrive file now can have one of the following type: online-only, locally available, and always available.

Using WinAPI how can I know that the file (e.g. "C:\Users\Username\OneDrive\Getting started with OneDrive.pdf") is online-only file?


Solution

  • After years, I'm still using FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS attribute described here to determine if a file or a directory is completely present locally or not.

    Microsoft docs says the following for FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS:

    When this attribute is set, it means that the file or directory is not fully present locally. For a file that means that not all of its data is on local storage (e.g. it may be sparse with some data still in remote storage). For a directory it means that some of the directory contents are being virtualized from another location. Reading the file / enumerating the directory will be more expensive than normal, e.g. it will cause at least some of the file/directory content to be fetched from a remote store. Only kernel-mode callers can set this bit.

    There are some advantages of FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS:

    1. It can be used for both files and directories.
    2. It can be set in kernel mode only, so there is no chance for anyone to set the attribute arbitrary.

    And as it described in this answer, there are still some interesting undocumented attributes which can provide additional information about cloud files.

    Note: I didn't accept Jonathan Potter's answer because I mentioned FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS attribute in comments and started using it a year earlier than he updated his answer.