• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 FILE_ACCESS_EXTENSION_INFO_H
17 #define FILE_ACCESS_EXTENSION_INFO_H
18 
19 #include <bitset>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "parcel.h"
24 #include "uri.h"
25 
26 namespace OHOS {
27 namespace FileAccessFwk {
28 //Properties of the Common file
29 const std::string DISPLAY_NAME = "display_name";
30 const std::string RELATIVE_PATH = "relative_path";
31 const std::string FILE_SIZE = "size";
32 const std::string DATE_MODIFIED = "date_modified";
33 const std::string DATE_ADDED = "date_added";
34 //Properties of the picture file
35 const std::string HEIGHT = "height";
36 const std::string WIDTH = "width";
37 //Properties of an image or audio file
38 const std::string DURATION = "duration";
39 
40 const std::string FILE_DATA_ATIME = "atime";
41 const std::string FILE_DATA_MTIME = "mtime";
42 /**
43  * Indicates the type of the device.
44  */
45 constexpr int32_t DEVICE_LOCAL_DISK = 1;                // Local c,d... disk
46 constexpr int32_t DEVICE_SHARED_DISK = 2;               // Multi-user shared disk
47 constexpr int32_t DEVICE_SHARED_TERMINAL = 3;           // Distributed networking terminal device
48 constexpr int32_t DEVICE_NETWORK_NEIGHBORHOODS = 4;     // Network neighbor device
49 constexpr int32_t DEVICE_EXTERNAL_MTP = 5;              // MTP device
50 constexpr int32_t DEVICE_EXTERNAL_USB = 6;              // USB device
51 constexpr int32_t DEVICE_EXTERNAL_CLOUD = 7;            // Cloud disk device
52 
53 /**
54  * Indicates the supported capabilities of the device.
55  */
56 const uint32_t DEVICE_FLAG_SUPPORTS_READ = 1;
57 const uint32_t DEVICE_FLAG_SUPPORTS_WRITE = 1 << 1;
58 
59 /**
60  * Indicates the supported capabilities of the file or directory.
61  */
62 const uint32_t DOCUMENT_FLAG_REPRESENTS_FILE = 1;
63 const uint32_t DOCUMENT_FLAG_REPRESENTS_DIR = 1 << 1;
64 const uint32_t DOCUMENT_FLAG_SUPPORTS_READ = 1 << 2;
65 const uint32_t DOCUMENT_FLAG_SUPPORTS_WRITE = 1 << 3;
66 
67 /**
68  * Indicators of the supported capabilities of the file descriptor.
69  */
70 constexpr int32_t READ = 0;
71 constexpr int32_t WRITE = 1;
72 constexpr int32_t WRITE_READ = 2;
73 
74 /**
75  * Indicates the supported Event change type.
76  */
77 constexpr int32_t ADD_EVENT = 0;
78 constexpr int32_t DELETE_EVENT = 1;
79 constexpr int32_t MOVED_TO = 2;
80 constexpr int32_t MOVED_FROM = 3;
81 constexpr int32_t MOVED_SELF = 4;
82 
83 struct FileInfo : public virtual OHOS::Parcelable {
84 public:
85     std::string uri { "" };
86     std::string relativePath { "" };
87     std::string fileName { "" };
88     int32_t mode { 0 };
89     int64_t size { 0 };
90     int64_t mtime { 0 };
91     std::string mimeType { "" };
92 
93     FileInfo() = default;
FileInfoFileInfo94     FileInfo(std::string uri, std::string relativePath, std::string fileName, int32_t mode, std::string mimeType)
95         : uri(uri), relativePath(relativePath), fileName(fileName), mode(mode), mimeType(mimeType)
96     {}
97 
ReadFromParcelFileInfo98     bool ReadFromParcel(Parcel &parcel)
99     {
100         uri = parcel.ReadString();
101         relativePath = parcel.ReadString();
102         fileName = parcel.ReadString();
103         mode = parcel.ReadInt32();
104         size = parcel.ReadInt64();
105         mtime = parcel.ReadInt64();
106         mimeType = parcel.ReadString();
107         return true;
108     }
109 
MarshallingFileInfo110     virtual bool Marshalling(Parcel &parcel) const override
111     {
112         if (!parcel.WriteString(uri)) {
113             return false;
114         }
115         if (!parcel.WriteString(relativePath)) {
116             return false;
117         }
118         if (!parcel.WriteString(fileName)) {
119             return false;
120         }
121         if (!parcel.WriteInt32(mode)) {
122             return false;
123         }
124         if (!parcel.WriteInt64(size)) {
125             return false;
126         }
127         if (!parcel.WriteInt64(mtime)) {
128             return false;
129         }
130         if (!parcel.WriteString(mimeType)) {
131             return false;
132         }
133         return true;
134     }
135 
UnmarshallingFileInfo136     static FileInfo *Unmarshalling(Parcel &parcel)
137     {
138         FileInfo *info = new (std::nothrow) FileInfo();
139         if (info == nullptr) {
140             return nullptr;
141         }
142 
143         if (!info->ReadFromParcel(parcel)) {
144             delete info;
145             info = nullptr;
146         }
147         return info;
148     }
149 };
150 
151 struct RootInfo : public virtual OHOS::Parcelable {
152 public:
153     int32_t deviceType { 0 };
154     std::string uri { "" };
155     std::string relativePath { "" };
156     std::string displayName { "" };
157     int32_t deviceFlags { 0 };
158 
ReadFromParcelRootInfo159     bool ReadFromParcel(Parcel &parcel)
160     {
161         deviceType = parcel.ReadInt32();
162         uri = parcel.ReadString();
163         relativePath = parcel.ReadString();
164         displayName = parcel.ReadString();
165         deviceFlags = parcel.ReadInt32();
166         return true;
167     }
168 
MarshallingRootInfo169     virtual bool Marshalling(Parcel &parcel) const override
170     {
171         if (!parcel.WriteInt32(deviceType)) {
172             return false;
173         }
174         if (!parcel.WriteString(uri)) {
175             return false;
176         }
177         if (!parcel.WriteString(relativePath)) {
178             return false;
179         }
180         if (!parcel.WriteString(displayName)) {
181             return false;
182         }
183         if (!parcel.WriteInt32(deviceFlags)) {
184             return false;
185         }
186         return true;
187     }
188 
UnmarshallingRootInfo189     static RootInfo *Unmarshalling(Parcel &parcel)
190     {
191         RootInfo *info = new (std::nothrow) RootInfo();
192         if (info == nullptr) {
193             return nullptr;
194         }
195 
196         if (!info->ReadFromParcel(parcel)) {
197             delete info;
198             info = nullptr;
199         }
200         return info;
201     }
202 };
203 
204 struct ThumbnailSize : public virtual OHOS::Parcelable {
205 public:
206     int32_t width { 0 };
207     int32_t height { 0 };
208 
ReadFromParcelThumbnailSize209     bool ReadFromParcel(Parcel &parcel)
210     {
211         width = parcel.ReadInt32();
212         height = parcel.ReadInt32();
213         return true;
214     }
215 
MarshallingThumbnailSize216     virtual bool Marshalling(Parcel &parcel) const override
217     {
218         if (!parcel.WriteInt32(width)) {
219             return false;
220         }
221         if (!parcel.WriteInt32(height)) {
222             return false;
223         }
224         return true;
225     }
226 
UnmarshallingThumbnailSize227     static ThumbnailSize *Unmarshalling(Parcel &parcel)
228     {
229         ThumbnailSize *size = new (std::nothrow) ThumbnailSize();
230         if (size == nullptr) {
231             return nullptr;
232         }
233 
234         if (!size->ReadFromParcel(parcel)) {
235             delete size;
236             size = nullptr;
237         }
238         return size;
239     }
240 };
241 
242 enum ResultType {
243     STRING_TYPE = 1,
244     INT32_TYPE,
245     INT64_TYPE,
246 };
247 
248 static const std::unordered_map<std::string, ResultType> FILE_RESULT_TYPE = {
249     { DISPLAY_NAME, STRING_TYPE },
250     { RELATIVE_PATH, STRING_TYPE },
251     { FILE_SIZE, INT64_TYPE },
252     { DATE_ADDED, INT64_TYPE },
253     { DATE_MODIFIED, INT64_TYPE },
254     { WIDTH, INT32_TYPE },
255     { HEIGHT, INT32_TYPE },
256     { DURATION, INT32_TYPE },
257 };
258 
259 static const std::unordered_map<std::string, std::string> CONVERT_FILE_COLUMN = {
260     {DATE_ADDED, FILE_DATA_ATIME},
261     {DATE_MODIFIED, FILE_DATA_MTIME}
262 };
263 
264 struct CopyResult : public virtual OHOS::Parcelable {
265 public:
266     std::string sourceUri { "" };
267     std::string destUri { "" };
268     int32_t errCode { 0 };
269     std::string errMsg { "" };
270 
271     CopyResult() = default;
CopyResultCopyResult272     CopyResult(std::string sourceUri, std::string destUri, int32_t errCode, std::string errMsg)
273         : sourceUri(sourceUri), destUri(destUri), errCode(errCode),  errMsg(errMsg)
274     {}
275 
ReadFromParcelCopyResult276     bool ReadFromParcel(Parcel &parcel)
277     {
278         sourceUri = parcel.ReadString();
279         destUri = parcel.ReadString();
280         errCode = parcel.ReadInt32();
281         errMsg = parcel.ReadString();
282         return true;
283     }
284 
MarshallingCopyResult285     virtual bool Marshalling(Parcel &parcel) const override
286     {
287         if (!parcel.WriteString(sourceUri)) {
288             return false;
289         }
290         if (!parcel.WriteString(destUri)) {
291             return false;
292         }
293         if (!parcel.WriteInt32(errCode)) {
294             return false;
295         }
296         if (!parcel.WriteString(errMsg)) {
297             return false;
298         }
299         return true;
300     }
301 
UnmarshallingCopyResult302     static CopyResult *Unmarshalling(Parcel &parcel)
303     {
304         CopyResult *result = new (std::nothrow)CopyResult();
305         if (result == nullptr) {
306             return nullptr;
307         }
308 
309         if (!result->ReadFromParcel(parcel)) {
310             delete result;
311             result = nullptr;
312         }
313         return result;
314     }
315 };
316 } // namespace FileAccessFwk
317 } // namespace OHOS
318 #endif // FILE_ACCESS_EXTENSION_INFO_H
319