| 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 <fcntl.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 | 14 |
| 15 namespace file_util { | 15 namespace file_util { |
| 16 | 16 |
| 17 const wchar_t kPathSeparator = L'/'; | 17 const wchar_t kPathSeparator = L'/'; |
| 18 | 18 |
| 19 bool GetTempDir(std::wstring* path) { | 19 bool GetTempDir(std::wstring* path) { |
| 20 const char* tmp = getenv("TMPDIR"); | 20 const char* tmp = getenv("TMPDIR"); |
| 21 if (tmp) | 21 if (tmp) |
| 22 *path = UTF8ToWide(tmp); | 22 *path = UTF8ToWide(tmp); |
| 23 else | 23 else |
| 24 *path = L"/tmp"; | 24 *path = L"/tmp"; |
| 25 return true; | 25 return true; |
| 26 } | 26 } |
| 27 | 27 |
| 28 // Does not copy attributes, permissions, metadata etc. |
| 28 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { | 29 bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) { |
| 29 int infile = open(WideToUTF8(from_path).c_str(), O_RDONLY); | 30 int infile = open(WideToUTF8(from_path).c_str(), O_RDONLY); |
| 30 if (infile < 0) | 31 if (infile < 0) |
| 31 return false; | 32 return false; |
| 32 | 33 |
| 33 int outfile = creat(WideToUTF8(to_path).c_str(), 0666); | 34 int outfile = creat(WideToUTF8(to_path).c_str(), 0666); |
| 34 if (outfile < 0) { | 35 if (outfile < 0) { |
| 35 close(infile); | 36 close(infile); |
| 36 return false; | 37 return false; |
| 37 } | 38 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 64 } | 65 } |
| 65 | 66 |
| 66 if (close(infile) < 0) | 67 if (close(infile) < 0) |
| 67 result = false; | 68 result = false; |
| 68 if (close(outfile) < 0) | 69 if (close(outfile) < 0) |
| 69 result = false; | 70 result = false; |
| 70 | 71 |
| 71 return result; | 72 return result; |
| 72 } | 73 } |
| 73 | 74 |
| 75 // TODO: Make this function symlink-aware. |
| 76 // Does not copy attributes, permissions, metadata etc. |
| 77 bool CopyDirectory(const std::wstring& from_path, const std::wstring& to_path, |
| 78 bool recursive) { |
| 79 if (!CreateDirectory(to_path)) { |
| 80 return false; |
| 81 } |
| 82 |
| 83 FileEnumerator enumerator(from_path, false, FileEnumerator::FILES); |
| 84 std::wstring path; |
| 85 while ((path = enumerator.Next()) != L"") { |
| 86 path = GetFilenameFromPath(path); |
| 87 std::wstring from = from_path; |
| 88 AppendToPath(&from, path); |
| 89 |
| 90 std::wstring to = to_path; |
| 91 AppendToPath(&to, path); |
| 92 |
| 93 if (!CopyFile(from, to)) { |
| 94 // If some files have been copied successfully they won't be deleted. |
| 95 return false; |
| 96 } |
| 97 } |
| 98 |
| 99 if (recursive) { |
| 100 FileEnumerator enumerator(from_path, false, FileEnumerator::DIRECTORIES); |
| 101 std::wstring path; |
| 102 while ((path = enumerator.Next()) != L"") { |
| 103 path = GetFilenameFromPath(path); |
| 104 std::wstring from = from_path; |
| 105 AppendToPath(&from, path); |
| 106 |
| 107 std::wstring to = to_path; |
| 108 AppendToPath(&to, path); |
| 109 |
| 110 if (!CopyDirectory(from, to, true)) { |
| 111 // If some files have been copied successfully they won't be deleted. |
| 112 return false; |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 return true; |
| 118 } |
| 119 |
| 74 } // namespace file_util | 120 } // namespace file_util |
| OLD | NEW |