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_origin_url(), context->dest_type(), |
| 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_origin_url(), context->src_type(), |
| 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_origin_url(), context->dest_type(), |
| 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 FilePath root = GetRootPath(context, origin_url, type); |
| 184 if (root.empty()) |
| 185 return FilePath(); |
| 186 return root.Append(virtual_path); |
| 187 } |
| 188 |
| 189 } // namespace fileapi |
OLD | NEW |