1 /*
2  * Copyright (c) 2024 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 #ifndef OHOS_FILE_FS_STAT_IMPL_H
17 #define OHOS_FILE_FS_STAT_IMPL_H
18 
19 #include "file_utils.h"
20 #include "filemgmt_libhilog.h"
21 #include "ffi_remote_data.h"
22 #include "cj_common_ffi.h"
23 #include "fd_guard.h"
24 #include "utils.h"
25 #include "uv.h"
26 
27 #include <sys/stat.h>
28 #include <uv.h>
29 
30 namespace OHOS {
31 namespace CJSystemapi {
32 namespace FileFs {
33 
34 constexpr int S_PREMISSION = 00000777;
35 
36 class StatImpl : public OHOS::FFI::FFIData {
37 public:
GetRuntimeType()38     OHOS::FFI::RuntimeType* GetRuntimeType() override { return GetClassType(); }
39 
StatImpl(uv_stat_t stat)40     explicit StatImpl(uv_stat_t stat) : real_(std::move(stat)) {}
41 
GetIno()42     inline int64_t GetIno() const
43     {
44         return static_cast<int64_t>(real_.st_ino);
45     }
46 
GetMode()47     inline int64_t GetMode() const
48     {
49         return static_cast<int64_t>(real_.st_mode & S_PREMISSION);
50     }
51 
GetUid()52     inline int64_t GetUid() const
53     {
54         return static_cast<int64_t>(real_.st_uid);
55     }
56 
GetGid()57     inline int64_t GetGid() const
58     {
59         return static_cast<int64_t>(real_.st_gid);
60     }
61 
GetSize()62     inline int64_t GetSize() const
63     {
64         return static_cast<int64_t>(real_.st_size);
65     }
66 
GetAtime()67     inline int64_t GetAtime() const
68     {
69         return static_cast<int64_t>(real_.st_atim.tv_sec);
70     }
71 
GetMtime()72     inline int64_t GetMtime() const
73     {
74         return static_cast<int64_t>(real_.st_mtim.tv_sec);
75     }
76 
GetCtime()77     inline int64_t GetCtime() const
78     {
79         return static_cast<int64_t>(real_.st_ctim.tv_sec);
80     }
81 
IsBlockDevice()82     bool IsBlockDevice()
83     {
84         return CheckStatMode(S_IFBLK);
85     }
86 
IsCharacterDevice()87     bool IsCharacterDevice()
88     {
89         return CheckStatMode(S_IFCHR);
90     }
91 
IsDirectory()92     bool IsDirectory()
93     {
94         return CheckStatMode(S_IFDIR);
95     }
96 
IsFIFO()97     bool IsFIFO()
98     {
99         return CheckStatMode(S_IFIFO);
100     }
101 
IsFile()102     bool IsFile()
103     {
104         return CheckStatMode(S_IFREG);
105     }
106 
IsSocket()107     bool IsSocket()
108     {
109         return CheckStatMode(S_IFSOCK);
110     }
111 
IsSymbolicLink()112     bool IsSymbolicLink()
113     {
114         return CheckStatMode(S_IFLNK);
115     }
116 
CheckStatMode(mode_t mode)117     bool CheckStatMode(mode_t mode)
118     {
119         bool check = (real_.st_mode & S_IFMT) == mode;
120         return check;
121     }
122 
123 private:
124     uv_stat_t real_;
125 
126     friend class OHOS::FFI::RuntimeType;
127     friend class OHOS::FFI::TypeBase;
GetClassType()128     static OHOS::FFI::RuntimeType* GetClassType()
129     {
130         static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("StatImpl");
131         return &runtimeType;
132     }
133 };
134 
135 }
136 }
137 }
138 #endif // OHOS_FILE_FS_STAT_IMPL_H