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