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