libUTL++
FSobject.h
1 #pragma once
2 
4 
5 #include <libutl/Pathname.h>
6 #include <libutl/Time.h>
7 
9 
10 struct stat;
11 
13 
14 UTL_NS_BEGIN;
15 
17 
18 class Directory;
19 class UnixStatBuf;
20 
22 
33 
35 class FSobject : public Object, protected FlagsMI
36 {
38 
39 public:
45  FSobject(const String& path, const Directory* dir = nullptr)
46  {
47  init();
48  setPath(path, dir);
49  }
50 
51  virtual int compare(const Object& rhs) const;
52 
53  virtual void copy(const Object& rhs);
54 
55  virtual void dump(Stream& os, uint_t level = uint_t_max) const;
56 
57  virtual const Object&
58  getKey() const
59  {
60  return _path;
61  }
62 
63  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
64 
65  virtual String
66  toString() const
67  {
68  return path();
69  }
70 
72  Pathname path() const;
73 
79  void setPath(const String& path, const Directory* dir = nullptr);
80 
82  Pathname
83  relativePath() const
84  {
85  return _path;
86  }
87 
89  const Directory*
90  directory() const
91  {
92  return _dir;
93  }
94 
96  void
97  setDirectory(const Directory* dir)
98  {
99  _dir = dir;
100  }
101 
107  bool exists(bool ignoreEx = false) const;
108 
114  void rename(const String& path, bool relative = true);
115 
117  virtual void unlink();
118 
126  static FSobject*
127  create(const String& path, const Directory* dir = nullptr, bool followLink = false);
128 
130  bool
131  isMarked() const
132  {
133  return getFlag(flg_marked);
134  }
135 
137  void
138  setMarked(bool marked)
139  {
140  setFlag(flg_marked, marked);
141  }
142 
143 #if UTL_HOST_TYPE == UTL_HT_UNIX
144 
145  void clearStat();
147  void takeStat(FSobject& rhs);
149  void stat(bool force = false) const;
150 #if UTL_HOST_OS != UTL_OS_MINGW
151 
152  void lstat(bool force = false) const;
153 #endif
154 
155  uint32_t device() const;
157  uint32_t inode() const;
159  uint32_t mode() const;
161  void setMode(uint32_t mode);
163  uint32_t numLinks() const;
165  uint32_t userId() const;
167  uint32_t groupId() const;
169  uint32_t deviceType() const;
171  size_t size() const;
173  ulong_t blockSize() const;
175  ulong_t numBlocks() const;
177  Time accessTime() const;
179  Time modifyTime() const;
181  Time changeTime() const;
182 #endif
183 
185  enum field_t
186  {
188  field_directory, /*< directory */
192 #if UTL_HOST_TYPE == UTL_HT_UNIX
196 #if UTL_HOST_OS != UTL_OS_MINGW
198 #endif
201  field_changeTime
202 #endif
203  };
204 private:
205  void
206  init()
207  {
208  _stat = nullptr;
209  _dir = nullptr;
210  }
211  void deInit();
212 
213 private:
214  enum flg_t
215  {
216  flg_stat = 0,
217  flg_lstat = 1,
218  flg_marked = 2
219  };
220 
221 private:
222  const Directory* _dir;
223  Pathname _path;
224 #if UTL_HOST_TYPE == UTL_HT_UNIX
225  mutable UnixStatBuf* _stat;
226 #endif
227 };
228 
230 
231 UTL_NS_END;
void setMarked(bool marked)
Set the marked flag.
Definition: FSobject.h:138
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
virtual String toString() const
Return a string representation of self.
Definition: FSobject.h:66
void deInit()
De-initialize UTL++.
Filesystem pathname.
Definition: Pathname.h:32
default representation (via getSerializeMode())
Definition: util.h:75
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Character string.
Definition: String.h:31
Mix-in to provide 64-bits for space-efficient storage of up to 64 boolean flags.
Definition: FlagsMI.h:25
const Directory * directory() const
Get the parent directory.
Definition: FSobject.h:90
bool isMarked() const
Get the marked flag.
Definition: FSobject.h:131
virtual const Object & getKey() const
Get the key for this object.
Definition: FSobject.h:58
unsigned int uint32_t
Unsigned 32-bit integer.
Definition: types.h:115
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
field_t
Filesystem object attributes.
Definition: FSobject.h:185
Filesystem object.
Definition: FSobject.h:35
void setDirectory(const Directory *dir)
Set the parent directory.
Definition: FSobject.h:97
Pathname relativePath() const
Get the relative path.
Definition: FSobject.h:83
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
Directory filesystem object.
Definition: Directory.h:75
Date/time.
Definition: Time.h:148
modification time (last write)
Definition: FSobject.h:200
const uint_t uint_t_max
Maximum uint_t value.
Stream I/O abstraction.
Definition: Stream.h:68
FSobject(const String &path, const Directory *dir=nullptr)
Constructor.
Definition: FSobject.h:45
unsigned long ulong_t
Unsigned long integer.
Definition: types.h:73
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void dump(const FwdIt &begin, const FwdIt &end, Stream &os, uint_t level=uint_t_max, bool key=false, bool printClassName=false, uint_t indent=0, const char *separator=nullptr)
Dump objects to the given stream (with Object::dump()).
void init()
Initialize UTL++.