1 /* 2 * Copyright (c) 2024-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 MTPFS_TYPE_DIR_H 17 #define MTPFS_TYPE_DIR_H 18 19 #include <mutex> 20 #include <set> 21 #include <string> 22 23 #include "mtpfs_type_basic.h" 24 #include "mtpfs_type_file.h" 25 26 class MtpFsTypeDir : public MtpFsTypeBasic { 27 public: 28 MtpFsTypeDir(); 29 MtpFsTypeDir(uint32_t id, uint32_t parentId, uint32_t storageId, const std::string &name); 30 MtpFsTypeDir(LIBMTP_file_t *file); 31 MtpFsTypeDir(const MtpFsTypeDir ©); 32 EnterCritical()33 void EnterCritical() const 34 { 35 accessMutex_.lock(); 36 } LeaveCritical()37 void LeaveCritical() const 38 { 39 accessMutex_.unlock(); 40 } 41 Clear()42 void Clear() 43 { 44 dirs_.clear(); 45 files_.clear(); 46 } 47 void SetFetched(bool f = true) 48 { 49 fetched_ = f; 50 } IsFetched()51 bool IsFetched() const 52 { 53 return fetched_; 54 } 55 void AddDir(const MtpFsTypeDir &dir); 56 void AddFile(const MtpFsTypeFile &file); 57 bool RemoveDir(const MtpFsTypeDir &dir); 58 bool RemoveFile(const MtpFsTypeFile &file); 59 bool ReplaceFile(const MtpFsTypeFile &oldFile, const MtpFsTypeFile &newFile); 60 DirCount()61 std::set<MtpFsTypeDir>::size_type DirCount() const 62 { 63 return dirs_.size(); 64 } FileCount()65 std::set<MtpFsTypeFile>::size_type FileCount() const 66 { 67 return files_.size(); 68 } 69 const MtpFsTypeDir *Dir(const std::string &name) const; 70 const MtpFsTypeFile *File(const std::string &name) const; Dirs()71 std::set<MtpFsTypeDir> Dirs() const 72 { 73 return dirs_; 74 } Files()75 std::set<MtpFsTypeFile> Files() const 76 { 77 return files_; 78 } IsEmpty()79 bool IsEmpty() const 80 { 81 return dirs_.empty() && files_.empty(); 82 } 83 ModificationDate()84 time_t ModificationDate() const 85 { 86 return modifyDate_; 87 } SetModificationDate(time_t modifyDate)88 void SetModificationDate(time_t modifyDate) 89 { 90 modifyDate_ = modifyDate; 91 } 92 93 LIBMTP_folder_t *ToLIBMTPFolder() const; 94 MtpFsTypeDir &operator = (const MtpFsTypeDir &rhs); 95 bool operator == (const std::string &rhs) const 96 { 97 return MtpFsTypeBasic::operator == (rhs); 98 } 99 bool operator == (const MtpFsTypeDir &rhs) const 100 { 101 return MtpFsTypeBasic::operator == (rhs); 102 } 103 bool operator < (const std::string &rhs) const 104 { 105 return MtpFsTypeBasic::operator < (rhs); 106 } 107 bool operator < (const MtpFsTypeDir &rhs) const 108 { 109 return MtpFsTypeBasic::operator < (rhs); 110 } 111 112 private: 113 mutable std::mutex accessMutex_; 114 bool fetched_; 115 time_t modifyDate_; 116 117 public: 118 std::set<MtpFsTypeDir> dirs_; 119 std::set<MtpFsTypeFile> files_; 120 }; 121 122 #endif // MTPFS_TYPE_DIR_H 123