Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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_system_file_util.h" | |
| 6 | |
| 7 #include "base/file_util_proxy.h" | |
| 8 #include "googleurl/src/gurl.h" | |
| 9 #include "webkit/fileapi/file_system_context.h" | |
| 10 #include "webkit/fileapi/file_system_operation_context.h" | |
| 11 #include "webkit/fileapi/file_system_path_manager.h" | |
| 12 #include "webkit/fileapi/file_system_types.h" | |
| 13 #include "webkit/fileapi/file_system_util.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 FilePath GetRootPath( | |
| 18 fileapi::FileSystemOperationContext* context, | |
| 19 const GURL& origin_url, | |
| 20 fileapi::FileSystemType type) { | |
| 21 return context->file_system_context()->path_manager()-> | |
| 22 GetFileSystemRootPathOnFileThread(origin_url, type, false); | |
| 23 } | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 namespace fileapi { | |
| 28 | |
| 29 LocalFileSystemFileUtil* LocalFileSystemFileUtil::GetInstance() { | |
| 30 return Singleton<LocalFileSystemFileUtil>::get(); | |
| 31 } | |
| 32 | |
| 33 PlatformFileError LocalFileSystemFileUtil::CreateOrOpen( | |
| 34 FileSystemOperationContext* context, | |
| 35 const FilePath& file_path, int file_flags, | |
| 36 PlatformFile* file_handle, bool* created) { | |
| 37 FilePath local_path = | |
| 38 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 39 file_path); | |
| 40 if (local_path.empty()) | |
| 41 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 42 return FileSystemFileUtil::GetInstance()->CreateOrOpen( | |
| 43 context, local_path, file_flags, file_handle, created); | |
| 44 } | |
| 45 | |
| 46 PlatformFileError LocalFileSystemFileUtil::EnsureFileExists( | |
| 47 FileSystemOperationContext* context, | |
| 48 const FilePath& file_path, | |
| 49 bool* created) { | |
| 50 FilePath local_path = | |
| 51 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 52 file_path); | |
| 53 if (local_path.empty()) | |
| 54 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 55 return FileSystemFileUtil::GetInstance()->EnsureFileExists( | |
| 56 context, local_path, created); | |
| 57 } | |
| 58 | |
| 59 PlatformFileError LocalFileSystemFileUtil::GetFileInfo( | |
| 60 FileSystemOperationContext* context, | |
| 61 const FilePath& file_path, | |
| 62 base::PlatformFileInfo* file_info) { | |
| 63 FilePath local_path = | |
| 64 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 65 file_path); | |
| 66 if (local_path.empty()) | |
| 67 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 68 return FileSystemFileUtil::GetInstance()->GetFileInfo( | |
| 69 context, local_path, file_info); | |
| 70 } | |
| 71 | |
| 72 PlatformFileError LocalFileSystemFileUtil::ReadDirectory( | |
| 73 FileSystemOperationContext* context, | |
| 74 const FilePath& file_path, | |
| 75 std::vector<base::FileUtilProxy::Entry>* entries) { | |
| 76 // TODO(kkanetkar): Implement directory read in multiple chunks. | |
| 77 FilePath local_path = | |
| 78 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 79 file_path); | |
| 80 if (local_path.empty()) | |
| 81 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 82 return FileSystemFileUtil::GetInstance()->ReadDirectory( | |
| 83 context, local_path, entries); | |
| 84 } | |
| 85 | |
| 86 PlatformFileError LocalFileSystemFileUtil::CreateDirectory( | |
| 87 FileSystemOperationContext* context, | |
| 88 const FilePath& file_path, | |
| 89 bool exclusive, | |
| 90 bool recursive) { | |
| 91 FilePath local_path = | |
| 92 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 93 file_path); | |
| 94 if (local_path.empty()) | |
| 95 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 96 return FileSystemFileUtil::GetInstance()->CreateDirectory( | |
| 97 context, local_path, exclusive, recursive); | |
| 98 } | |
| 99 | |
| 100 PlatformFileError LocalFileSystemFileUtil::Copy( | |
| 101 FileSystemOperationContext* context, | |
| 102 const FilePath& src_file_path, | |
| 103 const FilePath& dest_file_path) { | |
| 104 // TODO(ericu): If they share a root URL, this could be optimized. | |
| 105 FilePath local_src_path = | |
| 106 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 107 src_file_path); | |
| 108 if (local_src_path.empty()) | |
| 109 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 110 FilePath local_dest_path = | |
| 111 GetLocalPath(context, context->dest_root_url(), context->dest_type(), | |
|
kinuko
2011/03/14 11:03:57
Sometimes we call xxx_origin_url() but also call x
ericu
2011/03/15 02:43:11
You're right--this should be *_origin_url not *_ro
| |
| 112 dest_file_path); | |
| 113 if (local_dest_path.empty()) | |
| 114 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 115 return FileSystemFileUtil::GetInstance()->Copy( | |
| 116 context, local_src_path, local_dest_path); | |
| 117 } | |
| 118 | |
| 119 PlatformFileError LocalFileSystemFileUtil::Move( | |
| 120 FileSystemOperationContext* context, | |
| 121 const FilePath& src_file_path, | |
| 122 const FilePath& dest_file_path) { | |
| 123 // TODO(ericu): If they share a root URL, this could be optimized. | |
| 124 FilePath local_src_path = | |
| 125 GetLocalPath(context, context->src_root_url(), context->src_type(), | |
|
kinuko
2011/03/14 11:03:57
src_origin_url()?
ericu
2011/03/15 02:43:11
Done.
| |
| 126 src_file_path); | |
| 127 if (local_src_path.empty()) | |
| 128 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 129 FilePath local_dest_path = | |
| 130 GetLocalPath(context, context->dest_root_url(), context->dest_type(), | |
|
kinuko
2011/03/14 11:03:57
dest_origin_url()?
ericu
2011/03/15 02:43:11
Done.
| |
| 131 dest_file_path); | |
| 132 if (local_dest_path.empty()) | |
| 133 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 134 return FileSystemFileUtil::GetInstance()->Move( | |
| 135 context, local_src_path, local_dest_path); | |
| 136 } | |
| 137 | |
| 138 PlatformFileError LocalFileSystemFileUtil::Delete( | |
| 139 FileSystemOperationContext* context, | |
| 140 const FilePath& file_path, | |
| 141 bool recursive) { | |
| 142 FilePath local_path = | |
| 143 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 144 file_path); | |
| 145 if (local_path.empty()) | |
| 146 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 147 return FileSystemFileUtil::GetInstance()->Delete( | |
| 148 context, local_path, recursive); | |
| 149 } | |
| 150 | |
| 151 PlatformFileError LocalFileSystemFileUtil::Touch( | |
| 152 FileSystemOperationContext* context, | |
| 153 const FilePath& file_path, | |
| 154 const base::Time& last_access_time, | |
| 155 const base::Time& last_modified_time) { | |
| 156 FilePath local_path = | |
| 157 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 158 file_path); | |
| 159 if (local_path.empty()) | |
| 160 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 161 return FileSystemFileUtil::GetInstance()->Touch( | |
| 162 context, local_path, last_access_time, last_modified_time); | |
| 163 } | |
| 164 | |
| 165 PlatformFileError LocalFileSystemFileUtil::Truncate( | |
| 166 FileSystemOperationContext* context, | |
| 167 const FilePath& file_path, | |
| 168 int64 length) { | |
| 169 FilePath local_path = | |
| 170 GetLocalPath(context, context->src_origin_url(), context->src_type(), | |
| 171 file_path); | |
| 172 if (local_path.empty()) | |
| 173 return base::PLATFORM_FILE_ERROR_INVALID_OPERATION; | |
| 174 return FileSystemFileUtil::GetInstance()->Truncate( | |
| 175 context, local_path, length); | |
| 176 } | |
| 177 | |
| 178 FilePath LocalFileSystemFileUtil::GetLocalPath( | |
| 179 FileSystemOperationContext* context, | |
| 180 const GURL& origin_url, | |
| 181 FileSystemType type, | |
| 182 const FilePath& virtual_path) { | |
| 183 // TODO(ericu): This is the way we allow the tests to use real paths. Is it | |
| 184 // a security problem to do this? | |
| 185 if (!context->file_system_context()) { | |
| 186 return virtual_path; | |
| 187 } | |
|
kinuko
2011/03/14 11:03:57
I think we want to minimize the code that performs
ericu
2011/03/15 02:43:11
I like that idea, but I've altered it slightly; th
kinuko
2011/03/16 19:33:38
sgtm.
| |
| 188 FilePath root = GetRootPath(context, origin_url, type); | |
| 189 if (root.empty()) | |
| 190 return root; | |
|
kinuko
2011/03/14 11:03:57
If an intention is returning an empty path, return
ericu
2011/03/15 02:43:11
Done.
| |
| 191 return root.Append(virtual_path); | |
| 192 } | |
| 193 | |
| 194 } // namespace fileapi | |
| OLD | NEW |