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 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) 37 const size_t MAX_ATTR_NAME = 64; 38 const std::string CLOUD_LOCATION_ATTR = "user.cloud.location"; 39 enum Location { 40 LOCAL = 1 << 0, 41 CLOUD = 1 << 1 42 }; 43 #endif 44 45 class StatImpl : public OHOS::FFI::FFIData { 46 public: GetRuntimeType()47 OHOS::FFI::RuntimeType* GetRuntimeType() override { return GetClassType(); } 48 StatImpl(uv_stat_t stat)49 explicit StatImpl(uv_stat_t stat) : real_(std::move(stat)) {} 50 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) 51 RetDataI32 GetLocation(); 52 SetFileInfo(std::shared_ptr<FileInfo> info)53 void SetFileInfo(std::shared_ptr<FileInfo> info) 54 { 55 fileInfo_ = info; 56 } 57 #endif 58 GetIno()59 inline int64_t GetIno() const 60 { 61 return static_cast<int64_t>(real_.st_ino); 62 } 63 GetMode()64 inline int64_t GetMode() const 65 { 66 return static_cast<int64_t>(real_.st_mode & S_PREMISSION); 67 } 68 GetUid()69 inline int64_t GetUid() const 70 { 71 return static_cast<int64_t>(real_.st_uid); 72 } 73 GetGid()74 inline int64_t GetGid() const 75 { 76 return static_cast<int64_t>(real_.st_gid); 77 } 78 GetSize()79 inline int64_t GetSize() const 80 { 81 return static_cast<int64_t>(real_.st_size); 82 } 83 GetAtime()84 inline int64_t GetAtime() const 85 { 86 return static_cast<int64_t>(real_.st_atim.tv_sec); 87 } 88 GetMtime()89 inline int64_t GetMtime() const 90 { 91 return static_cast<int64_t>(real_.st_mtim.tv_sec); 92 } 93 GetCtime()94 inline int64_t GetCtime() const 95 { 96 return static_cast<int64_t>(real_.st_ctim.tv_sec); 97 } 98 IsBlockDevice()99 bool IsBlockDevice() 100 { 101 return CheckStatMode(S_IFBLK); 102 } 103 IsCharacterDevice()104 bool IsCharacterDevice() 105 { 106 return CheckStatMode(S_IFCHR); 107 } 108 IsDirectory()109 bool IsDirectory() 110 { 111 return CheckStatMode(S_IFDIR); 112 } 113 IsFIFO()114 bool IsFIFO() 115 { 116 return CheckStatMode(S_IFIFO); 117 } 118 IsFile()119 bool IsFile() 120 { 121 return CheckStatMode(S_IFREG); 122 } 123 IsSocket()124 bool IsSocket() 125 { 126 return CheckStatMode(S_IFSOCK); 127 } 128 IsSymbolicLink()129 bool IsSymbolicLink() 130 { 131 return CheckStatMode(S_IFLNK); 132 } 133 CheckStatMode(mode_t mode)134 bool CheckStatMode(mode_t mode) 135 { 136 bool check = (real_.st_mode & S_IFMT) == mode; 137 return check; 138 } 139 140 private: 141 uv_stat_t real_; 142 #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) 143 std::shared_ptr<FileInfo> fileInfo_ = nullptr; 144 #endif 145 146 friend class OHOS::FFI::RuntimeType; 147 friend class OHOS::FFI::TypeBase; GetClassType()148 static OHOS::FFI::RuntimeType* GetClassType() 149 { 150 static OHOS::FFI::RuntimeType runtimeType = OHOS::FFI::RuntimeType::Create<OHOS::FFI::FFIData>("StatImpl"); 151 return &runtimeType; 152 } 153 }; 154 155 } 156 } 157 } 158 #endif // OHOS_FILE_FS_STAT_IMPL_H