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