| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 | 6 |
| 7 #include <dirent.h> | 7 #include <dirent.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <fnmatch.h> | 10 #include <fnmatch.h> |
| 11 #include <fts.h> | 11 #include <fts.h> |
| 12 #include <libgen.h> | 12 #include <libgen.h> |
| 13 #include <stdio.h> | 13 #include <stdio.h> |
| 14 #include <string.h> | 14 #include <string.h> |
| 15 #include <sys/errno.h> | 15 #include <sys/errno.h> |
| 16 #include <sys/mman.h> | 16 #include <sys/mman.h> |
| 17 #include <sys/stat.h> | 17 #include <sys/stat.h> |
| 18 #include <sys/types.h> | 18 #include <sys/types.h> |
| 19 #include <time.h> | 19 #include <time.h> |
| 20 #include <unistd.h> | 20 #include <unistd.h> |
| 21 | 21 |
| 22 #include <fstream> | 22 #include <fstream> |
| 23 | 23 |
| 24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
| 25 #include "base/eintr_wrappers.h" |
| 25 #include "base/file_path.h" | 26 #include "base/file_path.h" |
| 26 #include "base/logging.h" | 27 #include "base/logging.h" |
| 27 #include "base/string_util.h" | 28 #include "base/string_util.h" |
| 28 #include "base/time.h" | 29 #include "base/time.h" |
| 29 | 30 |
| 30 namespace file_util { | 31 namespace file_util { |
| 31 | 32 |
| 32 #if defined(GOOGLE_CHROME_BUILD) | 33 #if defined(GOOGLE_CHROME_BUILD) |
| 33 static const char* kTempFileName = "com.google.chrome.XXXXXX"; | 34 static const char* kTempFileName = "com.google.chrome.XXXXXX"; |
| 34 #else | 35 #else |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 CreateFile(filename.c_str(), GENERIC_READ, | 329 CreateFile(filename.c_str(), GENERIC_READ, |
| 329 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, | 330 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, |
| 330 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); | 331 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
| 331 return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); | 332 return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); |
| 332 } | 333 } |
| 333 #endif | 334 #endif |
| 334 | 335 |
| 335 bool ReadFromFD(int fd, char* buffer, size_t bytes) { | 336 bool ReadFromFD(int fd, char* buffer, size_t bytes) { |
| 336 size_t total_read = 0; | 337 size_t total_read = 0; |
| 337 while (total_read < bytes) { | 338 while (total_read < bytes) { |
| 338 ssize_t bytes_read = read(fd, buffer + total_read, bytes - total_read); | 339 ssize_t bytes_read = |
| 339 if (bytes_read > 0) | 340 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); |
| 340 total_read += bytes_read; | 341 if (bytes_read <= 0) |
| 341 else if (bytes_read == 0 || errno != EINTR) | |
| 342 break; | 342 break; |
| 343 total_read += bytes_read; |
| 343 } | 344 } |
| 344 return total_read == bytes; | 345 return total_read == bytes; |
| 345 } | 346 } |
| 346 | 347 |
| 347 // Creates and opens a temporary file in |directory|, returning the | 348 // Creates and opens a temporary file in |directory|, returning the |
| 348 // file descriptor. |path| is set to the temporary file path. | 349 // file descriptor. |path| is set to the temporary file path. |
| 349 // Note TODO(erikkay) comment in header for BlahFileName() calls; the | 350 // Note TODO(erikkay) comment in header for BlahFileName() calls; the |
| 350 // intent is to rename these files BlahFile() (since they create | 351 // intent is to rename these files BlahFile() (since they create |
| 351 // files, not filenames). This function does NOT unlink() the file. | 352 // files, not filenames). This function does NOT unlink() the file. |
| 352 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { | 353 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 | 447 |
| 447 FILE* OpenFile(const FilePath& filename, const char* mode) { | 448 FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 448 return fopen(filename.value().c_str(), mode); | 449 return fopen(filename.value().c_str(), mode); |
| 449 } | 450 } |
| 450 | 451 |
| 451 int ReadFile(const FilePath& filename, char* data, int size) { | 452 int ReadFile(const FilePath& filename, char* data, int size) { |
| 452 int fd = open(filename.value().c_str(), O_RDONLY); | 453 int fd = open(filename.value().c_str(), O_RDONLY); |
| 453 if (fd < 0) | 454 if (fd < 0) |
| 454 return -1; | 455 return -1; |
| 455 | 456 |
| 456 int ret_value = read(fd, data, size); | 457 int ret_value = HANDLE_EINTR(read(fd, data, size)); |
| 457 close(fd); | 458 HANDLE_EINTR(close(fd)); |
| 458 return ret_value; | 459 return ret_value; |
| 459 } | 460 } |
| 460 | 461 |
| 461 int WriteFile(const FilePath& filename, const char* data, int size) { | 462 int WriteFile(const FilePath& filename, const char* data, int size) { |
| 462 int fd = creat(filename.value().c_str(), 0666); | 463 int fd = creat(filename.value().c_str(), 0666); |
| 463 if (fd < 0) | 464 if (fd < 0) |
| 464 return -1; | 465 return -1; |
| 465 | 466 |
| 466 // Allow for partial writes | 467 // Allow for partial writes |
| 467 ssize_t bytes_written_total = 0; | 468 ssize_t bytes_written_total = 0; |
| 468 do { | 469 do { |
| 469 ssize_t bytes_written_partial = write(fd, | 470 ssize_t bytes_written_partial = |
| 470 data + bytes_written_total, | 471 HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 471 size - bytes_written_total); | 472 size - bytes_written_total)); |
| 472 if (bytes_written_partial < 0) { | 473 if (bytes_written_partial < 0) { |
| 473 close(fd); | 474 HANDLE_EINTR(close(fd)); |
| 474 return -1; | 475 return -1; |
| 475 } | 476 } |
| 476 bytes_written_total += bytes_written_partial; | 477 bytes_written_total += bytes_written_partial; |
| 477 } while (bytes_written_total < size); | 478 } while (bytes_written_total < size); |
| 478 | 479 |
| 479 close(fd); | 480 HANDLE_EINTR(close(fd)); |
| 480 return bytes_written_total; | 481 return bytes_written_total; |
| 481 } | 482 } |
| 482 | 483 |
| 483 // Gets the current working directory for the process. | 484 // Gets the current working directory for the process. |
| 484 bool GetCurrentDirectory(FilePath* dir) { | 485 bool GetCurrentDirectory(FilePath* dir) { |
| 485 char system_buffer[PATH_MAX] = ""; | 486 char system_buffer[PATH_MAX] = ""; |
| 486 if (!getcwd(system_buffer, sizeof(system_buffer))) { | 487 if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 487 NOTREACHED(); | 488 NOTREACHED(); |
| 488 return false; | 489 return false; |
| 489 } | 490 } |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 munmap(data_, length_); | 633 munmap(data_, length_); |
| 633 if (file_ != -1) | 634 if (file_ != -1) |
| 634 close(file_); | 635 close(file_); |
| 635 | 636 |
| 636 data_ = NULL; | 637 data_ = NULL; |
| 637 length_ = 0; | 638 length_ = 0; |
| 638 file_ = -1; | 639 file_ = -1; |
| 639 } | 640 } |
| 640 | 641 |
| 641 } // namespace file_util | 642 } // namespace file_util |
| OLD | NEW |