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 "misc_info/misc_info.h"
17 #include <cstring>
18 #include <unistd.h>
19 #include "fs_manager/mount.h"
20 #include "log/log.h"
21 #include "securec.h"
22
23 namespace updater {
WriteUpdaterMessage(const std::string & path,const UpdateMessage & boot)24 bool WriteUpdaterMessage(const std::string &path, const UpdateMessage &boot)
25 {
26 char *realPath = realpath(path.c_str(), NULL);
27 UPDATER_FILE_CHECK(realPath != nullptr, "realPath is NULL", return false);
28 FILE* fp = fopen(realPath, "rb+");
29 free(realPath);
30 UPDATER_FILE_CHECK(fp != nullptr, "WriteUpdaterMessage fopen failed", return false);
31
32 size_t ret = fwrite(&boot, sizeof(UpdateMessage), 1, fp);
33 UPDATER_FILE_CHECK(ret == 1, "WriteUpdaterMessage fwrite failed", fclose(fp); return false);
34
35 ret = fclose(fp);
36 UPDATER_FILE_CHECK(ret == 0, "WriteUpdaterMessage fclose failed", return false);
37 return true;
38 }
39
ReadUpdaterMessage(const std::string & path,UpdateMessage & boot)40 bool ReadUpdaterMessage(const std::string &path, UpdateMessage &boot)
41 {
42 char *realPath = realpath(path.c_str(), NULL);
43 UPDATER_FILE_CHECK(realPath != nullptr, "realPath is NULL", return false);
44 FILE* fp = fopen(realPath, "rb");
45 free(realPath);
46 UPDATER_FILE_CHECK(fp != nullptr, "ReadUpdaterMessage fopen failed", return false);
47
48 struct UpdateMessage tempBoot {};
49 size_t ret = fread(&tempBoot, sizeof(UpdateMessage), 1, fp);
50 UPDATER_FILE_CHECK(ret == 1, "ReadUpdaterMessage fwrite failed", fclose(fp); return false);
51
52 ret = fclose(fp);
53 UPDATER_FILE_CHECK(ret == 0, "ReadUpdaterMessage fclose failed", return false);
54 UPDATER_FILE_CHECK(!memcpy_s(&boot, sizeof(UpdateMessage), &tempBoot, sizeof(UpdateMessage)),
55 "ReadUpdaterMessage memcpy failed", return false);
56 return true;
57 }
58 } // updater
59