alphaleonis / AlphaFS

AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes.
http://alphafs.alphaleonis.com/
MIT License
566 stars 99 forks source link

SetFsoDateTimeCore should always use BackupSemantics #372

Closed damiarnold closed 6 years ago

damiarnold commented 6 years ago

In the function SetFsoDateTimeCore(), there is a conditional set of flags/attributes for the underlying CreateFile() native function call.

The initial flags/attributes set for the call is determined by this line in the function: var attributes = isFolder ? ExtendedFileAttributes.BackupSemantics : ExtendedFileAttributes.ReadOnly;

This is problematic for a couple of reasons:

  1. This function calls CreateFileCore() with FileMode.Open (value of '3') which corresponds to CreateFile's dwCreationDisposition of OPEN_EXISTING. According to documentation (see https://msdn.microsoft.com/en-us/library/windows/desktop/aa363874(v=vs.85).aspx) all FILEATTRIBUTE values passed to CreateFile's dwFlagsAndAttributes parameter are ignored when opening existing files and only FILEFLAG values are honored. For this reason, the use of the ExtendedFileAttributes.ReadOnly is useless for existing files, anyway.

  2. Trying to set any file times for files that can only be accessed via privilege elevation require the BackupSemantics flag to be in play. It is currently not possible to use AlphaFS to set file times for files that are "blocked" for the current user (e.g. no effective allow ACEs in play), even if that user has the ability to override security checks via the Restore privilege.

I'm not sure why the distinction is made between files and folders for this function. My local fix is to simply always set the BackupSemantics flag, and remove the check for isFolder. However, once that is done, this function no longer has any use for the isFolder parameter, and all calling functions should be cleaned up to not pass this parameter anymore.

Let me know if I should create a patch for this function and all calling functions, and whether there is some other functionality or side effect I'm just missing here as for why this was setup this way in the first place.

Thanks!

damiarnold commented 6 years ago

In looking through the code base some more, it appears that the following logic is in a number of functions when calling File.CreateFileCore():

isFolder ? ExtendedFileAttributes.BackupSemantics : ExtendedFileAttributes.ReadOnly

Same reasoning as listed above may apply to some of these other cases as well....

Yomodo commented 6 years ago

Thanks for the patch offer; I've decided to do it myself as a chance to get back "into" the AlphaFS code.

damiarnold commented 6 years ago

No worries! I'll be watching for updates and test when ready.

damiarnold commented 6 years ago

Looks like the update is working for my case. Thanks!