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