X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffs%2Fstat.h;h=3f056a5609eea802716037eb734c3d42ef51905d;hp=9056c926d697ed03ae04df82c62d7e2ec8661455;hb=817e584903996a041692640720a5a272d847a3c7;hpb=d16185720fa344263367dbd50c61bfc8183d99a4 diff --git a/source/fs/stat.h b/source/fs/stat.h index 9056c92..3f056a5 100644 --- a/source/fs/stat.h +++ b/source/fs/stat.h @@ -1,38 +1,91 @@ #ifndef MSP_FS_STAT_H_ #define MSP_FS_STAT_H_ -#include +#include +#include +#include #include "path.h" namespace Msp { namespace FS { -/** Gets information about a file. Returns 0 on success or -1 on error. This -version can be used to check for file existence and get information in one -call. */ -int stat(const Path &fn, struct stat &st); +enum FileType +{ + UNKNOWN, + REGULAR, + DIRECTORY, + SYMLINK +}; -/** Returns information about a file. This version throws an exception if an -error occurs. */ -struct stat stat(const Path &fn); +typedef UInt64 FileSize; -/// Gets information about a file, without following symbolic links -int lstat(const Path &fn, struct stat &st); +/** +Holds file information. +*/ +class Stat +{ +private: + struct Private; -/// Returns information about a file, without following symbolic links -struct stat lstat(const Path &fn); + struct OwnerInfo + { + std::string owner; + std::string group; + }; + + bool exists; + FileType type; + FileSize size; + FileSize alloc_size; + Time::TimeStamp mtime; + mutable OwnerInfo owner_info; + Private *priv; + +public: + Stat(); + Stat(const Stat &); + Stat &operator=(const Stat &); + ~Stat(); + + FileType get_type() const { return type; } + bool is_regular() const { return type==REGULAR; } + bool is_directory() const { return type==DIRECTORY; } + bool is_symlink() const { return type==SYMLINK; } + FileSize get_size() const { return size; } + FileSize get_alloc_size() const { return alloc_size; } + const Time::TimeStamp &get_modify_time() const { return mtime; } + const std::string &get_owner() const; + const std::string &get_group() const; + + operator bool() const { return exists; } + + /// Returns a Stat object describing a file. + static Stat stat(const Path &); + static Stat lstat(const Path &); +}; + +/// Convenience wrapper for Stat::stat +inline Stat stat(const Path &path) +{ return Stat::stat(path); } + +/// Convenience wrapper for Stat::lstat +inline Stat lstat(const Path &path) +{ return Stat::lstat(path); } /// Tests for existence of a file bool exists(const Path &path); /// Tests whether a path refers to an existing regular file -bool is_reg(const Path &path); +inline bool is_reg(const Path &path) +{ return stat(path).is_regular(); } /// Tests whether a path refers to an existing directory -bool is_dir(const Path &path); +inline bool is_dir(const Path &path) +{ return stat(path).is_directory(); } /// Tests whether a path refers to a symbolic link -bool is_link(const Path &path); +inline bool is_link(const Path &path) +{ return lstat(path).is_symlink(); } } // namespace FS } // namespace Msp