// Copyright (c) 2020 Egor Tensin // This file is part of the "winapi-common" project. // For details, see https://github.com/egor-tensin/winapi-common. // Distributed under the MIT License. #include #include #include #include #include #include #include namespace winapi { namespace { std::wstring to_system_path(const std::string& path) { return widen(path); } std::wstring to_system_path(const CanonicalPath& path) { return widen(R"(\\?\)" + path.get()); } struct CreateFileParams { static CreateFileParams read() { CreateFileParams params; params.dwDesiredAccess = GENERIC_READ; params.dwShareMode = FILE_SHARE_READ; params.dwCreationDisposition = OPEN_EXISTING; return params; } static CreateFileParams write() { CreateFileParams params; params.dwDesiredAccess = GENERIC_WRITE; params.dwShareMode = FILE_SHARE_READ; params.dwCreationDisposition = OPEN_ALWAYS; return params; } DWORD dwDesiredAccess = 0; DWORD dwShareMode = 0; DWORD dwCreationDisposition = 0; private: CreateFileParams() = default; }; Handle open_file(const std::wstring& path, const CreateFileParams& params) { SECURITY_ATTRIBUTES attributes; std::memset(&attributes, 0, sizeof(attributes)); attributes.nLength = sizeof(attributes); attributes.bInheritHandle = TRUE; const auto handle = ::CreateFileW(path.c_str(), params.dwDesiredAccess, params.dwShareMode, &attributes, params.dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) { throw error::windows(GetLastError(), "CreateFileW"); } return Handle{handle}; } } // namespace Handle File::open_for_reading(const std::string& path) { return open_file(to_system_path(path), CreateFileParams::read()); } Handle File::open_for_reading(const CanonicalPath& path) { return open_file(to_system_path(path), CreateFileParams::read()); } Handle File::open_for_writing(const std::string& path) { return open_file(to_system_path(path), CreateFileParams::write()); } Handle File::open_for_writing(const CanonicalPath& path) { return open_file(to_system_path(path), CreateFileParams::write()); } } // namespace winapi