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 22 #include "parcel.h" 23 #include "uri.h" 24 25 namespace OHOS { 26 namespace FileAccessFwk { 27 /** 28 * Indicates the type of the device. 29 */ 30 constexpr int32_t DEVICE_LOCAL_DISK = 1; // Local c,d... disk 31 constexpr int32_t DEVICE_SHARED_DISK = 2; // Multi-user shared disk 32 constexpr int32_t DEVICE_SHARED_TERMINAL = 3; // Distributed networking terminal device 33 constexpr int32_t DEVICE_NETWORK_NEIGHBORHOODS = 4; // Network neighbor device 34 constexpr int32_t DEVICE_EXTERNAL_MTP = 5; // MTP device 35 constexpr int32_t DEVICE_EXTERNAL_USB = 6; // USB device 36 constexpr int32_t DEVICE_EXTERNAL_CLOUD = 7; // Cloud disk device 37 38 /** 39 * Indicates the supported capabilities of the device. 40 */ 41 const uint32_t DEVICE_FLAG_SUPPORTS_READ = 1; 42 const uint32_t DEVICE_FLAG_SUPPORTS_WRITE = 1 << 1; 43 44 /** 45 * Indicates the supported capabilities of the file or directory. 46 */ 47 const uint32_t DOCUMENT_FLAG_REPRESENTS_FILE = 1; 48 const uint32_t DOCUMENT_FLAG_REPRESENTS_DIR = 1 << 1; 49 const uint32_t DOCUMENT_FLAG_SUPPORTS_READ = 1 << 2; 50 const uint32_t DOCUMENT_FLAG_SUPPORTS_WRITE = 1 << 3; 51 52 struct FileInfo : public virtual OHOS::Parcelable { 53 public: 54 std::string uri { "" }; 55 std::string fileName { "" }; 56 int32_t mode { 0 }; 57 int64_t size { 0 }; 58 int64_t mtime { 0 }; 59 std::string mimeType { "" }; 60 61 FileInfo() = default; FileInfoFileInfo62 FileInfo(std::string uri, std::string fileName, int32_t mode, std::string mimeType) 63 : uri(uri), fileName(fileName), mode(mode), mimeType(mimeType) 64 {} 65 ReadFromParcelFileInfo66 bool ReadFromParcel(Parcel &parcel) 67 { 68 uri = parcel.ReadString(); 69 fileName = parcel.ReadString(); 70 mode = parcel.ReadInt32(); 71 size = parcel.ReadInt64(); 72 mtime = parcel.ReadInt64(); 73 mimeType = parcel.ReadString(); 74 return true; 75 } 76 MarshallingFileInfo77 virtual bool Marshalling(Parcel &parcel) const override 78 { 79 if (!parcel.WriteString(uri)) { 80 return false; 81 } 82 if (!parcel.WriteString(fileName)) { 83 return false; 84 } 85 if (!parcel.WriteInt32(mode)) { 86 return false; 87 } 88 if (!parcel.WriteInt64(size)) { 89 return false; 90 } 91 if (!parcel.WriteInt64(mtime)) { 92 return false; 93 } 94 if (!parcel.WriteString(mimeType)) { 95 return false; 96 } 97 return true; 98 } 99 UnmarshallingFileInfo100 static FileInfo *Unmarshalling(Parcel &parcel) 101 { 102 FileInfo *info = new (std::nothrow) FileInfo(); 103 if (info == nullptr) { 104 return nullptr; 105 } 106 107 if (!info->ReadFromParcel(parcel)) { 108 delete info; 109 info = nullptr; 110 } 111 return info; 112 } 113 }; 114 115 struct RootInfo : public virtual OHOS::Parcelable { 116 public: 117 int32_t deviceType { 0 }; 118 std::string uri { "" }; 119 std::string displayName { "" }; 120 int32_t deviceFlags { 0 }; 121 ReadFromParcelRootInfo122 bool ReadFromParcel(Parcel &parcel) 123 { 124 deviceType = parcel.ReadInt32(); 125 uri = parcel.ReadString(); 126 displayName = parcel.ReadString(); 127 deviceFlags = parcel.ReadInt32(); 128 return true; 129 } 130 MarshallingRootInfo131 virtual bool Marshalling(Parcel &parcel) const override 132 { 133 if (!parcel.WriteInt32(deviceType)) { 134 return false; 135 } 136 if (!parcel.WriteString(uri)) { 137 return false; 138 } 139 if (!parcel.WriteString(displayName)) { 140 return false; 141 } 142 if (!parcel.WriteInt32(deviceFlags)) { 143 return false; 144 } 145 return true; 146 } 147 UnmarshallingRootInfo148 static RootInfo *Unmarshalling(Parcel &parcel) 149 { 150 RootInfo *info = new (std::nothrow) RootInfo(); 151 if (info == nullptr) { 152 return nullptr; 153 } 154 155 if (!info->ReadFromParcel(parcel)) { 156 delete info; 157 info = nullptr; 158 } 159 return info; 160 } 161 }; 162 } // namespace FileAccessFwk 163 } // namespace OHOS 164 #endif // FILE_ACCESS_EXTENSION_INFO_H