aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2021-05-16 00:53:30 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2021-05-16 00:54:29 +0300
commit4eed412140b37b3d967ab712f6d4feee36f6eb87 (patch)
tree7d0d453a46b9a879a14deed4da2384d99f8621a2 /src
parentclang-format: SafeInt is no more (diff)
downloadwinapi-common-4eed412140b37b3d967ab712f6d4feee36f6eb87.tar.gz
winapi-common-4eed412140b37b3d967ab712f6d4feee36f6eb87.zip
File: add get_size/query_id
Diffstat (limited to '')
-rw-r--r--src/file.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/file.cpp b/src/file.cpp
index 165f36e..2b04f80 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -9,7 +9,10 @@
#include <winapi/path.hpp>
#include <winapi/utf8.hpp>
+#include <cstddef>
+#include <cstdint>
#include <cstring>
+#include <stdexcept>
#include <string>
namespace winapi {
@@ -32,6 +35,13 @@ struct CreateFileParams {
return params;
}
+ static CreateFileParams read_attributes() {
+ auto params = read();
+ params.dwDesiredAccess = FILE_READ_ATTRIBUTES;
+ params.dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
+ return params;
+ }
+
static CreateFileParams write() {
CreateFileParams params;
params.dwDesiredAccess = GENERIC_WRITE;
@@ -87,6 +97,38 @@ Handle File::open_r(const CanonicalPath& path) {
return open_file(to_system_path(path), CreateFileParams::read());
}
+Handle File::open_read_attributes(const std::string& path) {
+ return open_file(to_system_path(path), CreateFileParams::read_attributes());
+}
+
+Handle File::open_read_attributes(const CanonicalPath& path) {
+ return open_file(to_system_path(path), CreateFileParams::read_attributes());
+}
+
+std::size_t File::get_size() const {
+ LARGE_INTEGER size;
+
+ if (!GetFileSizeEx(get(), &size))
+ throw error::windows(GetLastError(), "GetFileSizeEx");
+
+ if (size.QuadPart < 0 || size.QuadPart > SIZE_MAX)
+ throw std::runtime_error{"invalid file size"};
+ return static_cast<std::size_t>(size.QuadPart);
+}
+
+bool operator==(const FILE_ID_128& a, const FILE_ID_128& b) {
+ return 0 == std::memcmp(a.Identifier, b.Identifier, sizeof(a.Identifier));
+}
+
+File::ID File::query_id() const {
+ FILE_ID_INFO id;
+
+ if (!GetFileInformationByHandleEx(get(), FileIdInfo, &id, sizeof(id)))
+ throw error::windows(GetLastError(), "GetFileInformationByHandleEx");
+
+ return {id};
+}
+
Handle File::open_w(const std::string& path) {
return open_file(to_system_path(path), CreateFileParams::write());
}