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.
5 #include "webkit/fileapi/quota_file_util.h"
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "webkit/fileapi/file_system_context.h"
10 #include "webkit/fileapi/file_system_operation_context.h"
11 #include "webkit/fileapi/file_system_quota_util.h"
12 #include "webkit/fileapi/native_file_util.h"
13 #include "webkit/quota/quota_manager.h"
15 using quota::QuotaManagerProxy
;
19 const int64
QuotaFileUtil::kNoLimit
= kint64max
;
23 // Checks if copying in the same filesystem can be performed.
24 // This method is not called for moving within a single filesystem.
26 const FilePath
& src_file_path
,
27 const FilePath
& dest_file_path
,
28 int64 allowed_bytes_growth
,
30 base::PlatformFileInfo src_file_info
;
31 if (!file_util::GetFileInfo(src_file_path
, &src_file_info
)) {
32 // Falling through to the actual copy/move operation.
35 base::PlatformFileInfo dest_file_info
;
36 if (!file_util::GetFileInfo(dest_file_path
, &dest_file_info
))
37 dest_file_info
.size
= 0;
38 int64 growth_local
= src_file_info
.size
- dest_file_info
.size
;
39 if (allowed_bytes_growth
!= QuotaFileUtil::kNoLimit
&&
40 growth_local
> 0 && growth_local
> allowed_bytes_growth
)
43 *growth
= growth_local
;
48 void NotifyUpdate(FileSystemOperationContext
* operation_context
,
49 const GURL
& origin_url
,
52 DCHECK(operation_context
);
53 DCHECK(operation_context
->file_system_context());
54 DCHECK(type
!= kFileSystemTypeUnknown
);
56 FileSystemQuotaUtil
* quota_util
=
57 operation_context
->file_system_context()->GetQuotaUtil(type
);
58 QuotaManagerProxy
* quota_manager_proxy
=
59 operation_context
->file_system_context()->quota_manager_proxy();
61 operation_context
->set_allowed_bytes_growth(
62 operation_context
->allowed_bytes_growth() - growth
);
64 quota_util
->UpdateOriginUsageOnFileThread(
65 quota_manager_proxy
, origin_url
, type
, growth
);
68 } // namespace (anonymous)
70 QuotaFileUtil::QuotaFileUtil(FileSystemFileUtil
* underlying_file_util
)
71 : FileSystemFileUtil(underlying_file_util
) {
74 QuotaFileUtil::~QuotaFileUtil() {
78 QuotaFileUtil
* QuotaFileUtil::CreateDefault() {
79 return new QuotaFileUtil(new NativeFileUtil());
82 base::PlatformFileError
QuotaFileUtil::Truncate(
83 FileSystemOperationContext
* fs_context
,
86 int64 allowed_bytes_growth
= fs_context
->allowed_bytes_growth();
89 base::PlatformFileInfo file_info
;
90 if (!file_util::GetFileInfo(path
, &file_info
))
91 return base::PLATFORM_FILE_ERROR_FAILED
;
93 growth
= length
- file_info
.size
;
94 if (allowed_bytes_growth
!= kNoLimit
&&
95 growth
> 0 && growth
> allowed_bytes_growth
)
96 return base::PLATFORM_FILE_ERROR_NO_SPACE
;
98 base::PlatformFileError error
= underlying_file_util()->Truncate(
99 fs_context
, path
, length
);
101 if (error
== base::PLATFORM_FILE_OK
)
102 NotifyUpdate(fs_context
,
103 fs_context
->src_origin_url(),
104 fs_context
->src_type(),
110 base::PlatformFileError
QuotaFileUtil::CopyOrMoveFile(
111 FileSystemOperationContext
* fs_context
,
112 const FilePath
& src_file_path
,
113 const FilePath
& dest_file_path
,
119 // It assumes copy/move operations are always in the same fs currently.
120 // TODO(dmikurube): Do quota check if moving between different fs.
122 int64 allowed_bytes_growth
= fs_context
->allowed_bytes_growth();
123 // The third argument (growth) is not used for now.
124 if (!CanCopy(src_file_path
, dest_file_path
, allowed_bytes_growth
, &growth
))
125 return base::PLATFORM_FILE_ERROR_NO_SPACE
;
127 base::PlatformFileInfo dest_file_info
;
128 if (!file_util::GetFileInfo(dest_file_path
, &dest_file_info
))
129 dest_file_info
.size
= 0;
130 growth
= -dest_file_info
.size
;
133 base::PlatformFileError error
= underlying_file_util()->CopyOrMoveFile(
134 fs_context
, src_file_path
, dest_file_path
, copy
);
136 if (error
== base::PLATFORM_FILE_OK
) {
137 // TODO(kinuko): For cross-filesystem move case, call this with -growth
138 // for source and growth for dest.
139 NotifyUpdate(fs_context
,
140 fs_context
->dest_origin_url(),
141 fs_context
->dest_type(),
148 base::PlatformFileError
QuotaFileUtil::DeleteFile(
149 FileSystemOperationContext
* fs_context
,
150 const FilePath
& file_path
) {
154 base::PlatformFileInfo file_info
;
155 if (!file_util::GetFileInfo(file_path
, &file_info
))
157 growth
= -file_info
.size
;
159 base::PlatformFileError error
= underlying_file_util()->DeleteFile(
160 fs_context
, file_path
);
162 if (error
== base::PLATFORM_FILE_OK
)
163 NotifyUpdate(fs_context
,
164 fs_context
->src_origin_url(),
165 fs_context
->src_type(),
171 } // namespace fileapi