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