• 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 #include "applypatch/store.h"
16 #include <algorithm>
17 #include <cstdio>
18 #include <fcntl.h>
19 #include <limits>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <sys/vfs.h>
23 #include <unistd.h>
24 #include "applypatch/transfer_manager.h"
25 #include "log/log.h"
26 #include "utils.h"
27 
28 using namespace Updater::Utils;
29 
30 namespace Updater {
DoFreeSpace(const std::string & directoryPath)31 int32_t Store::DoFreeSpace(const std::string &directoryPath)
32 {
33     std::vector<std::string> files;
34     if (GetFilesFromDirectory(directoryPath, files, true) <= 0) {
35         LOG(ERROR) << "Failed to get files for free space";
36         return -1;
37     }
38     for (const auto &file : files) {
39         if (DeleteFile(file.c_str()) == -1) {
40             LOG(ERROR) << "Failed to delete in do free space";
41             continue;
42         }
43     }
44     return 0;
45 }
46 
FreeStore(const std::string & dirPath,const std::string & fileName)47 int32_t Store::FreeStore(const std::string &dirPath, const std::string &fileName)
48 {
49     if (dirPath.empty()) {
50         return -1;
51     }
52     std::string path;
53     if (!fileName.empty()) {
54         path = dirPath + "/";
55     }
56     if (DeleteFile(path.c_str()) != -1) {
57         return 0;
58     }
59     LOG(ERROR) << "Failed to delete " << path;
60     return -1;
61 }
62 
CreateNewSpace(const std::string & path,bool needClear)63 int32_t Store::CreateNewSpace(const std::string &path, bool needClear)
64 {
65     if (path.empty()) {
66         LOG(ERROR) << "path is empty.";
67     }
68     std::string dirPath = path + '/';
69     struct stat fileStat {};
70     LOG(INFO) << "Create dir " << dirPath;
71     if (stat(dirPath.c_str(), &fileStat) == -1) {
72         if (errno != ENOENT) {
73             LOG(ERROR) << "Create new space, failed to stat";
74             return -1;
75         }
76         if (MkdirRecursive(dirPath, S_IRWXU) != 0) {
77             LOG(ERROR) << "Failed to make store";
78             return -1;
79         }
80         if (chown(dirPath.c_str(), O_USER_GROUP_ID, O_USER_GROUP_ID) != 0) {
81             LOG(ERROR) << "Failed to chown store";
82             return -1;
83         }
84     } else {
85         if (!needClear) {
86             return 0;
87         }
88         std::vector<std::string> files {};
89         if (GetFilesFromDirectory(dirPath, files) < 0) {
90             return -1;
91         }
92         if (files.empty()) {
93             return 0;
94         }
95         std::vector<std::string>::iterator iter = files.begin();
96         while (iter != files.end()) {
97             if (DeleteFile(*iter) == 0) {
98                 LOG(INFO) << "Delete " << *iter;
99             }
100             iter++;
101         }
102         files.clear();
103     }
104     return 0;
105 }
106 
WriteDataToStore(const std::string & dirPath,const std::string & fileName,const std::vector<uint8_t> & buffer,int size)107 int32_t Store::WriteDataToStore(const std::string &dirPath, const std::string &fileName,
108     const std::vector<uint8_t> &buffer, int size)
109 {
110     if (dirPath.empty()) {
111         return -1;
112     }
113     std::string pathTmp;
114     if (!fileName.empty()) {
115         pathTmp = dirPath + "/";
116     }
117     std::string path = pathTmp + fileName;
118     pathTmp = pathTmp + fileName;
119 
120     int fd = open(pathTmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
121     if (fd == -1) {
122         LOG(ERROR) << "Failed to create store";
123         return -1;
124     }
125     if (fchown(fd, O_USER_GROUP_ID, O_USER_GROUP_ID) != 0) {
126         LOG(ERROR) << "Failed to chown";
127         close(fd);
128         return -1;
129     }
130     LOG(INFO) << "Writing " << size << " blocks to " << path;
131     if (size < 0 || !WriteFully(fd, buffer.data(), static_cast<size_t>(size))) {
132         if (errno == EIO) {
133             close(fd);
134             return 1;
135         }
136         LOG(ERROR) << "Write to stash failed";
137         close(fd);
138         return -1;
139     }
140     if (fsync(fd) == -1) {
141         LOG(ERROR) << "Failed to fsync";
142         close(fd);
143         return -1;
144     }
145     close(fd);
146     int fdd = open(dirPath.c_str(), O_RDONLY | O_DIRECTORY);
147     if (fdd == -1) {
148         LOG(ERROR) << "Failed to open";
149         return -1;
150     }
151     close(fdd);
152     return 0;
153 }
154 
LoadDataFromStore(const std::string & dirPath,const std::string & fileName,std::vector<uint8_t> & buffer)155 int32_t Store::LoadDataFromStore(const std::string &dirPath, const std::string &fileName,
156     std::vector<uint8_t> &buffer)
157 {
158     LOG(INFO) << "Store base is " << dirPath << "/" << fileName;
159     std::string path = dirPath;
160     if (!fileName.empty()) {
161         path = path + "/" + fileName;
162     }
163     struct stat fileStat {};
164     if (stat(path.c_str(), &fileStat) == -1) {
165         LOG(WARNING) << "Failed to stat";
166         return -1;
167     }
168     if (fileStat.st_size % H_BLOCK_SIZE != 0) {
169         LOG(ERROR) << "Not multiple of block size 4096";
170         return -1;
171     }
172 
173     int fd = open(path.c_str(), O_RDONLY);
174     if (fd == -1) {
175         LOG(ERROR) << "Failed to create";
176         return -1;
177     }
178     buffer.resize(fileStat.st_size);
179     if (!ReadFully(fd, buffer.data(), fileStat.st_size)) {
180         LOG(ERROR) << "Failed to read store data";
181         close(fd);
182         fd = -1;
183         return -1;
184     }
185     close(fd);
186     fd = -1;
187     return 0;
188 }
189 } // namespace Updater
190