• 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 <dirent.h>
16 #include <cstdio>
17 #include <unistd.h>
18 #include <sys/stat.h>
19 #include "hilog_wrapper.h"
20 #include "file_deal.h"
21 
22 namespace OHOS {
23 namespace WallpaperMgrService {
FileDeal(void)24 FileDeal::FileDeal(void)
25 {
26 }
~FileDeal()27 FileDeal::~FileDeal()
28 {
29 }
DirIsExist(std::string path)30 bool FileDeal::DirIsExist(std::string path)
31 {
32     DIR* dp;
33     if ((dp = opendir(path.c_str())) == NULL) {
34         HILOG_INFO("FileDeal : opendir  %{public}s is not exist", path.c_str());
35         return false;
36     }
37     closedir(dp);
38     return true;
39 }
40 
Mkdir(std::string path)41 bool FileDeal::Mkdir(std::string path)
42 {
43     if (!DirIsExist(path)) {
44         int isCreate = ::mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
45         HILOG_INFO("FileDeal : mkdir result= %{public}d,errinfo=%{public}s ,path =  %{public}s ",
46             isCreate, strerror(errno), path.c_str());
47         return isCreate == 0 ? true : false;
48     }
49     return true;
50 }
51 
CopyFile(const std::string & sourceFile,const std::string & newFile)52 bool FileDeal::CopyFile(const std::string &sourceFile, const std::string &newFile)
53 {
54     HILOG_INFO("Copy file Star:from [%{public}s] to [%{public}s]", sourceFile.c_str(), newFile.c_str());
55     std::ifstream in;
56     std::ofstream out;
57 
58     in.open(sourceFile.c_str(), std::ios::binary);
59     if (in.fail()) {
60         HILOG_INFO("open file  %{public}s failed", sourceFile.c_str());
61         in.close();
62         out.close();
63         return false;
64     }
65     out.open(newFile.c_str(), std::ios::binary);
66     if (out.fail()) {
67         HILOG_INFO("open file  %{public}s failed", newFile.c_str());
68         out.close();
69         in.close();
70         return false;
71     }
72     out << in.rdbuf();
73     out.close();
74     in.close();
75     HILOG_INFO("copy file is %{public}s, new file is %{public}s,success", sourceFile.c_str(), newFile.c_str());
76     return true;
77 }
78 
FileIsExist(const std::string & name)79 bool FileDeal::FileIsExist(const std::string& name)
80 {
81     if (access(name.c_str(), F_OK) == 0) {
82         return true;
83     }
84     return false;
85 }
86 }
87 }
88 
89