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 = FileType::FILE;
38     uint64_t fileLen = 0;
39 };
40 
41 // Shield the representation method of file handles on different platforms
42 struct FileHandle;
43 
44 int CalFileSize(const std::string &fileUrl, uint64_t &size);
45 
46 bool CheckPathExistence(const std::string &filePath);
47 
48 int MakeDBDirectory(const std::string &directory);
49 
50 int RemoveFile(const std::string &filePath);
51 // Can only remove empty directory
52 int RemoveDBDirectory(const std::string &directory);
53 
54 int GetRealPath(const std::string &inOriPath, std::string &outRealPath);
55 
56 int GetCurrentSysTimeInMicrosecond(uint64_t &outTime);
57 
58 int GetMonotonicRelativeTimeInMicrosecond(uint64_t &outTime);
59 
60 int CreateFileByFileName(const std::string &fileName);
61 
62 void SplitFilePath(const std::string &filePath, std::string &fileDir, std::string &fileName);
63 
64 int GetFileAttrFromPath(const std::string &filePath, std::list<FileAttr> &files, bool isNeedAllPath = false);
65 
66 int GetFilePermissions(const std::string &fileName, uint32_t &permissions);
67 
68 int SetFilePermissions(const std::string &fileName, uint32_t permissions);
69 
70 int RenameFilePath(const std::string &oldFilePath, const std::string &newFilePath);
71 
72 int OpenFile(const std::string &fileName, FileHandle *&fileHandle);
73 int CloseFile(FileHandle *fileHandle);
74 
75 int FileLock(const FileHandle *fileHandle, bool isBlock); // be careful use block=true, may block process
76 int FileUnlock(FileHandle *fileHandle);
77 } // namespace OS
78 } // namespace DistributedDB
79 
80 #endif // PLATFORM_SPECIFIC_H