1/*
2 * Copyright (c) 2024 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 "file_impl.h"
17#include "file_fs_impl.h"
18#include "file_ffi.h"
19#include "macro.h"
20#include "uni_error.h"
21
22using namespace OHOS::FFI;
23using namespace OHOS::CJSystemapi;
24namespace OHOS {
25namespace CJSystemapi {
26namespace FileFs {
27
28extern "C" {
29RetDataI64 FfiOHOSFileFsOpen(const char* path, int64_t mode)
30{
31    LOGI("FS_TEST::FfiOHOSFILEOpen");
32    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
33    auto [state, nativeStream] = FileEntity::Open(path, mode);
34    if (state != SUCCESS_CODE) {
35        LOGE("FS_TEST::FfiOHOSFILEOpen error");
36        ret.code = GetErrorCode(state);
37        ret.data = 0;
38        return ret;
39    }
40    LOGI("FS_TEST::FfiOHOSFILEOpen success");
41    ret.code = state;
42    ret.data = nativeStream->GetID();
43    return ret;
44}
45
46int FfiOHOSFileFsCloseByFd(int32_t file)
47{
48    LOGI("FS_TEST::FfiOHOSFileFsClose");
49    int err = FileFsImpl::Close(file);
50    LOGI("FS_TEST::FfiOHOSFileFsClose success");
51    return err;
52}
53
54int FfiOHOSFileFsClose(int64_t file)
55{
56    LOGI("FS_TEST::FfiOHOSFileFsClose");
57    auto instance = FFIData::GetData<FileEntity>(file);
58    if (!instance) {
59        LOGE("Stream instance not exist %{public}" PRId64, file);
60        return ERR_INVALID_INSTANCE_CODE;
61    }
62    int err = FileFsImpl::Close(instance);
63    FFIData::Release(file);
64    LOGI("FS_TEST::FfiOHOSFileFsClose success");
65    return err;
66}
67
68RetDataI64 FfiOHOSFileFsDup(int32_t fd)
69{
70    LOGI("FS_TEST::FfiOHOSFileFsDup");
71    RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
72    auto [state, nativeFile] = FileEntity::Dup(fd);
73    if (state != SUCCESS_CODE) {
74        LOGE("FS_TEST::FfiOHOSFileFsDup error");
75        ret.code = GetErrorCode(state);
76        return ret;
77    }
78    LOGI("FS_TEST::FfiOHOSFileFsDup success");
79    ret.code = state;
80    ret.data = nativeFile->GetID();
81    return ret;
82}
83
84int FfiOHOSFILEFsGetFD(int64_t id)
85{
86    LOGI("FS_TEST::FfiOHOSFILEGetFD");
87    auto instance = FFIData::GetData<FileEntity>(id);
88    if (!instance) {
89        LOGE("FileEntity instance not exist %{public}" PRId64, id);
90        return ERR_INVALID_INSTANCE_CODE;
91    }
92    return instance->GetFD(id);
93}
94
95const char* FfiOHOSFILEFsGetPath(int64_t id)
96{
97    LOGI("FS_TEST::FfiOHOSFILEGetPath");
98    auto instance = FFIData::GetData<FileEntity>(id);
99    if (!instance) {
100        LOGE("FileEntity instance not exist %{public}" PRId64, id);
101        return "error";
102    }
103    return instance->GetPath(id);
104}
105
106const char* FfiOHOSFILEFsGetName(int64_t id)
107{
108    LOGI("FS_TEST::FfiOHOSFILEGetName");
109    auto instance = FFIData::GetData<FileEntity>(id);
110    if (!instance) {
111        LOGE("FileEntity instance not exist %{public}" PRId64, id);
112        return "error";
113    }
114    return instance->GetName(id);
115}
116
117RetCode FfiOHOSFILEFsTryLock(int64_t id, bool exclusive)
118{
119    LOGI("FS_TEST::FfiOHOSFILEFsTryLock");
120    auto instance = FFIData::GetData<FileEntity>(id);
121    if (!instance) {
122        LOGE("FileEntity instance not exist %{public}" PRId64, id);
123        return ERR_INVALID_INSTANCE_CODE;
124    }
125    return instance->TryLock(id, exclusive);
126}
127
128RetCode FfiOHOSFILEFsUnLock(int64_t id)
129{
130    LOGI("FS_TEST::FfiOHOSFILEFsUnLock");
131    auto instance = FFIData::GetData<FileEntity>(id);
132    if (!instance) {
133        LOGE("FileEntity instance not exist %{public}" PRId64, id);
134        return ERR_INVALID_INSTANCE_CODE;
135    }
136    return instance->UnLock(id);
137}
138
139RetDataCString FfiOHOSFILEFsGetParent(int64_t id)
140{
141    LOGI("FS_TEST::FfiOHOSFILEFsGetParent");
142    auto instance = FFIData::GetData<FileEntity>(id);
143    RetDataCString ret = { .code = EINVAL, .data = nullptr };
144    if (!instance) {
145        LOGE("FS_TEST::FfiOHOSFILEFsGetParent instance not exist %{public}" PRId64, id);
146        return ret;
147    }
148    ret = instance->GetParent();
149    if (ret.code != SUCCESS_CODE) {
150        ret.code = GetErrorCode(ret.code);
151    }
152    LOGI("FS_TEST::FfiOHOSFILEFsGetParent end");
153    return ret;
154}
155}
156} // namespace FileFs
157} // namespace CJSystemapi
158} // namespace OHOS