• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "util.h"
17 
18 #include <ctime>
19 #include <sys/time.h>
20 #include <string>
21 
22 #include <vsync_helper.h>
23 
24 namespace OHOS {
25 static const int MAX_FILE_NAME = 512;
26 static const int READ_SIZE = 8192;
GetNowTime()27 int64_t GetNowTime()
28 {
29     struct timeval start = {};
30     gettimeofday(&start, nullptr);
31     constexpr uint32_t secToUsec = 1000 * 1000;
32     return static_cast<int64_t>(start.tv_sec) * secToUsec + start.tv_usec;
33 }
34 
PostTask(std::function<void ()> func,uint32_t delayTime)35 void PostTask(std::function<void()> func, uint32_t delayTime)
36 {
37     auto handler = AppExecFwk::EventHandler::Current();
38     if (handler) {
39         handler->PostTask(func, delayTime);
40     }
41 }
42 
UnzipFile(const std::string & srcFilePath,const std::string & dstFilePath)43 bool UnzipFile(const std::string& srcFilePath, const std::string& dstFilePath)
44 {
45     zlib_filefunc_def *zipFuncPtrs = nullptr;
46     unzFile zipfile = unzOpen2(srcFilePath.c_str(), zipFuncPtrs);
47     if (zipfile == nullptr) {
48         LOG("zip file not found");
49         return false;
50     }
51 
52     unz_global_info globalInfo;
53     if (unzGetGlobalInfo(zipfile, &globalInfo) != UNZ_OK) {
54         LOG("could not read file global info");
55         unzClose(zipfile);
56         return false;
57     }
58 
59     char readBuffer[READ_SIZE];
60     RemoveDir(dstFilePath.c_str());
61     int ret = mkdir(dstFilePath.c_str(), 0700);
62     LOG("create dir bootpic, ret: %{public}d", ret);
63     if (ret == -1) {
64         LOG("pic dir is already exist");
65         return true;
66     }
67 
68     for (unsigned long i = 0; i < globalInfo.number_entry; ++i) {
69         unz_file_info fileInfo;
70         char filename[MAX_FILE_NAME];
71         if (unzGetCurrentFileInfo(
72             zipfile,
73             &fileInfo,
74             filename,
75             MAX_FILE_NAME,
76             nullptr, 0, nullptr, 0) != UNZ_OK) {
77             LOG("could not read file info");
78             unzClose(zipfile);
79             return false;
80         }
81 
82         const size_t fileNameLength = strlen(filename);
83 
84         std::string fileStr(dstFilePath + "/" + filename);
85         if (filename[fileNameLength - 1] == '/') {
86             LOG("mkdir: %{public}s", filename);
87             mkdir(fileStr.c_str(), 0700);
88         } else {
89             if (unzOpenCurrentFile(zipfile) != UNZ_OK) {
90                 LOG("could not open file");
91                 unzClose(zipfile);
92                 return false;
93             }
94             FILE *out = fopen(fileStr.c_str(), "wb");
95             if (out == nullptr) {
96                 LOG("could not open destination file");
97                 unzCloseCurrentFile(zipfile);
98                 unzClose(zipfile);
99                 return false;
100             }
101             int error = UNZ_OK;
102             do {
103                 error = unzReadCurrentFile(zipfile, readBuffer, READ_SIZE);
104                 if (error < 0) {
105                     LOG("unzReadCurrentFile error %{public}d", error);
106                     unzCloseCurrentFile(zipfile);
107                     unzClose(zipfile);
108                     fclose(out);
109                     return false;
110                 }
111                 if (error > 0) {
112                     fwrite(readBuffer, error, 1, out);
113                 }
114             } while (error > 0);
115             fclose(out);
116         }
117         unzCloseCurrentFile(zipfile);
118 
119         if (i < (globalInfo.number_entry - 1)) {
120             if (unzGoToNextFile(zipfile) != UNZ_OK) {
121                 LOG("could not read next file");
122                 unzClose(zipfile);
123                 return false;
124             }
125         }
126     }
127     return true;
128 }
129 
RemoveDir(const char * dir)130 int RemoveDir(const char *dir)
131 {
132     char curDir[] = ".";
133     char upDir[] = "..";
134     DIR *dirp;
135     struct dirent *dp;
136     struct stat dirStat;
137 
138     if (access(dir, F_OK) != 0) {
139         LOG("can not access dir");
140         return 0;
141     }
142     int statRet = stat(dir, &dirStat);
143     if (statRet < 0) {
144         LOG("dir statRet: %{public}d", statRet);
145         return -1;
146     }
147 
148     if (S_ISREG(dirStat.st_mode)) {
149         remove(dir);
150     } else if (S_ISDIR(dirStat.st_mode)) {
151         dirp = opendir(dir);
152         while ((dp = readdir(dirp)) != nullptr) {
153             if ((strcmp(curDir, dp->d_name) == 0) || (strcmp(upDir, dp->d_name) == 0)) {
154                 continue;
155             }
156 
157             std::string dirName = dir;
158             dirName += "/";
159             dirName += dp->d_name;
160             RemoveDir(dirName.c_str());
161         }
162         closedir(dirp);
163         LOG("remove empty dir finally");
164         rmdir(dir);
165     } else {
166         LOG("unknown file type");
167     }
168     return 0;
169 }
170 
CountPicNum(const char * dir,int32_t & picNum)171 int CountPicNum(const char *dir, int32_t& picNum)
172 {
173     char curDir[] = ".";
174     char upDir[] = "..";
175     DIR *dirp;
176     struct dirent *dp;
177     struct stat dirStat;
178 
179     if (access(dir, F_OK) != 0) {
180         LOG("can not access dir");
181         return picNum;
182     }
183     int statRet = stat(dir, &dirStat);
184     if (statRet < 0) {
185         LOG("dir statRet: %{public}d", statRet);
186         return picNum;
187     }
188     if (S_ISREG(dirStat.st_mode)) {
189         picNum += 1;
190     } else  if (S_ISDIR(dirStat.st_mode)) {
191         dirp = opendir(dir);
192         while ((dp = readdir(dirp)) != nullptr) {
193             if ((strcmp(curDir, dp->d_name) == 0) || (strcmp(upDir, dp->d_name) == 0)) {
194                 continue;
195             }
196 
197             std::string dirName = dir;
198             dirName += "/";
199             dirName += dp->d_name;
200             CountPicNum(dirName.c_str(), picNum);
201         }
202         closedir(dirp);
203         LOG("remove empty dir finally");
204         return picNum;
205     } else {
206         LOG("unknown file type");
207     }
208     return picNum;
209 }
210 
WaitRenderServiceInit()211 void WaitRenderServiceInit()
212 {
213     while (true) {
214         sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
215         sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(RENDER_SERVICE);
216         if (remoteObject != nullptr) {
217             LOG("renderService is inited");
218             break;
219         } else {
220             LOG("renderService is not inited, wait");
221             sleep(1);
222         }
223     }
224 }
225 } // namespace OHOS
226