2 * Copyright 2002-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * John Scipione, jscipione@gmail.com
7 * Ingo Weinhold, bonefish@users.sf.net
10 * headers/os/storage/Node.h hrev47402
11 * src/kits/storage/Node.cpp hrev47402
19 \brief Provides the BNode class and node_ref struct.
27 \brief Reference structure to a particular vnode on a device.
34 \fn node_ref::node_ref()
35 \brief Creates an uninitialized node_ref object.
42 \fn node_ref::node_ref(const node_ref& other)
43 \brief Creates a copy of the given node_ref object.
45 \param other the node_ref to be copied.
60 \fn bool node_ref::operator==(const node_ref& other) const
61 \brief Tests whether this node_ref and the supplied one are equal.
63 \param other The node_ref to be compared against.
65 \return \c true, if the objects are equal, \c false otherwise.
72 \fn bool node_ref::operator!=(const node_ref& other) const
73 \brief Tests whether this node_ref and the supplied one are NOT equal.
75 \param other The node_ref to be compared against.
77 \return \c true, if the objects are \b NOT equal, \c false otherwise.
84 \fn bool node_ref::operator<(const node_ref& other) const
85 \brief Tests whether this node_ref is less than the supplied one.
87 \param other The node_ref to be compared against.
89 \return \c true, if this node_ref is less than \a other,
97 \fn node_ref& node_ref::operator=(const node_ref& other)
98 \brief Makes this node ref a copy of the supplied one.
100 \param other The node_ref to be copied.
102 \return A reference to this object.
112 \var node_ref::device
113 \brief The device number on which the node is located.
121 \brief The node's inode number.
131 \brief A BNode represents a chunk of data in the filesystem.
133 The BNode class provides an interface for manipulating the data and
134 attributes belonging to filesystem entries. The BNode is unaware of the
135 name that refers to it in the filesystem (i.e. its entry), instead, a
136 BNode is concerned solely with the entry's data and attributes.
144 \brief Creates an uninitialized BNode object.
153 \fn BNode::BNode(const entry_ref* ref)
154 \brief Creates a BNode object and initializes it to the specified
157 \param ref The entry_ref referring to the node.
164 \fn BNode::BNode(const BEntry* entry)
165 \brief Creates a BNode object and initializes it to the specified
168 \param entry The BEntry referring to the node.
175 \fn BNode::BNode(const char* path)
176 \brief Creates a BNode object and initializes it to the entry referred
177 to by the specified path.
179 \param path The \a path referring to the node.
186 \fn BNode::BNode(const BDirectory* dir, const char* path)
187 \brief Creates a BNode object and initializes it to the entry referred
188 to by the specified path rooted in the specified directory.
190 \param dir The base BDirectory.
191 \param path The \a path name relative to \a dir.
198 \fn BNode::BNode(const BNode& other)
199 \brief Creates a copy of the given BNode.
201 \param other The BNode to be copied.
209 \brief Frees all resources associated with the BNode.
216 \fn status_t BNode::GetStat(struct stat* stat) const
217 \brief Fills in the given stat structure with the <tt>stat()</tt>
218 information for this object.
220 \param stat A pointer to a stat structure to be filled in.
222 \return \c B_OK on success or an error code otherwise.
223 \retval B_OK Everything went fine.
224 \retval B_BAD_VALUE: \c NULL \a stat.
232 \brief Gets the POSIX file descriptor referred to by this node.
234 Remember to call close() on the file descriptor when you're through
237 \returns A valid file descriptor, or -1 if something went wrong.
244 \name Constructor Helpers
252 \fn status_t BNode::InitCheck() const
253 \brief Checks whether the object has been properly initialized or not.
255 \returns \c B_OK if the object has been properly initialized, or
256 \c B_NO_INIT otherwise.
263 \fn status_t BNode::SetTo(const entry_ref* ref)
264 \brief Initializes the object to the specified entry_ref.
266 \param ref The entry_ref referring to the entry.
268 \return \c B_OK on success or an error code otherwise.
269 \retval B_OK: Everything went fine.
270 \retval B_BAD_VALUE: \a ref was \c NULL.
271 \retval B_ENTRY_NOT_FOUND: The entry could not be found.
272 \retval B_BUSY: The entry was locked.
279 \fn status_t BNode::SetTo(const BEntry* entry)
280 \brief Initializes the object to the specified filesystem \a entry.
282 \param entry The BEntry representing the entry.
284 \return \c B_OK on success or an error code otherwise.
285 \retval B_OK Everything went fine.
286 \retval B_BAD_VALUE \a entry was \c NULL.
287 \retval B_ENTRY_NOT_FOUND The \a entry could not be found.
288 \retval B_BUSY The \a entry was locked.
295 \fn status_t BNode::SetTo(const char* path)
296 \brief Initializes the object to the specified \a path.
298 \param path The entry's path name; \a path can be relative or absolute,
299 and can contain "." and ".." elements. If path is relative, it uses
300 the current working directory as the base directory.
302 \return \c B_OK on success or an error code otherwise.
303 \retval B_OK Everything went fine.
304 \retval B_BAD_VALUE \a path was \c NULL.
305 \retval B_ENTRY_NOT_FOUND The entry could not be found.
306 \retval B_BUSY The entry of was locked.
313 \fn status_t BNode::SetTo(const BDirectory* dir, const char* path)
314 \brief Initializes the object to the entry referred by the
315 specified \a path relative to the the specified directory.
317 \param dir The base BDirectory.
318 \param path The path name relative to \a dir.
320 \return \c B_OK on success or an error code otherwise.
321 \retval B_OK Everything went fine.
322 \retval B_BAD_VALUE \a dir was \c NULL.
323 \retval B_ENTRY_NOT_FOUND The entry could not be found.
324 \retval B_BUSY The entry was locked.
331 \fn void BNode::Unset()
332 \brief Returns the object to an uninitialized state.
342 \name Locking Methods
350 \fn status_t BNode::Lock()
351 \brief Attains an exclusive lock on the data referred to by this node
352 so that it may not be modified by any other objects or methods.
354 \return \c B_OK on success or an error code otherwise.
355 \retval B_OK Everything went fine.
356 \retval B_FILE_ERROR The object is not initialized.
357 \retval B_BUSY The node is already locked.
364 \fn status_t BNode::Unlock()
365 \brief Unlocks the date referred to by this node.
367 \return \c B_OK on success or an error code otherwise.
368 \retval B_OK Everything went fine.
369 \retval B_FILE_ERROR The object is not initialized.
370 \retval B_BAD_VALUE The node is not locked.
377 \fn status_t BNode::Sync()
378 \brief Immediately performs any pending disk actions on the node.
380 \return \c B_OK on success or an error code otherwise.
381 \retval B_OK Everything went fine.
382 \retval B_FILE_ERROR Something went wrong.
392 \name Attribute Methods
400 \fn ssize_t BNode::WriteAttr(const char* attr, type_code type,
401 off_t offset, const void* buffer, size_t length)
402 \brief Writes data from a buffer to an attribute.
404 Write \a length bytes of data from \a buffer to the attribute specified
405 by \a name after erasing any data that existed previously. The type
406 specified by \a type \em is remembered, and may be queried with
407 GetAttrInfo(). The value of \a offset is currently ignored.
409 \param attr The name of the attribute.
410 \param type The type of the attribute.
411 \param offset The index at which to write the data (currently ignored).
412 \param buffer The buffer containing the data to be written.
413 \param length The number of bytes to be written.
415 \return The number of bytes actually written.
416 \retval B_BAD_VALUE \a attr or \a buffer was \c NULL.
417 \retval B_FILE_ERROR The object was not initialized or the node it refers
419 \retval B_NOT_ALLOWED The node resides on a read only volume.
420 \retval B_DEVICE_FULL There was insufficient disk space to complete the
422 \retval B_NO_MEMORY There was insufficient memory to complete the
430 \fn ssize_t BNode::ReadAttr(const char* attr, type_code type,
431 off_t offset, void* buffer, size_t length) const
432 \brief Reads data from an attribute into \a buffer.
434 Reads \a length bytes of data from the attribute given by \a name into
435 \a buffer. \a type and \a offset are currently ignored.
437 \param attr The name of the attribute.
438 \param type The type of the attribute (currently ignored).
439 \param offset The index from which to read the data (currently ignored).
440 \param buffer The buffer for the data to be read.
441 \param length The number of bytes to be read.
443 \return The number of bytes actually read.
444 \retval B_BAD_VALUE \a attr or \a buffer was \c NULL.
445 \retval B_FILE_ERROR The object was not initialized.
446 \retval B_ENTRY_NOT_FOUND The node had no attribute \a attr.
453 \fn status_t BNode::RemoveAttr(const char* name)
454 \brief Deletes the attribute given by \a name.
456 \param name The name of the attribute to remove.
458 \return \c B_OK on success or an error code otherwise.
459 \retval B_OK Everything went fine.
460 \retval B_BAD_VALUE \a name was \c NULL.
461 \retval B_FILE_ERROR The object was not initialized or the node it
462 referred to was read-only.
463 \retval B_ENTRY_NOT_FOUND The node had no attribute \a name.
464 \retval B_NOT_ALLOWED The node resided on a read-only volume.
471 \fn status_t BNode::RenameAttr(const char* oldName, const char* newName)
472 \brief Moves the attribute given by \a oldName to \a newName.
474 If \a newName already exists, the data is clobbered.
476 \param oldName The name of the attribute to be renamed.
477 \param newName The new name for the attribute.
479 \return \c B_OK on success or an error code otherwise.
480 \retval B_OK Everything went fine.
481 \retval B_BAD_VALUE \a oldName or \a newName was \c NULL.
482 \retval B_FILE_ERROR The object was not initialized or the node it
483 referred to was read only.
484 \retval B_ENTRY_NOT_FOUND The node had no attribute \a oldName.
485 \retval B_NOT_ALLOWED The node resided on a read-only volume.
492 \fn status_t BNode::GetAttrInfo(const char* name,
493 struct attr_info* info) const
494 \brief Fills in the pre-allocated attr_info struct pointed to by \a info
495 with information about the attribute specified by \a name.
497 \param name The name of the attribute.
498 \param info The attr_info structure to be filled in.
500 \return \c B_OK on success or an error code otherwise.
501 \retval B_OK Everything went fine.
502 \retval B_BAD_VALUE \a name was \c NULL.
503 \retval B_FILE_ERROR The object was not initialized.
504 \retval B_ENTRY_NOT_FOUND The node had no attribute \a name.
511 \fn status_t BNode::GetNextAttrName(char* buffer)
512 \brief Copies the name of the attribute into \c buffer and then advances
513 the pointer to the next attribute.
515 The name of the node is first copied into \a buffer, which should be at
516 least \c B_ATTR_NAME_LENGTH characters long. The copied node name is
517 \0 terminated. Once the name is copied the attribute list pointer
518 is advanced to the next attribute in the list. When GetNextAttrName()
519 reaches the end of the list it returns \c B_ENTRY_NOT_FOUND.
521 \param buffer A buffer to copy the name of the attribute into.
523 \return \c B_OK on success or an error code otherwise.
524 \retval B_OK The Attribute name was copied and there are more attribute
526 \retval B_BAD_VALUE passed in \a buffer was \c NULL.
527 \retval B_FILE_ERROR The object was not initialized.
528 \retval B_ENTRY_NOT_FOUND There were no more attributes, the last attribute
529 name had already been copied.
536 \fn status_t BNode::RewindAttrs()
537 \brief Resets the object's attribute pointer to the first attribute in the
540 \return \c B_OK on success or an error code otherwise.
541 \retval B_OK Everything went fine.
542 \retval B_FILE_ERROR Some other error occurred.
549 \fn status_t BNode::WriteAttrString(const char* name, const BString* data)
550 \brief Writes the specified string to the specified attribute, clobbering
553 \param name the name of the attribute.
554 \param data the BString to be written to the attribute.
556 \return \c B_OK on success or an error code otherwise.
557 \retval B_OK Everything went fine.
558 \retval B_BAD_VALUE \a name or \a data was \c NULL.
559 \retval B_FILE_ERROR The object was not initialized or the node it
560 referred to was read-only.
561 \retval B_NOT_ALLOWED The node resided on a read-only volume.
562 \retval B_DEVICE_FULL There was insufficient disk space to complete the
564 \retval B_NO_MEMORY There was insufficient memory to complete the
572 \fn status_t BNode::ReadAttrString(const char* name, BString* result) const
573 \brief Reads the data of the specified attribute into the pre-allocated
576 \param name the name of the attribute.
577 \param result the BString to be set to the value of the attribute.
579 \return \c B_OK on success or an error code otherwise.
580 \retval B_OK Everything went fine.
581 \retval B_BAD_VALUE \a name or \a result was \c NULL.
582 \retval B_FILE_ERROR The object was not initialized.
583 \retval B_ENTRY_NOT_FOUND The node had no attribute \a attr.
601 \fn BNode& BNode::operator=(const BNode& other)
602 \brief Initializes the object as a copy of \a other.
604 \param other the BNode to be copied.
606 \returns A reference to this BNode object.
613 \fn bool BNode::operator==(const BNode &node) const
614 \brief Tests whether this and the supplied BNode object are equal.
616 Two BNode objects are said to be equal if they're set to the same node,
617 or if they're both unintialized
619 \param node The BNode to be compared against.
621 \return \c true, if the BNode objects are equal, \c false otherwise.
628 \fn bool BNode::operator!=(const BNode &node) const
629 \brief Tests whether this and the supplied BNode object are not equal.
631 Two BNode objects are said to be equal if they're set to the same node,
632 or if they're both unintialized.
634 \param node The BNode to be compared with
636 \return \c false, if the BNode objects are equal, \c true otherwise.