• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "remote_uri.h"
17 #include <string>
18 #include <fcntl.h>
19 #include <tuple>
20 #include <unistd.h>
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "hap_token_info.h"
24 
25 namespace OHOS {
26 namespace DistributedFS {
27 namespace ModuleRemoteUri {
28 
29 using namespace std;
30 
IsMediaUri(const string & uriString)31 bool RemoteUri::IsMediaUri(const string &uriString)
32 {
33     RemoteUri remoteUri = RemoteUri(uriString);
34     string scheme = remoteUri.GetScheme();
35     string path = remoteUri.GetPath();
36     std::size_t len = MEDIA.length();
37     if (path.length() > len) {
38         string media = path.substr(0, len);
39         return scheme == SCHEME && media == MEDIA;
40     }
41     return false;
42 }
43 
IsFileUri(const string & uriString)44 bool RemoteUri::IsFileUri(const string &uriString)
45 {
46     return RemoteUri(uriString).GetScheme() == SCHEME_FILE;
47 }
48 
IsAllDigits(string fdStr)49 static bool IsAllDigits(string fdStr)
50 {
51     for (size_t i = 0; i < fdStr.size(); i++) {
52         if (!isdigit(fdStr[i])) {
53             return false;
54         }
55     }
56     return true;
57 }
58 
GetCallingPkgName()59 static string GetCallingPkgName()
60 {
61     uint32_t pid = IPCSkeleton::GetCallingTokenID();
62     Security::AccessToken::HapTokenInfo tokenInfo = Security::AccessToken::HapTokenInfo();
63     Security::AccessToken::AccessTokenKit::GetHapTokenInfo(pid, tokenInfo);
64     return tokenInfo.bundleName;
65 }
66 
RemoveFd(int fd)67 void RemoteUri::RemoveFd(int fd)
68 {
69     auto iter = fdFromBinder.find(fd);
70     if (iter != fdFromBinder.end()) {
71         fdFromBinder.erase(iter);
72     }
73 }
74 
IsRemoteUri(const string & path,int & fd,const int & flags)75 bool RemoteUri::IsRemoteUri(const string& path, int &fd, const int& flags)
76 {
77     string::size_type posDatashare = path.find(SCHEME_TAG);
78     string::size_type posFragment = path.find(FRAGMENT_TAG);
79     string::size_type posFd = path.find(FD_TAG);
80     if (posDatashare == string::npos || posFragment == string::npos ||
81         posFd == string::npos) {
82         return false;
83     }
84 
85     string scheme = path.substr(0, posDatashare);
86     if (scheme != SCHEME) {
87         return false;
88     }
89 
90     string fragment = path.substr(posFragment + 1, REMOTE_URI_TAG.size());
91     if (fragment == REMOTE_URI_TAG) {
92         string fdStr = path.substr(posFd + 1);
93         if (IsAllDigits(fdStr)) {
94             fd = stoi(fdStr.c_str());
95             if (fd < 0 || flags != O_RDONLY) {
96                 fd = -1;
97             }
98             RemoveFd(fd);
99             return true;
100         }
101         fd = -1;
102         return true;
103     }
104     return false;
105 }
106 
ConvertUri(const int & fd,string & remoteUri)107 int RemoteUri::ConvertUri(const int &fd, string &remoteUri)
108 {
109     if (fd < 0) {
110         return -EINVAL;
111     }
112 
113     if (fdFromBinder.size() == MAX_URI_SIZE) {
114         close(*fdFromBinder.begin());
115         fdFromBinder.erase(fdFromBinder.begin());
116     }
117     fdFromBinder.emplace(fd);
118 
119     string pkgName = GetCallingPkgName();
120     remoteUri = SCHEME + ":///" + pkgName + "/" + FRAGMENT_TAG +
121                             REMOTE_URI_TAG + FD_TAG + to_string(fd);
122     return 0;
123 }
124 
OpenRemoteUri(const string & remoteUri)125 int RemoteUri::OpenRemoteUri(const string &remoteUri)
126 {
127     int fd = -1;
128     (void)IsRemoteUri(remoteUri, fd);
129 
130     return fd;
131 }
132 } // namespace ModuleRemoteUri
133 } // namespace DistributedFS
134 } // namespace OHOS
135