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 UPDATER_ERROR_CHECK(GetFilesFromDirectory(directoryPath, files, true) > 0,
35 "Failed to get files for free space", return -1);
36 for (const auto &file : files) {
37 UPDATER_ERROR_CHECK(DeleteFile(file.c_str()) != -1, "Failed to delete in do free space", continue);
38 }
39 return 0;
40 }
41
FreeStore(const std::string & dirPath,const std::string & fileName)42 int32_t Store::FreeStore(const std::string &dirPath, const std::string &fileName)
43 {
44 UPDATER_CHECK_ONLY_RETURN(!dirPath.empty(), return -1);
45 std::string path;
46 if (!fileName.empty()) {
47 path = dirPath + "/";
48 }
49 UPDATER_CHECK_ONLY_RETURN(DeleteFile(path.c_str()) == -1, return 0);
50 LOG(ERROR) << "Failed to delete " << path;
51 return -1;
52 }
53
CreateNewSpace(const std::string & path,bool needClear)54 int32_t Store::CreateNewSpace(const std::string &path, bool needClear)
55 {
56 UPDATER_ERROR_CHECK_NOT_RETURN(!path.empty(), "path is empty.");
57 std::string dirPath = path + '/';
58 struct stat fileStat {};
59 LOG(INFO) << "Create dir " << dirPath;
60 if (stat(dirPath.c_str(), &fileStat) == -1) {
61 UPDATER_ERROR_CHECK(errno == ENOENT, "Create new space, failed to stat", return -1);
62 UPDATER_ERROR_CHECK(MkdirRecursive(dirPath, S_IRWXU) == 0, "Failed to make store", return -1);
63 UPDATER_ERROR_CHECK(chown(dirPath.c_str(), O_USER_GROUP_ID, O_USER_GROUP_ID) == 0,
64 "Failed to chown store", return -1);
65 } else {
66 UPDATER_CHECK_ONLY_RETURN(needClear, return 0);
67 std::vector<std::string> files {};
68 UPDATER_CHECK_ONLY_RETURN(GetFilesFromDirectory(dirPath, files) >= 0, return -1);
69 UPDATER_CHECK_ONLY_RETURN(!files.empty(), return 0);
70 std::vector<std::string>::iterator iter = files.begin();
71 while (iter != files.end()) {
72 if (DeleteFile(*iter) == 0) {
73 LOG(INFO) << "Delete " << *iter;
74 }
75 iter++;
76 }
77 files.clear();
78 }
79 return 0;
80 }
81
WriteDataToStore(const std::string & dirPath,const std::string & fileName,const std::vector<uint8_t> & buffer,int size)82 int32_t Store::WriteDataToStore(const std::string &dirPath, const std::string &fileName,
83 const std::vector<uint8_t> &buffer, int size)
84 {
85 UPDATER_CHECK_ONLY_RETURN(!dirPath.empty(), return -1);
86 std::string pathTmp;
87 if (!fileName.empty()) {
88 pathTmp = dirPath + "/";
89 }
90 std::string path = pathTmp + fileName;
91 pathTmp = pathTmp + fileName;
92
93 int fd = open(pathTmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
94 UPDATER_ERROR_CHECK(fd != -1, "Failed to create store", return -1);
95 UPDATER_ERROR_CHECK(fchown(fd, O_USER_GROUP_ID, O_USER_GROUP_ID) == 0,
96 "Failed to chown", close(fd); return -1);
97 LOG(INFO) << "Writing " << size << " blocks to " << path;
98 if (!WriteFully(fd, const_cast<uint8_t *>(buffer.data()), size)) {
99 UPDATER_CHECK_ONLY_RETURN(errno != EIO, close(fd); return 1);
100 LOG(ERROR) << "Write to stash failed";
101 close(fd);
102 return -1;
103 }
104 UPDATER_ERROR_CHECK(fsync(fd) != -1, "Failed to fsync", close(fd); return -1);
105 close(fd);
106 int fdd = open(dirPath.c_str(), O_RDONLY | O_DIRECTORY);
107 UPDATER_ERROR_CHECK(fdd != -1, "Failed to open", return -1);
108 close(fdd);
109 return 0;
110 }
111
LoadDataFromStore(const std::string & dirPath,const std::string & fileName,std::vector<uint8_t> & buffer)112 int32_t Store::LoadDataFromStore(const std::string &dirPath, const std::string &fileName,
113 std::vector<uint8_t> &buffer)
114 {
115 LOG(INFO) << "Store base is " << dirPath << "/" << fileName;
116 std::string path = dirPath;
117 if (!fileName.empty()) {
118 path = path + "/" + fileName;
119 }
120 struct stat fileStat {};
121 UPDATER_WARING_CHECK(stat(path.c_str(), &fileStat) != -1, "Failed to stat", return -1);
122 UPDATER_ERROR_CHECK((fileStat.st_size % H_BLOCK_SIZE) == 0, "Not multiple of block size 4096", return -1);
123
124 int fd = open(path.c_str(), O_RDONLY);
125 UPDATER_ERROR_CHECK(fd != -1, "Failed to create", return -1);
126 buffer.resize(fileStat.st_size);
127 UPDATER_ERROR_CHECK(ReadFully(fd, buffer.data(), fileStat.st_size), "Failed to read store data",
128 close(fd); fd = -1; return -1);
129 close(fd);
130 fd = -1;
131 return 0;
132 }
133 } // namespace updater
134