| 6 |
| 7 | #if defined(OS_WIN) |
| 8 | #include <io.h> |
| 9 | #endif |
| 10 | #include <stdio.h> |
| 11 |
| 12 | #include <fstream> |
| 13 | #include <limits> |
| 14 | #include <memory> |
| 15 |
| 16 | #include "base/files/file_enumerator.h" |
| 17 | #include "base/files/file_path.h" |
| 18 | #include "base/logging.h" |
| 19 | #include "base/strings/string_piece.h" |
| 20 | #include "base/strings/string_util.h" |
| 21 | #include "base/strings/stringprintf.h" |
| 22 | #include "base/strings/utf_string_conversions.h" |
| 23 | #include "base/threading/scoped_blocking_call.h" |
| 24 | #include "build/build_config.h" |
| 25 |
| 26 | namespace base { |
| 27 |
| 28 | #if !defined(OS_NACL_NONSFI) |
| 29 | int64_t ComputeDirectorySize(const FilePath& root_path) { |
| 30 | int64_t running_size = 0; |
| 31 | FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |
| 32 | while (!file_iter.Next().empty()) |
| 33 | running_size += file_iter.GetInfo().GetSize(); |
| 34 | return running_size; |
| 35 | } |
| 36 |
| 37 | bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 38 | if (from_path.ReferencesParent() || to_path.ReferencesParent()) |
| 39 | return false; |
| 40 | return internal::MoveUnsafe(from_path, to_path); |
| 41 | } |
| 42 |
| 43 | bool ContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 44 | // We open the file in binary format even if they are text files because |
| 45 | // we are just comparing that bytes are exactly same in both files and not |
| 46 | // doing anything smart with text formatting. |
| 47 | #if defined(OS_WIN) |
| 48 | std::ifstream file1(filename1.value().c_str(), |
| 49 | std::ios::in | std::ios::binary); |
| 50 | std::ifstream file2(filename2.value().c_str(), |
| 51 | std::ios::in | std::ios::binary); |
| 52 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 53 | std::ifstream file1(filename1.value(), std::ios::in | std::ios::binary); |
| 54 | std::ifstream file2(filename2.value(), std::ios::in | std::ios::binary); |
| 55 | #endif // OS_WIN |
| 56 |
| 57 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 58 | // any unusable file yields a result of "false". |
| 59 | if (!file1.is_open() || !file2.is_open()) |
| 60 | return false; |
| 61 |
| 62 | const int BUFFER_SIZE = 2056; |
| 63 | char buffer1[BUFFER_SIZE], buffer2[BUFFER_SIZE]; |
| 64 | do { |
| 65 | file1.read(buffer1, BUFFER_SIZE); |
| 66 | file2.read(buffer2, BUFFER_SIZE); |
| 67 |
| 68 | if ((file1.eof() != file2.eof()) || |
| 69 | (file1.gcount() != file2.gcount()) || |
| 70 | (memcmp(buffer1, buffer2, static_cast<size_t>(file1.gcount())))) { |
| 71 | file1.close(); |
| 72 | file2.close(); |
| 73 | return false; |
| 74 | } |
| 75 | } while (!file1.eof() || !file2.eof()); |
| 76 |
| 77 | file1.close(); |
| 78 | file2.close(); |
| 79 | return true; |
| 80 | } |
| 81 |
| 82 | bool TextContentsEqual(const FilePath& filename1, const FilePath& filename2) { |
| 83 | #if defined(OS_WIN) |
| 84 | std::ifstream file1(filename1.value().c_str(), std::ios::in); |
| 85 | std::ifstream file2(filename2.value().c_str(), std::ios::in); |
| 86 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 87 | std::ifstream file1(filename1.value(), std::ios::in); |
| 88 | std::ifstream file2(filename2.value(), std::ios::in); |
| 89 | #endif // OS_WIN |
| 90 |
| 91 | // Even if both files aren't openable (and thus, in some sense, "equal"), |
| 92 | // any unusable file yields a result of "false". |
| 93 | if (!file1.is_open() || !file2.is_open()) |
| 94 | return false; |
| 95 |
| 96 | do { |
| 97 | std::string line1, line2; |
| 98 | getline(file1, line1); |
| 99 | getline(file2, line2); |
| 100 |
| 101 | // Check for mismatched EOF states, or any error state. |
| 102 | if ((file1.eof() != file2.eof()) || |
| 103 | file1.bad() || file2.bad()) { |
| 104 | return false; |
| 105 | } |
| 106 |
| 107 | // Trim all '\r' and '\n' characters from the end of the line. |
| 108 | std::string::size_type end1 = line1.find_last_not_of("\r\n"); |
| 109 | if (end1 == std::string::npos) |
| 110 | line1.clear(); |
| 111 | else if (end1 + 1 < line1.length()) |
| 112 | line1.erase(end1 + 1); |
| 113 |
| 114 | std::string::size_type end2 = line2.find_last_not_of("\r\n"); |
| 115 | if (end2 == std::string::npos) |
| 116 | line2.clear(); |
| 117 | else if (end2 + 1 < line2.length()) |
| 118 | line2.erase(end2 + 1); |
| 119 |
| 120 | if (line1 != line2) |
| 121 | return false; |
| 122 | } while (!file1.eof() || !file2.eof()); |
| 123 |
| 124 | return true; |
| 125 | } |
| 126 | #endif // !defined(OS_NACL_NONSFI) |
| 127 |
| 128 | bool ReadFileToStringWithMaxSize(const FilePath& path, |
| 129 | std::string* contents, |
| 130 | size_t max_size) { |
| 131 | if (contents) |
| 132 | contents->clear(); |
| 133 | if (path.ReferencesParent()) |
| 134 | return false; |
| 135 | FILE* file = OpenFile(path, "rb"); |
| 136 | if (!file) { |
| 137 | return false; |
| 138 | } |
| 139 |
| 140 | // Many files supplied in |path| have incorrect size (proc files etc). |
| 141 | // Hence, the file is read sequentially as opposed to a one-shot read, using |
| 142 | // file size as a hint for chunk size if available. |
| 143 | constexpr int64_t kDefaultChunkSize = 1 << 16; |
| 144 | int64_t chunk_size; |
| 145 | #if !defined(OS_NACL_NONSFI) |
| 146 | if (!GetFileSize(path, &chunk_size) || chunk_size <= 0) |
| 147 | chunk_size = kDefaultChunkSize - 1; |
| 148 | // We need to attempt to read at EOF for feof flag to be set so here we |
| 149 | // use |chunk_size| + 1. |
| 150 | chunk_size = std::min<uint64_t>(chunk_size, max_size) + 1; |
| 151 | #else |
| 152 | chunk_size = kDefaultChunkSize; |
| 153 | #endif // !defined(OS_NACL_NONSFI) |
| 154 | size_t bytes_read_this_pass; |
| 155 | size_t bytes_read_so_far = 0; |
| 156 | bool read_status = true; |
| 157 | std::string local_contents; |
| 158 | local_contents.resize(chunk_size); |
| 159 |
| 160 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 161 | while ((bytes_read_this_pass = fread(&local_contents[bytes_read_so_far], 1, |
| 162 | chunk_size, file)) > 0) { |
| 163 | if ((max_size - bytes_read_so_far) < bytes_read_this_pass) { |
| 164 | // Read more than max_size bytes, bail out. |
| 165 | bytes_read_so_far = max_size; |
| 166 | read_status = false; |
| 167 | break; |
| 168 | } |
| 169 | // In case EOF was not reached, iterate again but revert to the default |
| 170 | // chunk size. |
| 171 | if (bytes_read_so_far == 0) |
| 172 | chunk_size = kDefaultChunkSize; |
| 173 |
| 174 | bytes_read_so_far += bytes_read_this_pass; |
| 175 | // Last fread syscall (after EOF) can be avoided via feof, which is just a |
| 176 | // flag check. |
| 177 | if (feof(file)) |
| 178 | break; |
| 179 | local_contents.resize(bytes_read_so_far + chunk_size); |
| 180 | } |
| 181 | read_status = read_status && !ferror(file); |
| 182 | CloseFile(file); |
| 183 | if (contents) { |
| 184 | contents->swap(local_contents); |
| 185 | contents->resize(bytes_read_so_far); |
| 186 | } |
| 187 |
| 188 | return read_status; |
| 189 | } |
| 190 |
| 191 | bool ReadFileToString(const FilePath& path, std::string* contents) { |
| 192 | return ReadFileToStringWithMaxSize(path, contents, |
| 193 | std::numeric_limits<size_t>::max()); |
| 194 | } |
| 195 |
| 196 | #if !defined(OS_NACL_NONSFI) |
| 197 | bool IsDirectoryEmpty(const FilePath& dir_path) { |
| 198 | FileEnumerator files(dir_path, false, |
| 199 | FileEnumerator::FILES | FileEnumerator::DIRECTORIES); |
| 200 | if (files.Next().empty()) |
| 201 | return true; |
| 202 | return false; |
| 203 | } |
| 204 |
| 205 | FILE* CreateAndOpenTemporaryFile(FilePath* path) { |
| 206 | FilePath directory; |
| 207 | if (!GetTempDir(&directory)) |
| 208 | return nullptr; |
| 209 |
| 210 | return CreateAndOpenTemporaryFileInDir(directory, path); |
| 211 | } |
| 212 |
| 213 | bool CreateDirectory(const FilePath& full_path) { |
| 214 | return CreateDirectoryAndGetError(full_path, nullptr); |
| 215 | } |
| 216 |
| 217 | bool GetFileSize(const FilePath& file_path, int64_t* file_size) { |
| 218 | File::Info info; |
| 219 | if (!GetFileInfo(file_path, &info)) |
| 220 | return false; |
| 221 | *file_size = info.size; |
| 222 | return true; |
| 223 | } |
| 224 |
| 225 | bool TouchFile(const FilePath& path, |
| 226 | const Time& last_accessed, |
| 227 | const Time& last_modified) { |
| 228 | int flags = File::FLAG_OPEN | File::FLAG_WRITE_ATTRIBUTES; |
| 229 |
| 230 | #if defined(OS_WIN) |
| 231 | // On Windows, FILE_FLAG_BACKUP_SEMANTICS is needed to open a directory. |
| 232 | if (DirectoryExists(path)) |
| 233 | flags |= File::FLAG_BACKUP_SEMANTICS; |
| 234 | #elif defined(OS_FUCHSIA) |
| 235 | // On Fuchsia, we need O_RDONLY for directories, or O_WRONLY for files. |
| 236 | // TODO(https://crbug.com/947802): Find a cleaner workaround for this. |
| 237 | flags |= (DirectoryExists(path) ? File::FLAG_READ : File::FLAG_WRITE); |
| 238 | #endif |
| 239 |
| 240 | File file(path, flags); |
| 241 | if (!file.IsValid()) |
| 242 | return false; |
| 243 |
| 244 | return file.SetTimes(last_accessed, last_modified); |
| 245 | } |
| 246 | #endif // !defined(OS_NACL_NONSFI) |
| 247 |
| 248 | bool CloseFile(FILE* file) { |
| 249 | if (file == nullptr) |
| 250 | return true; |
| 251 | return fclose(file) == 0; |
| 252 | } |
| 253 |
| 254 | #if !defined(OS_NACL_NONSFI) |
| 255 | bool TruncateFile(FILE* file) { |
| 256 | if (file == nullptr) |
| 257 | return false; |
| 258 | long current_offset = ftell(file); |
| 259 | if (current_offset == -1) |
| 260 | return false; |
| 261 | #if defined(OS_WIN) |
| 262 | int fd = _fileno(file); |
| 263 | if (_chsize(fd, current_offset) != 0) |
| 264 | return false; |
| 265 | #else |
| 266 | int fd = fileno(file); |
| 267 | if (ftruncate(fd, current_offset) != 0) |
| 268 | return false; |
| 269 | #endif |
| 270 | return true; |
| 271 | } |
| 272 |
| 273 | int GetUniquePathNumber(const FilePath& path) { |
| 274 | DCHECK(!path.empty()); |
| 275 | if (!PathExists(path)) |
| 276 | return 0; |
| 277 |
| 278 | std::string number; |
| 279 | for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| 280 | StringAppendF(&number, " (%d)", count); |
| 281 | if (!PathExists(path.InsertBeforeExtensionASCII(number))) |
| 282 | return count; |
| 283 | number.clear(); |
| 284 | } |
| 285 |
| 286 | return -1; |
| 287 | } |
| 288 |
| 289 | FilePath GetUniquePath(const FilePath& path) { |
| 290 | DCHECK(!path.empty()); |
| 291 | const int uniquifier = GetUniquePathNumber(path); |
| 292 | if (uniquifier > 0) |
| 293 | return path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", uniquifier)); |
| 294 | return uniquifier == 0 ? path : base::FilePath(); |
| 295 | } |
| 296 |
| 297 | namespace internal { |
| 298 |
| 299 | bool PreReadFileSlow(const FilePath& file_path, int64_t max_bytes) { |
| 300 | DCHECK_GE(max_bytes, 0); |
| 301 |
| 302 | File file(file_path, File::FLAG_OPEN | File::FLAG_READ | |
| 303 | File::FLAG_SEQUENTIAL_SCAN | |
| 304 | File::FLAG_SHARE_DELETE); |
| 305 | if (!file.IsValid()) |
| 306 | return false; |
| 307 |
| 308 | constexpr int kBufferSize = 1024 * 1024; |
| 309 | // Ensures the buffer is deallocated at function exit. |
| 310 | std::unique_ptr<char[]> buffer_deleter(new char[kBufferSize]); |
| 311 | char* const buffer = buffer_deleter.get(); |
| 312 |
| 313 | while (max_bytes > 0) { |
| 314 | // The static_cast<int> is safe because kBufferSize is int, and both values |
| 315 | // are non-negative. So, the minimum is guaranteed to fit in int. |
| 316 | const int read_size = |
| 317 | static_cast<int>(std::min<int64_t>(max_bytes, kBufferSize)); |
| 318 | DCHECK_GE(read_size, 0); |
| 319 | DCHECK_LE(read_size, kBufferSize); |
| 320 |
| 321 | const int read_bytes = file.ReadAtCurrentPos(buffer, read_size); |
| 322 | if (read_bytes < 0) |
| 323 | return false; |
| 324 | if (read_bytes == 0) |
| 325 | break; |
| 326 |
| 327 | max_bytes -= read_bytes; |
| 328 | } |
| 329 |
| 330 | return true; |
| 331 | } |
| 332 |
| 333 | } // namespace internal |
| 334 |
| 335 | #endif // !defined(OS_NACL_NONSFI) |
| 336 |
| 337 | } // namespace base |
| 338 |