• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <algorithm>
18 #include <cstdio>
19 #include <dirent.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22 
23 #include "hilog_wrapper.h"
24 
25 namespace OHOS {
26 namespace WallpaperMgrService {
27 constexpr mode_t MODE = 0740;
FileDeal(void)28 FileDeal::FileDeal(void)
29 {
30 }
~FileDeal()31 FileDeal::~FileDeal()
32 {
33 }
IsDirExist(std::string path)34 bool FileDeal::IsDirExist(std::string path)
35 {
36     DIR *dp;
37     if ((dp = opendir(path.c_str())) == NULL) {
38         HILOG_INFO("FileDeal : openDir  is not exist, errInfo=%{public}s", strerror(errno));
39         return false;
40     }
41     closedir(dp);
42     return true;
43 }
44 
Mkdir(const std::string & path)45 bool FileDeal::Mkdir(const std::string &path)
46 {
47     if (!IsDirExist(path)) {
48         if (mkdir(path.c_str(), MODE) != 0) {
49             HILOG_INFO("FileDeal : mkdir errInfo=%{public}s", strerror(errno));
50             return false;
51         }
52     }
53     return true;
54 }
55 
CopyFile(const std::string & sourceFile,const std::string & newFile)56 bool FileDeal::CopyFile(const std::string &sourceFile, const std::string &newFile)
57 {
58     std::ifstream in;
59     std::ofstream out;
60 
61     in.open(sourceFile.c_str(), std::ios::binary);
62     if (in.fail()) {
63         HILOG_INFO("open file failed, errInfo=%{public}s", strerror(errno));
64         in.close();
65         return false;
66     }
67     out.open(newFile.c_str(), std::ios::binary);
68     if (out.fail()) {
69         HILOG_INFO("open file failed, errInfo=%{public}s", strerror(errno));
70         out.close();
71         in.close();
72         return false;
73     }
74     out << in.rdbuf();
75     out.close();
76     in.close();
77     HILOG_INFO("copy file success");
78     return true;
79 }
80 
DeleteFile(const std::string & sourceFile)81 bool FileDeal::DeleteFile(const std::string &sourceFile)
82 {
83     if (remove(sourceFile.c_str()) < 0) {
84         HILOG_INFO("Failed to remove source file, errInfo=%{public}s.", strerror(errno));
85         return false;
86     }
87     return true;
88 }
89 
IsFileExist(const std::string & name)90 bool FileDeal::IsFileExist(const std::string &name)
91 {
92     if (access(name.c_str(), F_OK) != 0) {
93         HILOG_INFO("FileDeal : access errInfo=%{public}s", strerror(errno));
94         return false;
95     }
96     return true;
97 }
98 
GetExtension(const std::string & filePath)99 std::string FileDeal::GetExtension(const std::string &filePath)
100 {
101     std::string filename = filePath;
102     std::string extension = "";
103     std::string::size_type pos = filePath.find_last_of('/');
104     if (pos != std::string::npos) {
105         if (pos + 1 < filePath.length()) {
106             filename = filePath.substr(pos + 1);
107         }
108     }
109 
110     pos = filename.find_last_of('.');
111     if (pos != std::string::npos && pos + 1 < filename.length()) {
112         extension = filename.substr(pos);
113         std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
114     }
115     return extension;
116 }
GetRealPath(const std::string & inOriPath,std::string & outRealPath)117 bool FileDeal::GetRealPath(const std::string &inOriPath, std::string &outRealPath)
118 {
119     char realPath[PATH_MAX + 1] = { 0x00 };
120     if (inOriPath.size() > PATH_MAX || realpath(inOriPath.c_str(), realPath) == nullptr) {
121         HILOG_ERROR("get real path fail");
122         return false;
123     }
124     outRealPath = std::string(realPath);
125     if (!IsFileExist(outRealPath)) {
126         HILOG_ERROR("real path file is not exist! %{public}s", outRealPath.c_str());
127         return false;
128     }
129     if (outRealPath != inOriPath) {
130         HILOG_ERROR("illegal file path input %{public}s", inOriPath.c_str());
131         return false;
132     }
133     return true;
134 }
135 } // namespace WallpaperMgrService
136 } // namespace OHOS
137