1 /*
2  * Copyright (c) 2022 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 #ifndef PREFERENCES_FILE_OPERATION_H
16 #define PREFERENCES_FILE_OPERATION_H
17 
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 
22 #include <string>
23 
24 #include "visibility.h"
25 
26 #ifndef _WIN32
27 #include <dlfcn.h>
28 #endif
29 
30 #if defined(WINDOWS_PLATFORM)
31 
32 #include <iostream>
33 #include <windows.h>
34 
35 #else
36 
37 #include <stdarg.h>
38 
39 #if !(defined(MAC_PLATFORM) || defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM))
40 
41 #include <cstdlib>
42 
43 #endif
44 
45 #endif
46 
47 #ifndef FILE_MODE
48 #define FILE_MODE 0770
49 #endif
50 
51 #ifndef FILE_EXIST
52 #define FILE_EXIST 0
53 #endif
54 
55 #ifndef INT_MAX
56 #define INT_MAX 2147483647
57 #endif
58 
59 namespace OHOS {
60 namespace NativePreferences {
61 
DBDlOpen()62 static UNUSED_FUNCTION void *DBDlOpen()
63 {
64 #ifndef _WIN32
65     return dlopen("libarkdata_db_core.z.so", RTLD_LAZY);
66 #else
67     return nullptr;
68 #endif
69 }
70 
Mkdir(const std::string & filePath)71 static UNUSED_FUNCTION int Mkdir(const std::string &filePath)
72 {
73 #if defined(WINDOWS_PLATFORM)
74     return mkdir(filePath.c_str());
75 #else
76     return mkdir(filePath.c_str(), FILE_MODE);
77 #endif
78 }
79 
Access(const std::string & filePath)80 static UNUSED_FUNCTION int Access(const std::string &filePath)
81 {
82 #if defined(WINDOWS_PLATFORM)
83     return _access(filePath.c_str(), FILE_EXIST);
84 #else
85     return access(filePath.c_str(), FILE_EXIST);
86 #endif
87 }
88 
Fsync(const std::string & filePath)89 static UNUSED_FUNCTION bool Fsync(const std::string &filePath)
90 {
91 #if defined(WINDOWS_PLATFORM)
92     int fd = _open(filePath.c_str(), _O_WRONLY, _S_IWRITE);
93     if (fd == -1) {
94         return false;
95     }
96     HANDLE handle = (HANDLE)_get_osfhandle(fd);
97     if (handle == INVALID_HANDLE_VALUE || !FlushFileBuffers(handle)) {
98         _close(fd);
99         return false;
100     }
101     _close(fd);
102 #else
103     int fd = open(filePath.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
104     if (fd == -1) {
105         return false;
106     }
107     if (fsync(fd) == -1) {
108         close(fd);
109         return false;
110     }
111     close(fd);
112 #endif
113     return true;
114 }
115 
ExtractFileName(const std::string & path)116 static UNUSED_FUNCTION std::string ExtractFileName(const std::string &path)
117 {
118     auto pos = path.rfind('/');
119     if (pos == std::string::npos) {
120         return path;
121     }
122     return path.substr(pos + 1);
123 }
124 } // namespace NativePreferences
125 } // namespace OHOS
126 #endif // PREFERENCES_FILE_OPERATION_H
127