• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "datashare_adapter_impl.h"
17 
18 #include <string>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <climits>
25 
26 #include "nweb_log.h"
27 #include "foundation/ability/ability_base/interfaces/kits/native/uri/include/uri.h"
28 #include "foundation/distributeddatamgr/data_share/interfaces/inner_api/consumer/include/datashare_helper.h"
29 #include "foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h"
30 #include "foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy/include/iservice_registry.h"
31 
32 namespace OHOS::NWeb {
33 
34 constexpr char MEDIALIBRARY_DATA_URI[] = "datashare:///media";
35 // static
GetInstance()36 DatashareAdapterImpl& DatashareAdapterImpl::GetInstance()
37 {
38     static DatashareAdapterImpl instance;
39     return instance;
40 }
41 
OpenDataShareUriForRead(const std::string & uriStr) const42 int DatashareAdapterImpl::OpenDataShareUriForRead(const std::string& uriStr) const
43 {
44     auto sam = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
45     if (sam == nullptr) {
46         WVLOG_E("open datashare uri read, system ability manager is null");
47         return -1;
48     }
49     auto remoteObj = sam->GetSystemAbility(OHOS::STORAGE_MANAGER_MANAGER_ID);
50     if (remoteObj == nullptr) {
51         WVLOG_E("open datashare uri read, remoteObj is null");
52         return -1;
53     }
54     auto dataShareHelper = DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
55     if (dataShareHelper == nullptr) {
56         WVLOG_E("open datashare uri read, dataShareHelper is null");
57         return -1;
58     }
59     Uri uri = Uri(uriStr);
60     return dataShareHelper->OpenFile(uri, "r");
61 }
62 
GetFileDisplayName(const std::string & uriStr)63 std::string DatashareAdapterImpl::GetFileDisplayName(const std::string& uriStr)
64 {
65     int fd = OpenDataShareUriForRead(uriStr);
66     if (fd < 0) {
67         WVLOG_E("fd is invaild ,return null");
68         return "";
69     }
70 
71     char buf[PATH_MAX] = {'\0'};
72     char filePath[PATH_MAX] = {'\0'};
73 
74     int ret = snprintf_s(buf, sizeof(buf), sizeof(buf), "/proc/self/fd/%d", fd);
75     if (ret < 0) {
76         close(fd);
77         WVLOG_E("GetFileDisplayName, snprintf failed with %{public}d", errno);
78         return "";
79     }
80 
81     ret = readlink(buf, filePath, PATH_MAX - 1);
82     if (ret < 0) {
83         close(fd);
84         WVLOG_E("GetFileDisplayName, readlink failed with %{public}d", errno);
85         return "";
86     }
87 
88     close(fd);
89     std::string fileName = filePath;
90     std::size_t firstSlash = fileName.rfind("/");
91     if (firstSlash == fileName.npos) {
92         WVLOG_E("GetFileDisplayName, get error path with %{public}s", fileName.c_str());
93         return "";
94     }
95     fileName = fileName.substr(firstSlash + 1, fileName.size() - firstSlash);
96     return fileName;
97 }
98 }  // namespace OHOS::NWeb
99