OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/filesystem/shared_impl.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <sys/stat.h> |
| 9 #include <sys/types.h> |
| 10 #include <time.h> |
| 11 #include <unistd.h> |
| 12 |
| 13 #include "base/logging.h" |
| 14 #include "components/filesystem/futimens.h" |
| 15 #include "components/filesystem/util.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace files { |
| 19 |
| 20 void StatFD(int fd, FileType type, const StatFDCallback& callback) { |
| 21 DCHECK_NE(fd, -1); |
| 22 |
| 23 struct stat buf; |
| 24 if (fstat(fd, &buf) != 0) { |
| 25 callback.Run(ErrnoToError(errno), nullptr); |
| 26 return; |
| 27 } |
| 28 |
| 29 FileInformationPtr file_info(FileInformation::New()); |
| 30 file_info->type = type; |
| 31 // Only fill in |size| for files. |
| 32 if (S_ISREG(buf.st_mode)) { |
| 33 file_info->size = static_cast<int64_t>(buf.st_size); |
| 34 } else { |
| 35 LOG_IF(WARNING, !S_ISDIR(buf.st_mode)) |
| 36 << "Unexpected fstat() of special file"; |
| 37 file_info->size = 0; |
| 38 } |
| 39 file_info->atime = Timespec::New(); |
| 40 file_info->mtime = Timespec::New(); |
| 41 #if defined(OS_ANDROID) |
| 42 file_info->atime->seconds = static_cast<int64_t>(buf.st_atime); |
| 43 file_info->atime->nanoseconds = static_cast<int32_t>(buf.st_atime_nsec); |
| 44 file_info->mtime->seconds = static_cast<int64_t>(buf.st_mtime); |
| 45 file_info->mtime->nanoseconds = static_cast<int32_t>(buf.st_mtime_nsec); |
| 46 #else |
| 47 file_info->atime->seconds = static_cast<int64_t>(buf.st_atim.tv_sec); |
| 48 file_info->atime->nanoseconds = static_cast<int32_t>(buf.st_atim.tv_nsec); |
| 49 file_info->mtime->seconds = static_cast<int64_t>(buf.st_mtim.tv_sec); |
| 50 file_info->mtime->nanoseconds = static_cast<int32_t>(buf.st_mtim.tv_nsec); |
| 51 #endif |
| 52 |
| 53 callback.Run(ERROR_OK, file_info.Pass()); |
| 54 } |
| 55 |
| 56 void TouchFD(int fd, |
| 57 TimespecOrNowPtr atime, |
| 58 TimespecOrNowPtr mtime, |
| 59 const TouchFDCallback& callback) { |
| 60 DCHECK_NE(fd, -1); |
| 61 |
| 62 struct timespec times[2]; |
| 63 if (Error error = TimespecOrNowToStandardTimespec(atime.get(), ×[0])) { |
| 64 callback.Run(error); |
| 65 return; |
| 66 } |
| 67 if (Error error = TimespecOrNowToStandardTimespec(mtime.get(), ×[1])) { |
| 68 callback.Run(error); |
| 69 return; |
| 70 } |
| 71 |
| 72 if (futimens(fd, times) != 0) { |
| 73 callback.Run(ErrnoToError(errno)); |
| 74 return; |
| 75 } |
| 76 |
| 77 callback.Run(ERROR_OK); |
| 78 } |
| 79 |
| 80 } // namespace files |
| 81 } // namespace mojo |
OLD | NEW |