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 GetFileContentByPath(const std::string &path, std::string &content);
40     bool IsExistFile(const std::string &path);
41     bool IsJsonFormat(const std::string &path);
42     bool IsJsonFileReady(const std::string &path);
43     bool IsExistDir(const std::string &path);
44     bool GetValidDeleteFileOperationFlag(const std::string &fileName);
45     void SetValidDeleteFileOperationFlag(const std::string &fileName, bool flag);
46     bool GetValidModifyFileOperationFlag(const std::string &fileName);
47     void SetValidModifyFileOperationFlag(const std::string &fileName, bool flag);
48     bool SetDirDelFlags(const std::string &dirpath);
49 
50 public:
51     mutable std::shared_timed_mutex fileLock_;
52 
53 private:
54     std::vector<std::string> validModifyFileOperationFlag_;
55     std::vector<std::string> validDeleteFileOperationFlag_;
56 };
57 }  // namespace AccountSA
58 }  // namespace OHOS
59 
60 #endif  // OS_ACCOUNT_SERVICES_ACCOUNTMGR_INCLUDE_ACCOUNT_FILE_OPERATOR_H
61