dmlite  0.6
inode.h
Go to the documentation of this file.
1 /// @file include/dmlite/cpp/inode.h
2 /// @brief Low-level access API.
3 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
4 #ifndef DMLITE_CPP_INODE_H
5 #define DMLITE_CPP_INODE_H
6 
7 #include "dmlite/common/config.h"
8 #include "base.h"
9 #include "exceptions.h"
10 #include "status.h"
11 #include "utils/extensible.h"
12 #include "utils/security.h"
13 #include "utils/checksums.h"
14 
15 #include <dirent.h>
16 #include <utime.h>
17 #include <string>
18 #include <vector>
19 
20 namespace dmlite {
21 
22  // Forward declarations.
23  class StackInstance;
24 
25  /// Typedef for directories.
26  struct IDirectory { virtual ~IDirectory(); };
27 
28  /// File/directory metadata.
29  struct ExtendedStat: public Extensible {
30  enum FileStatus { kOnline = '-',
31  kMigrated = 'm',
32  kDeleted = 'D'
33  };
34 
35  ino_t parent;
36  struct stat stat;
38  std::string name;
39  std::string guid;
40  std::string csumtype;
41  std::string csumvalue;
43 
44  bool operator == (const ExtendedStat&) const;
45  bool operator != (const ExtendedStat&) const;
46  bool operator < (const ExtendedStat&) const;
47  bool operator > (const ExtendedStat&) const;
48 
49  void fixchecksums();
50 
51  /// gets a checksum of type csumtype
52  /// if csumtype is empty, then it gets the legacy one (i.e. the easiest to get)
53  /// Please note that this function recognizes long checksum name
54  /// e.g. "adler32" , which internally will be looked up as "checksum.adler32'
55  int getchecksum(std::string &cktype, std::string &ckvalue);
56 
57  };
58 
59  /// Symbolic link
60  struct SymLink: public Extensible {
61  ino_t inode;
62  std::string link;
63 
64  bool operator == (const SymLink&) const;
65  bool operator != (const SymLink&) const;
66  bool operator < (const SymLink&) const;
67  bool operator > (const SymLink&) const;
68  };
69 
70  /// File replica metadata
71  struct Replica: public Extensible {
72  enum ReplicaStatus { kAvailable = '-',
75  };
76  enum ReplicaType { kVolatile = 'V',
77  kPermanent = 'P'
78  };
79  enum ReplicaPS { kPrimary = 'P',
80  kSecondary = 'S'
81  };
82 
83  int64_t replicaid;
84  int64_t fileid;
85 
86  int64_t nbaccesses;
87  time_t atime;
88  time_t ptime;
89  time_t ltime;
90 
94 
95  /// Historical field containing the uuid of the spacetoken that was chosen when
96  /// writing the replica. This is used for accounting
97  std::string setname;
98 
99  std::string server;
100  std::string rfn;
101 
102  bool operator == (const Replica&) const;
103  bool operator != (const Replica&) const;
104  bool operator < (const Replica&) const;
105  bool operator > (const Replica&) const;
106  };
107 
108  /// Low-level interface. Based on i-nodes.
109  /// @note Security checks NOT done on this level.
110  class INode: public virtual BaseInterface {
111  public:
112  /// Destructor
113  virtual ~INode();
114 
115  /// Start a transaction
116  virtual void begin(void) ;
117 
118  /// Commit a transaction
119  virtual void commit(void) ;
120 
121  /// Rollback changes
122  virtual void rollback(void) ;
123 
124  /// Create a new file or directory
125  /// @param f The file that will be inserted. Its fields must be initialized.
126  /// @return An stat of the created file.
127  virtual ExtendedStat create(const ExtendedStat& f) ;
128 
129  /// Create or modify the file inode to point to another file.
130  /// @param inode The file to modify.
131  /// @param link The new symbolic link.
132  /// @note This does NOT create the file. Use create first.
133  virtual void symlink(ino_t inode, const std::string &link) ;
134 
135  /// Remove a file or directory. It will fail if it is a directory and it is not empty,
136  /// or if it a file and it has replicas.
137  /// @param inode The inode of the file.
138  /// @note This will check for non empty directories.
139  /// @note This will remove associated comments and replicas.
140  virtual void unlink(ino_t inode) ;
141 
142  /// Move a file between two directories.
143  /// @param inode File to be moved.
144  /// @param dest The new parent.
145  virtual void move(ino_t inode, ino_t dest) ;
146 
147  /// Change the name of a file.
148  /// @param inode The inode of the file.
149  /// @param name New name.
150  virtual void rename(ino_t inode, const std::string& name) ;
151 
152  /// Do an extended stat of an entry using its inode.
153  /// @param inode The inode of the file.
154  /// @return The extended status of the file.
155  virtual ExtendedStat extendedStat(ino_t inode) ;
156 
157  /// Do an extended stat of an entry using its inode, exception-safe version.
158  /// @param xstat The extended status of the file.
159  /// @param inode The inode of the file.
160  /// @return The status of the operation.
161  virtual DmStatus extendedStat(ExtendedStat &xstat, ino_t inode) ;
162 
163  /// Do an extended stat of an entry using the parent inode and the name.
164  /// @param parent The parent inode.
165  /// @param name The file or directory name.
166  /// @note No security check will be done.
167  virtual ExtendedStat extendedStat(ino_t parent,
168  const std::string& name) ;
169 
170  /// Do an extended stat of an entry using the parent inode and the name, exception-safe version.
171  /// @param xstat The extended status of the file.
172  /// @param parent The parent inode.
173  /// @param name The file or directory name.
174  /// @return The status of the operation.
175  /// @note No security check will be done.
176  virtual DmStatus extendedStat(ExtendedStat &xstat, ino_t parent,
177  const std::string& name) ;
178 
179  /// Do an extended stat using the GUID.
180  /// @param guid The file GUID.
181  virtual ExtendedStat extendedStat(const std::string& guid) ;
182 
183  /// Get the symlink associated with a inode.
184  /// @param inode The inode of the file.
185  /// @return A SymLink struct.
186  /// @note If inode is not a symlink, an exception will be thrown.
187  virtual SymLink readLink(ino_t inode) ;
188 
189  /// Add a new replica for a file.
190  /// @param replica Stores the data that is going to be added. fileid must
191  /// point to the id of the logical file in the catalog.
192  virtual void addReplica(const Replica& replica) ;
193 
194  /// Delete a replica.
195  /// @param replica The replica to remove.
196  virtual void deleteReplica(const Replica& replica) ;
197 
198  /// Get a replica using the replica ID.
199  /// @param rid The replica ID.
200  virtual Replica getReplica(int64_t rid) ;
201 
202  /// Get a replica.
203  /// @param rfn The replica to retrieve.
204  virtual Replica getReplica(const std::string& rfn) ;
205 
206  /// Modify a replica.
207  /// @param replica The replica data.
208  virtual void updateReplica(const Replica& replica) ;
209 
210  /// Get replicas for a file.
211  /// @param inode The entry inode.
212  virtual std::vector<Replica> getReplicas(ino_t inode) ;
213 
214  /// Change access and/or modification time.
215  /// @param inode The inode of the file.
216  /// @param buf A struct holding the new times.
217  virtual void utime(ino_t inode,
218  const struct utimbuf* buf) ;
219 
220  /// Set the mode of a file.
221  /// @param inode The inode of the file.
222  /// @param uid The owner. If -1, not changed.
223  /// @param gid The group. If -1, not changed.
224  /// @param mode The new mode. S_IFMT bits are cleared, and kept as they
225  /// are in the DB.
226  /// @param acl The new ACL. If empty, not changed.
227  virtual void setMode(ino_t inode, uid_t uid, gid_t gid, mode_t mode,
228  const Acl& acl) ;
229 
230  /// Set the size of a file.
231  /// @param inode The inode of the file.
232  /// @param size The new size.
233  virtual void setSize(ino_t inode, size_t size) ;
234 
235  /// Set the checksum of a file.
236  /// @param inode The inode of the file.
237  /// @param csumtype The checksum type.
238  /// @param csumvalue The checksum value.
239  virtual void setChecksum(ino_t inode, const std::string& csumtype,
240  const std::string& csumvalue) ;
241 
242  /// Get the comment associated to a file.
243  /// @param inode The inode of the file.
244  /// @return The comment.
245  virtual std::string getComment(ino_t inode) ;
246 
247  /// Set the comment associated to a file.
248  /// @param inode The inode of the file.
249  /// @param comment The new comment.
250  virtual void setComment(ino_t inode,
251  const std::string& comment) ;
252 
253  /// Remove the associated comment.
254  /// @param inode The file whose comment will be removed.
255  virtual void deleteComment(ino_t inode) ;
256 
257  /// Set the GUID of a file.
258  /// @param inode The inode of the file.
259  /// @param guid The new GUID.
260  virtual void setGuid(ino_t inode,
261  const std::string& guid) ;
262 
263  /// Update extended metadata on the catalog.
264  /// @param attr The extended attributes struct.
265  virtual void updateExtendedAttributes(ino_t inode,
266  const Extensible& attr) ;
267 
268  /// Open a directory.
269  /// @param inode The inode of the directory.
270  /// @return An opaque pointer to a directory.
271  virtual IDirectory* openDir(ino_t inode) ;
272 
273  /// Close a directory.
274  /// @param dir The opaque structure to close.
275  virtual void closeDir(IDirectory* dir) ;
276 
277  /// Read the next entry.
278  /// @param dir The opaque structure of a directory.
279  /// @return NULL when finished. Extended stat of the next entry otherwise.
280  virtual ExtendedStat* readDirx(IDirectory* dir) ;
281 
282  /// Read the next entry.
283  /// @param dir The opaque structure of a directory.
284  /// @return NULL when finished. Extended stat of the next entry otherwise.
285  virtual struct dirent* readDir (IDirectory* dir) ;
286  };
287 
288  /// INodeFactory
289  class INodeFactory: public virtual BaseFactory {
290  public:
291  /// Destructor
292  virtual ~INodeFactory();
293 
294  protected:
295  // Stack instance is allowed to instantiate INodes
296  friend class StackInstance;
297 
298  /// Children of INodeFactory are allowed to instantiate too (decorator)
299  static INode* createINode(INodeFactory* factory,
300  PluginManager* pm) ;
301 
302  /// Instantiate a implementation of INode
303  virtual INode* createINode(PluginManager* pm) ;
304  };
305 
306 
307 
308 
309  /// Convenience class that releases a resource on destruction
310  class InodeTrans {
311  public:
313  {
314  obj = o;
315  obj->begin();
316  }
317 
319  if (obj != 0) obj->rollback();
320  obj = 0;
321  }
322 
323  void Commit() {
324  if (obj != 0) obj->commit();
325  obj = 0;
326  }
327 
328  private:
330 
331  };
332 
333 
334 
335 };
336 
337 #endif // DMLITE_CPP_INODE_H
Definition: inode.h:76
virtual void setChecksum(ino_t inode, const std::string &csumtype, const std::string &csumvalue)
ReplicaStatus status
Definition: inode.h:91
InodeTrans(INode *o)
Definition: inode.h:312
virtual ~INode()
Destructor.
File/directory metadata.
Definition: inode.h:29
Definition: inode.h:79
time_t ptime
Definition: inode.h:88
time_t atime
Definition: inode.h:87
virtual ExtendedStat extendedStat(ino_t inode)
Definition: inode.h:30
Base class for interfaces.
Definition: base.h:18
virtual ~INodeFactory()
Destructor.
Definition: security.h:51
Definition: status.h:17
std::string server
Definition: inode.h:99
ReplicaPS rtype
Definition: inode.h:93
Definition: dmlite.h:161
virtual void rename(ino_t inode, const std::string &name)
Definition: inode.h:74
bool operator<(const ExtendedStat &) const
virtual void setSize(ino_t inode, size_t size)
bool operator==(const Replica &) const
virtual ExtendedStat create(const ExtendedStat &f)
bool operator!=(const Replica &) const
virtual void deleteComment(ino_t inode)
std::string csumtype
Definition: inode.h:40
File replica metadata.
Definition: inode.h:71
virtual void addReplica(const Replica &replica)
Definition: inode.h:72
CatalogInterface can only be instantiated through this class.
Definition: dmlite.h:42
Convenience class that releases a resource on destruction.
Definition: inode.h:310
virtual std::string getComment(ino_t inode)
virtual void move(ino_t inode, ino_t dest)
virtual Replica getReplica(int64_t rid)
virtual SymLink readLink(ino_t inode)
virtual void unlink(ino_t inode)
~InodeTrans()
Definition: inode.h:318
virtual void setMode(ino_t inode, uid_t uid, gid_t gid, mode_t mode, const Acl &acl)
Definition: inode.h:32
Definition: inode.h:77
Definition: inode.h:73
Definition: inode.h:31
virtual void begin(void)
Start a transaction.
ino_t parent
Definition: inode.h:35
std::string guid
Definition: inode.h:39
virtual void rollback(void)
Rollback changes.
int64_t fileid
Definition: inode.h:84
INodeFactory.
Definition: inode.h:289
virtual void commit(void)
Commit a transaction.
virtual void deleteReplica(const Replica &replica)
ReplicaPS
Definition: inode.h:79
Exceptions used by the API.
FileStatus status
Definition: inode.h:37
virtual ~IDirectory()
INode * obj
Definition: inode.h:329
int64_t nbaccesses
Definition: inode.h:86
Helpful typedef for KeyValue containers.
Definition: extensible.h:20
bool operator==(const ExtendedStat &) const
virtual IDirectory * openDir(ino_t inode)
Base class for factories.
Definition: base.h:48
virtual std::vector< Replica > getReplicas(ino_t inode)
bool operator!=(const ExtendedStat &) const
time_t ltime
Definition: inode.h:89
virtual ExtendedStat * readDirx(IDirectory *dir)
std::string rfn
Definition: inode.h:100
virtual void utime(ino_t inode, const struct utimbuf *buf)
std::string name
Definition: inode.h:38
Definition: inode.h:110
virtual void symlink(ino_t inode, const std::string &link)
ReplicaStatus
Definition: inode.h:72
Extensible types (hold metadata).
Status objects used by the API.
virtual void updateExtendedAttributes(ino_t inode, const Extensible &attr)
ReplicaType
Definition: inode.h:76
int64_t replicaid
Definition: inode.h:83
void Commit()
Definition: inode.h:323
Base interfaces.
Acl acl
Definition: inode.h:42
virtual void updateReplica(const Replica &replica)
virtual void closeDir(IDirectory *dir)
Security functionality shared between modules.
bool operator>(const ExtendedStat &) const
Utility methods for checksum handling.
bool operator>(const Replica &) const
Definition: inode.h:80
virtual void setComment(ino_t inode, const std::string &comment)
static INode * createINode(INodeFactory *factory, PluginManager *pm)
Children of INodeFactory are allowed to instantiate too (decorator)
struct stat stat
Definition: inode.h:36
std::string setname
Definition: inode.h:97
ReplicaType type
Definition: inode.h:92
std::string csumvalue
Definition: inode.h:41
Typedef for directories.
Definition: inode.h:26
virtual struct dirent * readDir(IDirectory *dir)
bool operator<(const Replica &) const
int getchecksum(std::string &cktype, std::string &ckvalue)
FileStatus
Definition: inode.h:30
virtual void setGuid(ino_t inode, const std::string &guid)