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