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 "webkit/browser/fileapi/native_file_util.h" | 5 #include "storage/browser/fileapi/native_file_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "webkit/browser/fileapi/file_system_operation_context.h" | 11 #include "storage/browser/fileapi/file_system_operation_context.h" |
12 #include "webkit/browser/fileapi/file_system_url.h" | 12 #include "storage/browser/fileapi/file_system_url.h" |
13 | 13 |
14 namespace fileapi { | 14 namespace storage { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // Sets permissions on directory at |dir_path| based on the target platform. | 18 // Sets permissions on directory at |dir_path| based on the target platform. |
19 // Returns true on success, or false otherwise. | 19 // Returns true on success, or false otherwise. |
20 // | 20 // |
21 // TODO(benchan): Find a better place outside webkit to host this function. | 21 // TODO(benchan): Find a better place outside webkit to host this function. |
22 bool SetPlatformSpecificDirectoryPermissions(const base::FilePath& dir_path) { | 22 bool SetPlatformSpecificDirectoryPermissions(const base::FilePath& dir_path) { |
23 #if defined(OS_CHROMEOS) | 23 #if defined(OS_CHROMEOS) |
24 // System daemons on Chrome OS may run as a user different than the Chrome | 24 // System daemons on Chrome OS may run as a user different than the Chrome |
25 // process but need to access files under the directories created here. | 25 // process but need to access files under the directories created here. |
26 // Because of that, grant the execute permission on the created directory | 26 // Because of that, grant the execute permission on the created directory |
27 // to group and other users. | 27 // to group and other users. |
28 if (HANDLE_EINTR(chmod(dir_path.value().c_str(), | 28 if (HANDLE_EINTR( |
29 S_IRWXU | S_IXGRP | S_IXOTH)) != 0) { | 29 chmod(dir_path.value().c_str(), S_IRWXU | S_IXGRP | S_IXOTH)) != 0) { |
30 return false; | 30 return false; |
31 } | 31 } |
32 #endif | 32 #endif |
33 // Keep the directory permissions unchanged on non-Chrome OS platforms. | 33 // Keep the directory permissions unchanged on non-Chrome OS platforms. |
34 return true; | 34 return true; |
35 } | 35 } |
36 | 36 |
37 // Copies a file |from| to |to|, and ensure the written content is synced to | 37 // Copies a file |from| to |to|, and ensure the written content is synced to |
38 // the disk. This is essentially base::CopyFile followed by fsync(). | 38 // the disk. This is essentially base::CopyFile followed by fsync(). |
39 bool CopyFileAndSync(const base::FilePath& from, const base::FilePath& to) { | 39 bool CopyFileAndSync(const base::FilePath& from, const base::FilePath& to) { |
40 base::File infile(from, base::File::FLAG_OPEN | base::File::FLAG_READ); | 40 base::File infile(from, base::File::FLAG_OPEN | base::File::FLAG_READ); |
41 if (!infile.IsValid()) { | 41 if (!infile.IsValid()) { |
42 return false; | 42 return false; |
43 } | 43 } |
44 | 44 |
45 base::File outfile(to, | 45 base::File outfile(to, |
46 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 46 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
47 if (!outfile.IsValid()) { | 47 if (!outfile.IsValid()) { |
48 return false; | 48 return false; |
49 } | 49 } |
50 | 50 |
51 const int kBufferSize = 32768; | 51 const int kBufferSize = 32768; |
52 std::vector<char> buffer(kBufferSize); | 52 std::vector<char> buffer(kBufferSize); |
53 | 53 |
54 for (;;) { | 54 for (;;) { |
55 int bytes_read = infile.ReadAtCurrentPos(&buffer[0], kBufferSize); | 55 int bytes_read = infile.ReadAtCurrentPos(&buffer[0], kBufferSize); |
56 if (bytes_read < 0) | 56 if (bytes_read < 0) |
57 return false; | 57 return false; |
58 if (bytes_read == 0) | 58 if (bytes_read == 0) |
59 break; | 59 break; |
60 for (int bytes_written = 0; bytes_written < bytes_read; ) { | 60 for (int bytes_written = 0; bytes_written < bytes_read;) { |
61 int bytes_written_partial = outfile.WriteAtCurrentPos( | 61 int bytes_written_partial = outfile.WriteAtCurrentPos( |
62 &buffer[bytes_written], bytes_read - bytes_written); | 62 &buffer[bytes_written], bytes_read - bytes_written); |
63 if (bytes_written_partial < 0) | 63 if (bytes_written_partial < 0) |
64 return false; | 64 return false; |
65 bytes_written += bytes_written_partial; | 65 bytes_written += bytes_written_partial; |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 return outfile.Flush(); | 69 return outfile.Flush(); |
70 } | 70 } |
71 | 71 |
72 } // namespace | 72 } // namespace |
73 | 73 |
74 using base::PlatformFile; | 74 using base::PlatformFile; |
75 | 75 |
76 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { | 76 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { |
77 public: | 77 public: |
78 NativeFileEnumerator(const base::FilePath& root_path, | 78 NativeFileEnumerator(const base::FilePath& root_path, |
79 bool recursive, | 79 bool recursive, |
80 int file_type) | 80 int file_type) |
81 : file_enum_(root_path, recursive, file_type) { | 81 : file_enum_(root_path, recursive, file_type) {} |
82 } | |
83 | 82 |
84 virtual ~NativeFileEnumerator() {} | 83 virtual ~NativeFileEnumerator() {} |
85 | 84 |
86 virtual base::FilePath Next() OVERRIDE; | 85 virtual base::FilePath Next() OVERRIDE; |
87 virtual int64 Size() OVERRIDE; | 86 virtual int64 Size() OVERRIDE; |
88 virtual base::Time LastModifiedTime() OVERRIDE; | 87 virtual base::Time LastModifiedTime() OVERRIDE; |
89 virtual bool IsDirectory() OVERRIDE; | 88 virtual bool IsDirectory() OVERRIDE; |
90 | 89 |
91 private: | 90 private: |
92 base::FileEnumerator file_enum_; | 91 base::FileEnumerator file_enum_; |
(...skipping 13 matching lines...) Expand all Loading... |
106 | 105 |
107 base::Time NativeFileEnumerator::LastModifiedTime() { | 106 base::Time NativeFileEnumerator::LastModifiedTime() { |
108 return file_util_info_.GetLastModifiedTime(); | 107 return file_util_info_.GetLastModifiedTime(); |
109 } | 108 } |
110 | 109 |
111 bool NativeFileEnumerator::IsDirectory() { | 110 bool NativeFileEnumerator::IsDirectory() { |
112 return file_util_info_.IsDirectory(); | 111 return file_util_info_.IsDirectory(); |
113 } | 112 } |
114 | 113 |
115 NativeFileUtil::CopyOrMoveMode NativeFileUtil::CopyOrMoveModeForDestination( | 114 NativeFileUtil::CopyOrMoveMode NativeFileUtil::CopyOrMoveModeForDestination( |
116 const FileSystemURL& dest_url, bool copy) { | 115 const FileSystemURL& dest_url, |
| 116 bool copy) { |
117 if (copy) { | 117 if (copy) { |
118 return dest_url.mount_option().copy_sync_option() == COPY_SYNC_OPTION_SYNC ? | 118 return dest_url.mount_option().copy_sync_option() == COPY_SYNC_OPTION_SYNC |
119 COPY_SYNC : COPY_NOSYNC; | 119 ? COPY_SYNC |
| 120 : COPY_NOSYNC; |
120 } | 121 } |
121 return MOVE; | 122 return MOVE; |
122 } | 123 } |
123 | 124 |
124 base::File NativeFileUtil::CreateOrOpen(const base::FilePath& path, | 125 base::File NativeFileUtil::CreateOrOpen(const base::FilePath& path, |
125 int file_flags) { | 126 int file_flags) { |
126 if (!base::DirectoryExists(path.DirName())) { | 127 if (!base::DirectoryExists(path.DirName())) { |
127 // If its parent does not exist, should return NOT_FOUND error. | 128 // If its parent does not exist, should return NOT_FOUND error. |
128 return base::File(base::File::FILE_ERROR_NOT_FOUND); | 129 return base::File(base::File::FILE_ERROR_NOT_FOUND); |
129 } | 130 } |
130 | 131 |
131 // TODO(rvargas): Check |file_flags| instead. See bug 356358. | 132 // TODO(rvargas): Check |file_flags| instead. See bug 356358. |
132 if (base::DirectoryExists(path)) | 133 if (base::DirectoryExists(path)) |
133 return base::File(base::File::FILE_ERROR_NOT_A_FILE); | 134 return base::File(base::File::FILE_ERROR_NOT_A_FILE); |
134 | 135 |
135 return base::File(path, file_flags); | 136 return base::File(path, file_flags); |
136 } | 137 } |
137 | 138 |
138 base::File::Error NativeFileUtil::EnsureFileExists( | 139 base::File::Error NativeFileUtil::EnsureFileExists(const base::FilePath& path, |
139 const base::FilePath& path, | 140 bool* created) { |
140 bool* created) { | |
141 if (!base::DirectoryExists(path.DirName())) | 141 if (!base::DirectoryExists(path.DirName())) |
142 // If its parent does not exist, should return NOT_FOUND error. | 142 // If its parent does not exist, should return NOT_FOUND error. |
143 return base::File::FILE_ERROR_NOT_FOUND; | 143 return base::File::FILE_ERROR_NOT_FOUND; |
144 | 144 |
145 // Tries to create the |path| exclusively. This should fail | 145 // Tries to create the |path| exclusively. This should fail |
146 // with base::File::FILE_ERROR_EXISTS if the path already exists. | 146 // with base::File::FILE_ERROR_EXISTS if the path already exists. |
147 base::File file(path, base::File::FLAG_CREATE | base::File::FLAG_READ); | 147 base::File file(path, base::File::FLAG_CREATE | base::File::FLAG_READ); |
148 | 148 |
149 if (file.IsValid()) { | 149 if (file.IsValid()) { |
150 if (created) | 150 if (created) |
151 *created = file.created(); | 151 *created = file.created(); |
152 return base::File::FILE_OK; | 152 return base::File::FILE_OK; |
153 } | 153 } |
154 | 154 |
155 base::File::Error error_code = file.error_details(); | 155 base::File::Error error_code = file.error_details(); |
156 if (error_code == base::File::FILE_ERROR_EXISTS) { | 156 if (error_code == base::File::FILE_ERROR_EXISTS) { |
157 // Make sure created_ is false. | 157 // Make sure created_ is false. |
158 if (created) | 158 if (created) |
159 *created = false; | 159 *created = false; |
160 error_code = base::File::FILE_OK; | 160 error_code = base::File::FILE_OK; |
161 } | 161 } |
162 return error_code; | 162 return error_code; |
163 } | 163 } |
164 | 164 |
165 base::File::Error NativeFileUtil::CreateDirectory( | 165 base::File::Error NativeFileUtil::CreateDirectory(const base::FilePath& path, |
166 const base::FilePath& path, | 166 bool exclusive, |
167 bool exclusive, | 167 bool recursive) { |
168 bool recursive) { | |
169 // If parent dir of file doesn't exist. | 168 // If parent dir of file doesn't exist. |
170 if (!recursive && !base::PathExists(path.DirName())) | 169 if (!recursive && !base::PathExists(path.DirName())) |
171 return base::File::FILE_ERROR_NOT_FOUND; | 170 return base::File::FILE_ERROR_NOT_FOUND; |
172 | 171 |
173 bool path_exists = base::PathExists(path); | 172 bool path_exists = base::PathExists(path); |
174 if (exclusive && path_exists) | 173 if (exclusive && path_exists) |
175 return base::File::FILE_ERROR_EXISTS; | 174 return base::File::FILE_ERROR_EXISTS; |
176 | 175 |
177 // If file exists at the path. | 176 // If file exists at the path. |
178 if (path_exists && !base::DirectoryExists(path)) | 177 if (path_exists && !base::DirectoryExists(path)) |
179 return base::File::FILE_ERROR_EXISTS; | 178 return base::File::FILE_ERROR_EXISTS; |
180 | 179 |
181 if (!base::CreateDirectory(path)) | 180 if (!base::CreateDirectory(path)) |
182 return base::File::FILE_ERROR_FAILED; | 181 return base::File::FILE_ERROR_FAILED; |
183 | 182 |
184 if (!SetPlatformSpecificDirectoryPermissions(path)) { | 183 if (!SetPlatformSpecificDirectoryPermissions(path)) { |
185 // Since some file systems don't support permission setting, we do not treat | 184 // Since some file systems don't support permission setting, we do not treat |
186 // an error from the function as the failure of copying. Just log it. | 185 // an error from the function as the failure of copying. Just log it. |
187 LOG(WARNING) << "Setting directory permission failed: " | 186 LOG(WARNING) << "Setting directory permission failed: " |
188 << path.AsUTF8Unsafe(); | 187 << path.AsUTF8Unsafe(); |
189 } | 188 } |
190 | 189 |
191 return base::File::FILE_OK; | 190 return base::File::FILE_OK; |
192 } | 191 } |
193 | 192 |
194 base::File::Error NativeFileUtil::GetFileInfo( | 193 base::File::Error NativeFileUtil::GetFileInfo(const base::FilePath& path, |
195 const base::FilePath& path, | 194 base::File::Info* file_info) { |
196 base::File::Info* file_info) { | |
197 if (!base::PathExists(path)) | 195 if (!base::PathExists(path)) |
198 return base::File::FILE_ERROR_NOT_FOUND; | 196 return base::File::FILE_ERROR_NOT_FOUND; |
199 | 197 |
200 if (!base::GetFileInfo(path, file_info)) | 198 if (!base::GetFileInfo(path, file_info)) |
201 return base::File::FILE_ERROR_FAILED; | 199 return base::File::FILE_ERROR_FAILED; |
202 return base::File::FILE_OK; | 200 return base::File::FILE_OK; |
203 } | 201 } |
204 | 202 |
205 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> | 203 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> |
206 NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path, | 204 NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path, |
207 bool recursive) { | 205 bool recursive) { |
208 return make_scoped_ptr(new NativeFileEnumerator( | 206 return make_scoped_ptr( |
209 root_path, recursive, | 207 new NativeFileEnumerator(root_path, |
210 base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES)) | 208 recursive, |
| 209 base::FileEnumerator::FILES | |
| 210 base::FileEnumerator::DIRECTORIES)) |
211 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 211 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); |
212 } | 212 } |
213 | 213 |
214 base::File::Error NativeFileUtil::Touch( | 214 base::File::Error NativeFileUtil::Touch(const base::FilePath& path, |
215 const base::FilePath& path, | 215 const base::Time& last_access_time, |
216 const base::Time& last_access_time, | 216 const base::Time& last_modified_time) { |
217 const base::Time& last_modified_time) { | |
218 if (!base::TouchFile(path, last_access_time, last_modified_time)) | 217 if (!base::TouchFile(path, last_access_time, last_modified_time)) |
219 return base::File::FILE_ERROR_FAILED; | 218 return base::File::FILE_ERROR_FAILED; |
220 return base::File::FILE_OK; | 219 return base::File::FILE_OK; |
221 } | 220 } |
222 | 221 |
223 base::File::Error NativeFileUtil::Truncate(const base::FilePath& path, | 222 base::File::Error NativeFileUtil::Truncate(const base::FilePath& path, |
224 int64 length) { | 223 int64 length) { |
225 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_WRITE); | 224 base::File file(path, base::File::FLAG_OPEN | base::File::FLAG_WRITE); |
226 if (!file.IsValid()) | 225 if (!file.IsValid()) |
227 return file.error_details(); | 226 return file.error_details(); |
(...skipping 19 matching lines...) Expand all Loading... |
247 CopyOrMoveMode mode) { | 246 CopyOrMoveMode mode) { |
248 base::File::Info info; | 247 base::File::Info info; |
249 base::File::Error error = NativeFileUtil::GetFileInfo(src_path, &info); | 248 base::File::Error error = NativeFileUtil::GetFileInfo(src_path, &info); |
250 if (error != base::File::FILE_OK) | 249 if (error != base::File::FILE_OK) |
251 return error; | 250 return error; |
252 if (info.is_directory) | 251 if (info.is_directory) |
253 return base::File::FILE_ERROR_NOT_A_FILE; | 252 return base::File::FILE_ERROR_NOT_A_FILE; |
254 base::Time last_modified = info.last_modified; | 253 base::Time last_modified = info.last_modified; |
255 | 254 |
256 error = NativeFileUtil::GetFileInfo(dest_path, &info); | 255 error = NativeFileUtil::GetFileInfo(dest_path, &info); |
257 if (error != base::File::FILE_OK && | 256 if (error != base::File::FILE_OK && error != base::File::FILE_ERROR_NOT_FOUND) |
258 error != base::File::FILE_ERROR_NOT_FOUND) | |
259 return error; | 257 return error; |
260 if (info.is_directory) | 258 if (info.is_directory) |
261 return base::File::FILE_ERROR_INVALID_OPERATION; | 259 return base::File::FILE_ERROR_INVALID_OPERATION; |
262 if (error == base::File::FILE_ERROR_NOT_FOUND) { | 260 if (error == base::File::FILE_ERROR_NOT_FOUND) { |
263 error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info); | 261 error = NativeFileUtil::GetFileInfo(dest_path.DirName(), &info); |
264 if (error != base::File::FILE_OK) | 262 if (error != base::File::FILE_OK) |
265 return error; | 263 return error; |
266 if (!info.is_directory) | 264 if (!info.is_directory) |
267 return base::File::FILE_ERROR_NOT_FOUND; | 265 return base::File::FILE_ERROR_NOT_FOUND; |
268 } | 266 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 return base::File::FILE_ERROR_NOT_FOUND; | 303 return base::File::FILE_ERROR_NOT_FOUND; |
306 if (!base::DirectoryExists(path)) | 304 if (!base::DirectoryExists(path)) |
307 return base::File::FILE_ERROR_NOT_A_DIRECTORY; | 305 return base::File::FILE_ERROR_NOT_A_DIRECTORY; |
308 if (!base::IsDirectoryEmpty(path)) | 306 if (!base::IsDirectoryEmpty(path)) |
309 return base::File::FILE_ERROR_NOT_EMPTY; | 307 return base::File::FILE_ERROR_NOT_EMPTY; |
310 if (!base::DeleteFile(path, false)) | 308 if (!base::DeleteFile(path, false)) |
311 return base::File::FILE_ERROR_FAILED; | 309 return base::File::FILE_ERROR_FAILED; |
312 return base::File::FILE_OK; | 310 return base::File::FILE_OK; |
313 } | 311 } |
314 | 312 |
315 } // namespace fileapi | 313 } // namespace storage |
OLD | NEW |