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 #ifndef FILEMANAGEMENT_DFS_SERVICE_SOFTBUS_HANDLER_ASSET_H
17 #define FILEMANAGEMENT_DFS_SERVICE_SOFTBUS_HANDLER_ASSET_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <vector>
26 
27 #include "asset/asset_obj.h"
28 #include "third_party/zlib/contrib/minizip/unzip.h"
29 #include "third_party/zlib/contrib/minizip/zip.h"
30 #include "transport/socket.h"
31 #include "transport/trans_type.h"
32 
33 namespace OHOS {
34 namespace Storage {
35 namespace DistributedFile {
36 typedef enum {
37     DFS_ASSET_ROLE_SEND = 0,
38     DFS_ASSET_ROLE_RECV = 1,
39 } DFS_ASSET_ROLE;
40 
41 class SoftBusHandlerAsset {
42 public:
43     SoftBusHandlerAsset();
44     ~SoftBusHandlerAsset();
45     static SoftBusHandlerAsset &GetInstance();
46     void CreateAssetLocalSessionServer();
47     void DeleteAssetLocalSessionServer();
48 
49     int32_t AssetBind(const std::string &dstNetworkId, int32_t &socketId);
50     int32_t AssetSendFile(int32_t socketId, const std::string& sendFile, bool isSingleFile);
51     void closeAssetBind(int32_t socketId);
52     void OnAssetRecvBind(int32_t socketId, const std::string &srcNetWorkId);
53 
54     std::string GetClientInfo(int32_t socketId);
55     void RemoveClientInfo(int32_t socketId);
56     void AddAssetObj(int32_t socketId, const sptr<AssetObj> &assetObj);
57     sptr<AssetObj> GetAssetObj(int32_t socketId);
58     void RemoveAssetObj(int32_t socketId);
59 
60     int32_t GenerateAssetObjInfo(int32_t socketId,
61                                  const std::string &fileName,
62                                  const sptr<AssetObj> &assetObj);
63     std::vector<std::string> GenerateUris(const std::vector<std::string> &fileList,
64                                           const std::string &dstBundleName,
65                                           bool isSingleFile);
66     int32_t CompressFile(const std::vector<std::string> &fileList,
67                          const std::string &relativePath,
68                          const std::string &zipFileName);
69     std::vector<std::string> DecompressFile(const std::string &unZipFileName, const std::string &relativePath);
70     bool MkDirRecurse(const std::string& path, mode_t mode);
71 
72     void RemoveFile(const std::string &path, bool isRemove = true);
73 private:
74     static bool IsSameAccount(const std::string &networkId);
75     std::string GetDstFile(const std::string &file,
76                            const std::string &srcBundleName,
77                            const std::string &dstBundleName,
78                            const std::string &sessionId,
79                            bool isSingleFile);
80     std::string GetLocalNetworkId();
81     int32_t MkDir(const std::string &path, mode_t mode);
82 
83     bool IsDir(const std::string &path);
84     std::string ExtractFile(unzFile unZipFile, const std::string &dir);
85     std::mutex clientInfoMutex_;
86     std::map<int32_t, std::string> clientInfoMap_;
87     std::mutex serverIdMapMutex_;
88     std::map<std::string, int32_t> serverIdMap_;
89     std::mutex assetObjMapMutex_;
90     std::map<int32_t, sptr<AssetObj>> assetObjMap_;
91     std::map<DFS_ASSET_ROLE, ISocketListener> sessionListener_;
92     static inline const std::string SERVICE_NAME {"ohos.storage.distributedfile.daemon"};
93     static inline const std::string ASSET_LOCAL_SESSION_NAME {"DistributedFileService_assetListener"};
94 };
95 
96 } // namespace DistributedFile
97 } // namespace Storage
98 } // namespace OHOS
99 
100 #endif // FILEMANAGEMENT_DFS_SERVICE_SOFTBUS_HANDLER_ASSET_H
101