Support/FileSystem: Add status implementation.

llvm-svn: 120870
This commit is contained in:
Michael J. Spencer 2010-12-04 00:32:40 +00:00
parent 32a15547da
commit db5576a185
4 changed files with 87 additions and 3 deletions

View 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; }
};
/// @}

View 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)

View 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;

View 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.