OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/test/test_file_util.h" | 5 #include "base/test/test_file_util.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 | 19 |
20 namespace file_util { | 20 namespace file_util { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Deny |permission| on the file |path|. | 24 // Deny |permission| on the file |path|. |
25 bool DenyFilePermission(const FilePath& path, mode_t permission) { | 25 bool DenyFilePermission(const base::FilePath& path, mode_t permission) { |
26 struct stat stat_buf; | 26 struct stat stat_buf; |
27 if (stat(path.value().c_str(), &stat_buf) != 0) | 27 if (stat(path.value().c_str(), &stat_buf) != 0) |
28 return false; | 28 return false; |
29 stat_buf.st_mode &= ~permission; | 29 stat_buf.st_mode &= ~permission; |
30 | 30 |
31 int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode)); | 31 int rv = HANDLE_EINTR(chmod(path.value().c_str(), stat_buf.st_mode)); |
32 return rv == 0; | 32 return rv == 0; |
33 } | 33 } |
34 | 34 |
35 // Gets a blob indicating the permission information for |path|. | 35 // Gets a blob indicating the permission information for |path|. |
36 // |length| is the length of the blob. Zero on failure. | 36 // |length| is the length of the blob. Zero on failure. |
37 // Returns the blob pointer, or NULL on failure. | 37 // Returns the blob pointer, or NULL on failure. |
38 void* GetPermissionInfo(const FilePath& path, size_t* length) { | 38 void* GetPermissionInfo(const base::FilePath& path, size_t* length) { |
39 DCHECK(length); | 39 DCHECK(length); |
40 *length = 0; | 40 *length = 0; |
41 | 41 |
42 struct stat stat_buf; | 42 struct stat stat_buf; |
43 if (stat(path.value().c_str(), &stat_buf) != 0) | 43 if (stat(path.value().c_str(), &stat_buf) != 0) |
44 return NULL; | 44 return NULL; |
45 | 45 |
46 *length = sizeof(mode_t); | 46 *length = sizeof(mode_t); |
47 mode_t* mode = new mode_t; | 47 mode_t* mode = new mode_t; |
48 *mode = stat_buf.st_mode & ~S_IFMT; // Filter out file/path kind. | 48 *mode = stat_buf.st_mode & ~S_IFMT; // Filter out file/path kind. |
49 | 49 |
50 return mode; | 50 return mode; |
51 } | 51 } |
52 | 52 |
53 // Restores the permission information for |path|, given the blob retrieved | 53 // Restores the permission information for |path|, given the blob retrieved |
54 // using |GetPermissionInfo()|. | 54 // using |GetPermissionInfo()|. |
55 // |info| is the pointer to the blob. | 55 // |info| is the pointer to the blob. |
56 // |length| is the length of the blob. | 56 // |length| is the length of the blob. |
57 // Either |info| or |length| may be NULL/0, in which case nothing happens. | 57 // Either |info| or |length| may be NULL/0, in which case nothing happens. |
58 bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) { | 58 bool RestorePermissionInfo(const base::FilePath& path, |
| 59 void* info, size_t length) { |
59 if (!info || (length == 0)) | 60 if (!info || (length == 0)) |
60 return false; | 61 return false; |
61 | 62 |
62 DCHECK_EQ(sizeof(mode_t), length); | 63 DCHECK_EQ(sizeof(mode_t), length); |
63 mode_t* mode = reinterpret_cast<mode_t*>(info); | 64 mode_t* mode = reinterpret_cast<mode_t*>(info); |
64 | 65 |
65 int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode)); | 66 int rv = HANDLE_EINTR(chmod(path.value().c_str(), *mode)); |
66 | 67 |
67 delete mode; | 68 delete mode; |
68 | 69 |
69 return rv == 0; | 70 return rv == 0; |
70 } | 71 } |
71 | 72 |
72 } // namespace | 73 } // namespace |
73 | 74 |
74 bool DieFileDie(const FilePath& file, bool recurse) { | 75 bool DieFileDie(const base::FilePath& file, bool recurse) { |
75 // There is no need to workaround Windows problems on POSIX. | 76 // There is no need to workaround Windows problems on POSIX. |
76 // Just pass-through. | 77 // Just pass-through. |
77 return file_util::Delete(file, recurse); | 78 return file_util::Delete(file, recurse); |
78 } | 79 } |
79 | 80 |
80 // Mostly a verbatim copy of CopyDirectory | 81 // Mostly a verbatim copy of CopyDirectory |
81 bool CopyRecursiveDirNoCache(const FilePath& source_dir, | 82 bool CopyRecursiveDirNoCache(const base::FilePath& source_dir, |
82 const FilePath& dest_dir) { | 83 const base::FilePath& dest_dir) { |
83 char top_dir[PATH_MAX]; | 84 char top_dir[PATH_MAX]; |
84 if (base::strlcpy(top_dir, source_dir.value().c_str(), | 85 if (base::strlcpy(top_dir, source_dir.value().c_str(), |
85 arraysize(top_dir)) >= arraysize(top_dir)) { | 86 arraysize(top_dir)) >= arraysize(top_dir)) { |
86 return false; | 87 return false; |
87 } | 88 } |
88 | 89 |
89 // This function does not properly handle destinations within the source | 90 // This function does not properly handle destinations within the source |
90 FilePath real_to_path = dest_dir; | 91 base::FilePath real_to_path = dest_dir; |
91 if (PathExists(real_to_path)) { | 92 if (PathExists(real_to_path)) { |
92 if (!AbsolutePath(&real_to_path)) | 93 if (!AbsolutePath(&real_to_path)) |
93 return false; | 94 return false; |
94 } else { | 95 } else { |
95 real_to_path = real_to_path.DirName(); | 96 real_to_path = real_to_path.DirName(); |
96 if (!AbsolutePath(&real_to_path)) | 97 if (!AbsolutePath(&real_to_path)) |
97 return false; | 98 return false; |
98 } | 99 } |
99 if (real_to_path.value().compare(0, source_dir.value().size(), | 100 if (real_to_path.value().compare(0, source_dir.value().size(), |
100 source_dir.value()) == 0) | 101 source_dir.value()) == 0) |
101 return false; | 102 return false; |
102 | 103 |
103 bool success = true; | 104 bool success = true; |
104 int traverse_type = FileEnumerator::FILES | | 105 int traverse_type = FileEnumerator::FILES | |
105 FileEnumerator::SHOW_SYM_LINKS | FileEnumerator::DIRECTORIES; | 106 FileEnumerator::SHOW_SYM_LINKS | FileEnumerator::DIRECTORIES; |
106 FileEnumerator traversal(source_dir, true, traverse_type); | 107 FileEnumerator traversal(source_dir, true, traverse_type); |
107 | 108 |
108 // dest_dir may not exist yet, start the loop with dest_dir | 109 // dest_dir may not exist yet, start the loop with dest_dir |
109 FileEnumerator::FindInfo info; | 110 FileEnumerator::FindInfo info; |
110 FilePath current = source_dir; | 111 base::FilePath current = source_dir; |
111 if (stat(source_dir.value().c_str(), &info.stat) < 0) { | 112 if (stat(source_dir.value().c_str(), &info.stat) < 0) { |
112 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't stat source directory: " | 113 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't stat source directory: " |
113 << source_dir.value() << " errno = " << errno; | 114 << source_dir.value() << " errno = " << errno; |
114 success = false; | 115 success = false; |
115 } | 116 } |
116 | 117 |
117 while (success && !current.empty()) { | 118 while (success && !current.empty()) { |
118 // |current| is the source path, including source_dir, so paste | 119 // |current| is the source path, including source_dir, so paste |
119 // the suffix after source_dir onto dest_dir to create the target_path. | 120 // the suffix after source_dir onto dest_dir to create the target_path. |
120 std::string suffix(¤t.value().c_str()[source_dir.value().size()]); | 121 std::string suffix(¤t.value().c_str()[source_dir.value().size()]); |
121 // Strip the leading '/' (if any). | 122 // Strip the leading '/' (if any). |
122 if (!suffix.empty()) { | 123 if (!suffix.empty()) { |
123 DCHECK_EQ('/', suffix[0]); | 124 DCHECK_EQ('/', suffix[0]); |
124 suffix.erase(0, 1); | 125 suffix.erase(0, 1); |
125 } | 126 } |
126 const FilePath target_path = dest_dir.Append(suffix); | 127 const base::FilePath target_path = dest_dir.Append(suffix); |
127 | 128 |
128 if (S_ISDIR(info.stat.st_mode)) { | 129 if (S_ISDIR(info.stat.st_mode)) { |
129 if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 && | 130 if (mkdir(target_path.value().c_str(), info.stat.st_mode & 01777) != 0 && |
130 errno != EEXIST) { | 131 errno != EEXIST) { |
131 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't create directory: " | 132 DLOG(ERROR) << "CopyRecursiveDirNoCache() couldn't create directory: " |
132 << target_path.value() << " errno = " << errno; | 133 << target_path.value() << " errno = " << errno; |
133 success = false; | 134 success = false; |
134 } | 135 } |
135 } else if (S_ISREG(info.stat.st_mode)) { | 136 } else if (S_ISREG(info.stat.st_mode)) { |
136 if (CopyFile(current, target_path)) { | 137 if (CopyFile(current, target_path)) { |
(...skipping 10 matching lines...) Expand all Loading... |
147 } | 148 } |
148 | 149 |
149 current = traversal.Next(); | 150 current = traversal.Next(); |
150 traversal.GetFindInfo(&info); | 151 traversal.GetFindInfo(&info); |
151 } | 152 } |
152 | 153 |
153 return success; | 154 return success; |
154 } | 155 } |
155 | 156 |
156 #if !defined(OS_LINUX) && !defined(OS_MACOSX) | 157 #if !defined(OS_LINUX) && !defined(OS_MACOSX) |
157 bool EvictFileFromSystemCache(const FilePath& file) { | 158 bool EvictFileFromSystemCache(const base::FilePath& file) { |
158 // There doesn't seem to be a POSIX way to cool the disk cache. | 159 // There doesn't seem to be a POSIX way to cool the disk cache. |
159 NOTIMPLEMENTED(); | 160 NOTIMPLEMENTED(); |
160 return false; | 161 return false; |
161 } | 162 } |
162 #endif | 163 #endif |
163 | 164 |
164 std::wstring FilePathAsWString(const FilePath& path) { | 165 std::wstring FilePathAsWString(const base::FilePath& path) { |
165 return UTF8ToWide(path.value()); | 166 return UTF8ToWide(path.value()); |
166 } | 167 } |
167 FilePath WStringAsFilePath(const std::wstring& path) { | 168 base::FilePath WStringAsFilePath(const std::wstring& path) { |
168 return FilePath(WideToUTF8(path)); | 169 return base::FilePath(WideToUTF8(path)); |
169 } | 170 } |
170 | 171 |
171 bool MakeFileUnreadable(const FilePath& path) { | 172 bool MakeFileUnreadable(const base::FilePath& path) { |
172 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); | 173 return DenyFilePermission(path, S_IRUSR | S_IRGRP | S_IROTH); |
173 } | 174 } |
174 | 175 |
175 bool MakeFileUnwritable(const FilePath& path) { | 176 bool MakeFileUnwritable(const base::FilePath& path) { |
176 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); | 177 return DenyFilePermission(path, S_IWUSR | S_IWGRP | S_IWOTH); |
177 } | 178 } |
178 | 179 |
179 PermissionRestorer::PermissionRestorer(const FilePath& path) | 180 PermissionRestorer::PermissionRestorer(const base::FilePath& path) |
180 : path_(path), info_(NULL), length_(0) { | 181 : path_(path), info_(NULL), length_(0) { |
181 info_ = GetPermissionInfo(path_, &length_); | 182 info_ = GetPermissionInfo(path_, &length_); |
182 DCHECK(info_ != NULL); | 183 DCHECK(info_ != NULL); |
183 DCHECK_NE(0u, length_); | 184 DCHECK_NE(0u, length_); |
184 } | 185 } |
185 | 186 |
186 PermissionRestorer::~PermissionRestorer() { | 187 PermissionRestorer::~PermissionRestorer() { |
187 if (!RestorePermissionInfo(path_, info_, length_)) | 188 if (!RestorePermissionInfo(path_, info_, length_)) |
188 NOTREACHED(); | 189 NOTREACHED(); |
189 } | 190 } |
190 | 191 |
191 } // namespace file_util | 192 } // namespace file_util |
OLD | NEW |