* Copyright 2002-2014 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Tyler Dauwalder
* Ingo Weinhold, bonefish@users.sf.net
*/
#include <Statable.h>
#include <sys/stat.h>
#include <compat/sys/stat.h>
#include <Node.h>
#include <NodeMonitor.h>
#include <Volume.h>
class BStatable::Private {
public:
Private(const BStatable* object)
:
fObject(object)
{
}
status_t GetStatBeOS(struct stat_beos* stat)
{
return fObject->_GetStat(stat);
}
private:
const BStatable* fObject;
};
#if __GNUC__ > 3
BStatable::~BStatable()
{
}
#endif
bool
BStatable::IsFile() const
{
struct stat stat;
if (GetStat(&stat) == B_OK)
return S_ISREG(stat.st_mode);
else
return false;
}
bool
BStatable::IsDirectory() const
{
struct stat stat;
if (GetStat(&stat) == B_OK)
return S_ISDIR(stat.st_mode);
else
return false;
}
bool
BStatable::IsSymLink() const
{
struct stat stat;
if (GetStat(&stat) == B_OK)
return S_ISLNK(stat.st_mode);
else
return false;
}
status_t
BStatable::GetNodeRef(node_ref* ref) const
{
status_t result = (ref ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK) {
ref->device = stat.st_dev;
ref->node = stat.st_ino;
}
return result;
}
status_t
BStatable::GetOwner(uid_t* owner) const
{
status_t result = (owner ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*owner = stat.st_uid;
return result;
}
status_t
BStatable::SetOwner(uid_t owner)
{
struct stat stat = {};
stat.st_uid = owner;
return set_stat(stat, B_STAT_UID);
}
status_t
BStatable::GetGroup(gid_t* group) const
{
status_t result = (group ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*group = stat.st_gid;
return result;
}
status_t
BStatable::SetGroup(gid_t group)
{
struct stat stat = {};
stat.st_gid = group;
return set_stat(stat, B_STAT_GID);
}
status_t
BStatable::GetPermissions(mode_t* permissions) const
{
status_t result = (permissions ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*permissions = (stat.st_mode & S_IUMSK);
return result;
}
status_t
BStatable::SetPermissions(mode_t permissions)
{
struct stat stat = {};
stat.st_mode = permissions;
return set_stat(stat, B_STAT_MODE);
}
status_t
BStatable::GetSize(off_t* size) const
{
status_t result = (size ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*size = stat.st_size;
return result;
}
status_t
BStatable::GetModificationTime(time_t* mtime) const
{
status_t result = (mtime ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*mtime = stat.st_mtime;
return result;
}
status_t
BStatable::SetModificationTime(time_t mtime)
{
struct stat stat = {};
stat.st_mtime = mtime;
return set_stat(stat, B_STAT_MODIFICATION_TIME);
}
status_t
BStatable::GetCreationTime(time_t* ctime) const
{
status_t result = (ctime ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*ctime = stat.st_crtime;
return result;
}
status_t
BStatable::SetCreationTime(time_t ctime)
{
struct stat stat = {};
stat.st_crtime = ctime;
return set_stat(stat, B_STAT_CREATION_TIME);
}
status_t
BStatable::GetAccessTime(time_t* atime) const
{
status_t result = (atime ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
*atime = stat.st_atime;
return result;
}
status_t
BStatable::SetAccessTime(time_t atime)
{
struct stat stat = {};
stat.st_atime = atime;
return set_stat(stat, B_STAT_ACCESS_TIME);
}
status_t
BStatable::GetVolume(BVolume* volume) const
{
status_t result = (volume ? B_OK : B_BAD_VALUE);
struct stat stat = {};
if (result == B_OK)
result = GetStat(&stat);
if (result == B_OK)
result = volume->SetTo(stat.st_dev);
return result;
}
extern "C" status_t
#if __GNUC__ == 2
_OhSoStatable1__9BStatable(const BStatable* self, struct stat* stat)
#else
_ZN9BStatable14_OhSoStatable1Ev(const BStatable* self, struct stat* stat)
#endif
{
struct stat_beos oldStat = {};
status_t result = BStatable::Private(self).GetStatBeOS(&oldStat);
if (result != B_OK)
return result;
convert_from_stat_beos(&oldStat, stat);
return B_OK;
}
void BStatable::_OhSoStatable2() {}
void BStatable::_OhSoStatable3() {}