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 #include "preferences_anonymous.h"
26
27 #ifndef _WIN32
28 #include <dlfcn.h>
29 #endif
30
31 #if defined(WINDOWS_PLATFORM)
32
33 #include <iostream>
34 #include <windows.h>
35 #include <io.h>
36
37 #else
38
39 #include <stdarg.h>
40
41 #if !(defined(MAC_PLATFORM) || defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM))
42
43 #include <cstdlib>
44
45 #endif
46
47 #endif
48
49 #ifndef FILE_MODE
50 #define FILE_MODE 0770
51 #endif
52
53 #ifndef FILE_EXIST
54 #define FILE_EXIST 0
55 #endif
56
57 #ifndef INT_MAX
58 #define INT_MAX 2147483647
59 #endif
60
61 namespace OHOS {
62 namespace NativePreferences {
63
64 constexpr int32_t PRE_OFFSET_SIZE = 1;
65 constexpr int32_t AREA_MINI_SIZE = 4;
66 constexpr int32_t AREA_OFFSET_SIZE = 5;
67 constexpr int32_t FILE_PATH_MINI_SIZE = 6;
68
DBDlOpen()69 static UNUSED_FUNCTION void *DBDlOpen()
70 {
71 #ifndef _WIN32
72 return dlopen("libarkdata_db_core.z.so", RTLD_LAZY);
73 #else
74 return nullptr;
75 #endif
76 }
77
Mkdir(const std::string & filePath)78 static UNUSED_FUNCTION int Mkdir(const std::string &filePath)
79 {
80 #if defined(WINDOWS_PLATFORM)
81 return mkdir(filePath.c_str());
82 #else
83 return mkdir(filePath.c_str(), FILE_MODE);
84 #endif
85 }
86
Access(const std::string & filePath)87 static UNUSED_FUNCTION int Access(const std::string &filePath)
88 {
89 #if defined(WINDOWS_PLATFORM)
90 return _access(filePath.c_str(), FILE_EXIST);
91 #else
92 return access(filePath.c_str(), FILE_EXIST);
93 #endif
94 }
95
Open(const std::string & filePath)96 static UNUSED_FUNCTION int Open(const std::string &filePath)
97 {
98 #if defined(WINDOWS_PLATFORM)
99 return _open(filePath.c_str(), _O_WRONLY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
100 #else
101 return open(filePath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0660);
102 #endif
103 }
104
Write(int fd,const unsigned char * buffer,ssize_t count)105 static UNUSED_FUNCTION int Write(int fd, const unsigned char *buffer, ssize_t count)
106 {
107 #if defined(WINDOWS_PLATFORM)
108 HANDLE hFile = (HANDLE)_get_osfhandle(fd); // 转换文件描述符为 HANDLE
109 if (hFile == INVALID_HANDLE_VALUE) {
110 return -1;
111 }
112 DWORD bytesWritten = 0;
113 return WriteFile(hFile, buffer, (DWORD)count, &bytesWritten, NULL) ? (int)bytesWritten : -1;
114 #else
115 return write(fd, buffer, count);
116 #endif
117 }
118
Close(int fd)119 static UNUSED_FUNCTION int Close(int fd)
120 {
121 #if defined(WINDOWS_PLATFORM)
122 return _close(fd);
123 #else
124 return close(fd);
125 #endif
126 }
127
Fsync(int fd)128 static UNUSED_FUNCTION bool Fsync(int fd)
129 {
130 #if defined(WINDOWS_PLATFORM)
131 HANDLE handle = (HANDLE)_get_osfhandle(fd);
132 if (handle == INVALID_HANDLE_VALUE || !FlushFileBuffers(handle)) {
133 return false;
134 }
135 #else
136 if (fd == -1) {
137 return false;
138 }
139 if (fsync(fd) == -1) {
140 return false;
141 }
142 #endif
143 return true;
144 }
145
ExtractFileName(const std::string & path)146 static UNUSED_FUNCTION std::string ExtractFileName(const std::string &path)
147 {
148 auto pre = path.find("/");
149 auto end = path.rfind("/");
150 if (pre == std::string::npos || end - pre < FILE_PATH_MINI_SIZE) {
151 return Anonymous::ToBeAnonymous(path);
152 }
153 std::string fileName = path.substr(end + 1); // preferences file name
154 auto filePath = path.substr(pre, end - pre);
155 auto area = filePath.find("/el");
156 if (area == std::string::npos || area + AREA_MINI_SIZE > path.size()) {
157 filePath = "";
158 } else if (area + AREA_OFFSET_SIZE < path.size()) {
159 filePath = path.substr(area, AREA_MINI_SIZE) + "/***";
160 } else {
161 filePath = path.substr(area, AREA_MINI_SIZE);
162 }
163 fileName = Anonymous::ToBeAnonymous(fileName);
164 return path.substr(0, pre + PRE_OFFSET_SIZE) + "***" + filePath + "/"+ fileName;
165 }
166 } // namespace NativePreferences
167 } // namespace OHOS
168 #endif // PREFERENCES_FILE_OPERATION_H
169