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 #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 36 namespace OHOS { 37 namespace CJSystemapi { 38 constexpr int ERRNO_NOERR = 0; 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 enum ModeOfMoveDir { 49 DIRMODE_DIRECTORY_THROW_ERR = 0, 50 DIRMODE_FILE_THROW_ERR, 51 DIRMODE_FILE_REPLACE, 52 DIRMODE_DIRECTORY_REPLACE 53 }; 54 struct FileStruct { 55 bool isFd = false; 56 int fd; 57 FileFs::FileEntity *fileEntity; 58 }; 59 namespace FileFs { 60 class FileFsImpl { 61 public: 62 static std::tuple<int32_t, sptr<StatImpl>> Stat(int32_t file); 63 static std::tuple<int32_t, sptr<StatImpl>> Stat(std::string file); 64 static std::tuple<int32_t, sptr<StreamImpl>> CreateStream(std::string path, std::string mode); 65 static std::tuple<int32_t, sptr<StreamImpl>> FdopenStream(int32_t, std::string mode); 66 static std::tuple<int32_t, sptr<StatImpl>> Lstat(std::string path); 67 static std::tuple<int32_t, sptr<RandomAccessFileImpl>> CreateRandomAccessFileSync(std::string file, 68 unsigned int mode); 69 static std::tuple<int32_t, sptr<RandomAccessFileImpl>> CreateRandomAccessFileSync(sptr<FileEntity> entity, 70 unsigned int mode); 71 static int Mkdir(std::string path, bool recursion, bool isTwoArgs); 72 static int Rmdir(std::string path); 73 static int Rename(std::string oldPath, std::string newPath); 74 static int Unlink(std::string path); 75 static RetDataCArrConflictFiles MoveDir(std::string src, std::string dest, int32_t mode); 76 static RetDataI64 Read(int32_t fd, char* buf, int64_t bufLen, size_t length, int64_t offset); 77 static RetDataI64 ReadCur(int32_t fd, char* buf, int64_t bufLen, size_t length); 78 static RetDataI64 Write(int32_t fd, char* buf, size_t length, int64_t offset, std::string encode); 79 static RetDataI64 WriteCur(int32_t fd, char* buf, size_t length, std::string encode); 80 static std::tuple<int32_t, bool> Access(std::string path); 81 static int Truncate(std::string file, int64_t len); 82 static int Truncate(int32_t file, int64_t len); 83 static int Close(int32_t file); 84 static int Close(sptr<OHOS::CJSystemapi::FileFs::FileEntity> file); 85 static std::tuple<int32_t, sptr<ReadIteratorImpl>> ReadLines(char* file, std::string encoding); 86 static RetDataCString ReadText(char* path, int64_t offset, bool hasLen, int64_t len, char* encoding); 87 static int Utimes(std::string path, double mtime); 88 static std::tuple<int32_t, sptr<WatcherImpl>> CreateWatcher(std::string path, uint32_t events, 89 void (*callback)(CWatchEvent)); 90 }; 91 } // OHOS::FileManagement::ModuleFileIO 92 } 93 } 94 95 #endif // OHOS_FILE_FS_IMPL_H