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 
80  int64_t replicaid;
81  int64_t fileid;
82 
83  int64_t nbaccesses;
84  time_t atime;
85  time_t ptime;
86  time_t ltime;
87 
90 
91  /// Historical field containing the uuid of the spacetoken that was chosen when
92  /// writing the replica. This is used for accounting
93  std::string setname;
94 
95  std::string server;
96  std::string rfn;
97 
98  bool operator == (const Replica&) const;
99  bool operator != (const Replica&) const;
100  bool operator < (const Replica&) const;
101  bool operator > (const Replica&) const;
102  };
103 
104  /// Low-level interface. Based on i-nodes.
105  /// @note Security checks NOT done on this level.
106  class INode: public virtual BaseInterface {
107  public:
108  /// Destructor
109  virtual ~INode();
110 
111  /// Start a transaction
112  virtual void begin(void) ;
113 
114  /// Commit a transaction
115  virtual void commit(void) ;
116 
117  /// Rollback changes
118  virtual void rollback(void) ;
119 
120  /// Create a new file or directory
121  /// @param f The file that will be inserted. Its fields must be initialized.
122  /// @return An stat of the created file.
123  virtual ExtendedStat create(const ExtendedStat& f) ;
124 
125  /// Create or modify the file inode to point to another file.
126  /// @param inode The file to modify.
127  /// @param link The new symbolic link.
128  /// @note This does NOT create the file. Use create first.
129  virtual void symlink(ino_t inode, const std::string &link) ;
130 
131  /// Remove a file or directory. It will fail if it is a directory and it is not empty,
132  /// or if it a file and it has replicas.
133  /// @param inode The inode of the file.
134  /// @note This will check for non empty directories.
135  /// @note This will remove associated comments and replicas.
136  virtual void unlink(ino_t inode) ;
137 
138  /// Move a file between two directories.
139  /// @param inode File to be moved.
140  /// @param dest The new parent.
141  virtual void move(ino_t inode, ino_t dest) ;
142 
143  /// Change the name of a file.
144  /// @param inode The inode of the file.
145  /// @param name New name.
146  virtual void rename(ino_t inode, const std::string& name) ;
147 
148  /// Do an extended stat of an entry using its inode.
149  /// @param inode The inode of the file.
150  /// @return The extended status of the file.
151  virtual ExtendedStat extendedStat(ino_t inode) ;
152 
153  /// Do an extended stat of an entry using its inode, exception-safe version.
154  /// @param xstat The extended status of the file.
155  /// @param inode The inode of the file.
156  /// @return The status of the operation.
157  virtual DmStatus extendedStat(ExtendedStat &xstat, ino_t inode) ;
158 
159  /// Do an extended stat of an entry using the parent inode and the name.
160  /// @param parent The parent inode.
161  /// @param name The file or directory name.
162  /// @note No security check will be done.
163  virtual ExtendedStat extendedStat(ino_t parent,
164  const std::string& name) ;
165 
166  /// Do an extended stat of an entry using the parent inode and the name, exception-safe version.
167  /// @param xstat The extended status of the file.
168  /// @param parent The parent inode.
169  /// @param name The file or directory name.
170  /// @return The status of the operation.
171  /// @note No security check will be done.
172  virtual DmStatus extendedStat(ExtendedStat &xstat, ino_t parent,
173  const std::string& name) ;
174 
175  /// Do an extended stat using the GUID.
176  /// @param guid The file GUID.
177  virtual ExtendedStat extendedStat(const std::string& guid) ;
178 
179  /// Get the symlink associated with a inode.
180  /// @param inode The inode of the file.
181  /// @return A SymLink struct.
182  /// @note If inode is not a symlink, an exception will be thrown.
183  virtual SymLink readLink(ino_t inode) ;
184 
185  /// Add a new replica for a file.
186  /// @param replica Stores the data that is going to be added. fileid must
187  /// point to the id of the logical file in the catalog.
188  virtual void addReplica(const Replica& replica) ;
189 
190  /// Delete a replica.
191  /// @param replica The replica to remove.
192  virtual void deleteReplica(const Replica& replica) ;
193 
194  /// Get a replica using the replica ID.
195  /// @param rid The replica ID.
196  virtual Replica getReplica(int64_t rid) ;
197 
198  /// Get a replica.
199  /// @param rfn The replica to retrieve.
200  virtual Replica getReplica(const std::string& rfn) ;
201 
202  /// Modify a replica.
203  /// @param replica The replica data.
204  virtual void updateReplica(const Replica& replica) ;
205 
206  /// Get replicas for a file.
207  /// @param inode The entry inode.
208  virtual std::vector<Replica> getReplicas(ino_t inode) ;
209 
210  /// Change access and/or modification time.
211  /// @param inode The inode of the file.
212  /// @param buf A struct holding the new times.
213  virtual void utime(ino_t inode,
214  const struct utimbuf* buf) ;
215 
216  /// Set the mode of a file.
217  /// @param inode The inode of the file.
218  /// @param uid The owner. If -1, not changed.
219  /// @param gid The group. If -1, not changed.
220  /// @param mode The new mode. S_IFMT bits are cleared, and kept as they
221  /// are in the DB.
222  /// @param acl The new ACL. If empty, not changed.
223  virtual void setMode(ino_t inode, uid_t uid, gid_t gid, mode_t mode,
224  const Acl& acl) ;
225 
226  /// Set the size of a file.
227  /// @param inode The inode of the file.
228  /// @param size The new size.
229  virtual void setSize(ino_t inode, size_t size) ;
230 
231  /// Set the checksum of a file.
232  /// @param inode The inode of the file.
233  /// @param csumtype The checksum type.
234  /// @param csumvalue The checksum value.
235  virtual void setChecksum(ino_t inode, const std::string& csumtype,
236  const std::string& csumvalue) ;
237 
238  /// Get the comment associated to a file.
239  /// @param inode The inode of the file.
240  /// @return The comment.
241  virtual std::string getComment(ino_t inode) ;
242 
243  /// Set the comment associated to a file.
244  /// @param inode The inode of the file.
245  /// @param comment The new comment.
246  virtual void setComment(ino_t inode,
247  const std::string& comment) ;
248 
249  /// Remove the associated comment.
250  /// @param inode The file whose comment will be removed.
251  virtual void deleteComment(ino_t inode) ;
252 
253  /// Set the GUID of a file.
254  /// @param inode The inode of the file.
255  /// @param guid The new GUID.
256  virtual void setGuid(ino_t inode,
257  const std::string& guid) ;
258 
259  /// Update extended metadata on the catalog.
260  /// @param attr The extended attributes struct.
261  virtual void updateExtendedAttributes(ino_t inode,
262  const Extensible& attr) ;
263 
264  /// Open a directory.
265  /// @param inode The inode of the directory.
266  /// @return An opaque pointer to a directory.
267  virtual IDirectory* openDir(ino_t inode) ;
268 
269  /// Close a directory.
270  /// @param dir The opaque structure to close.
271  virtual void closeDir(IDirectory* dir) ;
272 
273  /// Read the next entry.
274  /// @param dir The opaque structure of a directory.
275  /// @return NULL when finished. Extended stat of the next entry otherwise.
276  virtual ExtendedStat* readDirx(IDirectory* dir) ;
277 
278  /// Read the next entry.
279  /// @param dir The opaque structure of a directory.
280  /// @return NULL when finished. Extended stat of the next entry otherwise.
281  virtual struct dirent* readDir (IDirectory* dir) ;
282  };
283 
284  /// INodeFactory
285  class INodeFactory: public virtual BaseFactory {
286  public:
287  /// Destructor
288  virtual ~INodeFactory();
289 
290  protected:
291  // Stack instance is allowed to instantiate INodes
292  friend class StackInstance;
293 
294  /// Children of INodeFactory are allowed to instantiate too (decorator)
295  static INode* createINode(INodeFactory* factory,
296  PluginManager* pm) ;
297 
298  /// Instantiate a implementation of INode
299  virtual INode* createINode(PluginManager* pm) ;
300  };
301 
302 
303 
304 
305  /// Convenience class that releases a resource on destruction
306  class InodeTrans {
307  public:
309  {
310  obj = o;
311  obj->begin();
312  }
313 
315  if (obj != 0) obj->rollback();
316  obj = 0;
317  }
318 
319  void Commit() {
320  if (obj != 0) obj->commit();
321  obj = 0;
322  }
323 
324  private:
326 
327  };
328 
329 
330 
331 };
332 
333 #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:88
InodeTrans(INode *o)
Definition: inode.h:308
virtual ~INode()
Destructor.
File/directory metadata.
Definition: inode.h:29
bool operator<(const ExtendedStat &) const
time_t ptime
Definition: inode.h:85
time_t atime
Definition: inode.h:84
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:95
Definition: dmlite.h:161
virtual void rename(ino_t inode, const std::string &name)
Definition: inode.h:74
virtual void setSize(ino_t inode, size_t size)
virtual ExtendedStat create(const ExtendedStat &f)
virtual void deleteComment(ino_t inode)
std::string csumtype
Definition: inode.h:40
bool operator<(const Replica &) const
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:306
virtual std::string getComment(ino_t inode)
bool operator>(const ExtendedStat &) const
bool operator==(const ExtendedStat &) const
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:314
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:81
INodeFactory.
Definition: inode.h:285
virtual void commit(void)
Commit a transaction.
virtual void deleteReplica(const Replica &replica)
Exceptions used by the API.
FileStatus status
Definition: inode.h:37
virtual ~IDirectory()
INode * obj
Definition: inode.h:325
int64_t nbaccesses
Definition: inode.h:83
Helpful typedef for KeyValue containers.
Definition: extensible.h:20
virtual IDirectory * openDir(ino_t inode)
Base class for factories.
Definition: base.h:48
virtual std::vector< Replica > getReplicas(ino_t inode)
time_t ltime
Definition: inode.h:86
virtual ExtendedStat * readDirx(IDirectory *dir)
std::string rfn
Definition: inode.h:96
virtual void utime(ino_t inode, const struct utimbuf *buf)
std::string name
Definition: inode.h:38
Definition: inode.h:106
bool operator!=(const Replica &) const
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:80
void Commit()
Definition: inode.h:319
Base interfaces.
Acl acl
Definition: inode.h:42
virtual void updateReplica(const Replica &replica)
virtual void closeDir(IDirectory *dir)
Security functionality shared between modules.
Utility methods for checksum handling.
bool operator!=(const ExtendedStat &) const
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:93
bool operator>(const Replica &) const
Namespace for the dmlite C++ API.
Definition: authn.h:15
bool operator==(const Replica &) const
ReplicaType type
Definition: inode.h:89
std::string csumvalue
Definition: inode.h:41
Typedef for directories.
Definition: inode.h:26
virtual struct dirent * readDir(IDirectory *dir)
int getchecksum(std::string &cktype, std::string &ckvalue)
FileStatus
Definition: inode.h:30
virtual void setGuid(ino_t inode, const std::string &guid)