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.
5 #include "components/filesystem/shared_impl.h"
13 #include "base/logging.h"
14 #include "components/filesystem/futimens.h"
15 #include "components/filesystem/util.h"
17 namespace filesystem
{
19 void StatFD(int fd
, FileType type
, const StatFDCallback
& callback
) {
23 if (fstat(fd
, &buf
) != 0) {
24 callback
.Run(ErrnoToError(errno
), nullptr);
28 FileInformationPtr
file_info(FileInformation::New());
29 file_info
->type
= type
;
30 // Only fill in |size| for files.
31 if (S_ISREG(buf
.st_mode
)) {
32 file_info
->size
= static_cast<int64_t>(buf
.st_size
);
34 LOG_IF(WARNING
, !S_ISDIR(buf
.st_mode
))
35 << "Unexpected fstat() of special file";
38 file_info
->atime
= Timespec::New();
39 file_info
->mtime
= Timespec::New();
40 #if defined(OS_ANDROID)
41 file_info
->atime
->seconds
= static_cast<int64_t>(buf
.st_atime
);
42 file_info
->atime
->nanoseconds
= static_cast<int32_t>(buf
.st_atime_nsec
);
43 file_info
->mtime
->seconds
= static_cast<int64_t>(buf
.st_mtime
);
44 file_info
->mtime
->nanoseconds
= static_cast<int32_t>(buf
.st_mtime_nsec
);
46 file_info
->atime
->seconds
= static_cast<int64_t>(buf
.st_atim
.tv_sec
);
47 file_info
->atime
->nanoseconds
= static_cast<int32_t>(buf
.st_atim
.tv_nsec
);
48 file_info
->mtime
->seconds
= static_cast<int64_t>(buf
.st_mtim
.tv_sec
);
49 file_info
->mtime
->nanoseconds
= static_cast<int32_t>(buf
.st_mtim
.tv_nsec
);
52 callback
.Run(ERROR_OK
, file_info
.Pass());
56 TimespecOrNowPtr atime
,
57 TimespecOrNowPtr mtime
,
58 const TouchFDCallback
& callback
) {
61 struct timespec times
[2];
62 if (Error error
= TimespecOrNowToStandardTimespec(atime
.get(), ×
[0])) {
66 if (Error error
= TimespecOrNowToStandardTimespec(mtime
.get(), ×
[1])) {
71 if (futimens(fd
, times
) != 0) {
72 callback
.Run(ErrnoToError(errno
));
76 callback
.Run(ERROR_OK
);
79 } // namespace filesystem