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/fileapi/native_file_util.h" | 5 #include "webkit/fileapi/native_file_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "webkit/fileapi/file_system_operation_context.h" | 9 #include "webkit/fileapi/file_system_operation_context.h" |
10 | 10 |
11 namespace fileapi { | 11 namespace fileapi { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Sets permissions on directory at |dir_path| based on the target platform. | 15 // Sets permissions on directory at |dir_path| based on the target platform. |
16 // Returns true on success, or false otherwise. | 16 // Returns true on success, or false otherwise. |
17 // | 17 // |
18 // TODO(benchan): Find a better place outside webkit to host this function. | 18 // TODO(benchan): Find a better place outside webkit to host this function. |
19 bool SetPlatformSpecificDirectoryPermissions(const FilePath& dir_path) { | 19 bool SetPlatformSpecificDirectoryPermissions(const base::FilePath& dir_path) { |
20 #if defined(OS_CHROMEOS) | 20 #if defined(OS_CHROMEOS) |
21 // System daemons on Chrome OS may run as a user different than the Chrome | 21 // System daemons on Chrome OS may run as a user different than the Chrome |
22 // process but need to access files under the directories created here. | 22 // process but need to access files under the directories created here. |
23 // Because of that, grant the execute permission on the created directory | 23 // Because of that, grant the execute permission on the created directory |
24 // to group and other users. | 24 // to group and other users. |
25 if (HANDLE_EINTR(chmod(dir_path.value().c_str(), | 25 if (HANDLE_EINTR(chmod(dir_path.value().c_str(), |
26 S_IRWXU | S_IXGRP | S_IXOTH)) != 0) { | 26 S_IRWXU | S_IXGRP | S_IXOTH)) != 0) { |
27 return false; | 27 return false; |
28 } | 28 } |
29 #endif | 29 #endif |
30 // Keep the directory permissions unchanged on non-Chrome OS platforms. | 30 // Keep the directory permissions unchanged on non-Chrome OS platforms. |
31 return true; | 31 return true; |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 using base::PlatformFile; | 36 using base::PlatformFile; |
37 using base::PlatformFileError; | 37 using base::PlatformFileError; |
38 | 38 |
39 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { | 39 class NativeFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator { |
40 public: | 40 public: |
41 NativeFileEnumerator(const FilePath& root_path, | 41 NativeFileEnumerator(const base::FilePath& root_path, |
42 bool recursive, | 42 bool recursive, |
43 int file_type) | 43 int file_type) |
44 : file_enum_(root_path, recursive, file_type) { | 44 : file_enum_(root_path, recursive, file_type) { |
45 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
46 memset(&file_util_info_, 0, sizeof(file_util_info_)); | 46 memset(&file_util_info_, 0, sizeof(file_util_info_)); |
47 #endif // defined(OS_WIN) | 47 #endif // defined(OS_WIN) |
48 } | 48 } |
49 | 49 |
50 ~NativeFileEnumerator() {} | 50 ~NativeFileEnumerator() {} |
51 | 51 |
52 virtual FilePath Next() OVERRIDE; | 52 virtual base::FilePath Next() OVERRIDE; |
53 virtual int64 Size() OVERRIDE; | 53 virtual int64 Size() OVERRIDE; |
54 virtual base::Time LastModifiedTime() OVERRIDE; | 54 virtual base::Time LastModifiedTime() OVERRIDE; |
55 virtual bool IsDirectory() OVERRIDE; | 55 virtual bool IsDirectory() OVERRIDE; |
56 | 56 |
57 private: | 57 private: |
58 file_util::FileEnumerator file_enum_; | 58 file_util::FileEnumerator file_enum_; |
59 file_util::FileEnumerator::FindInfo file_util_info_; | 59 file_util::FileEnumerator::FindInfo file_util_info_; |
60 }; | 60 }; |
61 | 61 |
62 FilePath NativeFileEnumerator::Next() { | 62 base::FilePath NativeFileEnumerator::Next() { |
63 FilePath rv = file_enum_.Next(); | 63 base::FilePath rv = file_enum_.Next(); |
64 if (!rv.empty()) | 64 if (!rv.empty()) |
65 file_enum_.GetFindInfo(&file_util_info_); | 65 file_enum_.GetFindInfo(&file_util_info_); |
66 return rv; | 66 return rv; |
67 } | 67 } |
68 | 68 |
69 int64 NativeFileEnumerator::Size() { | 69 int64 NativeFileEnumerator::Size() { |
70 return file_util::FileEnumerator::GetFilesize(file_util_info_); | 70 return file_util::FileEnumerator::GetFilesize(file_util_info_); |
71 } | 71 } |
72 | 72 |
73 base::Time NativeFileEnumerator::LastModifiedTime() { | 73 base::Time NativeFileEnumerator::LastModifiedTime() { |
74 return file_util::FileEnumerator::GetLastModifiedTime(file_util_info_); | 74 return file_util::FileEnumerator::GetLastModifiedTime(file_util_info_); |
75 } | 75 } |
76 | 76 |
77 bool NativeFileEnumerator::IsDirectory() { | 77 bool NativeFileEnumerator::IsDirectory() { |
78 return file_util::FileEnumerator::IsDirectory(file_util_info_); | 78 return file_util::FileEnumerator::IsDirectory(file_util_info_); |
79 } | 79 } |
80 | 80 |
81 PlatformFileError NativeFileUtil::CreateOrOpen( | 81 PlatformFileError NativeFileUtil::CreateOrOpen( |
82 const FilePath& path, int file_flags, | 82 const base::FilePath& path, int file_flags, |
83 PlatformFile* file_handle, bool* created) { | 83 PlatformFile* file_handle, bool* created) { |
84 if (!file_util::DirectoryExists(path.DirName())) { | 84 if (!file_util::DirectoryExists(path.DirName())) { |
85 // If its parent does not exist, should return NOT_FOUND error. | 85 // If its parent does not exist, should return NOT_FOUND error. |
86 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 86 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
87 } | 87 } |
88 PlatformFileError error_code = base::PLATFORM_FILE_OK; | 88 PlatformFileError error_code = base::PLATFORM_FILE_OK; |
89 *file_handle = base::CreatePlatformFile(path, file_flags, | 89 *file_handle = base::CreatePlatformFile(path, file_flags, |
90 created, &error_code); | 90 created, &error_code); |
91 return error_code; | 91 return error_code; |
92 } | 92 } |
93 | 93 |
94 PlatformFileError NativeFileUtil::Close(PlatformFile file_handle) { | 94 PlatformFileError NativeFileUtil::Close(PlatformFile file_handle) { |
95 if (!base::ClosePlatformFile(file_handle)) | 95 if (!base::ClosePlatformFile(file_handle)) |
96 return base::PLATFORM_FILE_ERROR_FAILED; | 96 return base::PLATFORM_FILE_ERROR_FAILED; |
97 return base::PLATFORM_FILE_OK; | 97 return base::PLATFORM_FILE_OK; |
98 } | 98 } |
99 | 99 |
100 PlatformFileError NativeFileUtil::EnsureFileExists( | 100 PlatformFileError NativeFileUtil::EnsureFileExists( |
101 const FilePath& path, | 101 const base::FilePath& path, |
102 bool* created) { | 102 bool* created) { |
103 if (!file_util::DirectoryExists(path.DirName())) | 103 if (!file_util::DirectoryExists(path.DirName())) |
104 // If its parent does not exist, should return NOT_FOUND error. | 104 // If its parent does not exist, should return NOT_FOUND error. |
105 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 105 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
106 PlatformFileError error_code = base::PLATFORM_FILE_OK; | 106 PlatformFileError error_code = base::PLATFORM_FILE_OK; |
107 // Tries to create the |path| exclusively. This should fail | 107 // Tries to create the |path| exclusively. This should fail |
108 // with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists. | 108 // with base::PLATFORM_FILE_ERROR_EXISTS if the path already exists. |
109 PlatformFile handle = base::CreatePlatformFile( | 109 PlatformFile handle = base::CreatePlatformFile( |
110 path, | 110 path, |
111 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | 111 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, |
112 created, &error_code); | 112 created, &error_code); |
113 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { | 113 if (error_code == base::PLATFORM_FILE_ERROR_EXISTS) { |
114 // Make sure created_ is false. | 114 // Make sure created_ is false. |
115 if (created) | 115 if (created) |
116 *created = false; | 116 *created = false; |
117 error_code = base::PLATFORM_FILE_OK; | 117 error_code = base::PLATFORM_FILE_OK; |
118 } | 118 } |
119 if (handle != base::kInvalidPlatformFileValue) | 119 if (handle != base::kInvalidPlatformFileValue) |
120 base::ClosePlatformFile(handle); | 120 base::ClosePlatformFile(handle); |
121 return error_code; | 121 return error_code; |
122 } | 122 } |
123 | 123 |
124 PlatformFileError NativeFileUtil::CreateDirectory( | 124 PlatformFileError NativeFileUtil::CreateDirectory( |
125 const FilePath& path, | 125 const base::FilePath& path, |
126 bool exclusive, | 126 bool exclusive, |
127 bool recursive) { | 127 bool recursive) { |
128 // If parent dir of file doesn't exist. | 128 // If parent dir of file doesn't exist. |
129 if (!recursive && !file_util::PathExists(path.DirName())) | 129 if (!recursive && !file_util::PathExists(path.DirName())) |
130 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 130 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
131 | 131 |
132 bool path_exists = file_util::PathExists(path); | 132 bool path_exists = file_util::PathExists(path); |
133 if (exclusive && path_exists) | 133 if (exclusive && path_exists) |
134 return base::PLATFORM_FILE_ERROR_EXISTS; | 134 return base::PLATFORM_FILE_ERROR_EXISTS; |
135 | 135 |
136 // If file exists at the path. | 136 // If file exists at the path. |
137 if (path_exists && !file_util::DirectoryExists(path)) | 137 if (path_exists && !file_util::DirectoryExists(path)) |
138 return base::PLATFORM_FILE_ERROR_EXISTS; | 138 return base::PLATFORM_FILE_ERROR_EXISTS; |
139 | 139 |
140 if (!file_util::CreateDirectory(path)) | 140 if (!file_util::CreateDirectory(path)) |
141 return base::PLATFORM_FILE_ERROR_FAILED; | 141 return base::PLATFORM_FILE_ERROR_FAILED; |
142 | 142 |
143 if (!SetPlatformSpecificDirectoryPermissions(path)) | 143 if (!SetPlatformSpecificDirectoryPermissions(path)) |
144 return base::PLATFORM_FILE_ERROR_FAILED; | 144 return base::PLATFORM_FILE_ERROR_FAILED; |
145 | 145 |
146 return base::PLATFORM_FILE_OK; | 146 return base::PLATFORM_FILE_OK; |
147 } | 147 } |
148 | 148 |
149 PlatformFileError NativeFileUtil::GetFileInfo( | 149 PlatformFileError NativeFileUtil::GetFileInfo( |
150 const FilePath& path, | 150 const base::FilePath& path, |
151 base::PlatformFileInfo* file_info) { | 151 base::PlatformFileInfo* file_info) { |
152 if (!file_util::PathExists(path)) | 152 if (!file_util::PathExists(path)) |
153 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 153 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
154 if (!file_util::GetFileInfo(path, file_info)) | 154 if (!file_util::GetFileInfo(path, file_info)) |
155 return base::PLATFORM_FILE_ERROR_FAILED; | 155 return base::PLATFORM_FILE_ERROR_FAILED; |
156 return base::PLATFORM_FILE_OK; | 156 return base::PLATFORM_FILE_OK; |
157 } | 157 } |
158 | 158 |
159 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> | 159 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> |
160 NativeFileUtil::CreateFileEnumerator(const FilePath& root_path, | 160 NativeFileUtil::CreateFileEnumerator(const base::FilePath& root_path, |
161 bool recursive) { | 161 bool recursive) { |
162 return make_scoped_ptr(new NativeFileEnumerator( | 162 return make_scoped_ptr(new NativeFileEnumerator( |
163 root_path, recursive, | 163 root_path, recursive, |
164 file_util::FileEnumerator::FILES | | 164 file_util::FileEnumerator::FILES | |
165 file_util::FileEnumerator::DIRECTORIES)) | 165 file_util::FileEnumerator::DIRECTORIES)) |
166 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); | 166 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>(); |
167 } | 167 } |
168 | 168 |
169 PlatformFileError NativeFileUtil::Touch( | 169 PlatformFileError NativeFileUtil::Touch( |
170 const FilePath& path, | 170 const base::FilePath& path, |
171 const base::Time& last_access_time, | 171 const base::Time& last_access_time, |
172 const base::Time& last_modified_time) { | 172 const base::Time& last_modified_time) { |
173 if (!file_util::TouchFile( | 173 if (!file_util::TouchFile( |
174 path, last_access_time, last_modified_time)) | 174 path, last_access_time, last_modified_time)) |
175 return base::PLATFORM_FILE_ERROR_FAILED; | 175 return base::PLATFORM_FILE_ERROR_FAILED; |
176 return base::PLATFORM_FILE_OK; | 176 return base::PLATFORM_FILE_OK; |
177 } | 177 } |
178 | 178 |
179 PlatformFileError NativeFileUtil::Truncate(const FilePath& path, int64 length) { | 179 PlatformFileError NativeFileUtil::Truncate(const base::FilePath& path, int64 len
gth) { |
180 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); | 180 PlatformFileError error_code(base::PLATFORM_FILE_ERROR_FAILED); |
181 PlatformFile file = | 181 PlatformFile file = |
182 base::CreatePlatformFile( | 182 base::CreatePlatformFile( |
183 path, | 183 path, |
184 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 184 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
185 NULL, | 185 NULL, |
186 &error_code); | 186 &error_code); |
187 if (error_code != base::PLATFORM_FILE_OK) { | 187 if (error_code != base::PLATFORM_FILE_OK) { |
188 return error_code; | 188 return error_code; |
189 } | 189 } |
190 DCHECK_NE(base::kInvalidPlatformFileValue, file); | 190 DCHECK_NE(base::kInvalidPlatformFileValue, file); |
191 if (!base::TruncatePlatformFile(file, length)) | 191 if (!base::TruncatePlatformFile(file, length)) |
192 error_code = base::PLATFORM_FILE_ERROR_FAILED; | 192 error_code = base::PLATFORM_FILE_ERROR_FAILED; |
193 base::ClosePlatformFile(file); | 193 base::ClosePlatformFile(file); |
194 return error_code; | 194 return error_code; |
195 } | 195 } |
196 | 196 |
197 bool NativeFileUtil::PathExists(const FilePath& path) { | 197 bool NativeFileUtil::PathExists(const base::FilePath& path) { |
198 return file_util::PathExists(path); | 198 return file_util::PathExists(path); |
199 } | 199 } |
200 | 200 |
201 bool NativeFileUtil::DirectoryExists(const FilePath& path) { | 201 bool NativeFileUtil::DirectoryExists(const base::FilePath& path) { |
202 return file_util::DirectoryExists(path); | 202 return file_util::DirectoryExists(path); |
203 } | 203 } |
204 | 204 |
205 PlatformFileError NativeFileUtil::CopyOrMoveFile( | 205 PlatformFileError NativeFileUtil::CopyOrMoveFile( |
206 const FilePath& src_path, | 206 const base::FilePath& src_path, |
207 const FilePath& dest_path, | 207 const base::FilePath& dest_path, |
208 bool copy) { | 208 bool copy) { |
209 base::PlatformFileInfo info; | 209 base::PlatformFileInfo info; |
210 base::PlatformFileError error = NativeFileUtil::GetFileInfo(src_path, &info); | 210 base::PlatformFileError error = NativeFileUtil::GetFileInfo(src_path, &info); |
211 if (error != base::PLATFORM_FILE_OK) | 211 if (error != base::PLATFORM_FILE_OK) |
212 return error; | 212 return error; |
213 if (info.is_directory) | 213 if (info.is_directory) |
214 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 214 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
215 | 215 |
216 error = NativeFileUtil::GetFileInfo(dest_path, &info); | 216 error = NativeFileUtil::GetFileInfo(dest_path, &info); |
217 if (error != base::PLATFORM_FILE_OK && | 217 if (error != base::PLATFORM_FILE_OK && |
(...skipping 12 matching lines...) Expand all Loading... |
230 if (copy) { | 230 if (copy) { |
231 if (file_util::CopyFile(src_path, dest_path)) | 231 if (file_util::CopyFile(src_path, dest_path)) |
232 return base::PLATFORM_FILE_OK; | 232 return base::PLATFORM_FILE_OK; |
233 } else { | 233 } else { |
234 if (file_util::Move(src_path, dest_path)) | 234 if (file_util::Move(src_path, dest_path)) |
235 return base::PLATFORM_FILE_OK; | 235 return base::PLATFORM_FILE_OK; |
236 } | 236 } |
237 return base::PLATFORM_FILE_ERROR_FAILED; | 237 return base::PLATFORM_FILE_ERROR_FAILED; |
238 } | 238 } |
239 | 239 |
240 PlatformFileError NativeFileUtil::DeleteFile(const FilePath& path) { | 240 PlatformFileError NativeFileUtil::DeleteFile(const base::FilePath& path) { |
241 if (!file_util::PathExists(path)) | 241 if (!file_util::PathExists(path)) |
242 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 242 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
243 if (file_util::DirectoryExists(path)) | 243 if (file_util::DirectoryExists(path)) |
244 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; | 244 return base::PLATFORM_FILE_ERROR_NOT_A_FILE; |
245 if (!file_util::Delete(path, false)) | 245 if (!file_util::Delete(path, false)) |
246 return base::PLATFORM_FILE_ERROR_FAILED; | 246 return base::PLATFORM_FILE_ERROR_FAILED; |
247 return base::PLATFORM_FILE_OK; | 247 return base::PLATFORM_FILE_OK; |
248 } | 248 } |
249 | 249 |
250 PlatformFileError NativeFileUtil::DeleteDirectory(const FilePath& path) { | 250 PlatformFileError NativeFileUtil::DeleteDirectory(const base::FilePath& path) { |
251 if (!file_util::PathExists(path)) | 251 if (!file_util::PathExists(path)) |
252 return base::PLATFORM_FILE_ERROR_NOT_FOUND; | 252 return base::PLATFORM_FILE_ERROR_NOT_FOUND; |
253 if (!file_util::DirectoryExists(path)) | 253 if (!file_util::DirectoryExists(path)) |
254 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; | 254 return base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; |
255 if (!file_util::IsDirectoryEmpty(path)) | 255 if (!file_util::IsDirectoryEmpty(path)) |
256 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; | 256 return base::PLATFORM_FILE_ERROR_NOT_EMPTY; |
257 if (!file_util::Delete(path, false)) | 257 if (!file_util::Delete(path, false)) |
258 return base::PLATFORM_FILE_ERROR_FAILED; | 258 return base::PLATFORM_FILE_ERROR_FAILED; |
259 return base::PLATFORM_FILE_OK; | 259 return base::PLATFORM_FILE_OK; |
260 } | 260 } |
261 | 261 |
262 } // namespace fileapi | 262 } // namespace fileapi |
OLD | NEW |