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

@@ -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];