Skip to content

Commit

Permalink
Add MileDeleteFileIgnoreReadonlyAttributeByHandle function.
Browse files Browse the repository at this point in the history
  • Loading branch information
MouriNaruto committed Nov 10, 2023
1 parent 3066f94 commit 2d2639c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Mile.Helpers/Mile.Helpers.Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,35 @@ EXTERN_C BOOL WINAPI MileDeleteFileByHandle(
&DispostionInfoEx,
sizeof(FILE_DISPOSITION_INFO_EX));
}

EXTERN_C BOOL WINAPI MileDeleteFileIgnoreReadonlyAttributeByHandle(
_In_ HANDLE FileHandle)
{
BOOL Result = FALSE;

DWORD OldAttribute = 0;
// Save old attributes.
if (::MileGetFileAttributesByHandle(FileHandle, &OldAttribute))
{
// Get hardlink count.
DWORD HardlinkCount = 0;
if (::MileGetFileHardlinkCountByHandle(FileHandle, &HardlinkCount))
{
// Remove readonly attribute.
if (::MileSetFileAttributesByHandle(
FileHandle,
OldAttribute & (-1 ^ FILE_ATTRIBUTE_READONLY)))
{
// Delete the file.
Result = ::MileDeleteFileByHandle(FileHandle);
if (!Result || HardlinkCount > 1)
{
// Restore attributes if failed or had another hard links.
::MileSetFileAttributesByHandle(FileHandle, OldAttribute);
}
}
}
}

return Result;
}
12 changes: 12 additions & 0 deletions Mile.Helpers/Mile.Helpers.Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,16 @@ EXTERN_C BOOL WINAPI MileGetFileHardlinkCountByHandle(
EXTERN_C BOOL WINAPI MileDeleteFileByHandle(
_In_ HANDLE FileHandle);

/**
* @brief Deletes an existing file, even the file have the readonly attribute.
* @param FileHandle The handle of the file to be deleted. This handle must be
* opened with the appropriate permissions for the requested
* change. This handle should not be a pipe handle.
* @return If the function succeeds, the return value is nonzero. If the
* function fails, the return value is zero. To get extended error
* information, call GetLastError.
*/
EXTERN_C BOOL WINAPI MileDeleteFileIgnoreReadonlyAttributeByHandle(
_In_ HANDLE FileHandle);

#endif // !MILE_WINDOWS_HELPERS_BASE
1 change: 1 addition & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@
- Add MileSetFileAttributesByHandle function.
- Add MileGetFileHardlinkCountByHandle function.
- Add MileDeleteFileByHandle function.
- Add MileDeleteFileIgnoreReadonlyAttributeByHandle function.

0 comments on commit 2d2639c

Please sign in to comment.