]> git.tdb.fi Git - libs/core.git/blobdiff - source/fs/stat.h
Use nullptr instead of 0 for pointers
[libs/core.git] / source / fs / stat.h
index 55a1fec51b575f50b0208878a4e066a20383c98f..c32e8798162787b40b47a4ff41d9c7603689e806 100644 (file)
@@ -1,49 +1,91 @@
-/* $Id$
-
-This file is part of libmspfs
-Copyright © 2006-2008  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_FS_STAT_H_
 #define MSP_FS_STAT_H_
 
-#include <sys/stat.h>
+#include <cstdint>
+#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
+};
+
+typedef uint64_t FileSize;
 
 /**
-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;
+
+       struct OwnerInfo
+       {
+               std::string owner;
+               std::string group;
+       };
+
+       bool exists = false;
+       FileType type = UNKNOWN;
+       FileSize size = 0;
+       FileSize alloc_size = 0;
+       Time::TimeStamp mtime;
+       mutable OwnerInfo owner_info;
+       Private *priv = nullptr;
+
+public:
+       Stat() = default;
+       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 &);
+};
 
-/// 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