1/* 2 * Copyright 2002-2011, Haiku Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Tyler Dauwalder, tylerdauwalder@users.sf.net 7 * John Scipione, jscipione@gmail.com 8 * Ingo Weinhold, bonefish@users.sf.net 9 * Corresponds to: 10 * /trunk/headers/os/app/Node.h rev 42803 11 * /trunk/src/kits/app/Node.cpp rev 42803 12 */ 13 14 15/*! 16 \file Node.h 17 \brief Provides the BNode class and node_ref structure. 18*/ 19 20 21/*! 22 \struct node_ref 23 \brief Reference structure to a particular vnode on a device. 24*/ 25 26 27/*! 28 \fn node_ref::node_ref() 29 \brief Creates an uninitialized node_ref object. 30*/ 31 32 33/*! 34 \fn node_ref::node_ref(const node_ref &ref) 35 \brief Creates a copy of the given node_ref object. 36 37 \param ref the node_ref to be copied. 38*/ 39 40 41/*! 42 \fn bool node_ref::operator==(const node_ref &ref) const 43 \brief Tests whether this node_ref and the supplied one are equal. 44 45 \param ref the node_ref to be compared with. 46 47 \return \c true, if the objects are equal, \c false otherwise. 48*/ 49 50 51/*! 52 \fn bool node_ref::operator!=(const node_ref &ref) const 53 \brief Tests whether this node_ref and the supplied one are not equal. 54 55 \param ref the node_ref to be compared with. 56 57 \return \c true, if the objects are \b not equal, \c false otherwise. 58*/ 59 60 61/*! 62 \fn node_ref& node_ref::operator=(const node_ref &ref) 63 \brief Makes this node ref a copy of the supplied one. 64 65 \param ref the node_ref to be copied. 66 67 \return a reference to this object. 68*/ 69 70 71/*! 72 \class BNode 73 \ingroup storage 74 \brief A BNode represents a chunk of data in the filesystem. 75 76 The BNode class provides an interface for manipulating the data and 77 attributes belonging to filesystem entries. The BNode is unaware of the 78 name that refers to it in the filesystem (i.e. its entry), instead, a 79 BNode is concerned solely with the entry's data and attributes. 80*/ 81 82 83/*! 84 \var BNode::fFd 85 File descriptor for the given node. 86*/ 87 88 89/*! 90 \var BNode::fAttrFd 91 File descriptor for the attribute directory of the node. Initialized lazily. 92*/ 93 94 95/*! 96 \var BNode::fCStatus 97 The object's initialization status. 98*/ 99 100 101/*! 102 \fn BNode::BNode() 103 \brief Creates an uninitialized BNode object. 104*/ 105 106 107/*! 108 \fn BNode::BNode(const entry_ref *ref) 109 \brief Creates a BNode object and initializes it to the specified 110 entry_ref. 111 112 \param ref the entry_ref referring to the entry. 113*/ 114 115 116/*! 117 \fn BNode::BNode(const BEntry *entry) 118 \brief Creates a BNode object and initializes it to the specified 119 filesystem entry. 120 121 \param entry the BEntry representing the entry. 122*/ 123 124 125/*! 126 \fn BNode::BNode(const char *path) 127 \brief Creates a BNode object and initializes it to the entry referred 128 to by the specified path. 129 130 \param path the path referring to the entry. 131*/ 132 133 134/*! 135 \fn BNode::BNode(const BDirectory *dir, const char *path) 136 \brief Creates a BNode object and initializes it to the entry referred 137 to by the specified path rooted in the specified directory. 138 139 \param dir the BDirectory, relative to which the entry's path name is 140 given. 141 \param path the entry's path name relative to \a dir. 142*/ 143 144 145/*! 146 \fn BNode::BNode(const BNode &node) 147 \brief Creates a copy of the given BNode. 148 149 \param node the BNode to be copied. 150*/ 151 152 153/*! 154 \fn BNode::~BNode() 155 \brief Frees all resources associated with the BNode. 156*/ 157 158 159/*! 160 \fn status_t BNode::InitCheck() const 161 \brief Checks whether the object has been properly initialized or not. 162 163 \returns B_OK if the object has been properly initialized, or an error 164 code otherwise. 165*/ 166 167 168/*! 169 \fn status_t BNode::GetStat(struct stat *st) const 170 \brief Fills in the given stat structure with the <tt>stat()</tt> 171 information for this object. 172 173 \param st a pointer to a stat structure to be filled in. 174 175 \retval B_OK Everything went fine. 176 \retval B_BAD_VALUE: \c NULL \a st. 177*/ 178 179 180/*! 181 \fn int BNode::Dup() 182 \brief Gets the POSIX file descriptor referred to by this node. 183 184 Remember to call close() on the file descriptor when you're through 185 with it. 186 187 \returns a valid file descriptor, or -1 if something went wrong. 188*/ 189 190 191/*! 192 \name Assignment Methods 193*/ 194 195 196//! @{ 197 198 199/*! 200 \fn BNode& BNode::operator=(const BNode &node) 201 \brief Initializes the object as a copy of the \a node. 202 203 \param node the BNode to be copied. 204 205 \returns a reference to this BNode object. 206*/ 207 208 209/*! 210 \fn status_t BNode::SetTo(const entry_ref *ref) 211 \brief Initializes the object to the specified entry_ref. 212 213 \param ref the entry_ref referring to the entry. 214 215 \retval B_OK: Everything went fine. 216 \retval B_BAD_VALUE: \c NULL \a ref. 217 \retval B_ENTRY_NOT_FOUND: The entry could not be found. 218 \retval B_BUSY: The entry is locked. 219*/ 220 221 222/*! 223 \fn status_t BNode::SetTo(const BEntry *entry) 224 \brief Initializes the object to the specified filesystem \a entry. 225 226 \param entry the BEntry representing the entry. 227 228 \retval B_OK Everything went fine. 229 \retval B_BAD_VALUE \c NULL \a entry. 230 \retval B_ENTRY_NOT_FOUND The entry could not be found. 231 \retval B_BUSY The entry is locked. 232*/ 233 234 235/*! 236 \fn status_t BNode::SetTo(const BDirectory *dir, const char *path) 237 \brief Initializes the object to the entry referred by the 238 specified \a path relative to the the specified directory. 239 240 \param dir the base BDirectory. 241 \param path the entry's path name relative to \a dir 242 243 \retval B_OK Everything went fine. 244 \retval B_BAD_VALUE \c NULL \a entry. 245 \retval B_ENTRY_NOT_FOUND The entry could not be found. 246 \retval B_BUSY The entry is locked. 247*/ 248 249 250/*! 251 \fn void BNode::Unset() 252 \brief Returns the object to an uninitialized state. 253*/ 254 255 256//! @} 257 258 259/*! 260 \name Locking Methods 261*/ 262 263 264//! @{ 265 266 267/*! 268 \fn status_t BNode::Lock() 269 \brief Attains an exclusive lock on the data referred to by this node 270 so that it may not be modified by any other objects or methods. 271 272 \retval B_OK Everything went fine. 273 \retval B_FILE_ERROR The object is not initialized. 274 \retval B_BUSY The node is already locked. 275*/ 276 277 278/*! 279 \fn status_t BNode::Unlock() 280 \brief Unlocks the date referred to by this node. 281 282 \retval B_OK Everything went fine. 283 \retval B_FILE_ERROR The object is not initialized. 284 \retval B_BAD_VALUE The node is not locked. 285*/ 286 287 288/*! 289 \fn status_t BNode::Sync() 290 \brief Immediately performs any pending disk actions on the node. 291 292 \retval B_OK Everything went fine. 293 \retval B_FILE_ERROR Something went wrong. 294*/ 295 296 297//! @} 298 299 300/*! 301 \name Attribute Methods 302*/ 303 304 305//! @{ 306 307 308/*! 309 \fn ssize_t BNode::WriteAttr(const char *attr, type_code type, 310 off_t offset, const void *buffer, size_t len) 311 \brief Writes data from a buffer to an attribute. 312 313 Write \a len bytes of data from \a buffer to the attribute specified 314 by \a name after erasing any data that existed previously. The type 315 specified by \a type \em is remembered, and may be queried with 316 GetAttrInfo(). The value of \a offset is currently ignored. 317 318 \param attr the name of the attribute. 319 \param type the type of the attribute. 320 \param offset the index at which to write the data (currently ignored). 321 \param buffer the buffer containing the data to be written. 322 \param len the number of bytes to be written. 323 324 \returns the number of bytes actually written. 325 \retval B_BAD_VALUE \a attr or \a buffer is \c NULL. 326 \retval B_FILE_ERROR The object is not initialized or the node it refers to 327 is read only. 328 \retval B_NOT_ALLOWED The node resides on a read only volume. 329 \retval B_DEVICE_FULL Insufficient disk space. 330 \retval B_NO_MEMORY Insufficient memory to complete the operation. 331*/ 332 333 334/*! 335 \fn ssize_t BNode::ReadAttr(const char *attr, type_code type, 336 off_t offset, void *buffer, size_t len) const 337 \brief Reads data from an attribute into \a buffer. 338 339 Reads \a len bytes of data from the attribute given by \a name into 340 \a buffer. \a type and \a offset are currently ignored. 341 342 \param attr the name of the attribute. 343 \param type the type of the attribute (currently ignored). 344 \param offset the index from which to read the data (currently ignored). 345 \param buffer the buffer for the data to be read. 346 \param len the number of bytes to be read. 347 348 \returns the number of bytes actually read 349 \retval B_BAD_VALUE \a attr or \a buffer is \c NULL. 350 \retval B_FILE_ERROR The object is not initialized. 351 \retval B_ENTRY_NOT_FOUND The node has no attribute \a attr. 352*/ 353 354 355/*! 356 \fn status_t BNode::RemoveAttr(const char *name) 357 \brief Deletes the attribute given by \a name. 358 359 \param name the name of the attribute to remove. 360 361 \retval B_OK Everything went fine. 362 \retval B_BAD_VALUE \a name is \c NULL. 363 \retval B_FILE_ERROR The object is not initialized or the node it 364 refers to read only. 365 \retval B_ENTRY_NOT_FOUND The node has no attribute \a name. 366 \retval B_NOT_ALLOWED The node resides on a read only volume. 367*/ 368 369 370/*! 371 \fn status_t BNode::RenameAttr(const char *oldname, const char *newname) 372 \brief Moves the attribute given by \a oldname to \a newname. 373 374 If \a newname already exists, the data is clobbered. 375 376 \param oldname the name of the attribute to be renamed. 377 \param newname the new name for the attribute. 378 379 \retval B_OK Everything went fine. 380 \retval B_BAD_VALUE \a oldname or \a newname is \c NULL. 381 \retval B_FILE_ERROR The object is not initialized or the node it 382 refers to is read only. 383 \retval B_ENTRY_NOT_FOUND The node has no attribute \a oldname. 384 \retval B_NOT_ALLOWED The node resides on a read only volume. 385*/ 386 387 388/*! 389 \fn status_t BNode::GetAttrInfo(const char *name, 390 struct attr_info *info) const 391 \brief Fills in the pre-allocated attr_info struct pointed to by \a info 392 with information about the attribute specified by \a name. 393 394 \param name the name of the attribute 395 \param info the attr_info structure to be filled in 396 397 \retval B_OK Everything went fine. 398 \retval B_BAD_VALUE \a name is \c NULL. 399 \retval B_FILE_ERROR The object is not initialized. 400 \retval B_ENTRY_NOT_FOUND The node has no attribute \a name. 401*/ 402 403 404/*! 405 \fn status_t BNode::GetNextAttrName(char *buffer) 406 \brief Copies the name of the attribute into \c buffer and then advances 407 the pointer to the next attribute. 408 409 The name of the node is first copied into \a buffer, which should be at 410 least \c B_ATTR_NAME_LENGTH characters long. The copied node name is 411 \c NUL terminated. Once the name is copied the attribute list pointer 412 is advanced to the next attribute in the list. When GetNextAttrName() 413 reaches the end of the list it returns \c B_ENTRY_NOT_FOUND. 414 415 \param buffer A buffer to copy the name of the attribute into. 416 417 \retval B_OK The Attribute name was copied and there are more attribute 418 names to copy. 419 \retval B_BAD_VALUE passed in \a buffer is \c NULL. 420 \retval B_FILE_ERROR The object is not initialized. 421 \retval B_ENTRY_NOT_FOUND There are no more attributes, the last attribute 422 name has already been copied. 423*/ 424 425 426/*! 427 \fn status_t BNode::RewindAttrs() 428 \brief Resets the object's attribute pointer to the first attribute in the 429 list. 430 431 \retval B_OK Everything went fine. 432 \retval B_FILE_ERROR Some other error occurred. 433*/ 434 435 436/*! 437 \fn status_t BNode::WriteAttrString(const char *name, const BString *data) 438 \brief Writes the specified string to the specified attribute, clobbering 439 any previous data. 440 441 \param name the name of the attribute. 442 \param data the BString to be written to the attribute. 443 444 \retval B_OK Everything went fine. 445 \retval B_BAD_VALUE \c NULL \a name or \a data 446 \retval B_FILE_ERROR The object is not initialized or the node it refers to 447 is read only. 448 \retval B_NOT_ALLOWED The node resides on a read only volume. 449 \retval B_DEVICE_FULL Insufficient disk space. 450 \retval B_NO_MEMORY Insufficient memory to complete the operation. 451*/ 452 453 454/*! 455 \fn status_t BNode::ReadAttrString(const char *name, BString *result) const 456 \brief Reads the data of the specified attribute into the pre-allocated 457 \a result. 458 459 \param name the name of the attribute. 460 \param result the BString to be set to the value of the attribute. 461 462 \retval B_OK Everything went fine. 463 \retval B_BAD_VALUE \a name or \a result is \c NULL. 464 \retval B_FILE_ERROR The object is not initialized. 465 \retval B_ENTRY_NOT_FOUND The node has no attribute \a attr. 466*/ 467 468 469//! @} 470 471 472/*! 473 \name Comparison Methods 474*/ 475 476 477//! @{ 478 479 480/*! 481 \fn bool BNode::operator==(const BNode &node) const 482 \brief Tests whether this and the supplied BNode object are equal. 483 484 Two BNode objects are said to be equal if they're set to the same node, 485 or if they're both \c B_NO_INIT. 486 487 \param node the BNode to be compared with. 488 489 \return \c true, if the BNode objects are equal, \c false otherwise. 490*/ 491 492 493/*! 494 \fn bool BNode::operator!=(const BNode &node) const 495 \brief Tests whether this and the supplied BNode object are not equal. 496 497 Two BNode objects are said to be equal if they're set to the same node, 498 or if they're both \c B_NO_INIT. 499 500 \param node the BNode to be compared with 501 502 \return \c false, if the BNode objects are equal, \c true otherwise. 503*/ 504 505 506//! @} 507 508 509/*! 510 \name Private Methods 511*/ 512 513 514//! @{ 515 516 517/*! 518 \fn status_t BNode::set_fd(int fd) 519 \brief Sets the node's file descriptor. 520 521 Used by each implementation (i.e. BNode, BFile, BDirectory, etc.) to set 522 the node's file descriptor. This allows each subclass to use the various 523 file-type specific system calls for opening file descriptors. 524 525 \note This method calls close_fd() to close previously opened FDs. Thus 526 derived classes should take care to first call set_fd() and set 527 class specific resources freed in their close_fd() version 528 thereafter. 529 530 \param fd the file descriptor this BNode should be set to (may be -1). 531 532 \returns \c B_OK if everything went fine, or an error code if something 533 went wrong. 534*/ 535 536 537/*! 538 \fn void BNode::close_fd() 539 \brief Closes the node's file descriptor(s). 540 541 To be implemented by subclasses to close the file descriptor using the 542 proper system call for the given file-type. This implementation calls 543 _kern_close(fFd) and also _kern_close(fAttrDir) if necessary. 544*/ 545 546 547/*! 548 \fn void BNode::set_status(status_t newStatus) 549 \brief Sets the BNode's status. 550 551 To be used by derived classes instead of accessing the BNode's private 552 \c fCStatus member directly. 553 554 \param newStatus the new value for the status variable. 555*/ 556 557 558/*! 559 \fn status_t BNode::_SetTo(int fd, const char *path, bool traverse) 560 \brief Initializes the BNode's file descriptor to the node referred to 561 by the given FD and path combo. 562 563 \a path must either be \c NULL, an absolute or a relative path. 564 In the first case, \a fd must not be \c NULL; the node it refers to will 565 be opened. If absolute, \a fd is ignored. If relative and \a fd is >= 0, 566 it will be reckoned off the directory identified by \a fd, otherwise off 567 the current working directory. 568 569 The method will first try to open the node with read and write permission. 570 If that fails due to a read-only FS or because the user has no write 571 permission for the node, it will re-try opening the node read-only. 572 573 The \a fCStatus member will be set to the return value of this method. 574 575 \param fd Either a directory FD or a value < 0. In the latter case \a path 576 must be specified. 577 \param path Either \a NULL in which case \a fd must be given, absolute, or 578 relative to the directory specified by \a fd (if given) or to the 579 current working directory. 580 \param traverse If the node identified by \a fd and \a path is a symlink 581 and \a traverse is \c true, the symlink will be resolved recursively. 582 583 \returns \c B_OK if everything went fine, or an error code if something 584 went wrong. 585*/ 586 587 588/*! 589 \fn status_t BNode::_SetTo(const entry_ref *ref, bool traverse) 590 \brief Initializes the BNode's file descriptor to the node referred to 591 by the given entry_ref. 592 593 The method will first try to open the node with read and write permission. 594 If that fails due to a read-only FS or because the user has no write 595 permission for the node, it will re-try opening the node read-only. 596 597 The \a fCStatus member will be set to the return value of this method. 598 599 \param ref An entry_ref identifying the node to be opened. 600 \param traverse If the node identified by \a ref is a symlink and 601 \a traverse is \c true, the symlink will be resolved recursively. 602 603 \returns \c B_OK if everything went fine, or an error code if something 604 went wrong. 605*/ 606 607 608/*! 609 \fn status_t BNode::set_stat(struct stat &st, uint32 what) 610 \brief Modifies a certain setting for this node based on \a what and the 611 corresponding value in \a st. 612 613 Inherited from and called by BStatable. 614 615 \param st a stat structure containing the value to be set. 616 \param what specifies what setting to be modified. 617 618 \returns \c B_OK if everything went fine, or an error code if something 619 went wrong. 620*/ 621 622 623/*! 624 \fn status_t BNode::InitAttrDir() 625 \brief Verifies that the BNode has been properly initialized, and then 626 (if necessary) opens the attribute directory on the node's file 627 descriptor, storing it in fAttrDir. 628 629 \returns \c B_OK if everything went fine, or an error code if something 630 went wrong. 631*/ 632 633 634//! @} 635