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