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

include/llvm/Support/FileSystem.h
lib/Support/PathV2.cpp
lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc

index 46bb3d68ba90a5ea8819a642aee17d858d63308a..fbf1e0d0a8290c5a69569f43b4b01f953656c8ef 100644 (file)
@@ -91,11 +91,13 @@ struct space_info {
 class file_status
 {
   // implementation defined status field.
+  file_type Type;
 public:
-  explicit file_status(file_type v=file_type::status_error);
+  explicit file_status(file_type v=file_type::status_error)
+    : Type(v) {}
 
-  file_type type() const;
-  void type(file_type v);
+  file_type type() const { return Type; }
+  void type(file_type v) { Type = v; }
 };
 
 /// @}
index 7d4d6d1fc2837bd0741fa43125b79f292e1f2862..829e107b2d5fa0362cd7950a5a7484f554713346 100644 (file)
@@ -37,6 +37,8 @@ namespace {
   const char      prefered_separator = '/';
 #endif
 
+  const llvm::error_code success;
+
   StringRef find_first_component(const StringRef  &path) {
     // Look for this first component in the following order.
     // * empty (in this case we return an empty string)
index d8575bf4e64ebb6f3671364e77ccbfe1bc00c3aa..f71212f5e6ecdb6edcd792ed9a727e488af3cf17 100644 (file)
@@ -277,6 +277,38 @@ error_code file_size(const Twine &path, uint64_t &result) {
   return make_error_code(errc::success);
 }
 
+error_code status(const Twine &path, file_status &result) {
+  SmallString<128> path_storage;
+  StringRef p = path.toNullTerminatedStringRef(path_storage);
+
+  struct stat status;
+  if (::stat(p.begin(), &status) != 0) {
+    error_code ec(errno, system_category());
+    if (ec == errc::no_such_file_or_directory)
+      result = file_status(file_type::file_not_found);
+    else
+      result = file_status(file_type::status_error);
+    return ec;
+  }
+
+  if (S_ISDIR(status.st_mode))
+    result = file_status(file_type::directory_file);
+  else if (S_ISREG(status.st_mode))
+    result = file_status(file_type::regular_file);
+  else if (S_ISBLK(status.st_mode))
+    result = file_status(file_type::block_file);
+  else if (S_ISCHR(status.st_mode))
+    result = file_status(file_type::character_file);
+  else if (S_ISFIFO(status.st_mode))
+    result = file_status(file_type::fifo_file);
+  else if (S_ISSOCK(status.st_mode))
+    result = file_status(file_type::socket_file);
+  else
+    result = file_status(file_type::type_unknown);
+
+  return success;
+}
+
 error_code unique_file(const Twine &model, int &result_fd,
                              SmallVectorImpl<char> &result_path) {
   SmallString<128> Model;
index a84783f96acbd19d1804cd4588b30c5073bfca91..7d9421ac768316d24cd5f14e43043ab3d653a683 100644 (file)
@@ -427,6 +427,54 @@ error_code file_size(const Twine &path, uint64_t &result) {
   return make_error_code(errc::success);
 }
 
+error_code status(const Twine &path, file_status &result) {
+  SmallString<128> path_storage;
+  SmallVector<wchar_t, 128> path_utf16;
+
+  if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage),
+                                  path_utf16))
+    return ec;
+
+  DWORD attr = ::GetFileAttributesW(path_utf16.begin());
+  if (attr == INVALID_FILE_ATTRIBUTES)
+    goto handle_status_error;
+
+  // Handle reparse points.
+  if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
+    AutoHandle h(
+      ::CreateFileW(path_utf16.begin(),
+                    0, // Attributes only.
+                    FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
+                    NULL,
+                    OPEN_EXISTING,
+                    FILE_FLAG_BACKUP_SEMANTICS,
+                    0));
+    if (h == INVALID_HANDLE_VALUE)
+      goto handle_status_error;
+  }
+
+  if (attr & FILE_ATTRIBUTE_DIRECTORY)
+    result = file_status(file_type::directory_file);
+  else
+    result = file_status(file_type::regular_file);
+
+  return success;
+
+handle_status_error:
+  error_code ec = windows_error(::GetLastError());
+  if (ec == windows_error::file_not_found ||
+      ec == windows_error::path_not_found)
+    result = file_status(file_type::file_not_found);
+  else if (ec == windows_error::sharing_violation)
+    result = file_status(file_type::type_unknown);
+  else {
+    result = file_status(file_type::status_error);
+    return ec;
+  }
+
+  return success;
+}
+
 error_code unique_file(const Twine &model, int &result_fd,
                              SmallVectorImpl<char> &result_path) {
   // Use result_path as temp storage.