aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/utils/file.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-05-17 06:00:20 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-05-17 06:00:20 +0300
commitc67055bad3cdfc93e2ac57d87f36c6e0993af690 (patch)
treeda8759c66f1af00415784e5824e6bfc2195aee92 /src/utils/file.cpp
downloadwinapi-debug-c67055bad3cdfc93e2ac57d87f36c6e0993af690.tar.gz
winapi-debug-c67055bad3cdfc93e2ac57d87f36c6e0993af690.zip
initial commit
Diffstat (limited to 'src/utils/file.cpp')
-rw-r--r--src/utils/file.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/utils/file.cpp b/src/utils/file.cpp
new file mode 100644
index 0000000..4150685
--- /dev/null
+++ b/src/utils/file.cpp
@@ -0,0 +1,51 @@
+// Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "PDB repository" project.
+// For details, see https://github.com/egor-tensin/pdb-repo.
+// Distributed under the MIT License.
+
+#include "pdb/all.hpp"
+
+#include <safeint.h>
+
+#include <Windows.h>
+
+#include <cstddef>
+
+#include <stdexcept>
+#include <string>
+
+namespace pdb
+{
+ namespace file
+ {
+ std::size_t get_size(const std::string& path)
+ {
+ const Handle handle{CreateFileA(
+ path.c_str(),
+ FILE_READ_ATTRIBUTES,
+ FILE_SHARE_READ,
+ NULL,
+ OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL,
+ NULL)};
+
+ if (handle.get() == INVALID_HANDLE_VALUE)
+ throw error::windows(GetLastError());
+
+ LARGE_INTEGER size;
+
+ if (!GetFileSizeEx(handle.get(), &size))
+ throw error::windows(GetLastError());
+
+ try
+ {
+ const msl::utilities::SafeInt<decltype(size.QuadPart)> safe_size{size.QuadPart};
+ return static_cast<std::size_t>(safe_size);
+ }
+ catch (const msl::utilities::SafeIntException&)
+ {
+ throw std::range_error{"invalid file size"};
+ }
+ }
+ }
+}