1 /* 2 * Copyright (c) 2021-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 OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H 17 #define OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H 18 19 #include <mutex> 20 #include <shared_mutex> 21 #include <string> 22 #include <sys/stat.h> 23 #include <vector> 24 25 #include "account_error_no.h" 26 27 namespace OHOS { 28 namespace AccountSA { 29 class AccountFileOperator { 30 public: 31 AccountFileOperator(); 32 virtual ~AccountFileOperator(); 33 34 ErrCode CreateDir(const std::string &path, mode_t mode = S_IRWXU); 35 ErrCode DeleteDirOrFile(const std::string &path); 36 ErrCode DeleteDir(const std::string &path); 37 ErrCode DeleteFile(const std::string &path); 38 ErrCode InputFileByPathAndContent(const std::string &path, const std::string &content); 39 ErrCode WriteFile(const std::string &path, const std::string &content); 40 ErrCode GetFileContentByPath(const std::string &path, std::string &content); 41 bool IsExistFile(const std::string &path); 42 bool IsJsonFormat(const std::string &path); 43 bool IsJsonFileReady(const std::string &path); 44 bool IsExistDir(const std::string &path); 45 ErrCode CheckFileExistence(const std::string &path); 46 #ifdef ENABLE_FILE_WATCHER 47 bool GetValidDeleteFileOperationFlag(const std::string &fileName); 48 void SetValidDeleteFileOperationFlag(const std::string &fileName, bool flag); 49 bool GetValidModifyFileOperationFlag(const std::string &fileName); 50 void SetValidModifyFileOperationFlag(const std::string &fileName, bool flag); 51 #endif // ENABLE_FILE_WATCHER 52 bool SetDirDelFlags(const std::string &dirpath); 53 54 public: 55 mutable std::shared_timed_mutex fileLock_; 56 57 #ifdef ENABLE_FILE_WATCHER 58 private: 59 std::vector<std::string> validModifyFileOperationFlag_; 60 std::vector<std::string> validDeleteFileOperationFlag_; 61 #endif // ENABLE_FILE_WATCHER 62 }; 63 } // namespace AccountSA 64 } // namespace OHOS 65 66 #endif // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H 67