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 #include "fuse_operations.h"
16
17 #include "cloud_disk_inode.h"
18 #include "file_operations_local.h"
19 #include "utils_log.h"
20
21 namespace OHOS {
22 namespace FileManagement {
23 namespace CloudDisk {
24 using namespace std;
Lookup(fuse_req_t req,fuse_ino_t parent,const char * name)25 void FuseOperations::Lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
26 {
27 struct CloudDiskInode *inoPtr = nullptr;
28 if (parent == FUSE_ROOT_ID) {
29 auto opsPtr = make_shared<FileOperationsLocal>();
30 opsPtr->Lookup(req, parent, name);
31 return;
32 }
33 inoPtr= reinterpret_cast<struct CloudDiskInode*>(parent);
34 inoPtr->ops->Lookup(req, parent, name);
35 }
36
Access(fuse_req_t req,fuse_ino_t ino,int mask)37 void FuseOperations::Access(fuse_req_t req, fuse_ino_t ino, int mask)
38 {
39 struct CloudDiskInode *inoPtr = nullptr;
40 if (ino == FUSE_ROOT_ID) {
41 auto opsPtr = make_shared<FileOperationsLocal>();
42 opsPtr->Access(req, ino, mask);
43 return;
44 }
45 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
46 inoPtr->ops->Access(req, ino, mask);
47 }
48
GetAttr(fuse_req_t req,fuse_ino_t ino,struct fuse_file_info * fi)49 void FuseOperations::GetAttr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
50 {
51 struct CloudDiskInode *inoPtr = nullptr;
52 if (ino == FUSE_ROOT_ID) {
53 auto opsPtr = make_shared<FileOperationsLocal>();
54 opsPtr->GetAttr(req, ino, fi);
55 return;
56 }
57 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
58 inoPtr->ops->GetAttr(req, ino, fi);
59 }
60
Open(fuse_req_t req,fuse_ino_t ino,struct fuse_file_info * fi)61 void FuseOperations::Open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
62 {
63 struct CloudDiskInode *inoPtr = nullptr;
64 if (ino == FUSE_ROOT_ID) {
65 auto opsPtr = make_shared<FileOperationsLocal>();
66 opsPtr->Open(req, ino, fi);
67 return;
68 }
69 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
70 inoPtr->ops->Open(req, ino, fi);
71 }
72
Forget(fuse_req_t req,fuse_ino_t ino,uint64_t nLookup)73 void FuseOperations::Forget(fuse_req_t req, fuse_ino_t ino, uint64_t nLookup)
74 {
75 struct CloudDiskInode *inoPtr = nullptr;
76 if (ino == FUSE_ROOT_ID) {
77 auto opsPtr = make_shared<FileOperationsLocal>();
78 opsPtr->Forget(req, ino, nLookup);
79 return;
80 }
81 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
82 inoPtr->ops->Forget(req, ino, nLookup);
83 }
84
ForgetMulti(fuse_req_t req,size_t count,struct fuse_forget_data * forgets)85 void FuseOperations::ForgetMulti(fuse_req_t req, size_t count, struct fuse_forget_data *forgets)
86 {
87 if (count == 0 || forgets[0].ino == FUSE_ROOT_ID) {
88 return;
89 }
90 struct CloudDiskInode *inoPtr = reinterpret_cast<struct CloudDiskInode*>(forgets[0].ino);
91 inoPtr->ops->ForgetMulti(req, count, forgets);
92 }
93
MkNod(fuse_req_t req,fuse_ino_t parent,const char * name,mode_t mode,dev_t rdev)94 void FuseOperations::MkNod(fuse_req_t req, fuse_ino_t parent, const char *name,
95 mode_t mode, dev_t rdev)
96 {
97 struct CloudDiskInode *inoPtr = nullptr;
98 if (parent == FUSE_ROOT_ID) {
99 auto opsPtr = make_shared<FileOperationsLocal>();
100 opsPtr->MkNod(req, parent, name, mode, rdev);
101 return;
102 }
103 inoPtr = reinterpret_cast<struct CloudDiskInode*>(parent);
104 inoPtr->ops->MkNod(req, parent, name, mode, rdev);
105 }
106
Create(fuse_req_t req,fuse_ino_t parent,const char * name,mode_t mode,struct fuse_file_info * fi)107 void FuseOperations::Create(fuse_req_t req, fuse_ino_t parent, const char *name,
108 mode_t mode, struct fuse_file_info *fi)
109 {
110 struct CloudDiskInode *inoPtr = nullptr;
111 if (parent == FUSE_ROOT_ID) {
112 auto opsPtr = make_shared<FileOperationsLocal>();
113 opsPtr->Create(req, parent, name, mode, fi);
114 return;
115 }
116 inoPtr = reinterpret_cast<struct CloudDiskInode*>(parent);
117 inoPtr->ops->Create(req, parent, name, mode, fi);
118 }
119
ReadDir(fuse_req_t req,fuse_ino_t ino,size_t size,off_t off,struct fuse_file_info * fi)120 void FuseOperations::ReadDir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
121 struct fuse_file_info *fi)
122 {
123 struct CloudDiskInode *inoPtr = nullptr;
124 (void) fi;
125 if (ino == FUSE_ROOT_ID) {
126 auto opsPtr = make_shared<FileOperationsLocal>();
127 opsPtr->ReadDir(req, ino, size, off, fi);
128 return;
129 }
130 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
131 inoPtr->ops->ReadDir(req, ino, size, off, fi);
132 }
133
SetXattr(fuse_req_t req,fuse_ino_t ino,const char * name,const char * value,size_t size,int flags)134 void FuseOperations::SetXattr(fuse_req_t req, fuse_ino_t ino, const char *name,
135 const char *value, size_t size, int flags)
136 {
137 struct CloudDiskInode *inoPtr = nullptr;
138 if (ino == FUSE_ROOT_ID) {
139 auto opsPtr = make_shared<FileOperationsLocal>();
140 opsPtr->SetXattr(req, ino, name, value, size, flags);
141 return;
142 }
143 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
144 inoPtr->ops->SetXattr(req, ino, name, value, size, flags);
145 }
146
GetXattr(fuse_req_t req,fuse_ino_t ino,const char * name,size_t size)147 void FuseOperations::GetXattr(fuse_req_t req, fuse_ino_t ino, const char *name,
148 size_t size)
149 {
150 struct CloudDiskInode *inoPtr = nullptr;
151 if (ino == FUSE_ROOT_ID) {
152 auto opsPtr = make_shared<FileOperationsLocal>();
153 opsPtr->GetXattr(req, ino, name, size);
154 return;
155 }
156 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
157 inoPtr->ops->GetXattr(req, ino, name, size);
158 }
159
MkDir(fuse_req_t req,fuse_ino_t parent,const char * name,mode_t mode)160 void FuseOperations::MkDir(fuse_req_t req, fuse_ino_t parent, const char *name, mode_t mode)
161 {
162 struct CloudDiskInode *inoPtr = nullptr;
163 if (parent == FUSE_ROOT_ID) {
164 auto opsPtr = make_shared<FileOperationsLocal>();
165 opsPtr->MkDir(req, parent, name, mode);
166 return;
167 }
168 inoPtr = reinterpret_cast<struct CloudDiskInode*>(parent);
169 inoPtr->ops->MkDir(req, parent, name, mode);
170 }
171
RmDir(fuse_req_t req,fuse_ino_t parent,const char * name)172 void FuseOperations::RmDir(fuse_req_t req, fuse_ino_t parent, const char *name)
173 {
174 struct CloudDiskInode *inoPtr = nullptr;
175 if (parent == FUSE_ROOT_ID) {
176 auto opsPtr = make_shared<FileOperationsLocal>();
177 opsPtr->RmDir(req, parent, name);
178 return;
179 }
180 inoPtr = reinterpret_cast<struct CloudDiskInode*>(parent);
181 inoPtr->ops->RmDir(req, parent, name);
182 }
183
Unlink(fuse_req_t req,fuse_ino_t parent,const char * name)184 void FuseOperations::Unlink(fuse_req_t req, fuse_ino_t parent, const char *name)
185 {
186 struct CloudDiskInode *inoPtr = nullptr;
187 if (parent == FUSE_ROOT_ID) {
188 auto opsPtr = make_shared<FileOperationsLocal>();
189 opsPtr->Unlink(req, parent, name);
190 return;
191 }
192 inoPtr = reinterpret_cast<struct CloudDiskInode*>(parent);
193 inoPtr->ops->Unlink(req, parent, name);
194 }
195
Rename(fuse_req_t req,fuse_ino_t parent,const char * name,fuse_ino_t newParent,const char * newName,unsigned int flags)196 void FuseOperations::Rename(fuse_req_t req, fuse_ino_t parent, const char *name,
197 fuse_ino_t newParent, const char *newName, unsigned int flags)
198 {
199 struct CloudDiskInode *inoPtr = nullptr;
200 if (parent == FUSE_ROOT_ID) {
201 auto opsPtr = make_shared<FileOperationsLocal>();
202 opsPtr->Rename(req, parent, name, newParent, newName, flags);
203 return;
204 }
205 inoPtr = reinterpret_cast<struct CloudDiskInode*>(parent);
206 inoPtr->ops->Rename(req, parent, name, newParent, newName, flags);
207 }
208
Read(fuse_req_t req,fuse_ino_t ino,size_t size,off_t offset,struct fuse_file_info * fi)209 void FuseOperations::Read(fuse_req_t req, fuse_ino_t ino, size_t size,
210 off_t offset, struct fuse_file_info *fi)
211 {
212 struct CloudDiskInode *inoPtr = nullptr;
213 if (ino == FUSE_ROOT_ID) {
214 auto opsPtr = make_shared<FileOperationsLocal>();
215 opsPtr->Read(req, ino, size, offset, fi);
216 return;
217 }
218 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
219 inoPtr->ops->Read(req, ino, size, offset, fi);
220 }
221
WriteBuf(fuse_req_t req,fuse_ino_t ino,struct fuse_bufvec * bufv,off_t off,struct fuse_file_info * fi)222 void FuseOperations::WriteBuf(fuse_req_t req, fuse_ino_t ino, struct fuse_bufvec *bufv,
223 off_t off, struct fuse_file_info *fi)
224 {
225 struct CloudDiskInode *inoPtr = nullptr;
226 if (ino == FUSE_ROOT_ID) {
227 auto opsPtr = make_shared<FileOperationsLocal>();
228 opsPtr->WriteBuf(req, ino, bufv, off, fi);
229 return;
230 }
231 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
232 inoPtr->ops->WriteBuf(req, ino, bufv, off, fi);
233 }
234
Release(fuse_req_t req,fuse_ino_t ino,struct fuse_file_info * fi)235 void FuseOperations::Release(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi)
236 {
237 struct CloudDiskInode *inoPtr = nullptr;
238 if (ino == FUSE_ROOT_ID) {
239 auto opsPtr = make_shared<FileOperationsLocal>();
240 opsPtr->Release(req, ino, fi);
241 return;
242 }
243 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
244 inoPtr->ops->Release(req, ino, fi);
245 }
SetAttr(fuse_req_t req,fuse_ino_t ino,struct stat * attr,int valid,struct fuse_file_info * fi)246 void FuseOperations::SetAttr(fuse_req_t req, fuse_ino_t ino, struct stat *attr,
247 int valid, struct fuse_file_info *fi)
248 {
249 struct CloudDiskInode *inoPtr = nullptr;
250 if (ino == FUSE_ROOT_ID) {
251 auto opsPtr = make_shared<FileOperationsLocal>();
252 opsPtr->SetAttr(req, ino, attr, valid, fi);
253 return;
254 }
255 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
256 inoPtr->ops->SetAttr(req, ino, attr, valid, fi);
257 }
Lseek(fuse_req_t req,fuse_ino_t ino,off_t off,int whence,struct fuse_file_info * fi)258 void FuseOperations::Lseek(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
259 struct fuse_file_info *fi)
260 {
261 struct CloudDiskInode *inoPtr = nullptr;
262 if (ino == FUSE_ROOT_ID) {
263 auto opsPtr = make_shared<FileOperationsLocal>();
264 opsPtr->Lseek(req, ino, off, whence, fi);
265 return;
266 }
267 inoPtr = reinterpret_cast<struct CloudDiskInode*>(ino);
268 inoPtr->ops->Lseek(req, ino, off, whence, fi);
269 }
270 } // namespace CloudDisk
271 } // namespace FileManagement
272 } // namespace OHOS
273