1 /*
2 * Copyright (c) 2022-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 "uri_handler.h"
16
17 #include "pasteboard_hilog.h"
18 #include "paste_data.h"
19
20 namespace OHOS::MiscServices {
GetRealPath(const std::string & inOriPath,std::string & outRealPath)21 bool UriHandler::GetRealPath(const std::string &inOriPath, std::string &outRealPath)
22 {
23 char realPath[PATH_MAX + 1] = { 0x00 };
24 if (inOriPath.size() > PATH_MAX || realpath(inOriPath.c_str(), realPath) == nullptr) {
25 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "get real path failed, len = %{public}zu.", inOriPath.size());
26 return false;
27 }
28 outRealPath = std::string(realPath);
29 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "file path:%{public}s", outRealPath.c_str());
30 return true;
31 }
32
IsFile(const std::string & uri) const33 bool UriHandler::IsFile(const std::string &uri) const
34 {
35 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "uri:%{public}s", uri.c_str());
36 if (uri.empty()) {
37 return false;
38 }
39 struct stat fileInfo {
40 };
41 if (stat(uri.c_str(), &fileInfo) == 0 && (fileInfo.st_mode & S_IFREG)) {
42 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "valid uri");
43 return true;
44 }
45 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "not file");
46 return false;
47 }
ToFd(const std::string & uri,bool isClient)48 int32_t UriHandler::ToFd(const std::string &uri, bool isClient)
49 {
50 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "uri: %{public}s", uri.c_str());
51 std::string fileRealPath;
52 if (!GetRealPath(uri, fileRealPath)) {
53 return INVALID_FD;
54 }
55 if (!IsFile(fileRealPath)) {
56 return INVALID_FD;
57 }
58 if (!isClient && (PasteData::sharePath.size() == 0 || fileRealPath.rfind(PasteData::sharePath, 0) != 0)) {
59 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "uri error, sharePath: %{public}s, fileRealPath: %{public}s",
60 PasteData::sharePath.c_str(), fileRealPath.c_str());
61 return INVALID_FD;
62 }
63
64 int32_t fd = open(fileRealPath.c_str(), O_RDONLY);
65 if (fd < 0) {
66 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "open file failed, maybe its not a legal file path %{public}s",
67 fileRealPath.c_str());
68 }
69 return fd;
70 }
71
ReleaseFd(int32_t fd)72 void UriHandler::ReleaseFd(int32_t fd)
73 {
74 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "close fd: %{public}d", fd);
75 if (fd >= 0) {
76 close(fd);
77 }
78 }
IsPaste() const79 bool UriHandler::IsPaste() const
80 {
81 return isPaste_;
82 }
83 } // namespace OHOS::MiscServices