1 /* 2 * Copyright (c) 2021 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 PLATFORM_SPECIFIC_H 17 #define PLATFORM_SPECIFIC_H 18 19 #include <string> 20 #include <cstdint> 21 #include <list> 22 23 namespace DistributedDB { 24 namespace OS { 25 enum FileType { 26 FILE = 0, 27 PATH = 1, 28 OTHER = 2, 29 }; 30 31 struct FileAttr { 32 std::string fileName; 33 FileType fileType; 34 uint64_t fileLen; 35 }; 36 37 // Shield the representation method of file handles on different platforms 38 struct FileHandle { 39 int handle = -1; 40 }; 41 42 int CalFileSize(const std::string &fileUrl, uint64_t &size); 43 44 bool CheckPathExistence(const std::string &filePath); 45 46 int MakeDBDirectory(const std::string &directory); 47 48 int RemoveFile(const std::string &filePath); 49 // Can only remove empty directory 50 int RemoveDBDirectory(const std::string &directory); 51 52 int GetRealPath(const std::string &inOriPath, std::string &outRealPath); 53 54 int GetCurrentSysTimeInMicrosecond(uint64_t &outTime); 55 56 int GetMonotonicRelativeTimeInMicrosecond(uint64_t &outTime); 57 58 int CreateFileByFileName(const std::string &fileName); 59 60 void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName); 61 62 int GetFileAttrFromPath(const std::string &filePath, std::list<FileAttr> &files, bool isNeedAllPath = false); 63 64 int GetFilePermissions(const std::string &fileName, uint32_t &permissions); 65 66 int SetFilePermissions(const std::string &fileName, uint32_t permissions); 67 68 int RenameFilePath(const std::string &oldFilePath, const std::string &newFilePath); 69 70 int OpenFile(const std::string &fileName, FileHandle &handle); 71 int CloseFile(FileHandle &handle); 72 73 int FileLock(const FileHandle &handle, bool isBlock); // be careful use block=true, may block process 74 int FileUnlock(FileHandle &handle); 75 } // namespace OS 76 } // namespace DistributedDB 77 78 #endif // PLATFORM_SPECIFIC_H