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_fs_ffi.h"
17 #include "copy_file.h"
18 #include "fdatasync.h"
19 #include "fsync.h"
20 #include "list_file.h"
21 #include "lseek.h"
22 #include "macro.h"
23 #include "mkdtemp.h"
24 #include "move_file.h"
25 #include "symlink.h"
26 #include "uni_error.h"
27
28 using namespace OHOS::FFI;
29
30 namespace OHOS {
31 namespace CJSystemapi {
32 namespace FileFs {
33
34 extern "C" {
FfiOHOSFileFsStatByID(int32_t file)35 RetDataI64 FfiOHOSFileFsStatByID(int32_t file)
36 {
37 LOGI("FS_TEST::FfiOHOSFileFsStatByID");
38 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
39 auto [state, nativeStat] = FileFsImpl::Stat(file);
40 if (nativeStat == nullptr) {
41 LOGI("FS_TEST::FfiOHOSFileFsStatByID error");
42 ret.code = state;
43 ret.data = 0;
44 return ret;
45 }
46 LOGI("FS_TEST::FfiOHOSFileFsStatByID success");
47 ret.code = state;
48 ret.data = nativeStat->GetID();
49 return ret;
50 }
51
FfiOHOSFileFsStatByString(const char * file)52 RetDataI64 FfiOHOSFileFsStatByString(const char* file)
53 {
54 LOGI("FS_TEST::FfiOHOSFileFsStatByString");
55 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
56 auto [state, nativeStat] = FileFsImpl::Stat(file);
57 if (nativeStat == nullptr) {
58 LOGI("FS_TEST::FfiOHOSFileFsStatByString error");
59 ret.code = state;
60 ret.data = 0;
61 return ret;
62 }
63 LOGI("FS_TEST::FfiOHOSFileFsStatByString success");
64 ret.code = state;
65 ret.data = nativeStat->GetID();
66 return ret;
67 }
68
FfiOHOSFileFsCreateStream(const char * path,const char * mode)69 RetDataI64 FfiOHOSFileFsCreateStream(const char* path, const char* mode)
70 {
71 LOGI("FS_TEST::FfiOHOSFileFsCreateStream");
72 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
73 auto [state, nativeStream] = FileFsImpl::CreateStream(path, mode);
74 ret.code = state;
75 if (nativeStream == nullptr) {
76 LOGI("FS_TEST::FfiOHOSFileFsCreateStream error");
77 ret.data = 0;
78 return ret;
79 }
80 LOGI("FS_TEST::FfiOHOSFileFsCreateStream success");
81 ret.data = nativeStream->GetID();
82 return ret;
83 }
84
FfiOHOSFileFsFdopenStream(int32_t fd,const char * mode)85 RetDataI64 FfiOHOSFileFsFdopenStream(int32_t fd, const char* mode)
86 {
87 LOGI("FS_TEST::FfiOHOSFileFsFdopenStream");
88 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
89 auto [state, nativeStream] = FileFsImpl::FdopenStream(fd, mode);
90 ret.code = state;
91 if (nativeStream == nullptr) {
92 LOGI("FS_TEST::FfiOHOSFileFsFdopenStream error");
93 ret.data = 0;
94 return ret;
95 }
96 LOGI("FS_TEST::FfiOHOSFileFsFdopenStream success");
97 ret.data = nativeStream->GetID();
98 return ret;
99 }
100
FfiOHOSFileFsLstat(const char * path)101 RetDataI64 FfiOHOSFileFsLstat(const char* path)
102 {
103 LOGI("FS_TEST::FfiOHOSFileFsLstat");
104 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
105 auto [state, nativeStat] = FileFsImpl::Lstat(path);
106 ret.code = state;
107 if (nativeStat == nullptr) {
108 LOGI("FS_TEST::FfiOHOSFileFsLstat error");
109 ret.data = 0;
110 return ret;
111 }
112 LOGI("FS_TEST::FfiOHOSFileFsLstat success");
113 ret.data = nativeStat->GetID();
114 return ret;
115 }
116
FfiOHOSFileFsRead(int32_t fd,char * buffer,int64_t bufLen,size_t length,int64_t offset)117 RetDataI64 FfiOHOSFileFsRead(int32_t fd, char* buffer, int64_t bufLen, size_t length, int64_t offset)
118 {
119 LOGI("FS_TEST::FfiOHOSFileFsRead");
120 auto ret = FileFsImpl::Read(fd, buffer, bufLen, length, offset);
121 if (ret.code != SUCCESS_CODE) {
122 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
123 return ret;
124 }
125 LOGI("FS_TEST::FfiOHOSFileFsRead success");
126 return ret;
127 }
128
FfiOHOSFileFsReadCur(int32_t fd,char * buffer,int64_t bufLen,size_t length)129 RetDataI64 FfiOHOSFileFsReadCur(int32_t fd, char* buffer, int64_t bufLen, size_t length)
130 {
131 LOGI("FS_TEST::FfiOHOSFileFsReadCur");
132 auto ret = FileFsImpl::ReadCur(fd, buffer, bufLen, length);
133 if (ret.code != SUCCESS_CODE) {
134 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
135 return ret;
136 }
137 LOGI("FS_TEST::FfiOHOSFileFsReadCur success");
138 return ret;
139 }
140
FfiOHOSFileFsWrite(int32_t fd,char * buffer,size_t length,int64_t offset,const char * encode)141 RetDataI64 FfiOHOSFileFsWrite(int32_t fd, char* buffer, size_t length, int64_t offset, const char* encode)
142 {
143 LOGI("FS_TEST::FfiOHOSFileFsWrite");
144 auto ret = FileFsImpl::Write(fd, buffer, length, offset, encode);
145 if (ret.code != SUCCESS_CODE) {
146 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
147 return ret;
148 }
149 LOGI("FS_TEST::FfiOHOSFileFsWrite success");
150 return ret;
151 }
152
FfiOHOSFileFsWriteCur(int32_t fd,char * buffer,size_t length,const char * encode)153 RetDataI64 FfiOHOSFileFsWriteCur(int32_t fd, char* buffer, size_t length, const char* encode)
154 {
155 LOGI("FS_TEST::FfiOHOSFileFsWriteCur");
156 auto ret = FileFsImpl::WriteCur(fd, buffer, length, encode);
157 if (ret.code != SUCCESS_CODE) {
158 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
159 return ret;
160 }
161 LOGI("FS_TEST::FfiOHOSFileFsWriteCur success");
162 return ret;
163 }
164
FfiOHOSFileFsCreateRandomAccessFileByString(const char * file,int64_t mode)165 RetDataI64 FfiOHOSFileFsCreateRandomAccessFileByString(const char* file, int64_t mode)
166 {
167 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFileByString");
168 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
169 auto [state, nativeCreateRandomAccessFile] = FileFsImpl::CreateRandomAccessFileSync(file, mode);
170 ret.code = state;
171 if (nativeCreateRandomAccessFile == nullptr) {
172 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFileByString error");
173 ret.data = 0;
174 return ret;
175 }
176 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFileByString success");
177 ret.data = nativeCreateRandomAccessFile->GetID();
178 return ret;
179 }
180
FfiOHOSFileFsCreateRandomAccessFileByID(int64_t file,int64_t mode)181 RetDataI64 FfiOHOSFileFsCreateRandomAccessFileByID(int64_t file, int64_t mode)
182 {
183 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFileByID");
184 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
185 auto instance = FFIData::GetData<FileEntity>(file);
186 auto [state, nativeCreateRandomAccessFile] = FileFsImpl::CreateRandomAccessFileSync(instance, mode);
187 ret.code = state;
188 if (nativeCreateRandomAccessFile == nullptr) {
189 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFileByID error");
190 ret.data = 0;
191 return ret;
192 }
193 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFileByID success");
194 ret.data = nativeCreateRandomAccessFile->GetID();
195 return ret;
196 }
197
FfiOHOSRandomAccessFileGetFd(int64_t id)198 int32_t FfiOHOSRandomAccessFileGetFd(int64_t id)
199 {
200 LOGI("FS_TEST::FfiOHOSRandomAccessFileGetFd");
201 auto instance = FFIData::GetData<RandomAccessFileImpl>(id);
202 if (!instance) {
203 LOGE("FfiOHOSRandomAccessFileGetFd instance not exist %{public}" PRId64, id);
204 return ERR_INVALID_INSTANCE_CODE;
205 }
206 LOGI("FS_TEST::FfiOHOSRandomAccessFileGetFd success");
207 return instance->GetFd();
208 }
209
FfiOHOSRandomAccessFileGetFPointer(int64_t id)210 int64_t FfiOHOSRandomAccessFileGetFPointer(int64_t id)
211 {
212 LOGI("FS_TEST::FfiOHOSRandomAccessFileGetFPointer");
213 auto instance = FFIData::GetData<RandomAccessFileImpl>(id);
214 if (!instance) {
215 LOGE("FfiOHOSRandomAccessFileGetFPointer instance not exist %{public}" PRId64, id);
216 return ERR_INVALID_INSTANCE_CODE;
217 }
218 LOGI("FS_TEST::FfiOHOSRandomAccessFileGetFPointer success");
219 return instance->GetFPointer();
220 }
221
FfiOHOSRandomAccessFileSetFilePointerSync(int64_t id,int64_t fd)222 void FfiOHOSRandomAccessFileSetFilePointerSync(int64_t id, int64_t fd)
223 {
224 LOGI("FS_TEST::FfiOHOSRandomAccessFileSetFilePointerSync");
225 auto instance = FFIData::GetData<RandomAccessFileImpl>(id);
226 if (!instance) {
227 LOGE("FfiOHOSRandomAccessFileSetFilePointerSync instance not exist %{public}" PRId64, id);
228 return;
229 }
230 LOGI("FS_TEST::FfiOHOSRandomAccessFileSetFilePointerSync success");
231 instance->SetFilePointerSync(fd);
232 return;
233 }
234
FfiOHOSRandomAccessFileClose(int64_t id)235 void FfiOHOSRandomAccessFileClose(int64_t id)
236 {
237 LOGI("FS_TEST::FfiOHOSRandomAccessFileClose");
238 auto instance = FFIData::GetData<RandomAccessFileImpl>(id);
239 if (!instance) {
240 LOGE("FfiOHOSRandomAccessFileClose instance not exist %{public}" PRId64, id);
241 return;
242 }
243 LOGI("FS_TEST::FfiOHOSRandomAccessFileClose success");
244 return instance->CloseSync();
245 }
246
FfiOHOSRandomAccessFileWrite(int64_t id,char * buf,size_t len,int64_t offset)247 RetDataI64 FfiOHOSRandomAccessFileWrite(int64_t id, char* buf, size_t len, int64_t offset)
248 {
249 LOGI("FS_TEST::FfiOHOSRandomAccessFileWriteByString");
250 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
251 auto instance = FFIData::GetData<RandomAccessFileImpl>(id);
252 if (!instance) {
253 LOGE("FfiOHOSRandomAccessFileWriteByString instance not exist %{public}" PRId64, id);
254 ret.code = ERR_INVALID_INSTANCE_CODE;
255 ret.data = 0;
256 return ret;
257 }
258 LOGI("FS_TEST::FfiOHOSRandomAccessFileWriteByString success");
259 auto [code, data] = instance->WriteSync(buf, len, offset);
260 ret.code = code;
261 ret.data = data;
262 return ret;
263 }
264
FfiOHOSRandomAccessFileRead(int64_t id,char * buf,size_t len,int64_t offset)265 RetDataI64 FfiOHOSRandomAccessFileRead(int64_t id, char* buf, size_t len, int64_t offset)
266 {
267 LOGI("FS_TEST::FfiOHOSRandomAccessFileReadByArray");
268 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
269 auto instance = FFIData::GetData<RandomAccessFileImpl>(id);
270 if (!instance) {
271 LOGE("FfiOHOSRandomAccessFileReadByArray instance not exist %{public}" PRId64, id);
272 ret.code = ERR_INVALID_INSTANCE_CODE;
273 ret.data = 0;
274 return ret;
275 }
276 LOGI("FS_TEST::FfiOHOSRandomAccessFileReadByArray success");
277 auto [code, data] = instance->ReadSync(buf, len, offset);
278 ret.code = code;
279 ret.data = data;
280 return ret;
281 }
282
FfiOHOSFileFsMkdir(const char * path,bool recursion,bool isTwoArgs)283 int32_t FfiOHOSFileFsMkdir(const char* path, bool recursion, bool isTwoArgs)
284 {
285 LOGI("FS_TEST::FfiOHOSFileFsMkdir");
286 auto code = FileFsImpl::Mkdir(path, recursion, isTwoArgs);
287 LOGI("FS_TEST::FfiOHOSFileFsMkdir success");
288 return code;
289 }
290
FfiOHOSFileFsRmdir(const char * path)291 int32_t FfiOHOSFileFsRmdir(const char* path)
292 {
293 LOGI("FS_TEST::FfiOHOSFileFsRmdir");
294 auto code = FileFsImpl::Rmdir(path);
295 LOGI("FS_TEST::FfiOHOSFileFsRmdir success");
296 return code;
297 }
298
FfiOHOSFileFsMoveDir(const char * src,const char * dest,int32_t mode)299 RetDataCArrConflictFiles FfiOHOSFileFsMoveDir(const char* src, const char* dest, int32_t mode)
300 {
301 LOGI("FS_TEST::FfiOHOSFileFsMovedir");
302 auto ret = FileFsImpl::MoveDir(src, dest, mode);
303 LOGI("FS_TEST::FfiOHOSFileFsMovedir success");
304 return ret;
305 }
306
FfiOHOSFileFsRename(const char * oldFile,const char * newFile)307 int32_t FfiOHOSFileFsRename(const char* oldFile, const char* newFile)
308 {
309 LOGI("FS_TEST::FfiOHOSFileFsRename");
310 auto code = FileFsImpl::Rename(oldFile, newFile);
311 LOGI("FS_TEST::FfiOHOSFileFsRename success");
312 return code;
313 }
314
FfiOHOSFileFsUnlink(const char * path)315 int32_t FfiOHOSFileFsUnlink(const char* path)
316 {
317 LOGI("FS_TEST::FfiOHOSFileFsUnlink");
318 auto code = FileFsImpl::Unlink(path);
319 LOGI("FS_TEST::FfiOHOSFileFsUnlink success");
320 return code;
321 }
322
FfiOHOSFileFsCopyDir(const char * src,const char * dest,int mode)323 RetDataCArrConflictFiles FfiOHOSFileFsCopyDir(const char* src, const char* dest, int mode)
324 {
325 LOGI("FS_TEST::FfiOHOSFileFsCopyDir start");
326 auto ret = CopyDirImpl::CopyDir(src, dest, mode);
327 if (ret.code != SUCCESS_CODE) {
328 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
329 }
330 LOGI("FS_TEST::FfiOHOSFileFsCopyDir success");
331 return ret;
332 }
333
FfiOHOSFileFsCopyFile(const char * src,const char * dest,int mode)334 int FfiOHOSFileFsCopyFile(const char* src, const char* dest, int mode)
335 {
336 LOGI("FS_TEST::FfiOHOSFileFsCopyFile start");
337 auto ret = CopyFileImpl::CopyFile(src, dest, mode);
338 if (ret != SUCCESS_CODE) {
339 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
340 }
341 LOGI("FS_TEST::FfiOHOSFileFsCopyFile success");
342 return ret;
343 }
344
FfiOHOSFileFsCopyFileSI(const char * src,int32_t dest,int mode)345 int FfiOHOSFileFsCopyFileSI(const char* src, int32_t dest, int mode)
346 {
347 LOGI("FS_TEST::FfiOHOSFileFsCopyFileSI start");
348 auto ret = CopyFileImpl::CopyFile(src, dest, mode);
349 if (ret != SUCCESS_CODE) {
350 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
351 }
352 LOGI("FS_TEST::FfiOHOSFileFsCopyFileSI success");
353 return ret;
354 }
355
FfiOHOSFileFsCopyFileIS(int32_t src,const char * dest,int mode)356 int FfiOHOSFileFsCopyFileIS(int32_t src, const char* dest, int mode)
357 {
358 LOGI("FS_TEST::FfiOHOSFileFsCopyFileIS start");
359 auto ret = CopyFileImpl::CopyFile(src, dest, mode);
360 if (ret != SUCCESS_CODE) {
361 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
362 }
363 LOGI("FS_TEST::FfiOHOSFileFsCopyFileIS success");
364 return ret;
365 }
366
FfiOHOSFileFsCopyFileII(int32_t src,int32_t dest,int mode)367 int FfiOHOSFileFsCopyFileII(int32_t src, int32_t dest, int mode)
368 {
369 LOGI("FS_TEST::FfiOHOSFileFsCopyFileII start");
370 auto ret = CopyFileImpl::CopyFile(src, dest, mode);
371 if (ret != SUCCESS_CODE) {
372 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
373 }
374 LOGI("FS_TEST::FfiOHOSFileFsCopyFileII success");
375 return ret;
376 }
377
FfiOHOSFileFsMoveFile(const char * src,const char * dest,int mode)378 int FfiOHOSFileFsMoveFile(const char* src, const char* dest, int mode)
379 {
380 LOGI("FS_TEST::FfiOHOSFileFsMoveFile start");
381 auto ret = MoveFileImpl::MoveFile(src, dest, mode);
382 if (ret != SUCCESS_CODE) {
383 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
384 }
385 LOGI("FS_TEST::FfiOHOSFileFsMoveFile success");
386 return ret;
387 }
388
FfiOHOSFileFsMkdtemp(const char * prefix)389 RetDataCString FfiOHOSFileFsMkdtemp(const char* prefix)
390 {
391 LOGI("FS_TEST::FfiOHOSFileFsMkdtemp start");
392 auto ret = MkdtempImpl::Mkdtemp(prefix);
393 if (ret.code != SUCCESS_CODE) {
394 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
395 }
396 LOGI("FS_TEST::FfiOHOSFileFsMkdtemp end");
397 return ret;
398 }
399
FfiOHOSFileFsAccess(const char * path)400 RetDataBool FfiOHOSFileFsAccess(const char* path)
401 {
402 LOGI("FS_TEST::FfiOHOSFileFsAccess");
403 RetDataBool ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
404 auto [status, accessStatus] = FileFsImpl::Access(path);
405 ret.code = status;
406 if (status != SUCCESS_CODE) {
407 LOGI("FS_TEST::FfiOHOSFileFsAccess error");
408 ret.data = false;
409 return ret;
410 }
411 LOGI("FS_TEST::FfiOHOSFileFsAccess success");
412 ret.data = accessStatus;
413 return ret;
414 }
415
FfiOHOSFileFsTruncateByString(const char * file,int64_t len)416 int32_t FfiOHOSFileFsTruncateByString(const char* file, int64_t len)
417 {
418 LOGI("FS_TEST::FfiOHOSFileFsTruncateByString");
419 auto code = FileFsImpl::Truncate(file, len);
420 LOGI("FS_TEST::FfiOHOSFileFsTruncateByString success");
421 return code;
422 }
423
FfiOHOSFileFsTruncateByFd(int32_t file,int64_t len)424 int32_t FfiOHOSFileFsTruncateByFd(int32_t file, int64_t len)
425 {
426 LOGI("FS_TEST::FfiOHOSFileFsTruncateByfd");
427 auto code = FileFsImpl::Truncate(file, len);
428 LOGI("FS_TEST::FfiOHOSFileFsTruncateByfd success");
429 return code;
430 }
431
FfiOHOSFileFsReadLines(char * path,char * encoding)432 RetDataI64 FfiOHOSFileFsReadLines(char* path, char* encoding)
433 {
434 LOGI("FS_TEST::FfiOHOSFileFsReadLine");
435 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
436 auto [state, nativeReaderIterator] = FileFsImpl::ReadLines(path, encoding);
437 ret.code = state;
438 if (nativeReaderIterator == nullptr) {
439 LOGI("FS_TEST::FfiOHOSFileFsReadLine error");
440 ret.data = 0;
441 return ret;
442 }
443 LOGI("FS_TEST::FfiOHOSFileFsReadLine success");
444 ret.data = nativeReaderIterator->GetID();
445 return ret;
446 }
447
FfiOHOSFileFsReaderIteratorNext(int64_t id)448 RetReaderIteratorResult FfiOHOSFileFsReaderIteratorNext(int64_t id)
449 {
450 LOGI("FS_TEST::FfiOHOSFileFsReaderIteratorNext");
451 RetReaderIteratorResult ret = { .code = ERR_INVALID_INSTANCE_CODE, .done = true, .data = nullptr };
452 auto instance = FFIData::GetData<ReadIteratorImpl>(id);
453 if (!instance) {
454 LOGE("[ImageSource] instance not exist %{public}" PRId64, id);
455 return ret;
456 }
457 auto [code, done, data] = instance->Next();
458 LOGI("FS_TEST::FfiOHOSFileFsReaderIteratorNext success");
459 ret.code = code;
460 ret.done = done;
461 ret.data = data;
462 return ret;
463 }
464
FfiOHOSFileListFile(const char * path,CListFileOptions options)465 RetDataCArrStringN FfiOHOSFileListFile(const char* path, CListFileOptions options)
466 {
467 LOGI("FS_TEST::FfiOHOSFileListFile start");
468 auto ret = ListFileImpl::ListFile(path, options);
469 if (ret.code != SUCCESS_CODE) {
470 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
471 }
472 LOGI("FS_TEST::FfiOHOSFileListFile end");
473 return ret;
474 }
475
476
FfiOHOSFileFsLseek(int32_t fd,int64_t offset,int whence)477 RetDataI64 FfiOHOSFileFsLseek(int32_t fd, int64_t offset, int whence)
478 {
479 LOGI("FS_TEST::FfiOHOSFileFsLseek start");
480 auto ret = LseekImpl::Lseek(fd, offset, whence);
481 if (ret.code != SUCCESS_CODE) {
482 ret.code = OHOS::CJSystemapi::FileFs::GetErrorCode(ret.code);
483 }
484 LOGI("FS_TEST::FfiOHOSFileFsLseek success");
485 return ret;
486 }
487
FfiOHOSFileFsFdatasync(int32_t fd)488 int FfiOHOSFileFsFdatasync(int32_t fd)
489 {
490 LOGI("FS_TEST::FfiOHOSFileFsFdatasync start");
491 auto ret = FdatasyncImpl::Fdatasync(fd);
492 if (ret != SUCCESS_CODE) {
493 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
494 }
495 LOGI("FS_TEST::FfiOHOSFileFsFdatasync success");
496 return ret;
497 }
498
FfiOHOSFileFsFsync(int32_t fd)499 int FfiOHOSFileFsFsync(int32_t fd)
500 {
501 LOGI("FS_TEST::FfiOHOSFileFsFsync start");
502 auto ret = FsyncImpl::Fsync(fd);
503 if (ret != SUCCESS_CODE) {
504 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
505 }
506 LOGI("FS_TEST::FfiOHOSFileFsFsync success");
507 return ret;
508 }
509
FfiOHOSFileFsSymlink(const char * target,const char * srcPath)510 int FfiOHOSFileFsSymlink(const char* target, const char* srcPath)
511 {
512 LOGI("FS_TEST::FfiOHOSFileFsSymlink start");
513 auto ret = SymlinkImpl::Symlink(target, srcPath);
514 if (ret != SUCCESS_CODE) {
515 return OHOS::CJSystemapi::FileFs::GetErrorCode(ret);
516 }
517 LOGI("FS_TEST::FfiOHOSFileFsSymlink success");
518 return ret;
519 }
520
FfiOHOSFileFsReadText(char * path,int64_t offset,bool hasLen,int64_t len,char * encoding)521 RetDataCString FfiOHOSFileFsReadText(char* path, int64_t offset, bool hasLen, int64_t len, char* encoding)
522 {
523 LOGI("FS_TEST::FfiOHOSFileFsReadText start");
524 RetDataCString ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = nullptr };
525 auto [state, cString] = FileFsImpl::ReadText(path, offset, hasLen, len, encoding);
526 LOGI("FS_TEST::FfiOHOSFileFsReadText success");
527 ret.code = state;
528 ret.data = cString;
529 return ret;
530 }
531
FfiOHOSFileFsUtimes(char * path,double mtime)532 int32_t FfiOHOSFileFsUtimes(char* path, double mtime)
533 {
534 LOGI("FS_TEST::FfiOHOSFileFsUtimes start");
535 auto code = FileFsImpl::Utimes(path, mtime);
536 LOGI("FS_TEST::FfiOHOSFileFsUtimes success");
537 return code;
538 }
539
FfiOHOSFileFsCreateWatcher(char * path,uint32_t events,void (* callback)(CWatchEvent))540 RetDataI64 FfiOHOSFileFsCreateWatcher(char* path, uint32_t events, void (*callback)(CWatchEvent))
541 {
542 LOGI("FS_TEST::FfiOHOSFileFsCreateWatcher start");
543 RetDataI64 ret = { .code = ERR_INVALID_INSTANCE_CODE, .data = 0 };
544 auto [state, nativeWatcher] = FileFsImpl::CreateWatcher(path, events, callback);
545 ret.code = state;
546 if (nativeWatcher == nullptr) {
547 LOGI("FS_TEST::FfiOHOSFileFsCreateRandomAccessFile error");
548 ret.data = 0;
549 return ret;
550 }
551 LOGI("FS_TEST::FfiOHOSFileFsCreateWatcher success");
552 ret.data = nativeWatcher->GetID();
553 return ret;
554 }
555
FfiOHOSFileFsWatcherStart(int64_t id)556 int32_t FfiOHOSFileFsWatcherStart(int64_t id)
557 {
558 LOGI("FS_TEST::FfiOHOSFileFsWatcherStart");
559 auto instance = FFIData::GetData<WatcherImpl>(id);
560 if (!instance) {
561 LOGE("FfiOHOSFileFsWatcherStart instance not exist %{public}" PRId64, id);
562 return ERR_INVALID_INSTANCE_CODE;
563 }
564 LOGI("FS_TEST::FfiOHOSFileFsWatcherStart success");
565 int32_t startCode = instance->StartNotify();
566 if (startCode != SUCCESS_CODE) {
567 return startCode;
568 }
569 instance->GetNotifyEvent();
570 return SUCCESS_CODE;
571 }
572
FfiOHOSFileFsWatcherStop(int64_t id)573 int32_t FfiOHOSFileFsWatcherStop(int64_t id)
574 {
575 LOGI("FS_TEST::FfiOHOSFileFsWatcherStart");
576 auto instance = FFIData::GetData<WatcherImpl>(id);
577 if (!instance) {
578 LOGE("FfiOHOSFileFsWatcherStart instance not exist %{public}" PRId64, id);
579 return ERR_INVALID_INSTANCE_CODE;
580 }
581 LOGI("FS_TEST::FfiOHOSFileFsWatcherStart success");
582 return instance->StopNotify();
583 }
584 }
585 } // namespace FileFs
586 } // namespace CJSystemapi
587 } // namespace OHOS