| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/quota_file_util.h" | 5 #include "webkit/fileapi/quota_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "webkit/fileapi/file_system_context.h" | 9 #include "webkit/fileapi/file_system_context.h" |
| 10 #include "webkit/fileapi/file_system_operation_context.h" | 10 #include "webkit/fileapi/file_system_operation_context.h" |
| 11 #include "webkit/fileapi/file_system_path_manager.h" | 11 #include "webkit/fileapi/file_system_path_manager.h" |
| 12 #include "webkit/fileapi/file_system_quota_util.h" | 12 #include "webkit/fileapi/file_system_quota_util.h" |
| 13 #include "webkit/fileapi/native_file_util.h" |
| 13 #include "webkit/quota/quota_manager.h" | 14 #include "webkit/quota/quota_manager.h" |
| 14 | 15 |
| 15 using quota::QuotaManagerProxy; | 16 using quota::QuotaManagerProxy; |
| 16 | 17 |
| 17 namespace fileapi { | 18 namespace fileapi { |
| 18 | 19 |
| 19 const int64 QuotaFileUtil::kNoLimit = kint64max; | 20 const int64 QuotaFileUtil::kNoLimit = kint64max; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 operation_context->set_allowed_bytes_growth( | 62 operation_context->set_allowed_bytes_growth( |
| 62 operation_context->allowed_bytes_growth() - growth); | 63 operation_context->allowed_bytes_growth() - growth); |
| 63 if (quota_util) | 64 if (quota_util) |
| 64 quota_util->UpdateOriginUsageOnFileThread( | 65 quota_util->UpdateOriginUsageOnFileThread( |
| 65 quota_manager_proxy, origin_url, type, growth); | 66 quota_manager_proxy, origin_url, type, growth); |
| 66 } | 67 } |
| 67 | 68 |
| 68 } // namespace (anonymous) | 69 } // namespace (anonymous) |
| 69 | 70 |
| 70 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) | 71 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil* underlying_file_util) |
| 71 : underlying_file_util_(underlying_file_util) { | 72 : FileSystemFileUtil(underlying_file_util) { |
| 72 } | 73 } |
| 73 | 74 |
| 74 QuotaFileUtil::~QuotaFileUtil() { | 75 QuotaFileUtil::~QuotaFileUtil() { |
| 75 } | 76 } |
| 76 | 77 |
| 77 // static | 78 // static |
| 78 QuotaFileUtil* QuotaFileUtil::CreateDefault() { | 79 QuotaFileUtil* QuotaFileUtil::CreateDefault() { |
| 79 return new QuotaFileUtil(new FileSystemFileUtil()); | 80 return new QuotaFileUtil(new NativeFileUtil()); |
| 81 } |
| 82 |
| 83 base::PlatformFileError QuotaFileUtil::Truncate( |
| 84 FileSystemOperationContext* fs_context, |
| 85 const FilePath& path, |
| 86 int64 length) { |
| 87 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| 88 |
| 89 int64 growth = 0; |
| 90 base::PlatformFileInfo file_info; |
| 91 if (!file_util::GetFileInfo(path, &file_info)) |
| 92 return base::PLATFORM_FILE_ERROR_FAILED; |
| 93 |
| 94 growth = length - file_info.size; |
| 95 if (allowed_bytes_growth != kNoLimit && |
| 96 growth > 0 && growth > allowed_bytes_growth) |
| 97 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 98 |
| 99 base::PlatformFileError error = underlying_file_util()->Truncate( |
| 100 fs_context, path, length); |
| 101 |
| 102 if (error == base::PLATFORM_FILE_OK) |
| 103 NotifyUpdate(fs_context, |
| 104 fs_context->src_origin_url(), |
| 105 fs_context->src_type(), |
| 106 growth); |
| 107 |
| 108 return error; |
| 80 } | 109 } |
| 81 | 110 |
| 82 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( | 111 base::PlatformFileError QuotaFileUtil::CopyOrMoveFile( |
| 83 FileSystemOperationContext* fs_context, | 112 FileSystemOperationContext* fs_context, |
| 84 const FilePath& src_file_path, | 113 const FilePath& src_file_path, |
| 85 const FilePath& dest_file_path, | 114 const FilePath& dest_file_path, |
| 86 bool copy) { | 115 bool copy) { |
| 87 DCHECK(fs_context); | 116 DCHECK(fs_context); |
| 88 | 117 |
| 89 int64 growth = 0; | 118 int64 growth = 0; |
| 90 | 119 |
| 91 // It assumes copy/move operations are always in the same fs currently. | 120 // It assumes copy/move operations are always in the same fs currently. |
| 92 // TODO(dmikurube): Do quota check if moving between different fs. | 121 // TODO(dmikurube): Do quota check if moving between different fs. |
| 93 if (copy) { | 122 if (copy) { |
| 94 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | 123 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); |
| 95 // The third argument (growth) is not used for now. | 124 // The third argument (growth) is not used for now. |
| 96 if (!CanCopy(src_file_path, dest_file_path, allowed_bytes_growth, &growth)) | 125 if (!CanCopy(src_file_path, dest_file_path, allowed_bytes_growth, &growth)) |
| 97 return base::PLATFORM_FILE_ERROR_NO_SPACE; | 126 return base::PLATFORM_FILE_ERROR_NO_SPACE; |
| 98 } else { | 127 } else { |
| 99 base::PlatformFileInfo dest_file_info; | 128 base::PlatformFileInfo dest_file_info; |
| 100 if (!file_util::GetFileInfo(dest_file_path, &dest_file_info)) | 129 if (!file_util::GetFileInfo(dest_file_path, &dest_file_info)) |
| 101 dest_file_info.size = 0; | 130 dest_file_info.size = 0; |
| 102 growth = -dest_file_info.size; | 131 growth = -dest_file_info.size; |
| 103 } | 132 } |
| 104 | 133 |
| 105 base::PlatformFileError error = underlying_file_util_->CopyOrMoveFile( | 134 base::PlatformFileError error = underlying_file_util()->CopyOrMoveFile( |
| 106 fs_context, src_file_path, dest_file_path, copy); | 135 fs_context, src_file_path, dest_file_path, copy); |
| 107 | 136 |
| 108 if (error == base::PLATFORM_FILE_OK) { | 137 if (error == base::PLATFORM_FILE_OK) { |
| 109 // TODO(kinuko): For cross-filesystem move case, call this with -growth | 138 // TODO(kinuko): For cross-filesystem move case, call this with -growth |
| 110 // for source and growth for dest. | 139 // for source and growth for dest. |
| 111 NotifyUpdate(fs_context, | 140 NotifyUpdate(fs_context, |
| 112 fs_context->dest_origin_url(), | 141 fs_context->dest_origin_url(), |
| 113 fs_context->dest_type(), | 142 fs_context->dest_type(), |
| 114 growth); | 143 growth); |
| 115 } | 144 } |
| 116 | 145 |
| 117 return error; | 146 return error; |
| 118 } | 147 } |
| 119 | 148 |
| 120 base::PlatformFileError QuotaFileUtil::DeleteFile( | 149 base::PlatformFileError QuotaFileUtil::DeleteFile( |
| 121 FileSystemOperationContext* fs_context, | 150 FileSystemOperationContext* fs_context, |
| 122 const FilePath& file_path) { | 151 const FilePath& file_path) { |
| 123 DCHECK(fs_context); | 152 DCHECK(fs_context); |
| 124 | 153 |
| 125 int64 growth = 0; | 154 int64 growth = 0; |
| 126 base::PlatformFileInfo file_info; | 155 base::PlatformFileInfo file_info; |
| 127 if (!file_util::GetFileInfo(file_path, &file_info)) | 156 if (!file_util::GetFileInfo(file_path, &file_info)) |
| 128 file_info.size = 0; | 157 file_info.size = 0; |
| 129 growth = -file_info.size; | 158 growth = -file_info.size; |
| 130 | 159 |
| 131 base::PlatformFileError error = underlying_file_util_->DeleteFile( | 160 base::PlatformFileError error = underlying_file_util()->DeleteFile( |
| 132 fs_context, file_path); | 161 fs_context, file_path); |
| 133 | 162 |
| 134 if (error == base::PLATFORM_FILE_OK) | 163 if (error == base::PLATFORM_FILE_OK) |
| 135 NotifyUpdate(fs_context, | 164 NotifyUpdate(fs_context, |
| 136 fs_context->src_origin_url(), | 165 fs_context->src_origin_url(), |
| 137 fs_context->src_type(), | 166 fs_context->src_type(), |
| 138 growth); | 167 growth); |
| 139 | 168 |
| 140 return error; | 169 return error; |
| 141 } | 170 } |
| 142 | 171 |
| 143 base::PlatformFileError QuotaFileUtil::Truncate( | |
| 144 FileSystemOperationContext* fs_context, | |
| 145 const FilePath& path, | |
| 146 int64 length) { | |
| 147 int64 allowed_bytes_growth = fs_context->allowed_bytes_growth(); | |
| 148 | |
| 149 int64 growth = 0; | |
| 150 base::PlatformFileInfo file_info; | |
| 151 if (!file_util::GetFileInfo(path, &file_info)) | |
| 152 return base::PLATFORM_FILE_ERROR_FAILED; | |
| 153 | |
| 154 growth = length - file_info.size; | |
| 155 if (allowed_bytes_growth != kNoLimit && | |
| 156 growth > 0 && growth > allowed_bytes_growth) | |
| 157 return base::PLATFORM_FILE_ERROR_NO_SPACE; | |
| 158 | |
| 159 base::PlatformFileError error = underlying_file_util_->Truncate( | |
| 160 fs_context, path, length); | |
| 161 | |
| 162 if (error == base::PLATFORM_FILE_OK) | |
| 163 NotifyUpdate(fs_context, | |
| 164 fs_context->src_origin_url(), | |
| 165 fs_context->src_type(), | |
| 166 growth); | |
| 167 | |
| 168 return error; | |
| 169 } | |
| 170 | |
| 171 } // namespace fileapi | 172 } // namespace fileapi |
| OLD | NEW |