* Rewrote header.

* Cleaned up private method names as much as possible (it's probably not a good
  idea to rename BStatable::set_stat() for compatibility reasons).
* Cleaned up sources.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34690 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2009-12-18 15:05:37 +00:00
parent dbf4cd6df1
commit 861f2ade03
2 changed files with 310 additions and 269 deletions
+69 -81
View File
@@ -2,128 +2,116 @@
* Copyright 2002-2009, Haiku, Inc. All Rights Reserved. * Copyright 2002-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
/*!
\file Entry.h
BEntry and entry_ref interface declarations.
*/
#ifndef _ENTRY_H #ifndef _ENTRY_H
#define _ENTRY_H #define _ENTRY_H
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <SupportDefs.h> #include <SupportDefs.h>
#include <Statable.h> #include <Statable.h>
#ifdef USE_OPENBEOS_NAMESPACE
namespace OpenBeOS {
#endif
class BDirectory; class BDirectory;
class BPath; class BPath;
struct entry_ref { struct entry_ref {
entry_ref(); entry_ref();
entry_ref(dev_t dev, ino_t dir, const char *name); entry_ref(dev_t dev, ino_t dir,
entry_ref(const entry_ref &ref); const char* name);
~entry_ref(); entry_ref(const entry_ref& ref);
~entry_ref();
status_t set_name(const char *name); status_t set_name(const char* name);
bool operator==(const entry_ref &ref) const; bool operator==(const entry_ref& ref) const;
bool operator!=(const entry_ref &ref) const; bool operator!=(const entry_ref& ref) const;
entry_ref &operator=(const entry_ref &ref); entry_ref& operator=(const entry_ref& ref);
dev_t device; dev_t device;
ino_t directory; ino_t directory;
char *name; char* name;
}; };
class BEntry : public BStatable { class BEntry : public BStatable {
public: public:
BEntry(); BEntry();
BEntry(const BDirectory *dir, const char *path, bool traverse = false); BEntry(const BDirectory* dir, const char* path,
BEntry(const entry_ref *ref, bool traverse = false); bool traverse = false);
BEntry(const char *path, bool traverse = false); BEntry(const entry_ref* ref,
BEntry(const BEntry &entry); bool traverse = false);
virtual ~BEntry(); BEntry(const char* path, bool traverse = false);
BEntry(const BEntry& entry);
virtual ~BEntry();
status_t InitCheck() const; status_t InitCheck() const;
bool Exists() const; bool Exists() const;
virtual status_t GetStat(struct stat *st) const; virtual status_t GetStat(struct stat* stat) const;
status_t SetTo(const BDirectory *dir, const char *path, status_t SetTo(const BDirectory* dir, const char* path,
bool traverse = false); bool traverse = false);
status_t SetTo(const entry_ref *ref, bool traverse = false); status_t SetTo(const entry_ref* ref,
status_t SetTo(const char *path, bool traverse = false); bool traverse = false);
void Unset(); status_t SetTo(const char* path, bool traverse = false);
void Unset();
status_t GetRef(entry_ref *ref) const; status_t GetRef(entry_ref *ref) const;
status_t GetPath(BPath *path) const; status_t GetPath(BPath *path) const;
status_t GetParent(BEntry *entry) const; status_t GetParent(BEntry *entry) const;
status_t GetParent(BDirectory *dir) const; status_t GetParent(BDirectory *dir) const;
status_t GetName(char *buffer) const; status_t GetName(char *buffer) const;
status_t Rename(const char *path, bool clobber = false); status_t Rename(const char *path, bool clobber = false);
status_t MoveTo(BDirectory *dir, const char *path = NULL, status_t MoveTo(BDirectory *dir, const char *path = NULL,
bool clobber = false); bool clobber = false);
status_t Remove(); status_t Remove();
bool operator==(const BEntry &item) const; bool operator==(const BEntry &item) const;
bool operator!=(const BEntry &item) const; bool operator!=(const BEntry &item) const;
BEntry &operator=(const BEntry &item); BEntry &operator=(const BEntry &item);
private: private:
friend class BDirectory; friend class BDirectory;
friend class BFile; friend class BFile;
friend class BNode; friend class BNode;
friend class BSymLink; friend class BSymLink;
virtual void _PennyEntry1(); virtual void _PennyEntry1();
virtual void _PennyEntry2(); virtual void _PennyEntry2();
virtual void _PennyEntry3(); virtual void _PennyEntry3();
virtual void _PennyEntry4(); virtual void _PennyEntry4();
virtual void _PennyEntry5(); virtual void _PennyEntry5();
virtual void _PennyEntry6(); virtual void _PennyEntry6();
/*! BEntry implementation of BStatable::set_stat() */ /*! BEntry implementation of BStatable::set_stat() */
virtual status_t set_stat(struct stat &st, uint32 what); virtual status_t set_stat(struct stat& stat, uint32 what);
status_t _SetTo(int dir, const char* path,
bool traverse);
status_t _SetName(const char* name);
status_t set(int dir, const char *path, bool traverse); status_t _Rename(BEntry& target, bool clobber);
status_t set_name(const char *name); void _Dump(const char* name = NULL);
status_t _Rename(BEntry& target, bool clobber); status_t _GetStat(struct stat* stat) const;
virtual status_t _GetStat(struct stat_beos* stat) const;
void Dump(const char *name = NULL);
status_t _GetStat(struct stat *st) const;
virtual status_t _GetStat(struct stat_beos *st) const;
private: private:
/*! Currently unused. */ int fDirFd;
uint32 _pennyData[4]; char* fName;
status_t fCStatus;
/*! File descriptor for the entry's parent directory. */ uint32 _reserved[4];
int fDirFd;
/*! Leaf name of the entry. */
char *fName;
/*! The object's initialization status. */
status_t fCStatus;
}; };
// C functions
status_t get_ref_for_path(const char *path, entry_ref *ref); status_t get_ref_for_path(const char* path, entry_ref* ref);
bool operator<(const entry_ref &a, const entry_ref &b); bool operator<(const entry_ref& a, const entry_ref& b);
#ifdef USE_OPENBEOS_NAMESPACE
}; // namespace OpenBeOS
#endif
#endif // _ENTRY_H #endif // _ENTRY_H
+241 -188
View File
@@ -13,6 +13,7 @@
BEntry and entry_ref implementations. BEntry and entry_ref implementations.
*/ */
#include <Entry.h> #include <Entry.h>
#include <fcntl.h> #include <fcntl.h>
@@ -42,79 +43,91 @@ using namespace std;
/*! \struct entry_ref /*! \struct entry_ref
\brief A filesystem entry represented as a name in a concrete directory. \brief A filesystem entry represented as a name in a concrete directory.
entry_refs may refer to pre-existing (concrete) files, as well as non-existing entry_refs may refer to pre-existing (concrete) files, as well as
(abstract) files. However, the parent directory of the file \b must exist. non-existing (abstract) files. However, the parent directory of the file
\b must exist.
The result of this dichotomy is a blending of the persistence gained by referring The result of this dichotomy is a blending of the persistence gained by
to entries with a reference to their internal filesystem node and the flexibility gained referring to entries with a reference to their internal filesystem node and
by referring to entries by name. the flexibility gained by referring to entries by name.
For example, if the directory in which the entry resides (or a For example, if the directory in which the entry resides (or a directory
directory further up in the hierarchy) is moved or renamed, the entry_ref will further up in the hierarchy) is moved or renamed, the entry_ref will still
still refer to the correct file (whereas a pathname to the previous location of the refer to the correct file (whereas a pathname to the previous location of
file would now be invalid). the file would now be invalid).
On the other hand, say that the entry_ref refers to a concrete file. If the file On the other hand, say that the entry_ref refers to a concrete file. If the
itself is renamed, the entry_ref now refers to an abstract file with the old name file itself is renamed, the entry_ref now refers to an abstract file with
(the upside in this case is that abstract entries may be represented by entry_refs the old name (the upside in this case is that abstract entries may be
without preallocating an internal filesystem node for them). represented by entry_refs without preallocating an internal filesystem node
for them).
*/ */
//! Creates an unitialized entry_ref. /*! Creates an unitialized entry_ref.
*/
entry_ref::entry_ref() entry_ref::entry_ref()
: device((dev_t)-1), :
directory((ino_t)-1), device((dev_t)-1),
name(NULL) directory((ino_t)-1),
name(NULL)
{ {
} }
/*! \brief Creates an entry_ref initialized to the given file name in the given /*! \brief Creates an entry_ref initialized to the given file name in the given
directory on the given device. directory on the given device.
\p name may refer to either a pre-existing file in the given \p name may refer to either a pre-existing file in the given directory, or
directory, or a non-existent file. No explicit checking is done to verify validity of the given arguments, but a non-existent file. No explicit checking is done to verify validity of the
later use of the entry_ref will fail if \p dev is not a valid device or \p dir given arguments, but later use of the entry_ref will fail if \p dev is not
is a not a directory on \p dev. a valid device or \p dir is a not a directory on \p dev.
\param dev the device on which the entry's parent directory resides \param dev the device on which the entry's parent directory resides
\param dir the directory in which the entry resides \param dir the directory in which the entry resides
\param name the leaf name of the entry, which is not required to exist \param name the leaf name of the entry, which is not required to exist
*/ */
entry_ref::entry_ref(dev_t dev, ino_t dir, const char *name) entry_ref::entry_ref(dev_t dev, ino_t dir, const char* name)
: device(dev), :
directory(dir), device(dev),
name(NULL) directory(dir),
name(NULL)
{ {
set_name(name); set_name(name);
} }
/*! \brief Creates a copy of the given entry_ref. /*! \brief Creates a copy of the given entry_ref.
\param ref a reference to an entry_ref to copy \param ref a reference to an entry_ref to copy
*/ */
entry_ref::entry_ref(const entry_ref &ref) entry_ref::entry_ref(const entry_ref& ref)
: device(ref.device), :
directory(ref.directory), device(ref.device),
name(NULL) directory(ref.directory),
name(NULL)
{ {
set_name(ref.name); set_name(ref.name);
} }
//! Destroys the object and frees the storage allocated for the leaf name, if necessary.
/*! \brief Destroys the object and frees the storage allocated for the leaf
name, if necessary.
*/
entry_ref::~entry_ref() entry_ref::~entry_ref()
{ {
free(name); free(name);
} }
/*! \brief Set the entry_ref's leaf name, freeing the storage allocated for any previous
name and then making a copy of the new name. /*! \brief Set the entry_ref's leaf name, freeing the storage allocated for any
previous name and then making a copy of the new name.
\param name pointer to a null-terminated string containing the new name for \param name pointer to a null-terminated string containing the new name for
the entry. May be \c NULL. the entry. May be \c NULL.
*/ */
status_t status_t
entry_ref::set_name(const char *name) entry_ref::set_name(const char* name)
{ {
free(this->name); free(this->name);
@@ -129,39 +142,44 @@ entry_ref::set_name(const char *name)
return B_OK; return B_OK;
} }
/*! \brief Compares the entry_ref with another entry_ref, returning true if they are equal.
/*! \brief Compares the entry_ref with another entry_ref, returning true if
they are equal.
\return \return
- \c true - The entry_refs are equal - \c true - The entry_refs are equal
- \c false - The entry_refs are not equal - \c false - The entry_refs are not equal
*/ */
bool bool
entry_ref::operator==(const entry_ref &ref) const entry_ref::operator==(const entry_ref& ref) const
{ {
return (device == ref.device return (device == ref.device
&& directory == ref.directory && directory == ref.directory
&& (name == ref.name && (name == ref.name
|| (name != NULL && ref.name != NULL || (name != NULL && ref.name != NULL
&& strcmp(name, ref.name) == 0))); && strcmp(name, ref.name) == 0)));
} }
/*! \brief Compares the entry_ref with another entry_ref, returning true if they are not equal.
/*! \brief Compares the entry_ref with another entry_ref, returning true if
they are not equal.
\return \return
- \c true - The entry_refs are not equal - \c true - The entry_refs are not equal
- \c false - The entry_refs are equal - \c false - The entry_refs are equal
*/ */
bool bool
entry_ref::operator!=(const entry_ref &ref) const entry_ref::operator!=(const entry_ref& ref) const
{ {
return !(*this == ref); return !(*this == ref);
} }
/*! \brief Makes the entry_ref a copy of the entry_ref specified by \a ref. /*! \brief Makes the entry_ref a copy of the entry_ref specified by \a ref.
\param ref the entry_ref to copy \param ref the entry_ref to copy
\return \return
- A reference to the copy - A reference to the copy
*/ */
entry_ref& entry_ref&
entry_ref::operator=(const entry_ref &ref) entry_ref::operator=(const entry_ref& ref)
{ {
if (this == &ref) if (this == &ref)
return *this; return *this;
@@ -172,19 +190,17 @@ entry_ref::operator=(const entry_ref &ref)
return *this; return *this;
} }
/*!
\var dev_t entry_ref::device /*! \var dev_t entry_ref::device
\brief The device id of the storage device on which the entry resides \brief The device id of the storage device on which the entry resides
*/ */
/*! /*! \var ino_t entry_ref::directory
\var ino_t entry_ref::directory
\brief The inode number of the directory in which the entry resides \brief The inode number of the directory in which the entry resides
*/ */
/*! /*! \var char *entry_ref::name
\var char *entry_ref::name
\brief The leaf name of the entry \brief The leaf name of the entry
*/ */
@@ -192,8 +208,7 @@ entry_ref::operator=(const entry_ref &ref)
// #pragma mark - BEntry // #pragma mark - BEntry
/*! /*! \class BEntry
\class BEntry
\brief A location in the filesystem \brief A location in the filesystem
The BEntry class defines objects that represent "locations" in the file system The BEntry class defines objects that represent "locations" in the file system
@@ -214,9 +229,10 @@ entry_ref::operator=(const entry_ref &ref)
\version 0.0.0 \version 0.0.0
*/ */
//! Creates an uninitialized BEntry object.
/*! Should be followed by a call to one of the SetTo functions, /*! \brief Creates an uninitialized BEntry object.
or an assignment: Should be followed by a call to one of the SetTo functions, or an
assignment:
- SetTo(const BDirectory*, const char*, bool) - SetTo(const BDirectory*, const char*, bool)
- SetTo(const entry_ref*, bool) - SetTo(const entry_ref*, bool)
- SetTo(const char*, bool) - SetTo(const char*, bool)
@@ -230,26 +246,31 @@ BEntry::BEntry()
{ {
} }
//! Creates a BEntry initialized to the given directory and path combination.
/*! If traverse is true and \c dir/path refers to a symlink, the BEntry will /*! \brief Creates a BEntry initialized to the given directory and path
refer to the linked file; if false, the BEntry will refer to the symlink itself. combination.
If traverse is true and \c dir/path refers to a symlink, the BEntry will
refer to the linked file; if false, the BEntry will refer to the symlink
itself.
\param dir directory in which \a path resides \param dir directory in which \a path resides
\param path relative path reckoned off of \a dir \param path relative path reckoned off of \a dir
\param traverse whether or not to traverse symlinks \param traverse whether or not to traverse symlinks
\see SetTo(const BDirectory*, const char *, bool) \see SetTo(const BDirectory*, const char *, bool)
*/ */
BEntry::BEntry(const BDirectory *dir, const char *path, bool traverse) BEntry::BEntry(const BDirectory* dir, const char* path, bool traverse)
: fDirFd(-1), :
fName(NULL), fDirFd(-1),
fCStatus(B_NO_INIT) fName(NULL),
fCStatus(B_NO_INIT)
{ {
SetTo(dir, path, traverse); SetTo(dir, path, traverse);
} }
//! Creates a BEntry for the file referred to by the given entry_ref.
/*! If traverse is true and \a ref refers to a symlink, the BEntry /*! \brief Creates a BEntry for the file referred to by the given entry_ref.
If traverse is true and \a ref refers to a symlink, the BEntry
will refer to the linked file; if false, the BEntry will refer will refer to the linked file; if false, the BEntry will refer
to the symlink itself. to the symlink itself.
@@ -257,56 +278,62 @@ BEntry::BEntry(const BDirectory *dir, const char *path, bool traverse)
\param traverse whether or not symlinks are to be traversed \param traverse whether or not symlinks are to be traversed
\see SetTo(const entry_ref*, bool) \see SetTo(const entry_ref*, bool)
*/ */
BEntry::BEntry(const entry_ref* ref, bool traverse)
BEntry::BEntry(const entry_ref *ref, bool traverse) :
: fDirFd(-1), fDirFd(-1),
fName(NULL), fName(NULL),
fCStatus(B_NO_INIT) fCStatus(B_NO_INIT)
{ {
SetTo(ref, traverse); SetTo(ref, traverse);
} }
//! Creates a BEntry initialized to the given path.
/*! If \a path is relative, it will /*! \brief Creates a BEntry initialized to the given path.
be reckoned off the current working directory. If \a path refers to a symlink and
traverse is true, the BEntry will refer to the linked file. If traverse is false, If \a path is relative, it will be reckoned off the current working
the BEntry will refer to the symlink itself. directory. If \a path refers to a symlink and traverse is true, the BEntry
will refer to the linked file. If traverse is false, the BEntry will refer
to the symlink itself.
\param path the file of interest \param path the file of interest
\param traverse whether or not symlinks are to be traversed \param traverse whether or not symlinks are to be traversed
\see SetTo(const char*, bool) \see SetTo(const char*, bool)
*/ */
BEntry::BEntry(const char *path, bool traverse) BEntry::BEntry(const char* path, bool traverse)
: fDirFd(-1), :
fName(NULL), fDirFd(-1),
fCStatus(B_NO_INIT) fName(NULL),
fCStatus(B_NO_INIT)
{ {
SetTo(path, traverse); SetTo(path, traverse);
} }
//! Creates a copy of the given BEntry.
/*! \param entry the entry to be copied /*! \brief Creates a copy of the given BEntry.
\param entry the entry to be copied
\see operator=(const BEntry&) \see operator=(const BEntry&)
*/ */
BEntry::BEntry(const BEntry &entry) BEntry::BEntry(const BEntry& entry)
: fDirFd(-1), :
fName(NULL), fDirFd(-1),
fCStatus(B_NO_INIT) fName(NULL),
fCStatus(B_NO_INIT)
{ {
*this = entry; *this = entry;
} }
//! Frees all of the BEntry's allocated resources.
/*! \see Unset() /*! \brief Frees all of the BEntry's allocated resources.
\see Unset()
*/ */
BEntry::~BEntry() BEntry::~BEntry()
{ {
Unset(); Unset();
} }
//! Returns the result of the most recent construction or SetTo() call.
/*! \return /*! \brief Returns the result of the most recent construction or SetTo() call.
\return
- \c B_OK Success - \c B_OK Success
- \c B_NO_INIT The object has been Unset() or is uninitialized - \c B_NO_INIT The object has been Unset() or is uninitialized
- <code>some error code</code> - <code>some error code</code>
@@ -317,8 +344,9 @@ BEntry::InitCheck() const
return fCStatus; return fCStatus;
} }
//! Returns true if the Entry exists in the filesytem, false otherwise.
/*! \return /*! \brief Returns true if the Entry exists in the filesytem, false otherwise.
\return
- \c true - The entry exists - \c true - The entry exists
- \c false - The entry does not exist - \c false - The entry does not exist
*/ */
@@ -332,13 +360,14 @@ BEntry::Exists() const
/*! \fn status_t BEntry::GetStat(struct stat *result) const /*! \fn status_t BEntry::GetStat(struct stat *result) const
\brief Fills in a stat structure for the entry. The information is copied into \brief Fills in a stat structure for the entry. The information is copied
the \c stat structure pointed to by \a result. into the \c stat structure pointed to by \a result.
\b NOTE: The BStatable object does not cache the stat structure; every time you \b NOTE: The BStatable object does not cache the stat structure; every time
call GetStat(), fresh stat information is retrieved. you call GetStat(), fresh stat information is retrieved.
\param result pointer to a pre-allocated structure into which the stat information will be copied \param result pointer to a pre-allocated structure into which the stat
information will be copied
\return \return
- \c B_OK - Success - \c B_OK - Success
- "error code" - Failure - "error code" - Failure
@@ -353,7 +382,7 @@ BEntry::Exists() const
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::SetTo(const BDirectory *dir, const char *path, bool traverse) BEntry::SetTo(const BDirectory* dir, const char* path, bool traverse)
{ {
// check params // check params
if (!dir) if (!dir)
@@ -374,18 +403,19 @@ BEntry::SetTo(const BDirectory *dir, const char *path, bool traverse)
int dirFD = _kern_dup(dir->get_fd()); int dirFD = _kern_dup(dir->get_fd());
if (dirFD < 0) if (dirFD < 0)
return (fCStatus = dirFD); return (fCStatus = dirFD);
return (fCStatus = set(dirFD, path, traverse)); return (fCStatus = _SetTo(dirFD, path, traverse));
} }
/*! \brief Reinitializes the BEntry to the entry_ref, resolving symlinks if /*! \brief Reinitializes the BEntry to the entry_ref, resolving symlinks if
traverse is true traverse is true.
\return \return
- \c B_OK - Success - \c B_OK - Success
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::SetTo(const entry_ref *ref, bool traverse) BEntry::SetTo(const entry_ref* ref, bool traverse)
{ {
Unset(); Unset();
if (ref == NULL) if (ref == NULL)
@@ -399,35 +429,35 @@ BEntry::SetTo(const entry_ref *ref, bool traverse)
int dirFD = _kern_open_dir_entry_ref(ref->device, ref->directory, NULL); int dirFD = _kern_open_dir_entry_ref(ref->device, ref->directory, NULL);
if (dirFD < 0) if (dirFD < 0)
return (fCStatus = dirFD); return (fCStatus = dirFD);
return (fCStatus = set(dirFD, ref->name, traverse)); return (fCStatus = _SetTo(dirFD, ref->name, traverse));
} }
/*! \brief Reinitializes the BEntry object to the path, resolving symlinks if /*! \brief Reinitializes the BEntry object to the path, resolving symlinks if
traverse is true traverse is true.
\return \return
- \c B_OK - Success - \c B_OK - Success
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::SetTo(const char *path, bool traverse) BEntry::SetTo(const char* path, bool traverse)
{ {
Unset(); Unset();
// check the argument // check the argument
if (!path) if (!path)
return (fCStatus = B_BAD_VALUE); return (fCStatus = B_BAD_VALUE);
return (fCStatus = set(-1, path, traverse)); return (fCStatus = _SetTo(-1, path, traverse));
} }
/*! \brief Reinitializes the BEntry to an uninitialized BEntry object */ /*! \brief Reinitializes the BEntry to an uninitialized BEntry object */
void void
BEntry::Unset() BEntry::Unset()
{ {
// Close the directory // Close the directory
if (fDirFd >= 0) { if (fDirFd >= 0)
_kern_close(fDirFd); _kern_close(fDirFd);
// BPrivate::Storage::close_dir(fDirFd);
}
// Free our leaf name // Free our leaf name
free(fName); free(fName);
@@ -437,16 +467,18 @@ BEntry::Unset()
fCStatus = B_NO_INIT; fCStatus = B_NO_INIT;
} }
/*! \brief Gets an entry_ref structure for the BEntry. /*! \brief Gets an entry_ref structure for the BEntry.
\param ref pointer to a preallocated entry_ref into which the result is copied \param ref pointer to a preallocated entry_ref into which the result is
copied
\return \return
- \c B_OK - Success - \c B_OK - Success
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::GetRef(entry_ref *ref) const BEntry::GetRef(entry_ref* ref) const
{ {
if (fCStatus != B_OK) if (fCStatus != B_OK)
return B_NO_INIT; return B_NO_INIT;
@@ -465,16 +497,18 @@ BEntry::GetRef(entry_ref *ref) const
return error; return error;
} }
/*! \brief Gets the path for the BEntry. /*! \brief Gets the path for the BEntry.
\param path pointer to a pre-allocated BPath object into which the result is stored \param path pointer to a pre-allocated BPath object into which the result
is stored
\return \return
- \c B_OK - Success - \c B_OK - Success
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::GetPath(BPath *path) const BEntry::GetPath(BPath* path) const
{ {
if (fCStatus != B_OK) if (fCStatus != B_OK)
return B_NO_INIT; return B_NO_INIT;
@@ -485,10 +519,11 @@ BEntry::GetPath(BPath *path) const
return path->SetTo(this); return path->SetTo(this);
} }
/*! \brief Gets the parent of the BEntry as another BEntry. /*! \brief Gets the parent of the BEntry as another BEntry.
If the function fails, the argument is Unset(). Destructive calls to GetParent() are If the function fails, the argument is Unset(). Destructive calls to
allowed, i.e.: GetParent() are allowed, i.e.:
\code \code
BEntry entry("/boot/home/fido"); BEntry entry("/boot/home/fido");
@@ -515,13 +550,15 @@ BEntry::GetPath(BPath *path) const
> . > .
\endcode \endcode
\param entry pointer to a pre-allocated BEntry object into which the result is stored \param entry pointer to a pre-allocated BEntry object into which the result
is stored
\return \return
- \c B_OK - Success - \c B_OK - Success
- \c B_ENTRY_NOT_FOUND - Attempted to get the parent of the root directory \c "/" - \c B_ENTRY_NOT_FOUND - Attempted to get the parent of the root directory
\c "/"
- "error code" - Failure - "error code" - Failure
*/ */
status_t BEntry::GetParent(BEntry *entry) const status_t BEntry::GetParent(BEntry* entry) const
{ {
// check parameter and initialization // check parameter and initialization
if (fCStatus != B_OK) if (fCStatus != B_OK)
@@ -546,24 +583,27 @@ status_t BEntry::GetParent(BEntry *entry) const
// init the entry // init the entry
entry->Unset(); entry->Unset();
entry->fDirFd = parentFD; entry->fDirFd = parentFD;
entry->fCStatus = entry->set_name(leafName); entry->fCStatus = entry->_SetName(leafName);
if (entry->fCStatus != B_OK) if (entry->fCStatus != B_OK)
entry->Unset(); entry->Unset();
return entry->fCStatus; return entry->fCStatus;
} }
/*! \brief Gets the parent of the BEntry as a BDirectory. /*! \brief Gets the parent of the BEntry as a BDirectory.
If the function fails, the argument is Unset(). If the function fails, the argument is Unset().
\param dir pointer to a pre-allocated BDirectory object into which the result is stored \param dir pointer to a pre-allocated BDirectory object into which the
result is stored
\return \return
- \c B_OK - Success - \c B_OK - Success
- \c B_ENTRY_NOT_FOUND - Attempted to get the parent of the root directory \c "/" - \c B_ENTRY_NOT_FOUND - Attempted to get the parent of the root directory
\c "/"
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::GetParent(BDirectory *dir) const BEntry::GetParent(BDirectory* dir) const
{ {
// check initialization and parameter // check initialization and parameter
if (fCStatus != B_OK) if (fCStatus != B_OK)
@@ -589,12 +629,15 @@ BEntry::GetParent(BDirectory *dir) const
// API for being able to do that. // API for being able to do that.
} }
/*! \brief Gets the name of the entry's leaf. /*! \brief Gets the name of the entry's leaf.
\c buffer must be pre-allocated and of sufficient \c buffer must be pre-allocated and of sufficient
length to hold the entire string. A length of \c B_FILE_NAME_LENGTH is recommended. length to hold the entire string. A length of \c B_FILE_NAME_LENGTH is
recommended.
\param buffer pointer to a pre-allocated string into which the result is copied \param buffer pointer to a pre-allocated string into which the result is
copied
\return \return
- \c B_OK - Success - \c B_OK - Success
- "error code" - Failure - "error code" - Failure
@@ -616,19 +659,23 @@ BEntry::GetName(char *buffer) const
return result; return result;
} }
/*! \brief Renames the BEntry to path, replacing an existing entry if clobber is true.
NOTE: The BEntry must refer to an existing file. If it is abstract, this method will fail. /*! \brief Renames the BEntry to path, replacing an existing entry if clobber
is true.
\param path Pointer to a string containing the new name for the entry. May NOTE: The BEntry must refer to an existing file. If it is abstract, this
be absolute or relative. If relative, the entry is renamed within its method will fail.
current directory.
\param clobber If \c false and a file with the name given by \c path already exists, \param path Pointer to a string containing the new name for the entry. May
the method will fail. If \c true and such a file exists, it will be absolute or relative. If relative, the entry is renamed within its
be overwritten. current directory.
\param clobber If \c false and a file with the name given by \c path already
exists, the method will fail. If \c true and such a file exists, it will
be overwritten.
\return \return
- \c B_OK - Success - \c B_OK - Success
- \c B_ENTRY_EXISTS - The new location is already taken and \c clobber was \c false - \c B_ENTRY_EXISTS - The new location is already taken and \c clobber was
\c false
- \c B_ENTRY_NOT_FOUND - Attempted to rename an abstract entry - \c B_ENTRY_NOT_FOUND - Attempted to rename an abstract entry
- "error code" - Failure - "error code" - Failure
@@ -651,32 +698,36 @@ BEntry::Rename(const char *path, bool clobber)
if (dirFD < 0) if (dirFD < 0)
return dirFD; return dirFD;
// init the entry // init the entry
error = target.fCStatus = target.set(dirFD, path, false); error = target.fCStatus = target._SetTo(dirFD, path, false);
} }
if (error != B_OK) if (error != B_OK)
return error; return error;
return _Rename(target, clobber); return _Rename(target, clobber);
} }
/*! \brief Moves the BEntry to directory or directory+path combination, replacing an existing entry if clobber is true.
NOTE: The BEntry must refer to an existing file. If it is abstract, this method will fail. /*! \brief Moves the BEntry to directory or directory+path combination,
replacing an existing entry if clobber is true.
\param dir Pointer to a pre-allocated BDirectory into which the entry should be moved. NOTE: The BEntry must refer to an existing file. If it is abstract, this
\param path Optional new leaf name for the entry. May be a simple leaf or a relative path; method will fail.
either way, \c path is reckoned off of \c dir. If \c NULL, the entry retains
its previous leaf name. \param dir Pointer to a pre-allocated BDirectory into which the entry should
\param clobber If \c false and an entry already exists at the specified destination, be moved.
the method will fail. If \c true and such an entry exists, it will \param path Optional new leaf name for the entry. May be a simple leaf or a
be overwritten. relative path; either way, \c path is reckoned off of \c dir. If
\return \c NULL, the entry retains its previous leaf name.
- \c B_OK - Success \param clobber If \c false and an entry already exists at the specified
- \c B_ENTRY_EXISTS - The new location is already taken and \c clobber was \c false destination, the method will fail. If \c true and such an entry exists,
- \c B_ENTRY_NOT_FOUND - Attempted to move an abstract entry it will be overwritten.
- "error code" - Failure \return - \c B_OK - Success
- \c B_ENTRY_EXISTS - The new location is already taken and \c clobber
was \c false
- \c B_ENTRY_NOT_FOUND - Attempted to move an abstract entry
- "error code" - Failure
*/ */
status_t status_t
BEntry::MoveTo(BDirectory *dir, const char *path, bool clobber) BEntry::MoveTo(BDirectory* dir, const char* path, bool clobber)
{ {
// check parameters and initialization // check parameters and initialization
if (fCStatus != B_OK) if (fCStatus != B_OK)
@@ -696,6 +747,7 @@ BEntry::MoveTo(BDirectory *dir, const char *path, bool clobber)
return _Rename(target, clobber); return _Rename(target, clobber);
} }
/*! \brief Removes the entry from the file system. /*! \brief Removes the entry from the file system.
NOTE: If any file descriptors are open on the file when Remove() is called, NOTE: If any file descriptors are open on the file when Remove() is called,
@@ -720,12 +772,12 @@ BEntry::Remove()
} }
/*! \brief Returns true if the BEntry and \c item refer to the same entry or /*! \brief Returns true if the BEntry and \c item refer to the same entry or
if they are both uninitialized. if they are both uninitialized.
\return \return \c true - Both BEntry objects refer to the same entry or they are
- true - Both BEntry objects refer to the same entry or they are both uninitialzed both uninitialzed
- false - The BEntry objects refer to different entries \c false - The BEntry objects refer to different entries
*/ */
bool bool
BEntry::operator==(const BEntry &item) const BEntry::operator==(const BEntry &item) const
@@ -750,26 +802,28 @@ BEntry::operator==(const BEntry &item) const
} }
/*! \brief Returns false if the BEntry and \c item refer to the same entry or
if they are both uninitialized.
\return /*! \brief Returns false if the BEntry and \c item refer to the same entry or
- true - The BEntry objects refer to different entries if they are both uninitialized.
- false - Both BEntry objects refer to the same entry or they are both uninitialzed
\return \c true - The BEntry objects refer to different entries
\c false - Both BEntry objects refer to the same entry or they are
both uninitialzed
*/ */
bool bool
BEntry::operator!=(const BEntry &item) const BEntry::operator!=(const BEntry& item) const
{ {
return !(*this == item); return !(*this == item);
} }
/*! \brief Reinitializes the BEntry to be a copy of the argument /*! \brief Reinitializes the BEntry to be a copy of the argument
\return \return
- A reference to the copy - A reference to the copy
*/ */
BEntry& BEntry&
BEntry::operator=(const BEntry &item) BEntry::operator=(const BEntry& item)
{ {
if (this == &item) if (this == &item)
return *this; return *this;
@@ -778,7 +832,7 @@ BEntry::operator=(const BEntry &item)
if (item.fCStatus == B_OK) { if (item.fCStatus == B_OK) {
fDirFd = _kern_dup(item.fDirFd); fDirFd = _kern_dup(item.fDirFd);
if (fDirFd >= 0) if (fDirFd >= 0)
fCStatus = set_name(item.fName); fCStatus = _SetName(item.fName);
else else
fCStatus = fDirFd; fCStatus = fDirFd;
@@ -789,23 +843,20 @@ BEntry::operator=(const BEntry &item)
return *this; return *this;
} }
/*! Reserved for future use. */
void BEntry::_PennyEntry1(){} void BEntry::_PennyEntry1(){}
/*! Reserved for future use. */
void BEntry::_PennyEntry2(){} void BEntry::_PennyEntry2(){}
/*! Reserved for future use. */
void BEntry::_PennyEntry3(){} void BEntry::_PennyEntry3(){}
/*! Reserved for future use. */
void BEntry::_PennyEntry4(){} void BEntry::_PennyEntry4(){}
/*! Reserved for future use. */
void BEntry::_PennyEntry5(){} void BEntry::_PennyEntry5(){}
/*! Reserved for future use. */
void BEntry::_PennyEntry6(){} void BEntry::_PennyEntry6(){}
/*! \brief Updates the BEntry with the data from the stat structure according to the mask.
/*! \brief Updates the BEntry with the data from the stat structure according
to the mask.
*/ */
status_t status_t
BEntry::set_stat(struct stat &st, uint32 what) BEntry::set_stat(struct stat& st, uint32 what)
{ {
if (fCStatus != B_OK) if (fCStatus != B_OK)
return B_FILE_ERROR; return B_FILE_ERROR;
@@ -814,6 +865,7 @@ BEntry::set_stat(struct stat &st, uint32 what)
what); what);
} }
/*! Sets the Entry to point to the entry specified by the path \a path relative /*! Sets the Entry to point to the entry specified by the path \a path relative
to the given directory. If \a traverse is \c true and the given entry is a to the given directory. If \a traverse is \c true and the given entry is a
symlink, the object is recursively set to point to the entry pointed to by symlink, the object is recursively set to point to the entry pointed to by
@@ -838,7 +890,7 @@ BEntry::set_stat(struct stat &st, uint32 what)
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::set(int dirFD, const char *path, bool traverse) BEntry::_SetTo(int dirFD, const char* path, bool traverse)
{ {
bool requireConcrete = false; bool requireConcrete = false;
FDCloser fdCloser(dirFD); FDCloser fdCloser(dirFD);
@@ -958,7 +1010,7 @@ BEntry::set(int dirFD, const char *path, bool traverse)
fcntl(dirFD, F_SETFD, FD_CLOEXEC); fcntl(dirFD, F_SETFD, FD_CLOEXEC);
// set the result // set the result
status_t error = set_name(leafName); status_t error = _SetName(leafName);
if (error != B_OK) if (error != B_OK)
return error; return error;
fdCloser.Detach(); fdCloser.Detach();
@@ -966,14 +1018,16 @@ BEntry::set(int dirFD, const char *path, bool traverse)
return B_OK; return B_OK;
} }
/*! \brief Handles string allocation, deallocation, and copying for the entry's leaf name.
/*! \brief Handles string allocation, deallocation, and copying for the entry's
leaf name.
\return \return
- B_OK - Success - B_OK - Success
- "error code" - Failure - "error code" - Failure
*/ */
status_t status_t
BEntry::set_name(const char *name) BEntry::_SetName(const char* name)
{ {
if (name == NULL) if (name == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
@@ -981,13 +1035,13 @@ BEntry::set_name(const char *name)
free(fName); free(fName);
fName = strdup(name); fName = strdup(name);
if (!fName) if (fName == NULL)
return B_NO_MEMORY; return B_NO_MEMORY;
return B_OK; return B_OK;
} }
// _Rename
/*! \brief Renames the entry referred to by this object to the location /*! \brief Renames the entry referred to by this object to the location
specified by \a target. specified by \a target.
@@ -1024,16 +1078,13 @@ BEntry::_Rename(BEntry& target, bool clobber)
} }
/*! Debugging function, dumps the given entry to stdout. This function is not part of /*! Debugging function, dumps the given entry to stdout.
the R5 implementation, and thus calls to it will mean you can't link with the
R5 Storage Kit.
\param name Pointer to a string to be printed along with the dump for identification \param name Pointer to a string to be printed along with the dump for
purposes. identification purposes.
*/
*/
void void
BEntry::Dump(const char *name) BEntry::_Dump(const char* name)
{ {
if (name != NULL) { if (name != NULL) {
printf("------------------------------------------------------------\n"); printf("------------------------------------------------------------\n");
@@ -1060,7 +1111,7 @@ BEntry::Dump(const char *name)
status_t status_t
BEntry::_GetStat(struct stat *st) const BEntry::_GetStat(struct stat* st) const
{ {
if (fCStatus != B_OK) if (fCStatus != B_OK)
return B_NO_INIT; return B_NO_INIT;
@@ -1070,7 +1121,7 @@ BEntry::_GetStat(struct stat *st) const
status_t status_t
BEntry::_GetStat(struct stat_beos *st) const BEntry::_GetStat(struct stat_beos* st) const
{ {
struct stat newStat; struct stat newStat;
status_t error = _GetStat(&newStat); status_t error = _GetStat(&newStat);
@@ -1082,7 +1133,9 @@ BEntry::_GetStat(struct stat_beos *st) const
} }
// get_ref_for_path // #pragma mark -
/*! \brief Returns an entry_ref for a given path. /*! \brief Returns an entry_ref for a given path.
\param path The path name referring to the entry \param path The path name referring to the entry
\param ref The entry_ref structure to be filled in \param ref The entry_ref structure to be filled in
@@ -1093,9 +1146,9 @@ BEntry::_GetStat(struct stat_beos *st) const
- \c B_NO_MEMORY - Insufficient memory for successful completion. - \c B_NO_MEMORY - Insufficient memory for successful completion.
*/ */
status_t status_t
get_ref_for_path(const char *path, entry_ref *ref) get_ref_for_path(const char* path, entry_ref* ref)
{ {
status_t error = (path && ref ? B_OK : B_BAD_VALUE); status_t error = path && ref ? B_OK : B_BAD_VALUE;
if (error == B_OK) { if (error == B_OK) {
BEntry entry(path); BEntry entry(path);
error = entry.InitCheck(); error = entry.InitCheck();
@@ -1105,7 +1158,7 @@ get_ref_for_path(const char *path, entry_ref *ref)
return error; return error;
} }
// <
/*! \brief Returns whether an entry is less than another. /*! \brief Returns whether an entry is less than another.
The components are compared in order \c device, \c directory, \c name. The components are compared in order \c device, \c directory, \c name.
A \c NULL \c name is less than any non-null name. A \c NULL \c name is less than any non-null name.
@@ -1115,7 +1168,7 @@ get_ref_for_path(const char *path, entry_ref *ref)
- false - a >= b - false - a >= b
*/ */
bool bool
operator<(const entry_ref & a, const entry_ref & b) operator<(const entry_ref& a, const entry_ref& b)
{ {
return (a.device < b.device return (a.device < b.device
|| (a.device == b.device || (a.device == b.device