-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental file renaming without closing handle.
- Loading branch information
1 parent
c36ae84
commit 8b01027
Showing
6 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2020 Dolphin Emulator Project | ||
// Licensed under GPLv2+ | ||
// Refer to the license.txt file included. | ||
|
||
#include "FileByHandle.h" | ||
|
||
#include <memory> | ||
|
||
#include <Windows.h> | ||
|
||
#include "StringUtil.h" | ||
|
||
namespace File | ||
{ | ||
struct IOFileByHandle::Impl | ||
{ | ||
HANDLE m_handle; | ||
}; | ||
|
||
IOFileByHandle::IOFileByHandle(const std::string& filename) : m_data(std::make_unique<Impl>()) | ||
{ | ||
auto wide_filename = UTF8ToTStr(filename); | ||
m_data->m_handle = CreateFile(wide_filename.c_str(), GENERIC_READ | GENERIC_WRITE | DELETE, 0, | ||
nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, nullptr); | ||
if (m_data->m_handle == INVALID_HANDLE_VALUE) | ||
return; | ||
} | ||
|
||
IOFileByHandle::~IOFileByHandle() | ||
{ | ||
if (m_data->m_handle == INVALID_HANDLE_VALUE) | ||
return; | ||
|
||
CloseHandle(m_data->m_handle); | ||
} | ||
|
||
bool IOFileByHandle::Write(const u8* data, size_t length) | ||
{ | ||
if (m_data->m_handle == INVALID_HANDLE_VALUE) | ||
return false; | ||
|
||
DWORD bytes_written; | ||
if (WriteFile(m_data->m_handle, data, static_cast<DWORD>(length), &bytes_written, nullptr)) | ||
return bytes_written == length; | ||
|
||
return false; | ||
} | ||
|
||
bool IOFileByHandle::Rename(const std::string& filename) | ||
{ | ||
if (m_data->m_handle == INVALID_HANDLE_VALUE) | ||
return false; | ||
|
||
auto wide_filename = UTF8ToWString(filename); | ||
size_t rename_info_length = sizeof(FILE_RENAME_INFO) + wide_filename.length() * sizeof(wchar_t); | ||
auto rename_info_buffer = std::make_unique<u8[]>(rename_info_length); | ||
|
||
FILE_RENAME_INFO* rename_info = reinterpret_cast<FILE_RENAME_INFO*>(rename_info_buffer.get()); | ||
rename_info->ReplaceIfExists = TRUE; | ||
rename_info->FileNameLength = static_cast<DWORD>(wide_filename.length()); | ||
std::memcpy(rename_info->FileName, wide_filename.c_str(), | ||
wide_filename.length() * sizeof(wchar_t)); | ||
|
||
if (SetFileInformationByHandle(m_data->m_handle, FileRenameInfo, rename_info_buffer.get(), | ||
static_cast<DWORD>(rename_info_length))) | ||
{ | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} // namespace File |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2020 Dolphin Emulator Project | ||
// Licensed under GPLv2+ | ||
// Refer to the license.txt file included. | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "Common/CommonTypes.h" | ||
|
||
namespace File | ||
{ | ||
class IOFileByHandle | ||
{ | ||
public: | ||
IOFileByHandle(const std::string& filename); | ||
~IOFileByHandle(); | ||
|
||
IOFileByHandle(const IOFileByHandle&) = delete; | ||
IOFileByHandle& operator=(const IOFileByHandle&) = delete; | ||
|
||
bool Write(const u8* data, size_t length); | ||
|
||
bool Rename(const std::string& filename); | ||
|
||
template <typename T> | ||
bool WriteArray(const T* elements, size_t count) | ||
{ | ||
return Write(reinterpret_cast<const u8*>(elements), count * sizeof(T)); | ||
} | ||
|
||
private: | ||
struct Impl; | ||
std::unique_ptr<Impl> m_data; | ||
}; | ||
|
||
} // namespace File |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters