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 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 return NULL; | 394 return NULL; |
395 | 395 |
396 return fdopen(fd, "a+"); | 396 return fdopen(fd, "a+"); |
397 } | 397 } |
398 | 398 |
399 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 399 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
400 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); | 400 int fd = CreateAndOpenFdForTemporaryFile(dir, temp_file); |
401 return ((fd >= 0) && !close(fd)); | 401 return ((fd >= 0) && !close(fd)); |
402 } | 402 } |
403 | 403 |
| 404 static bool CreateTemporaryDirInDirImpl(const FilePath& base_dir, |
| 405 const FilePath::StringType& name_tmpl, |
| 406 FilePath* new_dir) { |
| 407 CHECK(name_tmpl.find("XXXXXX") != FilePath::StringType::npos) |
| 408 << "Directory name template must contain \"XXXXXX\"."; |
| 409 |
| 410 FilePath sub_dir = base_dir.Append(name_tmpl); |
| 411 std::string sub_dir_string = sub_dir.value(); |
| 412 |
| 413 // this should be OK since mkdtemp just replaces characters in place |
| 414 char* buffer = const_cast<char*>(sub_dir_string.c_str()); |
| 415 char* dtemp = mkdtemp(buffer); |
| 416 if (!dtemp) |
| 417 return false; |
| 418 *new_dir = FilePath(dtemp); |
| 419 return true; |
| 420 } |
| 421 |
| 422 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 423 const FilePath::StringType& prefix, |
| 424 FilePath* new_dir) { |
| 425 FilePath::StringType mkdtemp_template = prefix; |
| 426 mkdtemp_template.append(FILE_PATH_LITERAL("XXXXXX")); |
| 427 return CreateTemporaryDirInDirImpl(base_dir, mkdtemp_template, new_dir); |
| 428 } |
| 429 |
404 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 430 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
405 FilePath* new_temp_path) { | 431 FilePath* new_temp_path) { |
406 FilePath tmpdir; | 432 FilePath tmpdir; |
407 if (!GetTempDir(&tmpdir)) | 433 if (!GetTempDir(&tmpdir)) |
408 return false; | 434 return false; |
409 tmpdir = tmpdir.Append(kTempFileName); | 435 |
410 std::string tmpdir_string = tmpdir.value(); | 436 return CreateTemporaryDirInDirImpl(tmpdir, kTempFileName, new_temp_path); |
411 // this should be OK since mkdtemp just replaces characters in place | |
412 char* buffer = const_cast<char*>(tmpdir_string.c_str()); | |
413 char* dtemp = mkdtemp(buffer); | |
414 if (!dtemp) | |
415 return false; | |
416 *new_temp_path = FilePath(dtemp); | |
417 return true; | |
418 } | 437 } |
419 | 438 |
420 bool CreateDirectory(const FilePath& full_path) { | 439 bool CreateDirectory(const FilePath& full_path) { |
421 std::vector<FilePath> subpaths; | 440 std::vector<FilePath> subpaths; |
422 | 441 |
423 // Collect a list of all parent directories. | 442 // Collect a list of all parent directories. |
424 FilePath last_path = full_path; | 443 FilePath last_path = full_path; |
425 subpaths.push_back(full_path); | 444 subpaths.push_back(full_path); |
426 for (FilePath path = full_path.DirName(); | 445 for (FilePath path = full_path.DirName(); |
427 path.value() != last_path.value(); path = path.DirName()) { | 446 path.value() != last_path.value(); path = path.DirName()) { |
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 if (HANDLE_EINTR(close(infile)) < 0) | 808 if (HANDLE_EINTR(close(infile)) < 0) |
790 result = false; | 809 result = false; |
791 if (HANDLE_EINTR(close(outfile)) < 0) | 810 if (HANDLE_EINTR(close(outfile)) < 0) |
792 result = false; | 811 result = false; |
793 | 812 |
794 return result; | 813 return result; |
795 } | 814 } |
796 #endif // defined(OS_MACOSX) | 815 #endif // defined(OS_MACOSX) |
797 | 816 |
798 } // namespace file_util | 817 } // namespace file_util |
OLD | NEW |