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