• 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 #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     if (stat(uri.c_str(), &fileInfo) == 0 && (fileInfo.st_mode & S_IFREG)) {
41         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_CLIENT, "valid uri");
42         return true;
43     }
44     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "not file");
45     return false;
46 }
ToFd(const std::string & uri,bool isClient)47 int32_t UriHandler::ToFd(const std::string &uri, bool isClient)
48 {
49     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "uri: %{public}s", uri.c_str());
50     std::string fileRealPath;
51     if (!GetRealPath(uri, fileRealPath)) {
52         return INVALID_FD;
53     }
54     if (!IsFile(fileRealPath)) {
55         return INVALID_FD;
56     }
57     if (!isClient && (PasteData::sharePath.size() == 0 || fileRealPath.rfind(PasteData::sharePath, 0) != 0)) {
58         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_CLIENT, "uri error, sharePath: %{public}s, fileRealPath: %{public}s",
59             PasteData::sharePath.c_str(), fileRealPath.c_str());
60         return INVALID_FD;
61     }
62 
63     int32_t fd = open(fileRealPath.c_str(), O_RDONLY);
64     if (fd < 0) {
65         PASTEBOARD_HILOGD(PASTEBOARD_MODULE_CLIENT, "open file failed, maybe its not a legal file path %{public}s",
66             fileRealPath.c_str());
67     }
68     return fd;
69 }
70 
ReleaseFd(int32_t fd)71 void UriHandler::ReleaseFd(int32_t fd)
72 {
73     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "close fd: %{public}d", fd);
74     if (fd >= 0) {
75         close(fd);
76     }
77 }
IsPaste() const78 bool UriHandler::IsPaste() const
79 {
80     return isPaste_;
81 }
82 } // namespace OHOS::MiscServices