| 120 | } |
| 121 | |
| 122 | bool SetFileModificationTime(const base::FilePath& path, |
| 123 | const timespec& mtime) { |
| 124 | #if BUILDFLAG(IS_APPLE) |
| 125 | // utimensat() isn't available on macOS until 10.13, so lutimes() is used |
| 126 | // instead. |
| 127 | struct stat st; |
| 128 | if (lstat(path.value().c_str(), &st) != 0) { |
| 129 | PLOG(ERROR) << "lstat " << path.value(); |
| 130 | return false; |
| 131 | } |
| 132 | timeval times[2]; |
| 133 | EXPECT_TRUE(TimespecToTimeval(st.st_atimespec, ×[0])); |
| 134 | EXPECT_TRUE(TimespecToTimeval(mtime, ×[1])); |
| 135 | if (lutimes(path.value().c_str(), times) != 0) { |
| 136 | PLOG(ERROR) << "lutimes " << path.value(); |
| 137 | return false; |
| 138 | } |
| 139 | return true; |
| 140 | #elif BUILDFLAG(IS_POSIX) |
| 141 | timespec times[2]; |
| 142 | times[0].tv_sec = 0; |
| 143 | times[0].tv_nsec = UTIME_OMIT; |
| 144 | times[1] = mtime; |
| 145 | if (utimensat(AT_FDCWD, path.value().c_str(), times, AT_SYMLINK_NOFOLLOW) != |
| 146 | 0) { |
| 147 | PLOG(ERROR) << "utimensat " << path.value(); |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | #elif BUILDFLAG(IS_WIN) |
| 152 | DWORD flags = FILE_FLAG_OPEN_REPARSE_POINT; |
| 153 | if (IsDirectory(path, true)) { |
| 154 | // required for directory handles |
| 155 | flags |= FILE_FLAG_BACKUP_SEMANTICS; |
| 156 | } |
| 157 | |
| 158 | ScopedFileHandle handle(::CreateFile(path.value().c_str(), |
| 159 | GENERIC_WRITE, |
| 160 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 161 | nullptr, |
| 162 | OPEN_EXISTING, |
| 163 | flags, |
| 164 | nullptr)); |
| 165 | if (!handle.is_valid()) { |
| 166 | PLOG(ERROR) << "CreateFile " << base::WideToUTF8(path.value()); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | FILETIME filetime = TimespecToFiletimeEpoch(mtime); |
| 171 | if (!SetFileTime(handle.get(), nullptr, nullptr, &filetime)) { |
| 172 | PLOG(ERROR) << "SetFileTime " << base::WideToUTF8(path.value()); |
| 173 | return false; |
| 174 | } |
| 175 | return true; |
| 176 | #endif // BUILDFLAG(IS_APPLE) |
| 177 | } |
| 178 | |
| 179 | #if !BUILDFLAG(IS_FUCHSIA) |