[llvm-commits] [llvm] r120870 - in /llvm/trunk: include/llvm/Support/FileSystem.h lib/Support/PathV2.cpp lib/Support/Unix/PathV2.inc lib/Support/Windows/PathV2.inc
Michael J. Spencer
bigcheesegs at gmail.com
Fri Dec 3 16:32:40 PST 2010
Author: mspencer
Date: Fri Dec 3 18:32:40 2010
New Revision: 120870
URL: http://llvm.org/viewvc/llvm-project?rev=120870&view=rev
Log:
Support/FileSystem: Add status implementation.
Modified:
llvm/trunk/include/llvm/Support/FileSystem.h
llvm/trunk/lib/Support/PathV2.cpp
llvm/trunk/lib/Support/Unix/PathV2.inc
llvm/trunk/lib/Support/Windows/PathV2.inc
Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=120870&r1=120869&r2=120870&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Fri Dec 3 18:32:40 2010
@@ -91,11 +91,13 @@
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; }
};
/// @}
Modified: llvm/trunk/lib/Support/PathV2.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/PathV2.cpp?rev=120870&r1=120869&r2=120870&view=diff
==============================================================================
--- llvm/trunk/lib/Support/PathV2.cpp (original)
+++ llvm/trunk/lib/Support/PathV2.cpp Fri Dec 3 18:32:40 2010
@@ -37,6 +37,8 @@
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)
Modified: llvm/trunk/lib/Support/Unix/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/PathV2.inc?rev=120870&r1=120869&r2=120870&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Unix/PathV2.inc Fri Dec 3 18:32:40 2010
@@ -277,6 +277,38 @@
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;
Modified: llvm/trunk/lib/Support/Windows/PathV2.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/PathV2.inc?rev=120870&r1=120869&r2=120870&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/PathV2.inc (original)
+++ llvm/trunk/lib/Support/Windows/PathV2.inc Fri Dec 3 18:32:40 2010
@@ -427,6 +427,54 @@
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.
More information about the llvm-commits
mailing list