]> git.tdb.fi Git - libs/core.git/blobdiff - source/fs/stat.h
Change stat functions so that it's again possible to determine file existence
[libs/core.git] / source / fs / stat.h
index e6b20022b43338d97fd0485c2ac07686247df51c..6ae551fd13836955bbd6a11f347a2b236abe0022 100644 (file)
@@ -1,42 +1,85 @@
 #ifndef MSP_FS_STAT_H_
 #define MSP_FS_STAT_H_
 
-#include <sys/stat.h>
+#include <string>
+#include <msp/time/timestamp.h>
 #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
+};
+
+#ifdef MSVC
+typedef __uint64 FileSize;
+#else
+typedef unsigned long long FileSize;
+#endif
 
 /**
-Returns information about a file.  This version throws an exception if an error
-occurs.
+Holds file information.
 */
-struct stat stat(const Path &fn);
+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