BTRFS: Reimplement TreeIterator, add some error checks and remove redundancies.
[haiku.git] / docs / user / storage / Entry.dox
blob5d3df3e530414f4b89bf9b3b383b559be7e9f8c2
1 /*
2  * Copyright 2011-2014 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *              Tyler Dauwalder
7  *              Simon Cusack, scusack@users.sf.net
8  *              John Scipione, jscipione@gmail.com
9  *
10  * Corresponds to:
11  *              headers/os/storage/Entry.h      hrev47402
12  *              src/kits/storage/Entry.cpp      hrev47402
13  */
16 /*!
17         \file Entry.h
18         \ingroup storage
19         \ingroup libbe
20         \brief Provides the BEntry class and entry_ref implementations.
24 /*!
25         \struct entry_ref
26         \ingroup storage
27         \ingroup libbe
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
32         \b must exist.
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
47         for them).
49         \since BeOS R3
53 /*!
54         \fn entry_ref::entry_ref()
55         \brief Creates an uninitialized entry_ref object.
57         \since BeOS R3
61 /*!
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.
75         \since BeOS R3
79 /*!
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.
85         \since BeOS R3
89 /*!
90         \fn entry_ref::~entry_ref()
91         \brief Destroys the object and frees the storage allocated for the leaf
92                name, if necessary.
94         \since BeOS R3
98 /*!
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.
106         \since BeOS R3
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.
117         \since BeOS R3
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.
128         \since BeOS R3
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.
140         \since BeOS R5
145         \var dev_t entry_ref::device
147         The device id of the storage device on which the entry resides.
149         \since BeOS R3
154         \var ino_t entry_ref::directory
156         The inode number of the directory in which the entry resides.
158         \since BeOS R3
163         \var char *entry_ref::name
165         The leaf name of the entry
167         \since BeOS R3
172         \class BEntry
173         \ingroup storage
174         \ingroup libbe
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:
181 \code
182 BEntry entry("/boot/home/fido");
183 \endcode
185         the resulting BEntry object represents the location of the file \c fido
186         within the <tt>/boot/home</tt> directory.
191         \fn BEntry::BEntry()
192         \brief Creates an uninitialized BEntry object.
194         Should be followed by a call to one of the SetTo() methods, or an
195         assignment.
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&)
202         \since BeOS R3
207         \fn BEntry::BEntry(const BDirectory* dir, const char* path, bool traverse)
208         \brief Creates a BEntry initialized to the given directory and path
209                 combination.
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
213         symlink itself.
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)
221         \since BeOS R3
226         \fn BEntry::BEntry(const entry_ref* ref, bool traverse)
227         \brief Creates a BEntry for the file referred to by the passed in
228                 entry_ref.
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)
239         \since BeOS R3
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)
257         \since BeOS R3
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&)
269         \since BeOS R3
274         \fn BEntry::~BEntry()
275         \brief Frees all of the allocated resources of the BEntry.
277         \sa Unset()
279         \since BeOS R3
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.
288         \retval B_OK Success
289         \retval B_NO_INIT The object has been Unset() or is uninitialized.
291         \since BeOS R3
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.
301         \since BeOS R3
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
310         by \a result.
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.
320         \since BeOS R3
325         \fn status_t BEntry::SetTo(const BDirectory* dir, const char* path,
326                 bool traverse)
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.
336         \since BeOS R3
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)
352         \since BeOS R3
357         \fn status_t BEntry::SetTo(const char* path, bool traverse)
358         \brief Reinitializes the BEntry object to the path, resolving symlinks if
359                traverse is \c true.
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)
368         \since BeOS R3
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().
380         \since BeOS R3
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
389                 result is copied.
391         \returns \c B_OK on success, or an error code on failure.
393         \since BeOS R3
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
402                 result is copied.
404         \returns \c B_OK on success, or an error code on failure.
406         \since BeOS R3
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.:
417 \code
418 BEntry entry("/boot/home/fido");
419 status_t err;
420 char name[B_FILE_NAME_LENGTH];
422 // Spit out the path components backwards, one at a time.
423 do {
424         entry.GetName(name);
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));
431 \endcode
433         will output:
435 \code
436 > fido
437 > home
438 > boot
439 > .
440 \endcode
442         \param entry A pointer to a pre-allocated BEntry object into which the
443                result is stored.
445         \returns A status code.
446         \retval B_OK Success
447         \retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root
448                 directory.
450         \since BeOS R3
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
461                result is copied.
463         \returns A status code.
464         \retval B_OK Success
465         \retval B_ENTRY_NOT_FOUND Attempted to get the parent of the root
466                 directory.
468         \since BeOS R3
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
480                is copied.
482         \returns \c B_OK on success, or an error code on failure.
484         \since BeOS R3
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.
504         \retval B_OK Success
505         \retval B_ENTRY_EXISTS The new location already exists and \c clobber
506                 is \c false.
507         \retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry.
509         \since BeOS R3
514         \fn status_t BEntry::MoveTo(BDirectory* dir, const char* path,
515                 bool clobber)
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
523                should be moved.
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.
532         \retval B_OK Success
533         \retval B_ENTRY_EXISTS The new location already exists and \c clobber
534                 is \c false.
535         \retval B_ENTRY_NOT_FOUND Attempted to rename an abstract entry.
537         \since BeOS R3
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.
552         \since BeOS R3
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
563                 both uninitialized.
564         \retval false The BEntry objects refer to different entries.
566         \since BeOS R3
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
578                 both uninitialized.
580         \since BeOS R3
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.
590         \since BeOS R3
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.
607         \since BeOS R4
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.
618         \retval true a < b
619         \retval false a >= b
621         \since Haiku R1