7

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?

Rom098
  • 2,445
  • 4
  • 35
  • 52

3 Answers3

5

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.

Rom098
  • 2,445
  • 4
  • 35
  • 52
  • Cool thanks! I just put it in code: https://rokdd.xyz/tech/on-demand-files-in-python-reading-file-attributes-in-windows/ – rokdd Jan 10 '22 at 19:17
4

To check for "online only" all you need is to call GetFileAttributes() and see if the FILE_ATTRIBUTE_OFFLINE attribute is set.

In fact this isn't new for OneDrive, that attribute has existed for a long time.

There are other OneDrive attributes available via the shell (although the property you need is PKEY_StorageProviderState rather than PKEY_FilePlaceholderStatus) but "online only" is easy to check for.

Edit: Another filesystem attribute, FILE_ATTRIBUTE_PINNED is new for Windows 10, and is used by OneDrive to indicate a file that's "always available".

Edit: As of 2019 it appears that OneDrive now uses FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS rather than FILE_ATTRIBUTE_OFFLINE, as suggested below.

Edit: PKEY_StorageProviderState was broken in Windows 10 1903, and still not fixed in 1909. It returns 4 ("uploading") for all files in any apps other than Explorer.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Are you sure FILE_ATTRIBUTE_OFFLINE is applicable for OneDrive files? – Rom098 Mar 16 '18 at 16:29
  • @Rom098 Yes. CD to your OneDrive folder in a DOS prompt and run the `attrib` command if you don't believe me :) – Jonathan Potter Mar 16 '18 at 17:09
  • some people believe that GetFileAttributes call on offline OneDrive files may cause downloading of the file. Is it true, what do you think? – Rom098 Mar 16 '18 at 19:53
  • It doesn't. But you can easily test this for yourself. – Jonathan Potter Mar 16 '18 at 20:14
  • There are few more attributes, e.g. FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS (https://superuser.com/questions/1214542/what-do-new-windows-8-10-attributes-mean-no-scrub-file-x-integrity-v-pinn/1287315) It looks like FILE_ATTRIBUTE_OFFLINE can be set by anyone, unlike FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS which can be set only in kernel mode. So FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS seems more preferrable. – Rom098 Mar 19 '18 at 15:47
  • Many apps are getting very confused by OneDrive on demand. Also looking for white papers on what on needs to do make one's app OneDrive on-demand friendly. Even PowerShell isn't compatible - use Copy-Item on a file that's just a placeholder (not downloaded) and it copies the right number of bytes but the target file is full of zeros, i.e. Copy-Item isn't triggering the file to be downloaded first. Not good! – Rob Nicholson Jun 05 '18 at 00:27
  • Hmm maybe Windows 10/PowerShell has been updated. Copy-Item now appears to work with placeholders. You also get an "Cloud provider not running" error if OneDrive isn't running – Rob Nicholson Jun 05 '18 at 00:31
1

Take a look at the PKEY_FilePlaceholderStatus property for the file (at the shell level, not the file-system level). This blog post has a example program you can test. This question also hints to some undocumented properties you might want to take a look at.

Microsoft has a UWP example on MSDN.

Anders
  • 97,548
  • 12
  • 110
  • 164