1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "statvfs_ani.h"
17
18 #include "error_handler.h"
19 #include "filemgmt_libhilog.h"
20 #include "statvfs_core.h"
21 #include "type_converter.h"
22
23 namespace OHOS {
24 namespace FileManagement {
25 namespace ModuleFileIO {
26 namespace ANI {
27
28 using namespace std;
29 using namespace OHOS::FileManagement::ModuleFileIO;
30 using namespace OHOS::FileManagement::ModuleStatvfs;
31
GetFreeSizeSync(ani_env * env,ani_class clazz,ani_string path)32 ani_double StatvfsAni::GetFreeSizeSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path)
33 {
34 auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path);
35 if (!succPath) {
36 HILOGE("Invalid path");
37 ErrorHandler::Throw(env, EINVAL);
38 return 0;
39 }
40
41 auto ret = StatvfsCore::DoGetFreeSize(srcPath);
42 if (!ret.IsSuccess()) {
43 HILOGE("Get freesize failed");
44 const auto &err = ret.GetError();
45 ErrorHandler::Throw(env, err);
46 return 0;
47 }
48
49 return static_cast<double>(ret.GetData().value());
50 }
51
GetTotalSizeSync(ani_env * env,ani_class clazz,ani_string path)52 ani_double StatvfsAni::GetTotalSizeSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani_string path)
53 {
54 auto [succPath, srcPath] = TypeConverter::ToUTF8String(env, path);
55 if (!succPath) {
56 HILOGE("Invalid path");
57 ErrorHandler::Throw(env, EINVAL);
58 return 0;
59 }
60
61 auto ret = StatvfsCore::DoGetTotalSize(srcPath);
62 if (!ret.IsSuccess()) {
63 HILOGE("Get totalsize failed");
64 const auto &err = ret.GetError();
65 ErrorHandler::Throw(env, err);
66 return 0;
67 }
68
69 return static_cast<double>(ret.GetData().value());
70 }
71
72 } // namespace ANI
73 } // namespace ModuleFileIO
74 } // namespace FileManagement
75 } // namespace OHOS