From e778d35fdc76c74644bf712046b2af3995c50972 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 26 Apr 2017 16:11:16 +0100 Subject: [PATCH] Refactored some internal win32 file attribute functions --- modules/juce_core/native/juce_win32_Files.cpp | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/modules/juce_core/native/juce_win32_Files.cpp b/modules/juce_core/native/juce_win32_Files.cpp index 5a3a2d0284..fed4fc5eac 100644 --- a/modules/juce_core/native/juce_win32_Files.cpp +++ b/modules/juce_core/native/juce_win32_Files.cpp @@ -35,12 +35,25 @@ //============================================================================== namespace WindowsFileHelpers { - DWORD getAtts (const String& path) + DWORD getAtts (const String& path) noexcept { return GetFileAttributes (path.toWideCharPointer()); } - int64 fileTimeToTime (const FILETIME* const ft) + bool changeAtts (const String& path, DWORD bitsToSet, DWORD bitsToClear) noexcept + { + auto oldAtts = getAtts (path); + + if (oldAtts == INVALID_FILE_ATTRIBUTES) + return false; + + auto newAtts = ((oldAtts | bitsToSet) & ~bitsToClear); + + return newAtts == oldAtts + || SetFileAttributes (path.toWideCharPointer(), newAtts) != FALSE; + } + + int64 fileTimeToTime (const FILETIME* const ft) noexcept { static_assert (sizeof (ULARGE_INTEGER) == sizeof (FILETIME), "ULARGE_INTEGER is too small to hold FILETIME: please report!"); @@ -149,7 +162,7 @@ bool File::hasWriteAccess() const if (fullPath.isEmpty()) return true; - const DWORD attr = WindowsFileHelpers::getAtts (fullPath); + auto attr = WindowsFileHelpers::getAtts (fullPath); // NB: According to MS, the FILE_ATTRIBUTE_READONLY attribute doesn't work for // folders, and can be incorrectly set for some special folders, so we'll just say @@ -159,17 +172,11 @@ bool File::hasWriteAccess() const || (attr & FILE_ATTRIBUTE_READONLY) == 0; } -bool File::setFileReadOnlyInternal (const bool shouldBeReadOnly) const +bool File::setFileReadOnlyInternal (bool shouldBeReadOnly) const { - const DWORD oldAtts = WindowsFileHelpers::getAtts (fullPath); - - if (oldAtts == INVALID_FILE_ATTRIBUTES) - return false; - - const DWORD newAtts = shouldBeReadOnly ? (oldAtts | FILE_ATTRIBUTE_READONLY) - : (oldAtts & ~FILE_ATTRIBUTE_READONLY); - return newAtts == oldAtts - || SetFileAttributes (fullPath.toWideCharPointer(), newAtts) != FALSE; + return WindowsFileHelpers::changeAtts (fullPath, + shouldBeReadOnly ? FILE_ATTRIBUTE_READONLY : 0, + shouldBeReadOnly ? 0 : FILE_ATTRIBUTE_READONLY); } bool File::setFileExecutableInternal (bool /*shouldBeExecutable*/) const