Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(247)

Side by Side Diff: webkit/fileapi/local_file_util.cc

Issue 14895013: Move webkit/fileapi/file_system_mount_point_provider.h to webkit/browser/fileapi (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "webkit/fileapi/local_file_util.h"
6
7 #include "base/files/file_util_proxy.h"
8 #include "googleurl/src/gurl.h"
9 #include "webkit/fileapi/file_system_context.h"
10 #include "webkit/fileapi/file_system_mount_point_provider.h"
11 #include "webkit/fileapi/file_system_operation_context.h"
12 #include "webkit/fileapi/file_system_types.h"
13 #include "webkit/fileapi/file_system_url.h"
14 #include "webkit/fileapi/file_system_util.h"
15 #include "webkit/fileapi/native_file_util.h"
16
17 namespace fileapi {
18
19 using base::PlatformFileError;
20
21 class LocalFileEnumerator : public FileSystemFileUtil::AbstractFileEnumerator {
22 public:
23 LocalFileEnumerator(const base::FilePath& platform_root_path,
24 const base::FilePath& virtual_root_path,
25 int file_type)
26 : file_enum_(platform_root_path, false /* recursive */, file_type),
27 platform_root_path_(platform_root_path),
28 virtual_root_path_(virtual_root_path) {
29 #if defined(OS_WIN)
30 memset(&file_util_info_, 0, sizeof(file_util_info_));
31 #endif // defined(OS_WIN)
32 }
33
34 virtual ~LocalFileEnumerator() {}
35
36 virtual base::FilePath Next() OVERRIDE;
37 virtual int64 Size() OVERRIDE;
38 virtual base::Time LastModifiedTime() OVERRIDE;
39 virtual bool IsDirectory() OVERRIDE;
40
41 private:
42 file_util::FileEnumerator file_enum_;
43 file_util::FileEnumerator::FindInfo file_util_info_;
44 base::FilePath platform_root_path_;
45 base::FilePath virtual_root_path_;
46 };
47
48 base::FilePath LocalFileEnumerator::Next() {
49 base::FilePath next = file_enum_.Next();
50 // Don't return symlinks.
51 while (!next.empty() && file_util::IsLink(next))
52 next = file_enum_.Next();
53 if (next.empty())
54 return next;
55 file_enum_.GetFindInfo(&file_util_info_);
56
57 base::FilePath path;
58 platform_root_path_.AppendRelativePath(next, &path);
59 return virtual_root_path_.Append(path);
60 }
61
62 int64 LocalFileEnumerator::Size() {
63 return file_util::FileEnumerator::GetFilesize(file_util_info_);
64 }
65
66 base::Time LocalFileEnumerator::LastModifiedTime() {
67 return file_util::FileEnumerator::GetLastModifiedTime(file_util_info_);
68 }
69
70 bool LocalFileEnumerator::IsDirectory() {
71 return file_util::FileEnumerator::IsDirectory(file_util_info_);
72 }
73
74 LocalFileUtil::LocalFileUtil() {
75 }
76
77 LocalFileUtil::~LocalFileUtil() {
78 }
79
80 PlatformFileError LocalFileUtil::CreateOrOpen(
81 FileSystemOperationContext* context,
82 const FileSystemURL& url, int file_flags,
83 base::PlatformFile* file_handle, bool* created) {
84 *created = false;
85 base::FilePath file_path;
86 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
87 if (error != base::PLATFORM_FILE_OK)
88 return error;
89 // Disallow opening files in symlinked paths.
90 if (file_util::IsLink(file_path))
91 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
92 return NativeFileUtil::CreateOrOpen(
93 file_path, file_flags, file_handle, created);
94 }
95
96 PlatformFileError LocalFileUtil::Close(FileSystemOperationContext* context,
97 base::PlatformFile file) {
98 return NativeFileUtil::Close(file);
99 }
100
101 PlatformFileError LocalFileUtil::EnsureFileExists(
102 FileSystemOperationContext* context,
103 const FileSystemURL& url,
104 bool* created) {
105 base::FilePath file_path;
106 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
107 if (error != base::PLATFORM_FILE_OK)
108 return error;
109 return NativeFileUtil::EnsureFileExists(file_path, created);
110 }
111
112 PlatformFileError LocalFileUtil::CreateDirectory(
113 FileSystemOperationContext* context,
114 const FileSystemURL& url,
115 bool exclusive,
116 bool recursive) {
117 base::FilePath file_path;
118 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
119 if (error != base::PLATFORM_FILE_OK)
120 return error;
121 return NativeFileUtil::CreateDirectory(file_path, exclusive, recursive);
122 }
123
124 PlatformFileError LocalFileUtil::GetFileInfo(
125 FileSystemOperationContext* context,
126 const FileSystemURL& url,
127 base::PlatformFileInfo* file_info,
128 base::FilePath* platform_file_path) {
129 base::FilePath file_path;
130 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
131 if (error != base::PLATFORM_FILE_OK)
132 return error;
133 // We should not follow symbolic links in sandboxed file system.
134 if (file_util::IsLink(file_path))
135 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
136 error = NativeFileUtil::GetFileInfo(file_path, file_info);
137 if (error == base::PLATFORM_FILE_OK)
138 *platform_file_path = file_path;
139 return error;
140 }
141
142 scoped_ptr<FileSystemFileUtil::AbstractFileEnumerator> LocalFileUtil::
143 CreateFileEnumerator(
144 FileSystemOperationContext* context,
145 const FileSystemURL& root_url) {
146 base::FilePath file_path;
147 if (GetLocalFilePath(context, root_url, &file_path) !=
148 base::PLATFORM_FILE_OK) {
149 return make_scoped_ptr(new EmptyFileEnumerator)
150 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
151 }
152 return make_scoped_ptr(new LocalFileEnumerator(
153 file_path, root_url.path(),
154 file_util::FileEnumerator::FILES |
155 file_util::FileEnumerator::DIRECTORIES))
156 .PassAs<FileSystemFileUtil::AbstractFileEnumerator>();
157 }
158
159 PlatformFileError LocalFileUtil::GetLocalFilePath(
160 FileSystemOperationContext* context,
161 const FileSystemURL& url,
162 base::FilePath* local_file_path) {
163 FileSystemMountPointProvider* provider =
164 context->file_system_context()->GetMountPointProvider(url.type());
165 DCHECK(provider);
166 base::FilePath root = provider->GetFileSystemRootPathOnFileThread(url, false);
167 if (root.empty())
168 return base::PLATFORM_FILE_ERROR_NOT_FOUND;
169 *local_file_path = root.Append(url.path());
170 return base::PLATFORM_FILE_OK;
171 }
172
173 PlatformFileError LocalFileUtil::Touch(
174 FileSystemOperationContext* context,
175 const FileSystemURL& url,
176 const base::Time& last_access_time,
177 const base::Time& last_modified_time) {
178 base::FilePath file_path;
179 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
180 if (error != base::PLATFORM_FILE_OK)
181 return error;
182 return NativeFileUtil::Touch(file_path, last_access_time, last_modified_time);
183 }
184
185 PlatformFileError LocalFileUtil::Truncate(
186 FileSystemOperationContext* context,
187 const FileSystemURL& url,
188 int64 length) {
189 base::FilePath file_path;
190 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
191 if (error != base::PLATFORM_FILE_OK)
192 return error;
193 return NativeFileUtil::Truncate(file_path, length);
194 }
195
196 PlatformFileError LocalFileUtil::CopyOrMoveFile(
197 FileSystemOperationContext* context,
198 const FileSystemURL& src_url,
199 const FileSystemURL& dest_url,
200 bool copy) {
201 base::FilePath src_file_path;
202 PlatformFileError error = GetLocalFilePath(context, src_url, &src_file_path);
203 if (error != base::PLATFORM_FILE_OK)
204 return error;
205
206 base::FilePath dest_file_path;
207 error = GetLocalFilePath(context, dest_url, &dest_file_path);
208 if (error != base::PLATFORM_FILE_OK)
209 return error;
210
211 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, copy);
212 }
213
214 PlatformFileError LocalFileUtil::CopyInForeignFile(
215 FileSystemOperationContext* context,
216 const base::FilePath& src_file_path,
217 const FileSystemURL& dest_url) {
218 if (src_file_path.empty())
219 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
220
221 base::FilePath dest_file_path;
222 PlatformFileError error =
223 GetLocalFilePath(context, dest_url, &dest_file_path);
224 if (error != base::PLATFORM_FILE_OK)
225 return error;
226 return NativeFileUtil::CopyOrMoveFile(src_file_path, dest_file_path, true);
227 }
228
229 PlatformFileError LocalFileUtil::DeleteFile(
230 FileSystemOperationContext* context,
231 const FileSystemURL& url) {
232 base::FilePath file_path;
233 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
234 if (error != base::PLATFORM_FILE_OK)
235 return error;
236 return NativeFileUtil::DeleteFile(file_path);
237 }
238
239 PlatformFileError LocalFileUtil::DeleteDirectory(
240 FileSystemOperationContext* context,
241 const FileSystemURL& url) {
242 base::FilePath file_path;
243 PlatformFileError error = GetLocalFilePath(context, url, &file_path);
244 if (error != base::PLATFORM_FILE_OK)
245 return error;
246 return NativeFileUtil::DeleteDirectory(file_path);
247 }
248
249 webkit_blob::ScopedFile LocalFileUtil::CreateSnapshotFile(
250 FileSystemOperationContext* context,
251 const FileSystemURL& url,
252 base::PlatformFileError* error,
253 base::PlatformFileInfo* file_info,
254 base::FilePath* platform_path) {
255 DCHECK(file_info);
256 // We're just returning the local file information.
257 *error = GetFileInfo(context, url, file_info, platform_path);
258 if (*error == base::PLATFORM_FILE_OK && file_info->is_directory)
259 *error = base::PLATFORM_FILE_ERROR_NOT_A_FILE;
260 return webkit_blob::ScopedFile();
261 }
262
263 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698