• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fs_stat.h"
17 
18 #include <cstdio>
19 #include <cstdlib>
20 #include <cstring>
21 #include <memory>
22 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
23 #include <sys/xattr.h>
24 #endif
25 
26 #include "file_utils.h"
27 #include "filemgmt_libhilog.h"
28 
29 namespace OHOS::FileManagement::ModuleFileIO {
30 using namespace std;
31 
CheckStatMode(mode_t mode)32 bool FsStat::CheckStatMode(mode_t mode)
33 {
34     return (entity->stat_.st_mode & S_IFMT) == mode;
35 }
36 
IsBlockDevice()37 bool FsStat::IsBlockDevice()
38 {
39     return CheckStatMode(S_IFBLK);
40 }
41 
IsCharacterDevice()42 bool FsStat::IsCharacterDevice()
43 {
44     return CheckStatMode(S_IFCHR);
45 }
46 
IsDirectory()47 bool FsStat::IsDirectory()
48 {
49     return CheckStatMode(S_IFDIR);
50 }
51 
IsFIFO()52 bool FsStat::IsFIFO()
53 {
54     return CheckStatMode(S_IFIFO);
55 }
56 
IsFile()57 bool FsStat::IsFile()
58 {
59     return CheckStatMode(S_IFREG);
60 }
61 
IsSocket()62 bool FsStat::IsSocket()
63 {
64     return CheckStatMode(S_IFSOCK);
65 }
66 
IsSymbolicLink()67 bool FsStat::IsSymbolicLink()
68 {
69     return CheckStatMode(S_IFLNK);
70 }
71 
GetIno()72 int64_t FsStat::GetIno()
73 {
74     return entity->stat_.st_ino;
75 }
76 
GetMode()77 int64_t FsStat::GetMode()
78 {
79     return entity->stat_.st_mode & S_PERMISSION;
80 }
81 
GetUid()82 int64_t FsStat::GetUid()
83 {
84     return entity->stat_.st_uid;
85 }
86 
GetGid()87 int64_t FsStat::GetGid()
88 {
89     return entity->stat_.st_gid;
90 }
91 
GetSize()92 int64_t FsStat::GetSize()
93 {
94     return entity->stat_.st_size;
95 }
96 
GetAtime()97 int64_t FsStat::GetAtime()
98 {
99     return static_cast<int64_t>(entity->stat_.st_atim.tv_sec);
100 }
101 
GetMtime()102 int64_t FsStat::GetMtime()
103 {
104     return static_cast<int64_t>(entity->stat_.st_mtim.tv_sec);
105 }
106 
GetCtime()107 int64_t FsStat::GetCtime()
108 {
109     return static_cast<int64_t>(entity->stat_.st_ctim.tv_sec);
110 }
111 
GetAtimeNs()112 int64_t FsStat::GetAtimeNs()
113 {
114     return static_cast<uint64_t>(entity->stat_.st_atim.tv_sec * SECOND_TO_NANOSECOND + entity->stat_.st_atim.tv_nsec);
115 }
116 
GetMtimeNs()117 int64_t FsStat::GetMtimeNs()
118 {
119     return static_cast<uint64_t>(entity->stat_.st_mtim.tv_sec * SECOND_TO_NANOSECOND + entity->stat_.st_mtim.tv_nsec);
120 }
121 
GetCtimeNs()122 int64_t FsStat::GetCtimeNs()
123 {
124     return static_cast<uint64_t>(entity->stat_.st_ctim.tv_sec * SECOND_TO_NANOSECOND + entity->stat_.st_ctim.tv_nsec);
125 }
126 
127 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM)
GetLocation()128 int32_t FsStat::GetLocation()
129 {
130     std::unique_ptr<char[]> value = CreateUniquePtr<char[]>(MAX_ATTR_NAME);
131     if (value == nullptr) {
132         HILOGE("Getxattr memory out, errno is %{public}d", errno);
133         return ENOMEM;
134     }
135 
136     ssize_t size = 0;
137     if (entity->fileInfo_->isPath) {
138         size = getxattr(entity->fileInfo_->path.get(), CLOUD_LOCATION_ATTR.c_str(), value.get(), MAX_ATTR_NAME);
139     } else {
140         size = fgetxattr(entity->fileInfo_->fdg->GetFD(), CLOUD_LOCATION_ATTR.c_str(), value.get(), MAX_ATTR_NAME);
141     }
142 
143     Location defaultLocation = LOCAL;
144     if (size <= 0) {
145         if (errno != ENODATA && errno != EOPNOTSUPP) {
146             HILOGE("Getxattr value failed, errno is %{public}d", errno);
147         }
148         return static_cast<int32_t>(defaultLocation);
149     }
150     std::string location = string(value.get(), static_cast<size_t>(size));
151     if (!std::all_of(location.begin(), location.end(), ::isdigit)) {
152         HILOGE("Getxattr location is not all digit!");
153         return static_cast<int32_t>(defaultLocation);
154     }
155     defaultLocation = static_cast<Location>(atoi(location.c_str()));
156     return static_cast<int32_t>(defaultLocation);
157 }
158 #endif
159 
Constructor()160 FsStat *FsStat::Constructor()
161 {
162     auto entity = CreateUniquePtr<StatEntity>();
163     if (entity == nullptr) {
164         HILOGE("Failed to request heap memory.");
165         return nullptr;
166     }
167 
168     return new FsStat(move(entity));
169 }
170 
171 } // namespace OHOS::FileManagement::ModuleFileIO