• 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 "file_uri.h"
17 
18 #include <unistd.h>
19 
20 #include "uri.h"
21 
22 #include "common_func.h"
23 #include "log.h"
24 #include "sandbox_helper.h"
25 
26 using namespace std;
27 namespace OHOS {
28 namespace AppFileService {
29 namespace ModuleFileUri {
30 const std::string PATH_SHARE = "/data/storage/el2/share";
31 const std::string MODE_RW = "/rw/";
32 const std::string MODE_R = "/r/";
33 const std::string FILE_SCHEME_PREFIX = "file://";
34 const std::string FILE_MANAGER_AUTHORITY = "docs";
35 const std::string MEDIA_AUTHORITY = "media";
GetName()36 string FileUri::GetName()
37 {
38     string sandboxPath = uri_.GetPath();
39     size_t posLast = sandboxPath.find_last_of("/");
40     if (posLast == string::npos) {
41         return "";
42     }
43 
44     if (posLast == sandboxPath.size()) {
45         return "";
46     }
47 
48     return SandboxHelper::Decode(sandboxPath.substr(posLast + 1));
49 }
50 
GetPath()51 string FileUri::GetPath()
52 {
53     string sandboxPath = SandboxHelper::Decode(uri_.GetPath());
54     string bundleName = uri_.GetAuthority();
55     if (bundleName == MEDIA_AUTHORITY && sandboxPath.find(".") != string::npos) {
56         size_t pos = sandboxPath.rfind("/");
57         return sandboxPath.substr(0, pos);
58     }
59 
60     return sandboxPath;
61 }
62 
GetRealPath()63 string FileUri::GetRealPath()
64 {
65     string sandboxPath = SandboxHelper::Decode(uri_.GetPath());
66     string realPath = sandboxPath;
67     string bundleName = uri_.GetAuthority();
68     LOGD("GetRealPath decode path is %{private}s", sandboxPath.c_str());
69     if (bundleName == FILE_MANAGER_AUTHORITY &&
70         access(realPath.c_str(), F_OK) == 0) {
71         return realPath;
72     }
73 
74     if ((bundleName != "") && (bundleName != CommonFunc::GetSelfBundleName())) {
75         realPath = PATH_SHARE + MODE_RW + bundleName + sandboxPath;
76         if (access(realPath.c_str(), F_OK) != 0) {
77             realPath = PATH_SHARE + MODE_R + bundleName + sandboxPath;
78         }
79     }
80     LOGD("GetRealPath real path is %{private}s", realPath.c_str());
81     return realPath;
82 }
83 
ToString()84 string FileUri::ToString()
85 {
86     return uri_.ToString();
87 }
88 
FileUri(const string & uriOrPath)89 FileUri::FileUri(const string &uriOrPath): uri_(
90     (uriOrPath.find(FILE_SCHEME_PREFIX) == 0) ? uriOrPath : CommonFunc::GetUriFromPath(uriOrPath)
91 )
92 {}
93 }
94 }  // namespace AppFileService
95 }  // namespace OHOS