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 #ifndef OHOS_FILE_FS_IMPL_H 17 #define OHOS_FILE_FS_IMPL_H 18 19 #include <filesystem> 20 #include <deque> 21 #include <string_view> 22 23 #include "stat_impl.h" 24 #include "stream_impl.h" 25 #include "macro.h" 26 #include "randomAccessFile_impl.h" 27 #include "readerIterator_impl.h" 28 #include "watcher_impl.h" 29 #include "file_impl.h" 30 #include "copy_dir.h" 31 #include "ffi_remote_data.h" 32 #include "cj_common_ffi.h" 33 #include "uni_error.h" 34 #include "rust_file.h" 35 #include "napi/native_api.h" 36 37 namespace OHOS { 38 namespace CJSystemapi { 39 constexpr int DIR_DEFAULT_PERM = 0770; 40 41 constexpr int FILE_DISMATCH = 0; 42 constexpr int FILE_MATCH = 1; 43 44 // use for moveDir 45 constexpr int DIRMODE_MIN = 0; 46 constexpr int DIRMODE_MAX = 3; 47 48 struct StreamEntity { 49 std::unique_ptr<FILE, decltype(&fclose)> fp = { nullptr, fclose }; 50 }; 51 52 enum ModeOfMoveDir { 53 DIRMODE_DIRECTORY_THROW_ERR = 0, 54 DIRMODE_FILE_THROW_ERR, 55 DIRMODE_FILE_REPLACE, 56 DIRMODE_DIRECTORY_REPLACE 57 }; 58 struct FileStruct { 59 bool isFd = false; 60 int fd; 61 FileFs::FileEntity *fileEntity; 62 }; 63 namespace FileFs { 64 enum AccessFlag : int32_t { 65 DEFAULT_FLAG = -1, 66 LOCAL_FLAG, 67 }; 68 class FileFsImpl { 69 public: 70 static std::tuple<int32_t, sptr<StatImpl>> Stat(int32_t file); 71 static std::tuple<int32_t, sptr<StatImpl>> Stat(std::string file); 72 static std::tuple<int32_t, sptr<StreamImpl>> CreateStream(std::string path, std::string mode); 73 static std::tuple<int32_t, sptr<StreamImpl>> FdopenStream(int32_t, std::string mode); 74 static std::tuple<int32_t, sptr<StatImpl>> Lstat(std::string path); 75 static std::tuple<int32_t, sptr<RandomAccessFileImpl>> CreateRandomAccessFileSync(std::string file, 76 unsigned int mode); 77 static std::tuple<int32_t, sptr<RandomAccessFileImpl>> CreateRandomAccessFileSync(sptr<FileEntity> entity, 78 unsigned int mode); 79 static int Mkdir(std::string path, bool recursion, bool isTwoArgs); 80 static int Rmdir(std::string path); 81 static int Rename(std::string oldPath, std::string newPath); 82 static int Unlink(std::string path); 83 static RetDataCArrConflictFiles MoveDir(std::string src, std::string dest, int32_t mode); 84 static RetDataI64 Read(int32_t fd, char* buf, int64_t bufLen, size_t length, int64_t offset); 85 static RetDataI64 ReadCur(int32_t fd, char* buf, int64_t bufLen, size_t length); 86 static RetDataI64 Write(int32_t fd, void* buf, size_t length, int64_t offset, std::string encode); 87 static RetDataI64 WriteCur(int32_t fd, void* buf, size_t length, std::string encode); 88 static std::tuple<int32_t, bool> Access(std::string path, int32_t mode = 0, int32_t flag = DEFAULT_FLAG); 89 static int Truncate(std::string file, int64_t len); 90 static int Truncate(int32_t file, int64_t len); 91 static int Close(int32_t file); 92 static int Close(sptr<OHOS::CJSystemapi::FileFs::FileEntity> file); 93 static std::tuple<int32_t, sptr<ReadIteratorImpl>> ReadLines(char* file, std::string encoding); 94 static RetDataCString ReadText(char* path, int64_t offset, bool hasLen, int64_t len, char* encoding); 95 static int Utimes(std::string path, double mtime); 96 static std::tuple<int32_t, sptr<WatcherImpl>> CreateWatcher(std::string path, uint32_t events, 97 void (*callback)(CWatchEvent)); 98 }; 99 } // OHOS::FileManagement::ModuleFileIO 100 } 101 } 102 103 #endif // OHOS_FILE_FS_IMPL_H