X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Ffs%2Fstat.h;h=6ae551fd13836955bbd6a11f347a2b236abe0022;hp=9056c926d697ed03ae04df82c62d7e2ec8661455;hb=5a006c4fce0893b63f881a6c9ecd010f15770428;hpb=eeb6ed7b20d56578ff8c68f3c94e887d5ac768ee diff --git a/source/fs/stat.h b/source/fs/stat.h index 9056c92..6ae551f 100644 --- a/source/fs/stat.h +++ b/source/fs/stat.h @@ -1,38 +1,85 @@ #ifndef MSP_FS_STAT_H_ #define MSP_FS_STAT_H_ -#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); +#ifdef MSVC +typedef __uint64 FileSize; +#else +typedef unsigned long long FileSize; +#endif + +/** +Holds file information. +*/ +class Stat +{ +private: + struct Private; + + bool exists; + FileType type; + FileSize size; + FileSize alloc_size; + Time::TimeStamp mtime; + std::string owner; + std::string group; + +public: + 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 { return owner; } + const std::string &get_group() const { return group; } + + operator bool() const { return exists; } + + /// Returns a Stat object describing a file. + static Stat stat(const Path &); + static Stat lstat(const Path &); +}; -/// Gets information about a file, without following symbolic links -int lstat(const Path &fn, struct stat &st); +/// Convenience wrapper for Stat::stat +inline Stat stat(const Path &path) +{ return Stat::stat(path); } -/// Returns information about a file, without following symbolic links -struct stat lstat(const Path &fn); +/// 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