• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef STORAGE_DAEMON_KEY_BACKUP_H
17 #define STORAGE_DAEMON_KEY_BACKUP_H
18 
19 #include <sys/stat.h>
20 
21 #include "base_key.h"
22 
23 namespace OHOS {
24 namespace StorageDaemon {
25 struct FileAttr {
26     uid_t uid;
27     gid_t gid;
28     mode_t mode;
29 };
30 
31 class KeyBackup {
32 public:
GetInstance()33     static KeyBackup &GetInstance()
34     {
35         static KeyBackup instance;
36         return instance;
37     }
38 
39     void CreateBackup(const std::string &from, const std::string &to, bool removeOld = true);
40     int32_t RemoveNode(const std::string &pathName);
41     int32_t TryRestoreKey(const std::shared_ptr<BaseKey> &baseKey, const UserAuth &auth);
42     int32_t TryRestoreUeceKey(const std::shared_ptr<BaseKey> &baseKey,
43                               const UserAuth &auth,
44                               KeyBlob &planKey,
45                               KeyBlob &decryptedKey);
46     int32_t GetBackupDir(std::string &origDir, std::string &backupDir);
47     void ListAndCheckDir(std::string &origDir);
48 
49 private:
KeyBackup()50     KeyBackup() {};
~KeyBackup()51     ~KeyBackup() {};
52     KeyBackup(const KeyBackup &) = delete;
53     KeyBackup &operator=(const KeyBackup &) = delete;
54 
55     void FsyncFile(const std::string &dirName);
56     int32_t MkdirParent(const std::string &pathName, mode_t mode);
57     int32_t MkdirParentWithRetry(const std::string &pathName, mode_t mode);
58     void CleanFile(const std::string &path);
59     void CheckAndCopyFiles(const std::string &from, const std::string &to);
60     int32_t CheckAndCopyOneFile(const std::string &from, const std::string &to);
61     bool ReadFileToString(const std::string &filePath, std::string &content);
62     bool GetRealPath(const std::string &path, std::string &realPath);
63     bool WriteStringToFd(int fd, const std::string &content);
64     bool WriteStringToFile(const std::string &payload, const std::string &fileName);
65     int32_t CompareFile(const std::string &fileA, const std::string fileB);
66     int32_t GetAttr(const std::string &path, struct FileAttr &attr);
67     int32_t SetAttr(const std::string &path, struct FileAttr &attr);
68     int32_t HandleCopyDir(const std::string &from, const std::string &to);
69     void CheckAndFixFiles(const std::string &from, const std::string &to);
70     int32_t GetFileList(const std::string &origDir, const std::string &backDir,
71         std::vector<struct FileNode> &fileListm, uint32_t diffNum);
72     void AddOrigFileToList(const std::string &fileName, const std::string &origDir,
73         std::vector<struct FileNode> &fileList);
74     void AddBackupFileToList(const std::string &fileName, const std::string &backDir,
75         std::vector<struct FileNode> &fileList);
76     uint32_t GetDiffFilesNum(const std::vector<struct FileNode> &fileList);
77     int32_t CopySameFilesToTempDir(const std::string &backupDir, std::string &tempDir,
78         std::vector<struct FileNode> &fileList);
79     int32_t CreateTempDirForMixFiles(const std::string &backupDir, std::string &tempDir);
80     uint32_t GetLoopMaxNum(uint32_t diffNum);
81     int32_t CopyMixFilesToTempDir(uint32_t diffNum, uint32_t num, const std::string &tempDir,
82         const std::vector<struct FileNode> &fileList);
83     bool IsRegFile(const std::string &filePath);
84     int32_t DoResotreKeyMix(std::shared_ptr<BaseKey> &baseKey, const UserAuth &auth, const std::string &keyDir,
85         const std::string &backupDir);
86 
87 private:
88     constexpr static mode_t DEFAULT_DIR_PERM = 0700;
89     constexpr static mode_t DEFAULT_WRITE_FILE_PERM = 0644;
90     constexpr static uint32_t MAX_FILE_NUM = 5;
91 };
92 } // namespace StorageDaemon
93 } // namespace OHOS
94 
95 #endif // STORAGE_DAEMON_KEY_BACKUP_H
96