| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 #include "base/basictypes.h" | 33 #include "base/basictypes.h" |
| 34 #include "base/eintr_wrapper.h" | 34 #include "base/eintr_wrapper.h" |
| 35 #include "base/file_path.h" | 35 #include "base/file_path.h" |
| 36 #include "base/lock.h" | 36 #include "base/lock.h" |
| 37 #include "base/logging.h" | 37 #include "base/logging.h" |
| 38 #include "base/scoped_ptr.h" | 38 #include "base/scoped_ptr.h" |
| 39 #include "base/singleton.h" | 39 #include "base/singleton.h" |
| 40 #include "base/string_util.h" | 40 #include "base/string_util.h" |
| 41 #include "base/sys_string_conversions.h" | 41 #include "base/sys_string_conversions.h" |
| 42 #include "base/thread_restrictions.h" | |
| 43 #include "base/time.h" | 42 #include "base/time.h" |
| 44 #include "base/utf_string_conversions.h" | 43 #include "base/utf_string_conversions.h" |
| 45 | 44 |
| 46 namespace file_util { | 45 namespace file_util { |
| 47 | 46 |
| 48 namespace { | 47 namespace { |
| 49 | 48 |
| 50 // Helper for NormalizeFilePath(), defined below. | 49 // Helper for NormalizeFilePath(), defined below. |
| 51 bool RealPath(const FilePath& path, FilePath* real_path) { | 50 bool RealPath(const FilePath& path, FilePath* real_path) { |
| 52 base::ThreadRestrictions::AssertIOAllowed(); // For realpath(). | |
| 53 FilePath::CharType buf[PATH_MAX]; | 51 FilePath::CharType buf[PATH_MAX]; |
| 54 if (!realpath(path.value().c_str(), buf)) | 52 if (!realpath(path.value().c_str(), buf)) |
| 55 return false; | 53 return false; |
| 56 | 54 |
| 57 *real_path = FilePath(buf); | 55 *real_path = FilePath(buf); |
| 58 return true; | 56 return true; |
| 59 } | 57 } |
| 60 | 58 |
| 61 } // namespace | 59 } // namespace |
| 62 | 60 |
| 63 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) || \ | 61 #if defined(OS_OPENBSD) || defined(OS_FREEBSD) || \ |
| 64 (defined(OS_MACOSX) && \ | 62 (defined(OS_MACOSX) && \ |
| 65 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) | 63 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5) |
| 66 typedef struct stat stat_wrapper_t; | 64 typedef struct stat stat_wrapper_t; |
| 67 static int CallStat(const char *path, stat_wrapper_t *sb) { | 65 static int CallStat(const char *path, stat_wrapper_t *sb) { |
| 68 base::ThreadRestrictions::AssertIOAllowed(); | |
| 69 return stat(path, sb); | 66 return stat(path, sb); |
| 70 } | 67 } |
| 71 #else | 68 #else |
| 72 typedef struct stat64 stat_wrapper_t; | 69 typedef struct stat64 stat_wrapper_t; |
| 73 static int CallStat(const char *path, stat_wrapper_t *sb) { | 70 static int CallStat(const char *path, stat_wrapper_t *sb) { |
| 74 base::ThreadRestrictions::AssertIOAllowed(); | |
| 75 return stat64(path, sb); | 71 return stat64(path, sb); |
| 76 } | 72 } |
| 77 #endif | 73 #endif |
| 78 | 74 |
| 79 | 75 |
| 80 #if defined(GOOGLE_CHROME_BUILD) | 76 #if defined(GOOGLE_CHROME_BUILD) |
| 81 static const char* kTempFileName = ".com.google.chrome.XXXXXX"; | 77 static const char* kTempFileName = ".com.google.chrome.XXXXXX"; |
| 82 #else | 78 #else |
| 83 static const char* kTempFileName = ".org.chromium.XXXXXX"; | 79 static const char* kTempFileName = ".org.chromium.XXXXXX"; |
| 84 #endif | 80 #endif |
| 85 | 81 |
| 86 bool AbsolutePath(FilePath* path) { | 82 bool AbsolutePath(FilePath* path) { |
| 87 base::ThreadRestrictions::AssertIOAllowed(); // For realpath(). | |
| 88 char full_path[PATH_MAX]; | 83 char full_path[PATH_MAX]; |
| 89 if (realpath(path->value().c_str(), full_path) == NULL) | 84 if (realpath(path->value().c_str(), full_path) == NULL) |
| 90 return false; | 85 return false; |
| 91 *path = FilePath(full_path); | 86 *path = FilePath(full_path); |
| 92 return true; | 87 return true; |
| 93 } | 88 } |
| 94 | 89 |
| 95 int CountFilesCreatedAfter(const FilePath& path, | 90 int CountFilesCreatedAfter(const FilePath& path, |
| 96 const base::Time& comparison_time) { | 91 const base::Time& comparison_time) { |
| 97 base::ThreadRestrictions::AssertIOAllowed(); | |
| 98 int file_count = 0; | 92 int file_count = 0; |
| 99 | 93 |
| 100 DIR* dir = opendir(path.value().c_str()); | 94 DIR* dir = opendir(path.value().c_str()); |
| 101 if (dir) { | 95 if (dir) { |
| 102 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) && \ | 96 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) && \ |
| 103 !defined(OS_OPENBSD) && !defined(OS_SOLARIS) | 97 !defined(OS_OPENBSD) && !defined(OS_SOLARIS) |
| 104 #error Port warning: depending on the definition of struct dirent, \ | 98 #error Port warning: depending on the definition of struct dirent, \ |
| 105 additional space for pathname may be needed | 99 additional space for pathname may be needed |
| 106 #endif | 100 #endif |
| 107 struct dirent ent_buf; | 101 struct dirent ent_buf; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 138 closedir(dir); | 132 closedir(dir); |
| 139 } | 133 } |
| 140 return file_count; | 134 return file_count; |
| 141 } | 135 } |
| 142 | 136 |
| 143 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" | 137 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
| 144 // which works both with and without the recursive flag. I'm not sure we need | 138 // which works both with and without the recursive flag. I'm not sure we need |
| 145 // that functionality. If not, remove from file_util_win.cc, otherwise add it | 139 // that functionality. If not, remove from file_util_win.cc, otherwise add it |
| 146 // here. | 140 // here. |
| 147 bool Delete(const FilePath& path, bool recursive) { | 141 bool Delete(const FilePath& path, bool recursive) { |
| 148 base::ThreadRestrictions::AssertIOAllowed(); | |
| 149 const char* path_str = path.value().c_str(); | 142 const char* path_str = path.value().c_str(); |
| 150 stat_wrapper_t file_info; | 143 stat_wrapper_t file_info; |
| 151 int test = CallStat(path_str, &file_info); | 144 int test = CallStat(path_str, &file_info); |
| 152 if (test != 0) { | 145 if (test != 0) { |
| 153 // The Windows version defines this condition as success. | 146 // The Windows version defines this condition as success. |
| 154 bool ret = (errno == ENOENT || errno == ENOTDIR); | 147 bool ret = (errno == ENOENT || errno == ENOTDIR); |
| 155 return ret; | 148 return ret; |
| 156 } | 149 } |
| 157 if (!S_ISDIR(file_info.st_mode)) | 150 if (!S_ISDIR(file_info.st_mode)) |
| 158 return (unlink(path_str) == 0); | 151 return (unlink(path_str) == 0); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 178 | 171 |
| 179 while (success && !directories.empty()) { | 172 while (success && !directories.empty()) { |
| 180 FilePath dir = FilePath(directories.top()); | 173 FilePath dir = FilePath(directories.top()); |
| 181 directories.pop(); | 174 directories.pop(); |
| 182 success = (rmdir(dir.value().c_str()) == 0); | 175 success = (rmdir(dir.value().c_str()) == 0); |
| 183 } | 176 } |
| 184 return success; | 177 return success; |
| 185 } | 178 } |
| 186 | 179 |
| 187 bool Move(const FilePath& from_path, const FilePath& to_path) { | 180 bool Move(const FilePath& from_path, const FilePath& to_path) { |
| 188 base::ThreadRestrictions::AssertIOAllowed(); | |
| 189 // Windows compatibility: if to_path exists, from_path and to_path | 181 // Windows compatibility: if to_path exists, from_path and to_path |
| 190 // must be the same type, either both files, or both directories. | 182 // must be the same type, either both files, or both directories. |
| 191 stat_wrapper_t to_file_info; | 183 stat_wrapper_t to_file_info; |
| 192 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { | 184 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
| 193 stat_wrapper_t from_file_info; | 185 stat_wrapper_t from_file_info; |
| 194 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { | 186 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { |
| 195 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) | 187 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) |
| 196 return false; | 188 return false; |
| 197 } else { | 189 } else { |
| 198 return false; | 190 return false; |
| 199 } | 191 } |
| 200 } | 192 } |
| 201 | 193 |
| 202 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) | 194 if (rename(from_path.value().c_str(), to_path.value().c_str()) == 0) |
| 203 return true; | 195 return true; |
| 204 | 196 |
| 205 if (!CopyDirectory(from_path, to_path, true)) | 197 if (!CopyDirectory(from_path, to_path, true)) |
| 206 return false; | 198 return false; |
| 207 | 199 |
| 208 Delete(from_path, true); | 200 Delete(from_path, true); |
| 209 return true; | 201 return true; |
| 210 } | 202 } |
| 211 | 203 |
| 212 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { | 204 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { |
| 213 base::ThreadRestrictions::AssertIOAllowed(); | |
| 214 return (rename(from_path.value().c_str(), to_path.value().c_str()) == 0); | 205 return (rename(from_path.value().c_str(), to_path.value().c_str()) == 0); |
| 215 } | 206 } |
| 216 | 207 |
| 217 bool CopyDirectory(const FilePath& from_path, | 208 bool CopyDirectory(const FilePath& from_path, |
| 218 const FilePath& to_path, | 209 const FilePath& to_path, |
| 219 bool recursive) { | 210 bool recursive) { |
| 220 base::ThreadRestrictions::AssertIOAllowed(); | |
| 221 // Some old callers of CopyDirectory want it to support wildcards. | 211 // Some old callers of CopyDirectory want it to support wildcards. |
| 222 // After some discussion, we decided to fix those callers. | 212 // After some discussion, we decided to fix those callers. |
| 223 // Break loudly here if anyone tries to do this. | 213 // Break loudly here if anyone tries to do this. |
| 224 // TODO(evanm): remove this once we're sure it's ok. | 214 // TODO(evanm): remove this once we're sure it's ok. |
| 225 DCHECK(to_path.value().find('*') == std::string::npos); | 215 DCHECK(to_path.value().find('*') == std::string::npos); |
| 226 DCHECK(from_path.value().find('*') == std::string::npos); | 216 DCHECK(from_path.value().find('*') == std::string::npos); |
| 227 | 217 |
| 228 char top_dir[PATH_MAX]; | 218 char top_dir[PATH_MAX]; |
| 229 if (base::strlcpy(top_dir, from_path.value().c_str(), | 219 if (base::strlcpy(top_dir, from_path.value().c_str(), |
| 230 arraysize(top_dir)) >= arraysize(top_dir)) { | 220 arraysize(top_dir)) >= arraysize(top_dir)) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 300 } |
| 311 | 301 |
| 312 current = traversal.Next(); | 302 current = traversal.Next(); |
| 313 traversal.GetFindInfo(&info); | 303 traversal.GetFindInfo(&info); |
| 314 } | 304 } |
| 315 | 305 |
| 316 return success; | 306 return success; |
| 317 } | 307 } |
| 318 | 308 |
| 319 bool PathExists(const FilePath& path) { | 309 bool PathExists(const FilePath& path) { |
| 320 base::ThreadRestrictions::AssertIOAllowed(); | |
| 321 return access(path.value().c_str(), F_OK) == 0; | 310 return access(path.value().c_str(), F_OK) == 0; |
| 322 } | 311 } |
| 323 | 312 |
| 324 bool PathIsWritable(const FilePath& path) { | 313 bool PathIsWritable(const FilePath& path) { |
| 325 base::ThreadRestrictions::AssertIOAllowed(); | |
| 326 return access(path.value().c_str(), W_OK) == 0; | 314 return access(path.value().c_str(), W_OK) == 0; |
| 327 } | 315 } |
| 328 | 316 |
| 329 bool DirectoryExists(const FilePath& path) { | 317 bool DirectoryExists(const FilePath& path) { |
| 330 base::ThreadRestrictions::AssertIOAllowed(); | |
| 331 stat_wrapper_t file_info; | 318 stat_wrapper_t file_info; |
| 332 if (CallStat(path.value().c_str(), &file_info) == 0) | 319 if (CallStat(path.value().c_str(), &file_info) == 0) |
| 333 return S_ISDIR(file_info.st_mode); | 320 return S_ISDIR(file_info.st_mode); |
| 334 return false; | 321 return false; |
| 335 } | 322 } |
| 336 | 323 |
| 337 // TODO(erikkay): implement | 324 // TODO(erikkay): implement |
| 338 #if 0 | 325 #if 0 |
| 339 bool GetFileCreationLocalTimeFromHandle(int fd, | 326 bool GetFileCreationLocalTimeFromHandle(int fd, |
| 340 LPSYSTEMTIME creation_time) { | 327 LPSYSTEMTIME creation_time) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 371 break; | 358 break; |
| 372 total_read += bytes_read; | 359 total_read += bytes_read; |
| 373 } | 360 } |
| 374 return total_read == bytes; | 361 return total_read == bytes; |
| 375 } | 362 } |
| 376 | 363 |
| 377 // Creates and opens a temporary file in |directory|, returning the | 364 // Creates and opens a temporary file in |directory|, returning the |
| 378 // file descriptor. |path| is set to the temporary file path. | 365 // file descriptor. |path| is set to the temporary file path. |
| 379 // This function does NOT unlink() the file. | 366 // This function does NOT unlink() the file. |
| 380 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { | 367 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
| 381 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). | |
| 382 *path = directory.Append(kTempFileName); | 368 *path = directory.Append(kTempFileName); |
| 383 const std::string& tmpdir_string = path->value(); | 369 const std::string& tmpdir_string = path->value(); |
| 384 // this should be OK since mkstemp just replaces characters in place | 370 // this should be OK since mkstemp just replaces characters in place |
| 385 char* buffer = const_cast<char*>(tmpdir_string.c_str()); | 371 char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
| 386 | 372 |
| 387 return mkstemp(buffer); | 373 return mkstemp(buffer); |
| 388 } | 374 } |
| 389 | 375 |
| 390 bool CreateTemporaryFile(FilePath* path) { | 376 bool CreateTemporaryFile(FilePath* path) { |
| 391 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | |
| 392 FilePath directory; | 377 FilePath directory; |
| 393 if (!GetTempDir(&directory)) | 378 if (!GetTempDir(&directory)) |
| 394 return false; | 379 return false; |
| 395 int fd = CreateAndOpenFdForTemporaryFile(directory, path); | 380 int fd = CreateAndOpenFdForTemporaryFile(directory, path); |
| 396 if (fd < 0) | 381 if (fd < 0) |
| 397 return false; | 382 return false; |
| 398 close(fd); | 383 close(fd); |
| 399 return true; | 384 return true; |
| 400 } | 385 } |
| 401 | 386 |
| 402 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { | 387 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
| 403 FilePath directory; | 388 FilePath directory; |
| 404 if (!GetShmemTempDir(&directory)) | 389 if (!GetShmemTempDir(&directory)) |
| 405 return NULL; | 390 return NULL; |
| 406 | 391 |
| 407 return CreateAndOpenTemporaryFileInDir(directory, path); | 392 return CreateAndOpenTemporaryFileInDir(directory, path); |
| 408 } | 393 } |
| 409 | 394 |
| 410 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 395 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| 411 int fd = CreateAndOpenFdForTemporaryFile(dir, path); | 396 int fd = CreateAndOpenFdForTemporaryFile(dir, path); |
| 412 if (fd < 0) | 397 if (fd < 0) |
| 413 return NULL; | 398 return NULL; |
| 414 | 399 |
| 415 return fdopen(fd, "a+"); | 400 return fdopen(fd, "a+"); |
| 416 } | 401 } |
| 417 | 402 |
| 418 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 403 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
| 419 base::ThreadRestrictions::AssertIOAllowed(); // For call to close(). | |
| 420 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); | 404 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
| 421 return ((fd >= 0) && !close(fd)); | 405 return ((fd >= 0) && !close(fd)); |
| 422 } | 406 } |
| 423 | 407 |
| 424 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, | 408 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
| 425 const FilePath::StringType& name_tmpl, | 409 const FilePath::StringType& name_tmpl, |
| 426 FilePath* new_dir) { | 410 FilePath* new_dir) { |
| 427 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdtemp(). | |
| 428 CHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) | 411 CHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
| 429 << "Directory name template must contain \"XXXXXX\"."; | 412 << "Directory name template must contain \"XXXXXX\"."; |
| 430 | 413 |
| 431 FilePath sub_dir = base_dir.Append(name_tmpl); | 414 FilePath sub_dir = base_dir.Append(name_tmpl); |
| 432 std::string sub_dir_string = sub_dir.value(); | 415 std::string sub_dir_string = sub_dir.value(); |
| 433 | 416 |
| 434 // this should be OK since mkdtemp just replaces characters in place | 417 // this should be OK since mkdtemp just replaces characters in place |
| 435 char* buffer = const_cast<char*>(sub_dir_string.c_str()); | 418 char* buffer = const_cast<char*>(sub_dir_string.c_str()); |
| 436 char* dtemp = mkdtemp(buffer); | 419 char* dtemp = mkdtemp(buffer); |
| 437 if (!dtemp) { | 420 if (!dtemp) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 453 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 436 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 454 FilePath* new_temp_path) { | 437 FilePath* new_temp_path) { |
| 455 FilePath tmpdir; | 438 FilePath tmpdir; |
| 456 if (!GetTempDir(&tmpdir)) | 439 if (!GetTempDir(&tmpdir)) |
| 457 return false; | 440 return false; |
| 458 | 441 |
| 459 return CreateTemporaryDirInDirImpl(tmpdir, kTempFileName, new_temp_path); | 442 return CreateTemporaryDirInDirImpl(tmpdir, kTempFileName, new_temp_path); |
| 460 } | 443 } |
| 461 | 444 |
| 462 bool CreateDirectory(const FilePath& full_path) { | 445 bool CreateDirectory(const FilePath& full_path) { |
| 463 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). | |
| 464 std::vector<FilePath> subpaths; | 446 std::vector<FilePath> subpaths; |
| 465 | 447 |
| 466 // Collect a list of all parent directories. | 448 // Collect a list of all parent directories. |
| 467 FilePath last_path = full_path; | 449 FilePath last_path = full_path; |
| 468 subpaths.push_back(full_path); | 450 subpaths.push_back(full_path); |
| 469 for (FilePath path = full_path.DirName(); | 451 for (FilePath path = full_path.DirName(); |
| 470 path.value() != last_path.value(); path = path.DirName()) { | 452 path.value() != last_path.value(); path = path.DirName()) { |
| 471 subpaths.push_back(path); | 453 subpaths.push_back(path); |
| 472 last_path = path; | 454 last_path = path; |
| 473 } | 455 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 495 return false; | 477 return false; |
| 496 results->is_directory = S_ISDIR(file_info.st_mode); | 478 results->is_directory = S_ISDIR(file_info.st_mode); |
| 497 results->size = file_info.st_size; | 479 results->size = file_info.st_size; |
| 498 results->last_modified = base::Time::FromTimeT(file_info.st_mtime); | 480 results->last_modified = base::Time::FromTimeT(file_info.st_mtime); |
| 499 results->last_accessed = base::Time::FromTimeT(file_info.st_atime); | 481 results->last_accessed = base::Time::FromTimeT(file_info.st_atime); |
| 500 results->creation_time = base::Time::FromTimeT(file_info.st_ctime); | 482 results->creation_time = base::Time::FromTimeT(file_info.st_ctime); |
| 501 return true; | 483 return true; |
| 502 } | 484 } |
| 503 | 485 |
| 504 bool GetInode(const FilePath& path, ino_t* inode) { | 486 bool GetInode(const FilePath& path, ino_t* inode) { |
| 505 base::ThreadRestrictions::AssertIOAllowed(); // For call to stat(). | |
| 506 struct stat buffer; | 487 struct stat buffer; |
| 507 int result = stat(path.value().c_str(), &buffer); | 488 int result = stat(path.value().c_str(), &buffer); |
| 508 if (result < 0) | 489 if (result < 0) |
| 509 return false; | 490 return false; |
| 510 | 491 |
| 511 *inode = buffer.st_ino; | 492 *inode = buffer.st_ino; |
| 512 return true; | 493 return true; |
| 513 } | 494 } |
| 514 | 495 |
| 515 FILE* OpenFile(const std::string& filename, const char* mode) { | 496 FILE* OpenFile(const std::string& filename, const char* mode) { |
| 516 return OpenFile(FilePath(filename), mode); | 497 return OpenFile(FilePath(filename), mode); |
| 517 } | 498 } |
| 518 | 499 |
| 519 FILE* OpenFile(const FilePath& filename, const char* mode) { | 500 FILE* OpenFile(const FilePath& filename, const char* mode) { |
| 520 base::ThreadRestrictions::AssertIOAllowed(); | |
| 521 return fopen(filename.value().c_str(), mode); | 501 return fopen(filename.value().c_str(), mode); |
| 522 } | 502 } |
| 523 | 503 |
| 524 int ReadFile(const FilePath& filename, char* data, int size) { | 504 int ReadFile(const FilePath& filename, char* data, int size) { |
| 525 base::ThreadRestrictions::AssertIOAllowed(); | |
| 526 int fd = open(filename.value().c_str(), O_RDONLY); | 505 int fd = open(filename.value().c_str(), O_RDONLY); |
| 527 if (fd < 0) | 506 if (fd < 0) |
| 528 return -1; | 507 return -1; |
| 529 | 508 |
| 530 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); | 509 ssize_t bytes_read = HANDLE_EINTR(read(fd, data, size)); |
| 531 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 510 if (int ret = HANDLE_EINTR(close(fd)) < 0) |
| 532 return ret; | 511 return ret; |
| 533 return bytes_read; | 512 return bytes_read; |
| 534 } | 513 } |
| 535 | 514 |
| 536 int WriteFile(const FilePath& filename, const char* data, int size) { | 515 int WriteFile(const FilePath& filename, const char* data, int size) { |
| 537 base::ThreadRestrictions::AssertIOAllowed(); | |
| 538 int fd = creat(filename.value().c_str(), 0666); | 516 int fd = creat(filename.value().c_str(), 0666); |
| 539 if (fd < 0) | 517 if (fd < 0) |
| 540 return -1; | 518 return -1; |
| 541 | 519 |
| 542 int bytes_written = WriteFileDescriptor(fd, data, size); | 520 int bytes_written = WriteFileDescriptor(fd, data, size); |
| 543 if (int ret = HANDLE_EINTR(close(fd)) < 0) | 521 if (int ret = HANDLE_EINTR(close(fd)) < 0) |
| 544 return ret; | 522 return ret; |
| 545 return bytes_written; | 523 return bytes_written; |
| 546 } | 524 } |
| 547 | 525 |
| 548 int WriteFileDescriptor(const int fd, const char* data, int size) { | 526 int WriteFileDescriptor(const int fd, const char* data, int size) { |
| 549 // Allow for partial writes. | 527 // Allow for partial writes. |
| 550 ssize_t bytes_written_total = 0; | 528 ssize_t bytes_written_total = 0; |
| 551 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; | 529 for (ssize_t bytes_written_partial = 0; bytes_written_total < size; |
| 552 bytes_written_total += bytes_written_partial) { | 530 bytes_written_total += bytes_written_partial) { |
| 553 bytes_written_partial = | 531 bytes_written_partial = |
| 554 HANDLE_EINTR(write(fd, data + bytes_written_total, | 532 HANDLE_EINTR(write(fd, data + bytes_written_total, |
| 555 size - bytes_written_total)); | 533 size - bytes_written_total)); |
| 556 if (bytes_written_partial < 0) | 534 if (bytes_written_partial < 0) |
| 557 return -1; | 535 return -1; |
| 558 } | 536 } |
| 559 | 537 |
| 560 return bytes_written_total; | 538 return bytes_written_total; |
| 561 } | 539 } |
| 562 | 540 |
| 563 // Gets the current working directory for the process. | 541 // Gets the current working directory for the process. |
| 564 bool GetCurrentDirectory(FilePath* dir) { | 542 bool GetCurrentDirectory(FilePath* dir) { |
| 565 // getcwd can return ENOENT, which implies it checks against the disk. | |
| 566 base::ThreadRestrictions::AssertIOAllowed(); | |
| 567 | |
| 568 char system_buffer[PATH_MAX] = ""; | 543 char system_buffer[PATH_MAX] = ""; |
| 569 if (!getcwd(system_buffer, sizeof(system_buffer))) { | 544 if (!getcwd(system_buffer, sizeof(system_buffer))) { |
| 570 NOTREACHED(); | 545 NOTREACHED(); |
| 571 return false; | 546 return false; |
| 572 } | 547 } |
| 573 *dir = FilePath(system_buffer); | 548 *dir = FilePath(system_buffer); |
| 574 return true; | 549 return true; |
| 575 } | 550 } |
| 576 | 551 |
| 577 // Sets the current working directory for the process. | 552 // Sets the current working directory for the process. |
| 578 bool SetCurrentDirectory(const FilePath& path) { | 553 bool SetCurrentDirectory(const FilePath& path) { |
| 579 base::ThreadRestrictions::AssertIOAllowed(); | |
| 580 int ret = chdir(path.value().c_str()); | 554 int ret = chdir(path.value().c_str()); |
| 581 return !ret; | 555 return !ret; |
| 582 } | 556 } |
| 583 | 557 |
| 584 /////////////////////////////////////////////// | 558 /////////////////////////////////////////////// |
| 585 // FileEnumerator | 559 // FileEnumerator |
| 586 | 560 |
| 587 FileEnumerator::FileEnumerator(const FilePath& root_path, | 561 FileEnumerator::FileEnumerator(const FilePath& root_path, |
| 588 bool recursive, | 562 bool recursive, |
| 589 FileEnumerator::FILE_TYPE file_type) | 563 FileEnumerator::FILE_TYPE file_type) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 directory_entries_.push_back(*i); | 648 directory_entries_.push_back(*i); |
| 675 } | 649 } |
| 676 } | 650 } |
| 677 | 651 |
| 678 return root_path_.Append(directory_entries_[current_directory_entry_ | 652 return root_path_.Append(directory_entries_[current_directory_entry_ |
| 679 ].filename); | 653 ].filename); |
| 680 } | 654 } |
| 681 | 655 |
| 682 bool FileEnumerator::ReadDirectory(std::vector<DirectoryEntryInfo>* entries, | 656 bool FileEnumerator::ReadDirectory(std::vector<DirectoryEntryInfo>* entries, |
| 683 const FilePath& source, bool show_links) { | 657 const FilePath& source, bool show_links) { |
| 684 base::ThreadRestrictions::AssertIOAllowed(); | |
| 685 DIR* dir = opendir(source.value().c_str()); | 658 DIR* dir = opendir(source.value().c_str()); |
| 686 if (!dir) | 659 if (!dir) |
| 687 return false; | 660 return false; |
| 688 | 661 |
| 689 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) && \ | 662 #if !defined(OS_LINUX) && !defined(OS_MACOSX) && !defined(OS_FREEBSD) && \ |
| 690 !defined(OS_OPENBSD) && !defined(OS_SOLARIS) | 663 !defined(OS_OPENBSD) && !defined(OS_SOLARIS) |
| 691 #error Port warning: depending on the definition of struct dirent, \ | 664 #error Port warning: depending on the definition of struct dirent, \ |
| 692 additional space for pathname may be needed | 665 additional space for pathname may be needed |
| 693 #endif | 666 #endif |
| 694 | 667 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 723 /////////////////////////////////////////////// | 696 /////////////////////////////////////////////// |
| 724 // MemoryMappedFile | 697 // MemoryMappedFile |
| 725 | 698 |
| 726 MemoryMappedFile::MemoryMappedFile() | 699 MemoryMappedFile::MemoryMappedFile() |
| 727 : file_(base::kInvalidPlatformFileValue), | 700 : file_(base::kInvalidPlatformFileValue), |
| 728 data_(NULL), | 701 data_(NULL), |
| 729 length_(0) { | 702 length_(0) { |
| 730 } | 703 } |
| 731 | 704 |
| 732 bool MemoryMappedFile::MapFileToMemoryInternal() { | 705 bool MemoryMappedFile::MapFileToMemoryInternal() { |
| 733 base::ThreadRestrictions::AssertIOAllowed(); | |
| 734 | |
| 735 struct stat file_stat; | 706 struct stat file_stat; |
| 736 if (fstat(file_, &file_stat) == base::kInvalidPlatformFileValue) { | 707 if (fstat(file_, &file_stat) == base::kInvalidPlatformFileValue) { |
| 737 LOG(ERROR) << "Couldn't fstat " << file_ << ", errno " << errno; | 708 LOG(ERROR) << "Couldn't fstat " << file_ << ", errno " << errno; |
| 738 return false; | 709 return false; |
| 739 } | 710 } |
| 740 length_ = file_stat.st_size; | 711 length_ = file_stat.st_size; |
| 741 | 712 |
| 742 data_ = static_cast<uint8*>( | 713 data_ = static_cast<uint8*>( |
| 743 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); | 714 mmap(NULL, length_, PROT_READ, MAP_SHARED, file_, 0)); |
| 744 if (data_ == MAP_FAILED) | 715 if (data_ == MAP_FAILED) |
| 745 LOG(ERROR) << "Couldn't mmap " << file_ << ", errno " << errno; | 716 LOG(ERROR) << "Couldn't mmap " << file_ << ", errno " << errno; |
| 746 | 717 |
| 747 return data_ != MAP_FAILED; | 718 return data_ != MAP_FAILED; |
| 748 } | 719 } |
| 749 | 720 |
| 750 void MemoryMappedFile::CloseHandles() { | 721 void MemoryMappedFile::CloseHandles() { |
| 751 base::ThreadRestrictions::AssertIOAllowed(); | |
| 752 | |
| 753 if (data_ != NULL) | 722 if (data_ != NULL) |
| 754 munmap(data_, length_); | 723 munmap(data_, length_); |
| 755 if (file_ != base::kInvalidPlatformFileValue) | 724 if (file_ != base::kInvalidPlatformFileValue) |
| 756 close(file_); | 725 close(file_); |
| 757 | 726 |
| 758 data_ = NULL; | 727 data_ = NULL; |
| 759 length_ = 0; | 728 length_ = 0; |
| 760 file_ = base::kInvalidPlatformFileValue; | 729 file_ = base::kInvalidPlatformFileValue; |
| 761 } | 730 } |
| 762 | 731 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 bool GetShmemTempDir(FilePath* path) { | 763 bool GetShmemTempDir(FilePath* path) { |
| 795 *path = FilePath("/dev/shm"); | 764 *path = FilePath("/dev/shm"); |
| 796 return true; | 765 return true; |
| 797 } | 766 } |
| 798 | 767 |
| 799 FilePath GetHomeDir() { | 768 FilePath GetHomeDir() { |
| 800 const char* home_dir = getenv("HOME"); | 769 const char* home_dir = getenv("HOME"); |
| 801 if (home_dir && home_dir[0]) | 770 if (home_dir && home_dir[0]) |
| 802 return FilePath(home_dir); | 771 return FilePath(home_dir); |
| 803 | 772 |
| 804 // g_get_home_dir calls getpwent, which can fall through to LDAP calls. | |
| 805 base::ThreadRestrictions::AssertIOAllowed(); | |
| 806 | |
| 807 home_dir = g_get_home_dir(); | 773 home_dir = g_get_home_dir(); |
| 808 if (home_dir && home_dir[0]) | 774 if (home_dir && home_dir[0]) |
| 809 return FilePath(home_dir); | 775 return FilePath(home_dir); |
| 810 | 776 |
| 811 FilePath rv; | 777 FilePath rv; |
| 812 if (file_util::GetTempDir(&rv)) | 778 if (file_util::GetTempDir(&rv)) |
| 813 return rv; | 779 return rv; |
| 814 | 780 |
| 815 // Last resort. | 781 // Last resort. |
| 816 return FilePath("/tmp"); | 782 return FilePath("/tmp"); |
| 817 } | 783 } |
| 818 | 784 |
| 819 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 785 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
| 820 base::ThreadRestrictions::AssertIOAllowed(); | |
| 821 int infile = open(from_path.value().c_str(), O_RDONLY); | 786 int infile = open(from_path.value().c_str(), O_RDONLY); |
| 822 if (infile < 0) | 787 if (infile < 0) |
| 823 return false; | 788 return false; |
| 824 | 789 |
| 825 int outfile = creat(to_path.value().c_str(), 0666); | 790 int outfile = creat(to_path.value().c_str(), 0666); |
| 826 if (outfile < 0) { | 791 if (outfile < 0) { |
| 827 close(infile); | 792 close(infile); |
| 828 return false; | 793 return false; |
| 829 } | 794 } |
| 830 | 795 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 858 if (HANDLE_EINTR(close(infile)) < 0) | 823 if (HANDLE_EINTR(close(infile)) < 0) |
| 859 result = false; | 824 result = false; |
| 860 if (HANDLE_EINTR(close(outfile)) < 0) | 825 if (HANDLE_EINTR(close(outfile)) < 0) |
| 861 result = false; | 826 result = false; |
| 862 | 827 |
| 863 return result; | 828 return result; |
| 864 } | 829 } |
| 865 #endif // defined(OS_MACOSX) | 830 #endif // defined(OS_MACOSX) |
| 866 | 831 |
| 867 } // namespace file_util | 832 } // namespace file_util |
| OLD | NEW |