2 * Copyright 2011-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 * Simon Cusack, scusack@users.sf.net
8 * John Scipione, jscipione@gmail.com
11 * headers/os/storage/Entry.h hrev47402
12 * src/kits/storage/Entry.cpp hrev47402
20 \brief Provides the BEntry class and entry_ref implementations.
28 \brief A filesystem entry represented as a name in a concrete directory.
30 entry_refs may refer to pre-existing (concrete) files, as well as
31 non-existing (abstract) files. However, the parent directory of the file
34 The result of this dichotomy is a blending of the persistence gained by
35 referring to entries with a reference to their internal filesystem node and
36 the flexibility gained by referring to entries by name.
38 For example, if the directory in which the entry resides (or a directory
39 further up in the hierarchy) is moved or renamed, the entry_ref will still
40 refer to the correct file (whereas a pathname to the previous location of
41 the file would now be invalid).
43 On the other hand, say that the entry_ref refers to a concrete file. If the
44 file itself is renamed, the entry_ref now refers to an abstract file with
45 the old name (the upside in this case is that abstract entries may be
46 represented by entry_refs without preallocating an internal filesystem node
54 \fn entry_ref::entry_ref()
55 \brief Creates an uninitialized entry_ref object.
62 \fn entry_ref::entry_ref(dev_t dev, ino_t dir, const char* name)
63 \brief Creates an entry_ref object initialized to the given file name in
64 the given directory on the given device.
66 \a name may refer to either a pre-existing file in the given directory, or
67 a non-existent file. No explicit checking is done to verify validity of the
68 given arguments, but later use of the entry_ref will fail if \p dev is not
69 a valid device or \a dir is a not a directory on \p dev.
71 \param dev The \a device on which the entry's parent directory resides.
72 \param dir The directory in which the entry resides.
73 \param name The leaf name of the entry, which is not required to exist.
80 \fn entry_ref::entry_ref(const entry_ref& ref)
81 \brief Returns a copy of the passed in entry_ref object.
83 \param ref A reference to an entry_ref to copy.
90 \fn entry_ref::~entry_ref()
91 \brief Destroys the object and frees the storage allocated for the leaf
99 \fn status_t entry_ref::set_name(const char* name)
100 \brief Set the entry_ref's leaf name, freeing the storage allocated for any
101 previous name and then making a copy of the new name.
103 \param name Pointer to a null-terminated string containing the new name for
104 the entry. May be \c NULL.
111 \fn bool entry_ref::operator==(const entry_ref& ref) const
112 \brief Compares the entry_ref object with the passed in entry_ref,
113 returning \c true if they are equal.
115 \returns \c true if he entry_refs are equal, \c false otherwise.
122 \fn bool entry_ref::operator!=(const entry_ref& ref) const
123 \brief Compares the entry_ref object with the passed in entry_ref,
124 returning \c true if they are NOT equal.
126 \returns \c true if the entry_refs are NOT equal, \c false otherwise.
133 \fn entry_ref& entry_ref::operator=(const entry_ref& ref)
134 \brief Makes the entry_ref object a copy of the passed in entry_ref.
136 \param ref The entry_ref to copy.
138 \returns A pointer to the resulting entry_ref copy.
145 \var dev_t entry_ref::device
147 The device id of the storage device on which the entry resides.
154 \var ino_t entry_ref::directory
156 The inode number of the directory in which the entry resides.
163 \var char *entry_ref::name
165 The leaf name of the entry
175 \brief A location in the filesystem.
177 The BEntry class defines objects that represent "locations" in the file
178 system hierarchy. Each location (or entry) is given as a name within a
179 directory. For example, if you create a BEntry like this:
182 BEntry entry("/boot/home/fido");
185 the resulting BEntry object represents the location of the file \c fido
186 within the <tt>/boot/home</tt> directory.
192 \brief Creates an uninitialized BEntry object.
194 Should be followed by a call to one of the SetTo() methods, or an
197 \sa SetTo(const BDirectory*, const char*, bool)
198 \sa SetTo(const entry_ref*, bool)
199 \sa SetTo(const char*, bool)
200 \sa operator=(const BEntry&)
207 \fn BEntry::BEntry(const BDirectory* dir, const char* path, bool traverse)
208 \brief Creates a BEntry initialized to the given directory and path
211 If traverse is \c true and \a path refers to a symlink, the BEntry will
212 refer to the linked file; if \c false, the BEntry will refer to the
215 \param dir The base directory in which the \a path resides.
216 \param path Relative path based off of \a dir.
217 \param traverse Whether or not to traverse symbolic links.
219 \see BEntry::SetTo(const BDirectory*, const char *, bool)
226 \fn BEntry::BEntry(const entry_ref* ref, bool traverse)
227 \brief Creates a BEntry for the file referred to by the passed in
230 If traverse is \c true and \a ref refers to a symlink, the BEntry
231 will refer to the linked file; if \c false, the BEntry will refer
232 to the symlink itself.
234 \param ref The entry_ref referring to the given file.
235 \param traverse Whether or not symlinks are to be traversed.
237 \sa BEntry::SetTo(const entry_ref*, bool)
244 \fn BEntry::BEntry(const char* path, bool traverse)
245 \brief Creates a BEntry initialized to the given path.
247 If \a path is relative, it will be reckoned off the current working
248 directory. If \a path refers to a symlink and traverse is \c true, the
249 BEntry will refer to the linked file. If traverse is \c false, the
250 BEntry will refer to the symlink itself.
252 \param path The path of the file.
253 \param traverse Whether or not symlinks are to be traversed.
255 \sa BEntry::SetTo(const char*, bool)
262 \fn BEntry::BEntry(const BEntry& entry)
263 \brief Creates a copy of the given BEntry.
265 \param entry the entry to be copied
267 \sa operator=(const BEntry&)
274 \fn BEntry::~BEntry()
275 \brief Frees all of the allocated resources of the BEntry.
284 \fn status_t BEntry::InitCheck() const
285 \brief Returns the result of the most recent construction or SetTo() call.
287 \returns A status code.
289 \retval B_NO_INIT The object has been Unset() or is uninitialized.
296 \fn bool BEntry::Exists() const
297 \brief Returns whether or not the entry exists in the filesystem.
299 \returns \c true if the entry exists, \c false if not.
306 \fn status_t BEntry::GetStat(struct stat *result) const
307 \brief Fills in a stat structure for the entry.
309 The information is copied into the \c stat structure pointed to
312 \note The BStatable object does not cache the stat structure -- each time
313 you call GetStat() fresh stat information is retrieved.
315 \param result A pointer to a pre-allocated structure into which the stat
316 information is copied.
318 \returns \c B_OK on success, or an error code on failure.
325 \fn status_t BEntry::SetTo(const BDirectory* dir, const char* path,
327 \brief Reinitializes the BEntry to the path or directory path combination,
328 resolving symlinks if traverse is \c true.
330 \param dir The base directory in which the \a path resides.
331 \param path Relative path based off of \a dir.
332 \param traverse Whether or not to traverse symbolic links.
334 \returns \c B_OK on success, or an error code on failure.
341 \fn status_t BEntry::SetTo(const entry_ref* ref, bool traverse)
342 \brief Reinitializes the BEntry to the passed in entry_ref object
343 resolving symlinks if traverse is \c true.
345 \param ref The entry_ref referring to the given file.
346 \param traverse Whether or not symlinks are to be traversed.
348 \returns \c B_OK on success, or an error code on failure.
350 \sa BEntry::BEntry(const entry_ref* ref, bool traverse)
357 \fn status_t BEntry::SetTo(const char* path, bool traverse)
358 \brief Reinitializes the BEntry object to the path, resolving symlinks if
361 \param path The path of the file.
362 \param traverse Whether or not symlinks are to be traversed.
364 \returns \c B_OK on success, or an error code on failure.
366 \sa BEntry::BEntry(const char* path, bool traverse)
373 \fn void BEntry::Unset()
374 \brief Reset the BEntry to the uninitialized state
376 The BEntry forgets any information about the entry it was referencing.
377 Any attempt to get information from it after this will return B_NO_INIT.
378 The BEntry can later be pointed to another entry using SetTo().
385 \fn status_t BEntry::GetRef(entry_ref* ref) const
386 \brief Gets an entry_ref structure for the BEntry.
388 \param ref A pointer to a preallocated entry_ref object into which the
391 \returns \c B_OK on success, or an error code on failure.
398 \fn status_t BEntry::GetPath(BPath* path) const
399 \brief Gets the path for the BEntry.
401 \param path A pointer to a pre-allocated BPath object into which the
404 \returns \c B_OK on success, or an error code on failure.
411 \fn status_t BEntry::GetParent(BEntry* entry) const
412 \brief Gets the parent of the BEntry as a BEntry.
414 If the function fails, the argument is Unset(). Destructive calls to
415 GetParent() are allowed, i.e.:
418 BEntry entry("/boot/home/fido");
420 char name[B_FILE_NAME_LENGTH];
422 // Spit out the path components backwards, one at a time.
425 printf("> %s\n", name);
426 } while ((err=entry.GetParent(&entry)) == B_OK);
428 // Complain for reasons other than reaching the top.
429 if (err != B_ENTRY_NOT_FOUND)
430 printf(">> Error: %s\n", strerror(err));
442 \param entry A pointer to a pre-allocated BEntry object into which the
445 \returns A status code.
447 \retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root
455 \fn status_t BEntry::GetParent(BDirectory* dir) const
456 \brief Gets the parent of the BEntry as a BDirectory.
458 If the function fails, the argument is Unset().
460 \param dir A pointer to a pre-allocated BDirectory object into which the
463 \returns A status code.
465 \retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root
473 \fn status_t BEntry::GetName(char* buffer) const
474 \brief Gets the name of the leaf of the BEntry object.
476 \c buffer must be pre-allocated and of sufficient length to hold the
477 entire string. A length of \c B_FILE_NAME_LENGTH is recommended.
479 \param buffer A pointer to a pre-allocated string into which the result
482 \returns \c B_OK on success, or an error code on failure.
489 \fn status_t BEntry::Rename(const char* path, bool clobber)
490 \brief Renames the BEntry to \a path replacing an existing entry
491 if \a clobber is \c true.
493 \note The BEntry object must refer to an existing file, if it is abstract,
494 this method will fail.
496 \param path A pointer to a string containing the new name for the entry.
497 It may be an absolute or relative path. If it is a relative path
498 the entry is renamed within its current directory.
499 \param clobber If \c false and a file with the name given by \c path
500 already exists, the method will fail. If \c true and such a file
501 exists, it will be overwritten.
503 \returns A status code.
505 \retval B_ENTRY_EXISTS The new location already exists and \c clobber
507 \retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry.
514 \fn status_t BEntry::MoveTo(BDirectory* dir, const char* path,
516 \brief Moves the BEntry to directory or directory and path combination,
517 replacing an existing entry if clobber is true.
519 \note The BEntry object must refer to an existing file, if it is abstract,
520 this method will fail.
522 \param dir A pointer to a pre-allocated BDirectory into which the entry
524 \param path (optional) new leaf name for the entry. May be a simple leaf
525 or a relative path; either way, \c path is reckoned off of \c dir.
526 If \c NULL, the entry retains its previous leaf name.
527 \param clobber If \c false and an entry already exists at the specified
528 Spdestination, the method will fail. If \c true and such an entry
529 exists, it will be overwritten.
531 \returns A status code.
533 \retval B_ENTRY_EXISTS The new location already exists and \c clobber
535 \retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry.
542 \fn status_t BEntry::Remove()
543 \brief Removes the entry from the file system.
545 \note If any file descriptors are open on the file when Remove() is called
546 the chunk of data they refer to will continue to exist until all such file
547 descriptors are closed. The BEntry object, however, becomes abstract and
548 no longer refers to any actual data in the filesystem.
550 \returns \c B_OK on success, or an error code on failure.
557 \fn bool BEntry::operator==(const BEntry& item) const
558 \brief Returns \c true if the BEntry and \a item refer to the same entry
559 or if they are both uninitialized.
561 \returns Whether or not the items refer to the same entry.
562 \retval true Both BEntry objects refer to the same entry or they are
564 \retval false The BEntry objects refer to different entries.
571 \fn bool BEntry::operator!=(const BEntry& item) const
572 \brief Returns false if the BEntry and \c item refer to the same entry or
573 if they are both uninitialized.
575 \returns Whether or not the items do NOT refer to the same entry.
576 \retval true The BEntry objects refer to different entries.
577 \retval false Both BEntry objects refer to the same entry or they are
585 \fn BEntry& BEntry::operator=(const BEntry& item)
586 \brief Reinitializes the BEntry to be a copy of \a item.
588 \returns A pointer to the copy.
595 \fn status_t get_ref_for_path(const char* path, entry_ref* ref)
596 \brief Returns an entry_ref for a given path.
598 \param path The path name referring to the entry.
599 \param ref The entry_ref structure to be filled in.
601 \returns A status code.
602 \retval B_OK Everything went fine.
603 \retval B_BAD_VALUE \c NULL \a path or \a ref.
604 \retval B_ENTRY_NOT_FOUND A (non-leaf) path component does not exist.
605 \retval B_NO_MEMORY Insufficient memory for successful completion.
612 \fn bool operator<(const entry_ref& a, const entry_ref& b)
613 \brief Returns whether an entry is less than another.
615 The components are compared in order \c device, \c directory, \c name.
616 A \c NULL \c name is less than any non-<tt>NULL</tt> name.