• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "mtpfs_type_dir.h"
17 
MtpFsTypeDir()18 MtpFsTypeDir::MtpFsTypeDir() : MtpFsTypeBasic(), dirs_(), files_(), accessMutex_(), fetched_(false), modifyDate_(0) {}
19 
MtpFsTypeDir(uint32_t id,uint32_t parentId,uint32_t storageId,const std::string & name)20 MtpFsTypeDir::MtpFsTypeDir(uint32_t id, uint32_t parentId, uint32_t storageId, const std::string &name)
21     : MtpFsTypeBasic(id, parentId, storageId, name),
22       dirs_(),
23       files_(),
24       accessMutex_(),
25       fetched_(false),
26       modifyDate_(0)
27 {}
28 
MtpFsTypeDir(LIBMTP_file_t * file)29 MtpFsTypeDir::MtpFsTypeDir(LIBMTP_file_t *file)
30     : MtpFsTypeBasic(file->item_id, file->parent_id, file->storage_id, std::string(file->filename)),
31       dirs_(),
32       files_(),
33       accessMutex_(),
34       fetched_(false),
35       modifyDate_(file->modificationdate)
36 {}
37 
MtpFsTypeDir(const MtpFsTypeDir & copy)38 MtpFsTypeDir::MtpFsTypeDir(const MtpFsTypeDir &copy)
39     : MtpFsTypeBasic(copy),
40       dirs_(copy.dirs_),
41       files_(copy.files_),
42       accessMutex_(),
43       fetched_(copy.fetched_),
44       modifyDate_(copy.modifyDate_)
45 {}
46 
ToLIBMTPFolder() const47 LIBMTP_folder_t *MtpFsTypeDir::ToLIBMTPFolder() const
48 {
49     LIBMTP_folder_t *f = static_cast<LIBMTP_folder_t *>(malloc(sizeof(LIBMTP_folder_t)));
50     if (f == nullptr) {
51         return nullptr;
52     }
53     f->folder_id = id_;
54     f->parent_id = parentId_;
55     f->storage_id = storageId_;
56     f->name = strdup(name_.c_str());
57     f->sibling = nullptr;
58     f->child = nullptr;
59     return f;
60 }
61 
AddDir(const MtpFsTypeDir & dir)62 void MtpFsTypeDir::AddDir(const MtpFsTypeDir &dir)
63 {
64     EnterCritical();
65     dirs_.insert(dir);
66     LeaveCritical();
67 }
68 
AddFile(const MtpFsTypeFile & file)69 void MtpFsTypeDir::AddFile(const MtpFsTypeFile &file)
70 {
71     EnterCritical();
72     files_.insert(file);
73     LeaveCritical();
74 }
75 
RemoveDir(const MtpFsTypeDir & dir)76 bool MtpFsTypeDir::RemoveDir(const MtpFsTypeDir &dir)
77 {
78     EnterCritical();
79     auto it = std::find(dirs_.begin(), dirs_.end(), dir);
80     if (it == dirs_.end()) {
81         LeaveCritical();
82         return false;
83     }
84     dirs_.erase(it);
85     LeaveCritical();
86     return true;
87 }
88 
RemoveFile(const MtpFsTypeFile & file)89 bool MtpFsTypeDir::RemoveFile(const MtpFsTypeFile &file)
90 {
91     EnterCritical();
92     auto it = std::find(files_.begin(), files_.end(), file);
93     if (it == files_.end()) {
94         LeaveCritical();
95         return false;
96     }
97     files_.erase(it);
98     LeaveCritical();
99     return true;
100 }
101 
ReplaceFile(const MtpFsTypeFile & oldFile,const MtpFsTypeFile & newFile)102 bool MtpFsTypeDir::ReplaceFile(const MtpFsTypeFile &oldFile, const MtpFsTypeFile &newFile)
103 {
104     EnterCritical();
105     auto it = std::find(files_.begin(), files_.end(), oldFile);
106     if (it == files_.end()) {
107         LeaveCritical();
108         return false;
109     }
110     files_.erase(it);
111     files_.insert(newFile);
112     LeaveCritical();
113     return true;
114 }
115 
operator =(const MtpFsTypeDir & rhs)116 MtpFsTypeDir &MtpFsTypeDir::operator = (const MtpFsTypeDir &rhs)
117 {
118     MtpFsTypeBasic::operator = (rhs);
119     dirs_ = rhs.dirs_;
120     files_ = rhs.files_;
121     fetched_ = rhs.fetched_;
122     return *this;
123 }
124 
Dir(const std::string & name) const125 const MtpFsTypeDir *MtpFsTypeDir::Dir(const std::string &name) const
126 {
127     EnterCritical();
128     auto it = std::find(dirs_.begin(), dirs_.end(), name);
129     LeaveCritical();
130     if (it == dirs_.end()) {
131         return nullptr;
132     }
133     return &*it;
134 }
135 
File(const std::string & name) const136 const MtpFsTypeFile *MtpFsTypeDir::File(const std::string &name) const
137 {
138     EnterCritical();
139     auto it = std::find(files_.begin(), files_.end(), name);
140     LeaveCritical();
141     if (it == files_.end()) {
142         return nullptr;
143     }
144     return &*it;
145 }
146