1 /*
2 * Copyright (c) 2025 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 "stat_impl.h"
17 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
18 #include <sys/xattr.h>
19 #endif
20 #include "macro.h"
21 #include "uni_error.h"
22
23 namespace OHOS {
24 namespace CJSystemapi {
25 namespace FileFs {
26 using namespace std;
27
28 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
GetLocation()29 RetDataI32 StatImpl::GetLocation()
30 {
31 auto value = static_cast<char*>(malloc(MAX_ATTR_NAME));
32 if (value == nullptr) {
33 return { .code = GetErrorCode(ENOMEM), .data = LOCAL };
34 }
35 ssize_t size = 0;
36 if (fileInfo_->isPath) {
37 size = getxattr(fileInfo_->path.get(), CLOUD_LOCATION_ATTR.c_str(), value, MAX_ATTR_NAME);
38 } else {
39 size = fgetxattr(fileInfo_->fdg->GetFD(), CLOUD_LOCATION_ATTR.c_str(), value, MAX_ATTR_NAME);
40 }
41 Location defaultLocation = LOCAL;
42 if (size <= 0) {
43 if (errno != ENODATA && errno != EOPNOTSUPP) {
44 LOGE("Getxattr value failed, errno is %{public}d", errno);
45 }
46 free(value);
47 return { .code = SUCCESS_CODE, .data = defaultLocation };
48 }
49 std::string location = string(value, static_cast<size_t>(size));
50 free(value);
51 if (!std::all_of(location.begin(), location.end(), ::isdigit)) {
52 LOGE("Getxattr location is not all digit!");
53 return { .code = SUCCESS_CODE, .data = defaultLocation };
54 }
55 defaultLocation = static_cast<Location>(atoi(location.c_str()));
56 return { .code = SUCCESS_CODE, .data = defaultLocation };
57 }
58 #endif
59 }
60 }
61 }
62