1 /* 2 * Copyright 2002-2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Tyler Dauwalder 7 * Ingo Weinhold, bonefish@users.sf.net 8 */ 9 10 /*! 11 \file Statable.cpp 12 BStatable implementation. 13 */ 14 15 #include <Statable.h> 16 #include <Node.h> 17 #include <NodeMonitor.h> 18 #include <Volume.h> 19 20 #include <sys/stat.h> 21 22 23 #if __GNUC__ > 3 24 BStatable::~BStatable() 25 { 26 } 27 #endif 28 29 30 /*! \fn status_t GetStat(struct stat *st) const 31 \brief Returns the stat stucture for the node. 32 \param st the stat structure to be filled in. 33 \return 34 - \c B_OK: Worked fine 35 - \c B_NO_MEMORY: Could not allocate the memory for the call. 36 - \c B_BAD_VALUE: The current node does not exist. 37 - \c B_NOT_ALLOWED: Read only node or volume. 38 */ 39 40 /*! \brief Returns if the current node is a file. 41 \return \c true, if the BNode is properly initialized and is a file, 42 \c false otherwise. 43 */ 44 bool 45 BStatable::IsFile() const 46 { 47 struct stat statData; 48 if (GetStat(&statData) == B_OK) 49 return S_ISREG(statData.st_mode); 50 else 51 return false; 52 } 53 54 /*! \brief Returns if the current node is a directory. 55 \return \c true, if the BNode is properly initialized and is a file, 56 \c false otherwise. 57 */ 58 bool 59 BStatable::IsDirectory() const 60 { 61 struct stat statData; 62 if (GetStat(&statData) == B_OK) 63 return S_ISDIR(statData.st_mode); 64 else 65 return false; 66 } 67 68 /*! \brief Returns if the current node is a symbolic link. 69 \return \c true, if the BNode is properly initialized and is a symlink, 70 \c false otherwise. 71 */ 72 bool 73 BStatable::IsSymLink() const 74 { 75 struct stat statData; 76 if (GetStat(&statData) == B_OK) 77 return S_ISLNK(statData.st_mode); 78 else 79 return false; 80 } 81 82 /*! \brief Returns a node_ref for the current node. 83 \param ref the node_ref structure to be filled in 84 \see GetStat() for return codes 85 */ 86 status_t 87 BStatable::GetNodeRef(node_ref *ref) const 88 { 89 status_t error = (ref ? B_OK : B_BAD_VALUE); 90 struct stat statData; 91 if (error == B_OK) 92 error = GetStat(&statData); 93 if (error == B_OK) { 94 ref->device = statData.st_dev; 95 ref->node = statData.st_ino; 96 } 97 return error; 98 } 99 100 /*! \brief Returns the owner of the node. 101 \param owner a pointer to a uid_t variable to be set to the result 102 \see GetStat() for return codes 103 */ 104 status_t 105 BStatable::GetOwner(uid_t *owner) const 106 { 107 status_t error = (owner ? B_OK : B_BAD_VALUE); 108 struct stat statData; 109 if (error == B_OK) 110 error = GetStat(&statData); 111 if (error == B_OK) 112 *owner = statData.st_uid; 113 return error; 114 } 115 116 /*! \brief Sets the owner of the node. 117 \param owner the new owner 118 \see GetStat() for return codes 119 */ 120 status_t 121 BStatable::SetOwner(uid_t owner) 122 { 123 struct stat statData; 124 statData.st_uid = owner; 125 return set_stat(statData, B_STAT_UID); 126 } 127 128 /*! \brief Returns the group owner of the node. 129 \param group a pointer to a gid_t variable to be set to the result 130 \see GetStat() for return codes 131 */ 132 status_t 133 BStatable::GetGroup(gid_t *group) const 134 { 135 status_t error = (group ? B_OK : B_BAD_VALUE); 136 struct stat statData; 137 if (error == B_OK) 138 error = GetStat(&statData); 139 if (error == B_OK) 140 *group = statData.st_gid; 141 return error; 142 } 143 144 /*! \brief Sets the group owner of the node. 145 \param group the new group 146 \see GetStat() for return codes 147 */ 148 status_t 149 BStatable::SetGroup(gid_t group) 150 { 151 struct stat statData; 152 statData.st_gid = group; 153 return set_stat(statData, B_STAT_GID); 154 } 155 156 /*! \brief Returns the permissions of the node. 157 \param perms a pointer to a mode_t variable to be set to the result 158 \see GetStat() for return codes 159 */ 160 status_t 161 BStatable::GetPermissions(mode_t *perms) const 162 { 163 status_t error = (perms ? B_OK : B_BAD_VALUE); 164 struct stat statData; 165 if (error == B_OK) 166 error = GetStat(&statData); 167 if (error == B_OK) 168 *perms = (statData.st_mode & S_IUMSK); 169 return error; 170 } 171 172 /*! \brief Sets the permissions of the node. 173 \param perms the new permissions 174 \see GetStat() for return codes 175 */ 176 status_t 177 BStatable::SetPermissions(mode_t perms) 178 { 179 struct stat statData; 180 // the FS should do the correct masking -- only the S_IUMSK part is 181 // modifiable 182 statData.st_mode = perms; 183 return set_stat(statData, B_STAT_MODE); 184 } 185 186 /*! \brief Get the size of the node's data (not counting attributes). 187 \param size a pointer to a variable to be set to the result 188 \see GetStat() for return codes 189 */ 190 status_t 191 BStatable::GetSize(off_t *size) const 192 { 193 status_t error = (size ? B_OK : B_BAD_VALUE); 194 struct stat statData; 195 if (error == B_OK) 196 error = GetStat(&statData); 197 if (error == B_OK) 198 *size = statData.st_size; 199 return error; 200 } 201 202 /*! \brief Returns the last time the node was modified. 203 \param mtime a pointer to a variable to be set to the result 204 \see GetStat() for return codes 205 */ 206 status_t 207 BStatable::GetModificationTime(time_t *mtime) const 208 { 209 status_t error = (mtime ? B_OK : B_BAD_VALUE); 210 struct stat statData; 211 if (error == B_OK) 212 error = GetStat(&statData); 213 if (error == B_OK) 214 *mtime = statData.st_mtime; 215 return error; 216 } 217 218 /*! \brief Sets the last time the node was modified. 219 \param mtime the new modification time 220 \see GetStat() for return codes 221 */ 222 status_t 223 BStatable::SetModificationTime(time_t mtime) 224 { 225 struct stat statData; 226 statData.st_mtime = mtime; 227 return set_stat(statData, B_STAT_MODIFICATION_TIME); 228 } 229 230 /*! \brief Returns the time the node was created. 231 \param ctime a pointer to a variable to be set to the result 232 \see GetStat() for return codes 233 */ 234 status_t 235 BStatable::GetCreationTime(time_t *ctime) const 236 { 237 status_t error = (ctime ? B_OK : B_BAD_VALUE); 238 struct stat statData; 239 if (error == B_OK) 240 error = GetStat(&statData); 241 if (error == B_OK) 242 *ctime = statData.st_crtime; 243 return error; 244 } 245 246 /*! \brief Sets the time the node was created. 247 \param ctime the new creation time 248 \see GetStat() for return codes 249 */ 250 status_t 251 BStatable::SetCreationTime(time_t ctime) 252 { 253 struct stat statData; 254 statData.st_crtime = ctime; 255 return set_stat(statData, B_STAT_CREATION_TIME); 256 } 257 258 /*! \brief Returns the time the node was accessed. 259 Not used. 260 \see GetModificationTime() 261 \see GetStat() for return codes 262 */ 263 status_t 264 BStatable::GetAccessTime(time_t *atime) const 265 { 266 status_t error = (atime ? B_OK : B_BAD_VALUE); 267 struct stat statData; 268 if (error == B_OK) 269 error = GetStat(&statData); 270 if (error == B_OK) 271 *atime = statData.st_atime; 272 return error; 273 } 274 275 /*! \brief Sets the time the node was accessed. 276 Not used. 277 \see GetModificationTime() 278 \see GetStat() for return codes 279 */ 280 status_t 281 BStatable::SetAccessTime(time_t atime) 282 { 283 struct stat statData; 284 statData.st_atime = atime; 285 return set_stat(statData, B_STAT_ACCESS_TIME); 286 } 287 288 /*! \brief Returns the volume the node lives on. 289 \param vol a pointer to a variable to be set to the result 290 \see BVolume 291 \see GetStat() for return codes 292 */ 293 status_t 294 BStatable::GetVolume(BVolume *vol) const 295 { 296 status_t error = (vol ? B_OK : B_BAD_VALUE); 297 struct stat statData; 298 if (error == B_OK) 299 error = GetStat(&statData); 300 if (error == B_OK) 301 error = vol->SetTo(statData.st_dev); 302 return error; 303 } 304 305 void BStatable::_OhSoStatable1() {} 306 void BStatable::_OhSoStatable2() {} 307 void BStatable::_OhSoStatable3() {} 308 309