1 /* 2 * Copyright (c) 2021-2025 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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H 18 19 #include "account_error_no.h" 20 #include "rwlock.h" 21 #include <map> 22 #include <mutex> 23 #include <shared_mutex> 24 #include <string> 25 #include <sys/stat.h> 26 #include <vector> 27 namespace OHOS { 28 namespace AccountSA { 29 /** 30 * @brief file transaction class, this class is multi-thread dangerous. 31 * The status variable would be invalid in multi-thread env. 32 * Read transaction is not supported 33 */ 34 class FileTransaction { 35 friend class AccountFileOperator; 36 public: 37 /** 38 * @brief Init function 39 * @param path file path, should be absolute path. 40 * @param rwlock rwlock for this certain file. 41 */ FileTransaction(std::string path,std::shared_ptr<Utils::RWLock> & rwlock)42 FileTransaction(std::string path, std::shared_ptr<Utils::RWLock> &rwlock) : path_(path), rwlock_(rwlock) {}; 43 virtual ~FileTransaction(); 44 45 ErrCode BeginWriteTransaction(); 46 virtual ErrCode EndTransaction(); 47 ErrCode Rollback(); 48 void ForceUnlock(); 49 50 bool IsTempFileExist(); 51 static bool IsTempFileExist(const std::string &path); 52 std::string GetPath() const; 53 54 protected: 55 ErrCode DeleteTempFile(); 56 ErrCode SwapFileNames(); 57 ErrCode WriteFile(const std::string &content); 58 ErrCode ReadFile(std::string &content); 59 std::string GetTempFilePath() const; 60 static std::string GetTempFilePath(const std::string &path); 61 private: 62 void ReleaseAction(); 63 64 std::string path_; 65 std::shared_ptr<Utils::RWLock> rwlock_; 66 67 bool isOpenTransaction_ = false; 68 bool isWriteSuccessOnce_ = false; 69 }; 70 71 typedef std::shared_ptr<FileTransaction> TransactionShared; 72 73 class AccountFileOperator { 74 public: 75 AccountFileOperator(); 76 virtual ~AccountFileOperator(); 77 78 ErrCode CreateDir(const std::string &path, mode_t mode = S_IRWXU); 79 ErrCode DeleteDirOrFile(const std::string &path); 80 ErrCode DeleteDir(const std::string &path); 81 ErrCode DeleteFile(const std::string &path); 82 ErrCode InputFileByPathAndContent(const std::string &path, const std::string &content); 83 ErrCode GetFileContentByPath(const std::string &path, std::string &content); 84 ErrCode InputFileByPathAndContentWithTransaction(const std::string &path, const std::string &content); 85 bool IsExistFile(const std::string &path); 86 bool IsJsonFormat(const std::string &path); 87 bool IsJsonFileReady(const std::string &path); 88 bool IsExistDir(const std::string &path); 89 ErrCode CheckFileExistence(const std::string &path); 90 #ifdef ENABLE_FILE_WATCHER 91 bool GetValidDeleteFileOperationFlag(const std::string &fileName); 92 void SetValidDeleteFileOperationFlag(const std::string &fileName, bool flag); 93 bool GetValidModifyFileOperationFlag(const std::string &fileName); 94 void SetValidModifyFileOperationFlag(const std::string &fileName, bool flag); 95 #endif // ENABLE_FILE_WATCHER 96 bool SetDirDelFlags(const std::string &dirpath); 97 98 TransactionShared GetFileTransaction(const std::string &path); 99 std::shared_ptr<Utils::RWLock> GetRWLock(const std::string &path); 100 public: 101 mutable std::shared_timed_mutex fileLock_; 102 103 #ifdef ENABLE_FILE_WATCHER 104 private: 105 std::vector<std::string> validModifyFileOperationFlag_; 106 std::vector<std::string> validDeleteFileOperationFlag_; 107 #endif // ENABLE_FILE_WATCHER 108 }; 109 } // namespace AccountSA 110 } // namespace OHOS 111 112 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H 113