Support/FileSystem: Add file_size implementation.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Sat, 4 Dec 2010 00:31:48 +0000 (00:31 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Sat, 4 Dec 2010 00:31:48 +0000 (00:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120867 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc

index b18023cde76a11ec2e0eaf61ef3e9db9b93ecb8e..d8575bf4e64ebb6f3671364e77ccbfe1bc00c3aa 100644 (file)
@@ -263,6 +263,20 @@ error_code equivalent(const Twine &A, const Twine &B, bool &result) {
   return make_error_code(errc::success);
 }
 
+error_code file_size(const Twine &path, uint64_t &result) {
+  SmallString<128> path_storage;
+  StringRef p = path.toNullTerminatedStringRef(path_storage);
+
+  struct stat status;
+  if (::stat(p.begin(), &status) == -1)
+    return error_code(errno, system_category());
+  if (!S_ISREG(status.st_mode))
+    return error_code(EPERM, system_category());
+
+  result = status.st_size;
+  return make_error_code(errc::success);
+}
+
 error_code unique_file(const Twine &model, int &result_fd,
                              SmallVectorImpl<char> &result_path) {
   SmallString<128> Model;
index e7aa93b6e4e7edcb2ac220d1277b8d0dfb72e415..dd7fefafc699f5b4106bfc9e6d411ebbb21ef708 100644 (file)
@@ -406,6 +406,27 @@ error_code equivalent(const Twine &A, const Twine &B, bool &result) {
   return make_error_code(errc::success);
 }
 
+error_code file_size(const Twine &path, uint64_t &result) {
+  SmallString<128> path_storage;
+  SmallVector<wchar_t, 128> path_utf16;
+
+  if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
+                                  path_utf16))
+    return ec;
+
+  WIN32_FILE_ATTRIBUTE_DATA FileData;
+  if (!::GetFileAttributesExW(path_utf16.begin(),
+                              ::GetFileExInfoStandard,
+                              &FileData))
+    return make_error_code(windows_error(::GetLastError()));
+
+  result =
+    (uint64_t(FileData.nFileSizeHigh) << (sizeof(FileData.nFileSizeLow) * 8))
+    + FileData.nFileSizeLow;
+
+  return make_error_code(errc::success);
+}
+
 error_code unique_file(const Twine &model, int &result_fd,
                              SmallVectorImpl<char> &result_path) {
   // Use result_path as temp storage.