Skip to content

Commit bed6c5c

Browse files
nikola-shLiryna
authored andcommitted
Fix SetFileAttributes behavior for memfs sample
Fixes issue that `SetFileAttributes(..., FILE_ATTRIBUTE_NORMAL)` can clean attributes prohibited to clean, `FILE_ATTRIBUTE_DIRECTORY` for example. See https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesw.
1 parent fd852a5 commit bed6c5c

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

samples/dokan_memfs/memfs_operations.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,15 +463,24 @@ static NTSTATUS DOKAN_CALLBACK memfs_setfileattributes(
463463
fileattributes);
464464
if (!f) return STATUS_OBJECT_NAME_NOT_FOUND;
465465

466-
// No attributes need to be changed
467-
if (fileattributes == 0) return STATUS_SUCCESS;
466+
// from https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-setfileattributesw
467+
DWORD const attributes_allowed_to_set =
468+
FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_NORMAL |
469+
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED | FILE_ATTRIBUTE_OFFLINE |
470+
FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM |
471+
FILE_ATTRIBUTE_TEMPORARY;
468472

469-
// FILE_ATTRIBUTE_NORMAL is override if any other attribute is set
470-
if (fileattributes & FILE_ATTRIBUTE_NORMAL &&
471-
(fileattributes & (fileattributes - 1)))
472-
fileattributes &= ~FILE_ATTRIBUTE_NORMAL;
473+
fileattributes &= attributes_allowed_to_set;
473474

474-
f->attributes = fileattributes;
475+
DWORD new_file_attributes =
476+
(f->attributes & ~attributes_allowed_to_set) | fileattributes;
477+
478+
// FILE_ATTRIBUTE_NORMAL is overriden if any other attribute is set
479+
if ((new_file_attributes & FILE_ATTRIBUTE_NORMAL) &&
480+
(new_file_attributes & ~static_cast<DWORD>(FILE_ATTRIBUTE_NORMAL)))
481+
new_file_attributes &= ~static_cast<DWORD>(FILE_ATTRIBUTE_NORMAL);
482+
483+
f->attributes = new_file_attributes;
475484
return STATUS_SUCCESS;
476485
}
477486

0 commit comments

Comments
 (0)