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
30namespace OHOS {
31namespace CJSystemapi {
32namespace FileFs {
33
34constexpr int S_PREMISSION = 00000777;
35
36class StatImpl : public OHOS::FFI::FFIData {
37public:
38    OHOS::FFI::RuntimeType* GetRuntimeType() override { return GetClassType(); }
39
40    explicit StatImpl(uv_stat_t stat) : real_(std::move(stat)) {}
41
42    inline int64_t GetIno() const
43    {
44        return static_cast<int64_t>(real_.st_ino);
45    }
46
47    inline int64_t GetMode() const
48    {
49        return static_cast<int64_t>(real_.st_mode & S_PREMISSION);
50    }
51
52    inline int64_t GetUid() const
53    {
54        return static_cast<int64_t>(real_.st_uid);
55    }
56
57    inline int64_t GetGid() const
58    {
59        return static_cast<int64_t>(real_.st_gid);
60    }
61
62    inline int64_t GetSize() const
63    {
64        return static_cast<int64_t>(real_.st_size);
65    }
66
67    inline int64_t GetAtime() const
68    {
69        return static_cast<int64_t>(real_.st_atim.tv_sec);
70    }
71
72    inline int64_t GetMtime() const
73    {
74        return static_cast<int64_t>(real_.st_mtim.tv_sec);
75    }
76
77    inline int64_t GetCtime() const
78    {
79        return static_cast<int64_t>(real_.st_ctim.tv_sec);
80    }
81
82    bool IsBlockDevice()
83    {
84        return CheckStatMode(S_IFBLK);
85    }
86
87    bool IsCharacterDevice()
88    {
89        return CheckStatMode(S_IFCHR);
90    }
91
92    bool IsDirectory()
93    {
94        return CheckStatMode(S_IFDIR);
95    }
96
97    bool IsFIFO()
98    {
99        return CheckStatMode(S_IFIFO);
100    }
101
102    bool IsFile()
103    {
104        return CheckStatMode(S_IFREG);
105    }
106
107    bool IsSocket()
108    {
109        return CheckStatMode(S_IFSOCK);
110    }
111
112    bool IsSymbolicLink()
113    {
114        return CheckStatMode(S_IFLNK);
115    }
116
117    bool CheckStatMode(mode_t mode)
118    {
119        bool check = (real_.st_mode & S_IFMT) == mode;
120        return check;
121    }
122
123private:
124    uv_stat_t real_;
125
126    friend class OHOS::FFI::RuntimeType;
127    friend class OHOS::FFI::TypeBase;
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