MCPcopy Create free account
hub / github.com/deathkiller/jazz2-native / ChangePermissions

Method ChangePermissions

Sources/Shared/IO/FileSystem.cpp:2632–2668  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2630 }
2631
2632 bool FileSystem::ChangePermissions(StringView path, Permission mode)
2633 {
2634 if (path.empty()) return false;
2635
2636#if defined(DEATH_TARGET_WINDOWS)
2637 SmallVector<wchar_t, MAX_PATH + 1> pathW(path.size() + 1);
2638 Utf8::ToUtf16(pathW.data(), std::int32_t(pathW.size()), path.data(), std::int32_t(path.size()));
2639 DWORD attrs = ::GetFileAttributesW(pathW.data());
2640 if (attrs != INVALID_FILE_ATTRIBUTES) {
2641 if ((mode & Permission::Write) == Permission::Write && (attrs & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) {
2642 // Adding the write permission
2643 attrs &= ~FILE_ATTRIBUTE_READONLY;
2644 return ::SetFileAttributesW(pathW.data(), attrs);
2645 } else if ((mode & Permission::Write) != Permission::Write && (attrs & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY) {
2646 // Removing the write permission
2647 attrs |= FILE_ATTRIBUTE_READONLY;
2648 return ::SetFileAttributesW(pathW.data(), attrs);
2649 }
2650 return true;
2651 }
2652 return false;
2653#else
2654 auto nullTerminatedPath = String::nullTerminatedView(path);
2655# if defined(DEATH_TARGET_ANDROID)
2656 if (AndroidAssetStream::TryGetAssetPath(nullTerminatedPath)) {
2657 return false;
2658 }
2659# endif
2660 struct stat sb;
2661 if (::stat(nullTerminatedPath.data(), &sb) != 0) {
2662 return false;
2663 }
2664 const std::uint32_t currentMode = sb.st_mode;
2665 std::uint32_t newMode = AddPermissionsToCurrent(currentMode & ~(S_IRUSR | S_IWUSR | S_IXUSR), mode);
2666 return (::chmod(nullTerminatedPath.data(), newMode) == 0);
2667#endif
2668 }
2669
2670 bool FileSystem::AddPermissions(StringView path, Permission mode)
2671 {

Callers

nothing calls this directly

Calls 6

AddPermissionsToCurrentFunction · 0.85
statClass · 0.70
ToUtf16Function · 0.50
emptyMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected