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 #include "file_deal.h"
16
17 #include <dirent.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21
22 #include <algorithm>
23 #include <cstdio>
24 #include <filesystem>
25
26 #include "hilog_wrapper.h"
27
28 namespace fs = std::filesystem;
29 namespace OHOS {
30 namespace WallpaperMgrService {
31 constexpr mode_t MODE = 0740;
FileDeal(void)32 FileDeal::FileDeal(void)
33 {
34 }
~FileDeal()35 FileDeal::~FileDeal()
36 {
37 }
IsDirExist(std::string path)38 bool FileDeal::IsDirExist(std::string path)
39 {
40 DIR *dp;
41 if ((dp = opendir(path.c_str())) == NULL) {
42 HILOG_INFO("FileDeal : openDir is not exist, errInfo=%{public}s", strerror(errno));
43 return false;
44 }
45 closedir(dp);
46 return true;
47 }
48
Mkdir(const std::string & path)49 bool FileDeal::Mkdir(const std::string &path)
50 {
51 if (!IsDirExist(path)) {
52 if (mkdir(path.c_str(), MODE) != 0) {
53 HILOG_INFO("FileDeal : mkdir errInfo=%{public}s", strerror(errno));
54 return false;
55 }
56 }
57 return true;
58 }
59
CopyFile(const std::string & sourceFile,const std::string & newFile)60 bool FileDeal::CopyFile(const std::string &sourceFile, const std::string &newFile)
61 {
62 std::filesystem::path dstPath(newFile);
63 std::filesystem::path srcPath(sourceFile);
64 std::error_code errCode;
65 if (!std::filesystem::copy_file(srcPath, dstPath, std::filesystem::copy_options::overwrite_existing, errCode)) {
66 HILOG_ERROR("Failed to copy file, error code: %{public}d", errCode.value());
67 return false;
68 }
69 if (!ForcedRefreshDisk(newFile)) {
70 HILOG_WARN("ForcedRefreshDisk failed");
71 }
72 return true;
73 }
74
DeleteFile(const std::string & sourceFile)75 bool FileDeal::DeleteFile(const std::string &sourceFile)
76 {
77 if (remove(sourceFile.c_str()) < 0) {
78 HILOG_INFO("Failed to remove source file, errInfo=%{public}s.", strerror(errno));
79 return false;
80 }
81 return true;
82 }
83
IsFileExist(const std::string & name)84 bool FileDeal::IsFileExist(const std::string &name)
85 {
86 if (access(name.c_str(), F_OK) != 0) {
87 HILOG_INFO("FileDeal : access errInfo=%{public}s", strerror(errno));
88 return false;
89 }
90 return true;
91 }
92
GetExtension(const std::string & filePath)93 std::string FileDeal::GetExtension(const std::string &filePath)
94 {
95 std::string filename = filePath;
96 std::string extension = "";
97 std::string::size_type pos = filePath.find_last_of('/');
98 if (pos != std::string::npos) {
99 if (pos + 1 < filePath.length()) {
100 filename = filePath.substr(pos + 1);
101 }
102 }
103
104 pos = filename.find_last_of('.');
105 if (pos != std::string::npos && pos + 1 < filename.length()) {
106 extension = filename.substr(pos);
107 std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
108 }
109 return extension;
110 }
GetRealPath(const std::string & inOriPath,std::string & outRealPath)111 bool FileDeal::GetRealPath(const std::string &inOriPath, std::string &outRealPath)
112 {
113 char realPath[PATH_MAX + 1] = { 0x00 };
114 if (inOriPath.size() > PATH_MAX || realpath(inOriPath.c_str(), realPath) == nullptr) {
115 HILOG_ERROR("get real path fail");
116 return false;
117 }
118 outRealPath = std::string(realPath);
119 if (!IsFileExist(outRealPath)) {
120 HILOG_ERROR("real path file is not exist! %{public}s", outRealPath.c_str());
121 return false;
122 }
123 if (outRealPath != inOriPath) {
124 HILOG_ERROR("illegal file path input %{public}s", inOriPath.c_str());
125 return false;
126 }
127 return true;
128 }
IsZipFile(const std::string & filePath)129 bool FileDeal::IsZipFile(const std::string &filePath)
130 {
131 fs::path file(filePath);
132 if (fs::exists(file) && fs::is_regular_file(file)) {
133 std::string extension = file.extension().string();
134 if (extension == ".zip") {
135 return true;
136 }
137 }
138 HILOG_ERROR("this is not a zip.filePath:%{private}s", filePath.c_str());
139 return false;
140 }
ForcedRefreshDisk(const std::string & sourcePath)141 bool FileDeal::ForcedRefreshDisk(const std::string &sourcePath)
142 {
143 std::string fileRealPath;
144 if (!GetRealPath(sourcePath, fileRealPath)) {
145 HILOG_ERROR("get real path file failed");
146 return false;
147 }
148 FILE *file = std::fopen(fileRealPath.c_str(), "rb");
149 if (file == nullptr) {
150 HILOG_ERROR("Fopen failed, %{public}s, %{public}s", fileRealPath.c_str(), strerror(errno));
151 return false;
152 }
153 if (fflush(file) != 0) {
154 HILOG_ERROR("fflush file failed, errno %{public}d", errno);
155 fclose(file);
156 return false;
157 }
158 if (fsync(fileno(file)) != 0) {
159 HILOG_ERROR("fsync file failed, errno %{public}d", errno);
160 fclose(file);
161 return false;
162 }
163 (void)fclose(file);
164 return true;
165 }
166 } // namespace WallpaperMgrService
167 } // namespace OHOS
168