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() || fileName.empty()) {
50 return -1;
51 }
52 std::string path = dirPath + "/" + fileName;
53 if (DeleteFile(path.c_str()) != -1) {
54 return 0;
55 }
56 LOG(ERROR) << "Failed to delete " << path;
57 return -1;
58 }
59
CreateNewSpace(const std::string & path,bool needClear)60 int32_t Store::CreateNewSpace(const std::string &path, bool needClear)
61 {
62 if (path.empty()) {
63 LOG(ERROR) << "path is empty.";
64 }
65 std::string dirPath = path + '/';
66 struct stat fileStat {};
67 LOG(INFO) << "Create dir " << dirPath;
68 if (stat(dirPath.c_str(), &fileStat) == -1) {
69 if (errno != ENOENT) {
70 LOG(ERROR) << "Create new space, failed to stat";
71 return -1;
72 }
73 if (MkdirRecursive(dirPath, S_IRWXU) != 0) {
74 LOG(ERROR) << "Failed to make store";
75 return -1;
76 }
77 } else {
78 if (!needClear) {
79 return 0;
80 }
81 std::vector<std::string> files {};
82 if (GetFilesFromDirectory(dirPath, files) < 0) {
83 return -1;
84 }
85 if (files.empty()) {
86 return 0;
87 }
88 std::vector<std::string>::iterator iter = files.begin();
89 while (iter != files.end()) {
90 if (DeleteFile(*iter) == 0) {
91 LOG(INFO) << "Delete " << *iter;
92 }
93 iter++;
94 }
95 files.clear();
96 }
97 return 0;
98 }
99
WriteDataToStore(const std::string & dirPath,const std::string & fileName,const std::vector<uint8_t> & buffer,int size)100 int32_t Store::WriteDataToStore(const std::string &dirPath, const std::string &fileName,
101 const std::vector<uint8_t> &buffer, int size)
102 {
103 if (dirPath.empty()) {
104 return -1;
105 }
106 std::string pathTmp;
107 if (!fileName.empty()) {
108 pathTmp = dirPath + "/";
109 }
110 std::string path = pathTmp + fileName;
111 pathTmp = pathTmp + fileName;
112
113 int fd = open(pathTmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
114 if (fd == -1) {
115 LOG(ERROR) << "Failed to create store";
116 return -1;
117 }
118 if (size < 0 || !WriteFully(fd, buffer.data(), static_cast<size_t>(size))) {
119 if (errno == EIO) {
120 close(fd);
121 return 1;
122 }
123 LOG(ERROR) << "Write to stash failed, " << size << " blocks to " << path;
124 close(fd);
125 return -1;
126 }
127 if (fsync(fd) == -1) {
128 LOG(ERROR) << "Failed to fsync";
129 close(fd);
130 return -1;
131 }
132 close(fd);
133 int fdd = open(dirPath.c_str(), O_RDONLY | O_DIRECTORY);
134 if (fdd == -1) {
135 LOG(ERROR) << "Failed to open";
136 return -1;
137 }
138 close(fdd);
139 return 0;
140 }
141
LoadDataFromStore(const std::string & dirPath,const std::string & fileName,std::vector<uint8_t> & buffer)142 int32_t Store::LoadDataFromStore(const std::string &dirPath, const std::string &fileName,
143 std::vector<uint8_t> &buffer)
144 {
145 LOG(INFO) << "Store base is " << dirPath << "/" << fileName;
146 std::string path = dirPath;
147 if (!fileName.empty()) {
148 path = path + "/" + fileName;
149 }
150 struct stat fileStat {};
151 if (stat(path.c_str(), &fileStat) == -1) {
152 LOG(WARNING) << "Failed to stat";
153 return -1;
154 }
155 if (fileStat.st_size % H_BLOCK_SIZE != 0) {
156 LOG(ERROR) << "Not multiple of block size 4096";
157 return -1;
158 }
159
160 int fd = open(path.c_str(), O_RDONLY);
161 if (fd == -1) {
162 LOG(ERROR) << "Failed to create";
163 return -1;
164 }
165 buffer.resize(fileStat.st_size);
166 if (!ReadFully(fd, buffer.data(), fileStat.st_size)) {
167 LOG(ERROR) << "Failed to read store data";
168 close(fd);
169 fd = -1;
170 return -1;
171 }
172 close(fd);
173 fd = -1;
174 return 0;
175 }
176 } // namespace Updater
177