1 /*
2  * Copyright (c) 2023 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 DLP_LINK_FILE_H
17 #define DLP_LINK_FILE_H
18 
19 #include <mutex>
20 #include <string>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include "dlp_file.h"
26 #include "rwlock.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace DlpPermission {
31 typedef struct DlpLinkFileInfo {
32     std::string dlpLinkName;
33     struct stat fileStat;
34 } DlpLinkFileInfo;
35 
36 class DlpLinkFile final {
37 public:
38     DlpLinkFile(std::string dlpLinkName, std::shared_ptr<DlpFile> dlpFile);
39     ~DlpLinkFile();
40     bool SubAndCheckZeroRef(int ref);
41     void IncreaseRef();
42     struct stat GetLinkStat();
43     void UpdateAtimeStat();
44     void UpdateMtimeStat();
45     int32_t Write(uint32_t offset, void* buf, uint32_t size);
46     int32_t Read(uint32_t offset, void* buf, uint32_t size, uint32_t uid);
GetDlpFilePtr()47     std::shared_ptr<DlpFile> GetDlpFilePtr()
48     {
49         return dlpFile_;
50     };
51 
setDlpFilePtr(std::shared_ptr<DlpFile> dlpFile)52     void setDlpFilePtr(std::shared_ptr<DlpFile> dlpFile)
53     {
54         dlpFile_ = dlpFile;
55     };
56 
GetLinkName()57     std::string& GetLinkName()
58     {
59         return dlpLinkName_;
60     }
61 
62     int32_t Truncate(uint32_t modifySize);
63 
stopLink()64     void stopLink()
65     {
66         stopLinkFlag_ = true;
67     };
68 
restartLink()69     void restartLink()
70     {
71         stopLinkFlag_ = false;
72     };
73 
GetFileStat()74     struct stat GetFileStat()
75     {
76         return fileStat_;
77     };
78 
79 private:
80     std::string dlpLinkName_;
81     std::shared_ptr<DlpFile> dlpFile_;
82     struct stat fileStat_;
83     std::atomic<int> refcount_;
84     std::mutex refLock_;
85     bool stopLinkFlag_;
86     bool hasRead_;
87 };
88 }  // namespace DlpPermission
89 }  // namespace Security
90 }  // namespace OHOS
91 
92 #endif
93