Add a version of sys::fs::status that uses fstat.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@186378 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2013-07-16 03:20:13 +00:00
parent 3a101048af
commit 87be8d353b
3 changed files with 71 additions and 40 deletions

View File

@ -529,6 +529,9 @@ error_code is_symlink(const Twine &path, bool &result);
/// platform specific error_code.
error_code status(const Twine &path, file_status &result);
/// @brief A version for when a file descriptor is already available.
error_code status(int FD, file_status &Result);
/// @brief Get file size.
///
/// @param Path Input path.

View File

@ -541,43 +541,55 @@ error_code getUniqueID(const Twine Path, uint64_t &Result) {
return error_code::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) {
static error_code fillStatus(int StatRet, const struct stat &Status,
file_status &Result) {
if (StatRet != 0) {
error_code ec(errno, system_category());
if (ec == errc::no_such_file_or_directory)
result = file_status(file_type::file_not_found);
Result = file_status(file_type::file_not_found);
else
result = file_status(file_type::status_error);
Result = file_status(file_type::status_error);
return ec;
}
perms prms = static_cast<perms>(status.st_mode);
file_type Type = file_type::type_unknown;
if (S_ISDIR(status.st_mode))
if (S_ISDIR(Status.st_mode))
Type = file_type::directory_file;
else if (S_ISREG(status.st_mode))
else if (S_ISREG(Status.st_mode))
Type = file_type::regular_file;
else if (S_ISBLK(status.st_mode))
else if (S_ISBLK(Status.st_mode))
Type = file_type::block_file;
else if (S_ISCHR(status.st_mode))
else if (S_ISCHR(Status.st_mode))
Type = file_type::character_file;
else if (S_ISFIFO(status.st_mode))
else if (S_ISFIFO(Status.st_mode))
Type = file_type::fifo_file;
else if (S_ISSOCK(status.st_mode))
else if (S_ISSOCK(Status.st_mode))
Type = file_type::socket_file;
result =
file_status(Type, prms, status.st_dev, status.st_ino, status.st_mtime,
status.st_uid, status.st_gid, status.st_size);
perms Perms = static_cast<perms>(Status.st_mode);
Result =
file_status(Type, Perms, Status.st_dev, Status.st_ino, Status.st_mtime,
Status.st_uid, Status.st_gid, Status.st_size);
return error_code::success();
}
error_code status(const Twine &Path, file_status &Result) {
SmallString<128> PathStorage;
StringRef P = Path.toNullTerminatedStringRef(PathStorage);
struct stat Status;
int StatRet = ::stat(P.begin(), &Status);
return fillStatus(StatRet, Status, Result);
}
error_code status(int FD, file_status &Result) {
struct stat Status;
int StatRet = ::fstat(FD, &Status);
return fillStatus(StatRet, Status, Result);
}
error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {
#if defined(HAVE_FUTIMENS)
timespec Times[2];

View File

@ -598,6 +598,35 @@ static bool isReservedName(StringRef path) {
return false;
}
static error_code getStatus(HANDLE FileHandle, file_status &Result) {
if (FileHandle == INVALID_HANDLE_VALUE)
goto handle_status_error;
BY_HANDLE_FILE_INFORMATION Info;
if (!::GetFileInformationByHandle(FileHandle, &Info))
goto handle_status_error;
Result = file_status(
file_type::regular_file, Info.ftLastWriteTime.dwHighDateTime,
Info.ftLastWriteTime.dwLowDateTime, Info.dwVolumeSerialNumber,
Info.nFileSizeHigh, Info.nFileSizeLow, Info.nFileIndexHigh,
Info.nFileIndexLow);
return error_code::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 error_code::success();
}
error_code status(const Twine &path, file_status &result) {
SmallString<128> path_storage;
SmallVector<wchar_t, 128> path_utf16;
@ -613,7 +642,7 @@ error_code status(const Twine &path, file_status &result) {
DWORD attr = ::GetFileAttributesW(path_utf16.begin());
if (attr == INVALID_FILE_ATTRIBUTES)
goto handle_status_error;
return getStatus(INVALID_HANDLE_VALUE, result);
// Handle reparse points.
if (attr & FILE_ATTRIBUTE_REPARSE_POINT) {
@ -626,7 +655,7 @@ error_code status(const Twine &path, file_status &result) {
FILE_FLAG_BACKUP_SEMANTICS,
0));
if (!h)
goto handle_status_error;
return getStatus(INVALID_HANDLE_VALUE, result);
}
if (attr & FILE_ATTRIBUTE_DIRECTORY)
@ -641,32 +670,19 @@ error_code status(const Twine &path, file_status &result) {
FILE_FLAG_BACKUP_SEMANTICS,
0));
if (!h)
goto handle_status_error;
return getStatus(INVALID_HANDLE_VALUE, result);
BY_HANDLE_FILE_INFORMATION Info;
if (!::GetFileInformationByHandle(h, &Info))
goto handle_status_error;
return getStatus(INVALID_HANDLE_VALUE, result);
result = file_status(
file_type::regular_file, Info.ftLastWriteTime.dwHighDateTime,
Info.ftLastWriteTime.dwLowDateTime, Info.dwVolumeSerialNumber,
Info.nFileSizeHigh, Info.nFileSizeLow, Info.nFileIndexHigh,
Info.nFileIndexLow);
return getStatus(h, result);
}
return error_code::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 error_code::success();
error_code status(int FD, file_status &Result) {
HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
return getStatus(FileHandle, Result);
}
error_code setLastModificationAndAccessTime(int FD, TimeValue Time) {