15bebb993Sopenharmony_ci/*
25bebb993Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
35bebb993Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45bebb993Sopenharmony_ci * you may not use this file except in compliance with the License.
55bebb993Sopenharmony_ci * You may obtain a copy of the License at
65bebb993Sopenharmony_ci *
75bebb993Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
85bebb993Sopenharmony_ci *
95bebb993Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105bebb993Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115bebb993Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bebb993Sopenharmony_ci * See the License for the specific language governing permissions and
135bebb993Sopenharmony_ci * limitations under the License.
145bebb993Sopenharmony_ci */
155bebb993Sopenharmony_ci
165bebb993Sopenharmony_ci#include "file_impl.h"
175bebb993Sopenharmony_ci#include "file_fs_impl.h"
185bebb993Sopenharmony_ci#include "file_ffi.h"
195bebb993Sopenharmony_ci#include "macro.h"
205bebb993Sopenharmony_ci#include "uni_error.h"
215bebb993Sopenharmony_ci
225bebb993Sopenharmony_ciusing namespace OHOS::FFI;
235bebb993Sopenharmony_ciusing namespace OHOS::CJSystemapi;
245bebb993Sopenharmony_cinamespace OHOS {
255bebb993Sopenharmony_cinamespace CJSystemapi {
265bebb993Sopenharmony_cinamespace FileFs {
275bebb993Sopenharmony_ci
285bebb993Sopenharmony_ciextern "C" {
295bebb993Sopenharmony_ciRetDataI64 FfiOHOSFileFsOpen(const char* path, int64_t mode)
305bebb993Sopenharmony_ci{
315bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEOpen");
325bebb993Sopenharmony_ci    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
335bebb993Sopenharmony_ci    auto [state, nativeStream] = FileEntity::Open(path, mode);
345bebb993Sopenharmony_ci    if (state != SUCCESS_CODE) {
355bebb993Sopenharmony_ci        LOGE("FS_TEST::FfiOHOSFILEOpen error");
365bebb993Sopenharmony_ci        ret.code = GetErrorCode(state);
375bebb993Sopenharmony_ci        ret.data = 0;
385bebb993Sopenharmony_ci        return ret;
395bebb993Sopenharmony_ci    }
405bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEOpen success");
415bebb993Sopenharmony_ci    ret.code = state;
425bebb993Sopenharmony_ci    ret.data = nativeStream->GetID();
435bebb993Sopenharmony_ci    return ret;
445bebb993Sopenharmony_ci}
455bebb993Sopenharmony_ci
465bebb993Sopenharmony_ciint FfiOHOSFileFsCloseByFd(int32_t file)
475bebb993Sopenharmony_ci{
485bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFileFsClose");
495bebb993Sopenharmony_ci    int err = FileFsImpl::Close(file);
505bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFileFsClose success");
515bebb993Sopenharmony_ci    return err;
525bebb993Sopenharmony_ci}
535bebb993Sopenharmony_ci
545bebb993Sopenharmony_ciint FfiOHOSFileFsClose(int64_t file)
555bebb993Sopenharmony_ci{
565bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFileFsClose");
575bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(file);
585bebb993Sopenharmony_ci    if (!instance) {
595bebb993Sopenharmony_ci        LOGE("Stream instance not exist %{public}" PRId64, file);
605bebb993Sopenharmony_ci        return ERR_INVALID_INSTANCE_CODE;
615bebb993Sopenharmony_ci    }
625bebb993Sopenharmony_ci    int err = FileFsImpl::Close(instance);
635bebb993Sopenharmony_ci    FFIData::Release(file);
645bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFileFsClose success");
655bebb993Sopenharmony_ci    return err;
665bebb993Sopenharmony_ci}
675bebb993Sopenharmony_ci
685bebb993Sopenharmony_ciRetDataI64 FfiOHOSFileFsDup(int32_t fd)
695bebb993Sopenharmony_ci{
705bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFileFsDup");
715bebb993Sopenharmony_ci    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
725bebb993Sopenharmony_ci    auto [state, nativeFile] = FileEntity::Dup(fd);
735bebb993Sopenharmony_ci    if (state != SUCCESS_CODE) {
745bebb993Sopenharmony_ci        LOGE("FS_TEST::FfiOHOSFileFsDup error");
755bebb993Sopenharmony_ci        ret.code = GetErrorCode(state);
765bebb993Sopenharmony_ci        return ret;
775bebb993Sopenharmony_ci    }
785bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFileFsDup success");
795bebb993Sopenharmony_ci    ret.code = state;
805bebb993Sopenharmony_ci    ret.data = nativeFile->GetID();
815bebb993Sopenharmony_ci    return ret;
825bebb993Sopenharmony_ci}
835bebb993Sopenharmony_ci
845bebb993Sopenharmony_ciint FfiOHOSFILEFsGetFD(int64_t id)
855bebb993Sopenharmony_ci{
865bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEGetFD");
875bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(id);
885bebb993Sopenharmony_ci    if (!instance) {
895bebb993Sopenharmony_ci        LOGE("FileEntity instance not exist %{public}" PRId64, id);
905bebb993Sopenharmony_ci        return ERR_INVALID_INSTANCE_CODE;
915bebb993Sopenharmony_ci    }
925bebb993Sopenharmony_ci    return instance->GetFD(id);
935bebb993Sopenharmony_ci}
945bebb993Sopenharmony_ci
955bebb993Sopenharmony_ciconst char* FfiOHOSFILEFsGetPath(int64_t id)
965bebb993Sopenharmony_ci{
975bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEGetPath");
985bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(id);
995bebb993Sopenharmony_ci    if (!instance) {
1005bebb993Sopenharmony_ci        LOGE("FileEntity instance not exist %{public}" PRId64, id);
1015bebb993Sopenharmony_ci        return "error";
1025bebb993Sopenharmony_ci    }
1035bebb993Sopenharmony_ci    return instance->GetPath(id);
1045bebb993Sopenharmony_ci}
1055bebb993Sopenharmony_ci
1065bebb993Sopenharmony_ciconst char* FfiOHOSFILEFsGetName(int64_t id)
1075bebb993Sopenharmony_ci{
1085bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEGetName");
1095bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(id);
1105bebb993Sopenharmony_ci    if (!instance) {
1115bebb993Sopenharmony_ci        LOGE("FileEntity instance not exist %{public}" PRId64, id);
1125bebb993Sopenharmony_ci        return "error";
1135bebb993Sopenharmony_ci    }
1145bebb993Sopenharmony_ci    return instance->GetName(id);
1155bebb993Sopenharmony_ci}
1165bebb993Sopenharmony_ci
1175bebb993Sopenharmony_ciRetCode FfiOHOSFILEFsTryLock(int64_t id, bool exclusive)
1185bebb993Sopenharmony_ci{
1195bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEFsTryLock");
1205bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(id);
1215bebb993Sopenharmony_ci    if (!instance) {
1225bebb993Sopenharmony_ci        LOGE("FileEntity instance not exist %{public}" PRId64, id);
1235bebb993Sopenharmony_ci        return ERR_INVALID_INSTANCE_CODE;
1245bebb993Sopenharmony_ci    }
1255bebb993Sopenharmony_ci    return instance->TryLock(id, exclusive);
1265bebb993Sopenharmony_ci}
1275bebb993Sopenharmony_ci
1285bebb993Sopenharmony_ciRetCode FfiOHOSFILEFsUnLock(int64_t id)
1295bebb993Sopenharmony_ci{
1305bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEFsUnLock");
1315bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(id);
1325bebb993Sopenharmony_ci    if (!instance) {
1335bebb993Sopenharmony_ci        LOGE("FileEntity instance not exist %{public}" PRId64, id);
1345bebb993Sopenharmony_ci        return ERR_INVALID_INSTANCE_CODE;
1355bebb993Sopenharmony_ci    }
1365bebb993Sopenharmony_ci    return instance->UnLock(id);
1375bebb993Sopenharmony_ci}
1385bebb993Sopenharmony_ci
1395bebb993Sopenharmony_ciRetDataCString FfiOHOSFILEFsGetParent(int64_t id)
1405bebb993Sopenharmony_ci{
1415bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEFsGetParent");
1425bebb993Sopenharmony_ci    auto instance = FFIData::GetData<FileEntity>(id);
1435bebb993Sopenharmony_ci    RetDataCString ret = { .code = EINVAL, .data = nullptr };
1445bebb993Sopenharmony_ci    if (!instance) {
1455bebb993Sopenharmony_ci        LOGE("FS_TEST::FfiOHOSFILEFsGetParent instance not exist %{public}" PRId64, id);
1465bebb993Sopenharmony_ci        return ret;
1475bebb993Sopenharmony_ci    }
1485bebb993Sopenharmony_ci    ret = instance->GetParent();
1495bebb993Sopenharmony_ci    if (ret.code != SUCCESS_CODE) {
1505bebb993Sopenharmony_ci        ret.code = GetErrorCode(ret.code);
1515bebb993Sopenharmony_ci    }
1525bebb993Sopenharmony_ci    LOGI("FS_TEST::FfiOHOSFILEFsGetParent end");
1535bebb993Sopenharmony_ci    return ret;
1545bebb993Sopenharmony_ci}
1555bebb993Sopenharmony_ci}
1565bebb993Sopenharmony_ci} // namespace FileFs
1575bebb993Sopenharmony_ci} // namespace CJSystemapi
1585bebb993Sopenharmony_ci} // namespace OHOS