• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "copy/ipc_wrapper.h"
17 
18 #include <sys/stat.h>
19 #include <filesystem>
20 
21 #include "dfs_error.h"
22 #include "securec.h"
23 #include "utils_log.h"
24 
25 #undef LOG_DOMAIN
26 #undef LOG_TAG
27 #define LOG_DOMAIN 0xD004315
28 #define LOG_TAG "distributedfile_daemon"
29 
30 namespace OHOS {
31 namespace Storage {
32 namespace DistributedFile {
33 constexpr size_t MAX_IPC_RAW_DATA_SIZE = 128 * 1024 * 1024;
34 
WriteUriByRawData(MessageParcel & data,const std::vector<std::string> & uriVec)35 bool IpcWrapper::WriteUriByRawData(MessageParcel &data, const std::vector<std::string> &uriVec)
36 {
37     MessageParcel tempParcel;
38     tempParcel.SetMaxCapacity(MAX_IPC_RAW_DATA_SIZE);
39     if (!tempParcel.WriteStringVector(uriVec)) {
40         LOGE("Write uris failed");
41         return false;
42     }
43     size_t dataSize = tempParcel.GetDataSize();
44     if (!data.WriteInt32(static_cast<int32_t>(dataSize))) {
45         LOGE("Write data size failed");
46         return false;
47     }
48     if (!data.WriteRawData(reinterpret_cast<uint8_t *>(tempParcel.GetData()), dataSize)) {
49         LOGE("Write raw data failed");
50         return false;
51     }
52     return true;
53 }
54 
WriteBatchUris(MessageParcel & data,const std::vector<std::string> & uriVec)55 bool IpcWrapper::WriteBatchUris(MessageParcel &data, const std::vector<std::string> &uriVec)
56 {
57     if (!data.WriteUint32(uriVec.size())) {
58         LOGE("Write uri size failed");
59         return false;
60     }
61     if (!WriteUriByRawData(data, uriVec)) {
62         LOGE("Write uri by raw data failed");
63         return false;
64     }
65     return true;
66 }
67 
GetData(void * & buffer,size_t size,const void * data)68 bool IpcWrapper::GetData(void *&buffer, size_t size, const void *data)
69 {
70     if (data == nullptr) {
71         LOGE("null data");
72         return false;
73     }
74     if (size == 0 || size > MAX_IPC_RAW_DATA_SIZE) {
75         LOGE("size invalid: %{public}zu", size);
76         return false;
77     }
78     buffer = malloc(size);
79     if (buffer == nullptr) {
80         LOGE("malloc buffer failed");
81         return false;
82     }
83     if (memcpy_s(buffer, size, data, size) != FileManagement::E_OK) {
84         free(buffer);
85         LOGE("memcpy failed");
86         return false;
87     }
88     return true;
89 }
90 
ReadBatchUriByRawData(MessageParcel & data,std::vector<std::string> & uriVec)91 bool IpcWrapper::ReadBatchUriByRawData(MessageParcel &data, std::vector<std::string> &uriVec)
92 {
93     size_t dataSize = static_cast<size_t>(data.ReadInt32());
94     if (dataSize == 0) {
95         LOGE("parcel no data");
96         return false;
97     }
98 
99     void *buffer = nullptr;
100     if (!GetData(buffer, dataSize, data.ReadRawData(dataSize))) {
101         LOGE("read raw data failed: %{public}zu", dataSize);
102         return false;
103     }
104 
105     MessageParcel tempParcel;
106     if (!tempParcel.ParseFrom(reinterpret_cast<uintptr_t>(buffer), dataSize)) {
107         LOGE("failed to parseFrom");
108         return false;
109     }
110     tempParcel.ReadStringVector(&uriVec);
111     return true;
112 }
113 
ReadBatchUris(MessageParcel & data,std::vector<std::string> & uriVec)114 int32_t IpcWrapper::ReadBatchUris(MessageParcel &data, std::vector<std::string> &uriVec)
115 {
116     uint32_t size = data.ReadUint32();
117     if (size == 0) {
118         LOGE("out of range: %{public}u", size);
119         return OHOS::FileManagement::E_INVAL_ARG;
120     }
121     if (!ReadBatchUriByRawData(data, uriVec)) {
122         LOGE("read uris failed");
123         return OHOS::FileManagement::E_IPC_READ_FAILED;
124     }
125     if (size != uriVec.size()) {
126         LOGE("size read err, except: %{public}u, actual: %{public}zu", size, uriVec.size());
127         return OHOS::FileManagement::E_IPC_READ_FAILED;
128     }
129     return FileManagement::E_OK;
130 }
131 } // namespace DistributedFile
132 } // namespace Storage
133 } // namespace OHOS
134