dmlite  0.4
p/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 <utime.h>
8 #include <string>
9 #include <vector>
10 #include "base.h"
11 #include "exceptions.h"
12 #include "utils/extensible.h"
13 #include "utils/security.h"
14 
15 namespace dmlite {
16 
17  // Forward declarations.
18  class StackInstance;
19 
20  /// Typedef for directories.
21  struct IDirectory { virtual ~IDirectory() = 0; };
22 
23  /// File/directory metadata.
24  struct ExtendedStat: public Extensible {
25  enum FileStatus { kOnline = '-',
26  kMigrated = 'm'
27  };
28 
29  ino_t parent;
30  struct stat stat;
32  std::string name;
33  std::string guid;
34  std::string csumtype;
35  std::string csumvalue;
37 
38  bool operator == (const ExtendedStat&) const;
39  bool operator != (const ExtendedStat&) const;
40  bool operator < (const ExtendedStat&) const;
41  bool operator > (const ExtendedStat&) const;
42  };
43 
44  /// Symbolic link
45  struct SymLink: public Extensible {
46  ino_t inode;
47  std::string link;
48 
49  bool operator == (const SymLink&) const;
50  bool operator != (const SymLink&) const;
51  bool operator < (const SymLink&) const;
52  bool operator > (const SymLink&) const;
53  };
54 
55  /// File replica metadata
56  struct Replica: public Extensible {
57  enum ReplicaStatus { kAvailable = '-',
60  };
61  enum ReplicaType { kVolatile = 'V',
62  kPermanent = 'P'
63  };
64 
65  int64_t replicaid;
66  int64_t fileid;
67 
68  int64_t nbaccesses;
69  time_t atime;
70  time_t ptime;
71  time_t ltime;
72 
75 
76  std::string server;
77  std::string rfn;
78 
79  bool operator == (const Replica&) const;
80  bool operator != (const Replica&) const;
81  bool operator < (const Replica&) const;
82  bool operator > (const Replica&) const;
83  };
84 
85  /// Low-level interface. Based on i-nodes.
86  /// @note Security checks NOT done on this level.
87  class INode: public virtual BaseInterface {
88  public:
89  /// Destructor
90  virtual ~INode();
91 
92  /// Start a transaction
93  virtual void begin(void) throw (DmException) = 0;
94 
95  /// Commit a transaction
96  virtual void commit(void) throw (DmException) = 0;
97 
98  /// Rollback changes
99  virtual void rollback(void) throw (DmException) = 0;
100 
101  /// Create a new file or directory
102  /// @param f The file that will be inserted. Its fields must be initialized.
103  /// @return An stat of the created file.
104  virtual ExtendedStat create(const ExtendedStat& f) throw (DmException) = 0;
105 
106  /// Create or modify the file inode to point to another file.
107  /// @param inode The file to modify.
108  /// @param link The new symbolic link.
109  /// @note This does NOT create the file. Use create first.
110  virtual void symlink(ino_t inode, const std::string &link) throw (DmException) = 0;
111 
112  /// Remove a file or directory. It will fail if it is a directory and it is not empty,
113  /// or if it a file and it has replicas.
114  /// @param inode The inode of the file.
115  /// @note This will check for non empty directories.
116  /// @note This will remove associated comments and replicas.
117  virtual void unlink(ino_t inode) throw (DmException) = 0;
118 
119  /// Move a file between two directories.
120  /// @param inode File to be moved.
121  /// @param dest The new parent.
122  virtual void move(ino_t inode, ino_t dest) throw (DmException) = 0;
123 
124  /// Change the name of a file.
125  /// @param inode The inode of the file.
126  /// @param name New name.
127  virtual void rename(ino_t inode, const std::string& name) throw (DmException) = 0;
128 
129  /// Do an extended stat of en entry using its inode.
130  /// @param inode The inode of the file.
131  /// @return The extended status of the file.
132  virtual ExtendedStat extendedStat(ino_t inode) throw (DmException) = 0;
133 
134  /// Do an extended stat of an entry using the parent inode and the name.
135  /// @param parent The parent inode.
136  /// @param name The file or directory name.
137  /// @note No security check will be done.
138  virtual ExtendedStat extendedStat(ino_t parent,
139  const std::string& name) throw (DmException) = 0;
140 
141  /// Do an extended stat using the GUID.
142  /// @param guid The file GUID.
143  virtual ExtendedStat extendedStat(const std::string& guid) throw (DmException) = 0;
144 
145  /// Get the symlink associated with a inode.
146  /// @param inode The inode of the file.
147  /// @return A SymLink struct.
148  /// @note If inode is not a symlink, an exception will be thrown.
149  virtual SymLink readLink(ino_t inode) throw (DmException) = 0;
150 
151  /// Add a new replica for a file.
152  /// @param replica Stores the data that is going to be added. fileid must
153  /// point to the id of the logical file in the catalog.
154  virtual void addReplica(const Replica& replica) throw (DmException) = 0;
155 
156  /// Delete a replica.
157  /// @param replica The replica to remove.
158  virtual void deleteReplica(const Replica& replica) throw (DmException) = 0;
159 
160  /// Get a replica using the replica ID.
161  /// @param rid The replica ID.
162  virtual Replica getReplica(int64_t rid) throw (DmException) = 0;
163 
164  /// Get a replica.
165  /// @param rfn The replica to retrieve.
166  virtual Replica getReplica(const std::string& rfn) throw (DmException) = 0;
167 
168  /// Modify a replica.
169  /// @param replica The replica data.
170  virtual void updateReplica(const Replica& replica) throw (DmException) = 0;
171 
172  /// Get replicas for a file.
173  /// @param inode The entry inode.
174  virtual std::vector<Replica> getReplicas(ino_t inode) throw (DmException) = 0;
175 
176  /// Change access and/or modification time.
177  /// @param inode The inode of the file.
178  /// @param buf A struct holding the new times.
179  virtual void utime(ino_t inode,
180  const struct utimbuf* buf) throw (DmException) = 0;
181 
182  /// Set the mode of a file.
183  /// @param inode The inode of the file.
184  /// @param uid The owner. If -1, not changed.
185  /// @param gid The group. If -1, not changed.
186  /// @param mode The new mode. S_IFMT bits are cleared, and kept as they
187  /// are in the DB.
188  /// @param acl The new ACL. If empty, not changed.
189  virtual void setMode(ino_t inode, uid_t uid, gid_t gid, mode_t mode,
190  const Acl& acl) throw (DmException) = 0;
191 
192  /// Set the size of a file.
193  /// @param inode The inode of the file.
194  /// @param size The new size.
195  virtual void setSize(ino_t inode, size_t size) throw (DmException) = 0;
196 
197  /// Set the checksum of a file.
198  /// @param inode The inode of the file.
199  /// @param csumtype The checksum type.
200  /// @param csumvalue The checksum value.
201  virtual void setChecksum(ino_t inode, const std::string& csumtype,
202  const std::string& csumvalue) throw (DmException) = 0;
203 
204  /// Get the comment associated to a file.
205  /// @param inode The inode of the file.
206  /// @return The comment.
207  virtual std::string getComment(ino_t inode) throw (DmException) = 0;
208 
209  /// Set the comment associated to a file.
210  /// @param inode The inode of the file.
211  /// @param comment The new comment.
212  virtual void setComment(ino_t inode,
213  const std::string& comment) throw (DmException) = 0;
214 
215  /// Remove the associated comment.
216  /// @param inode The file whose comment will be removed.
217  virtual void deleteComment(ino_t inode) throw (DmException) = 0;
218 
219  /// Set the GUID of a file.
220  /// @param inode The inode of the file.
221  /// @param guid The new GUID.
222  virtual void setGuid(ino_t inode,
223  const std::string& guid) throw (DmException) = 0;
224 
225  /// Update extended metadata on the catalog.
226  /// @param attr The extended attributes struct.
227  virtual void updateExtendedAttributes(ino_t inode,
228  const Extensible& attr) throw (DmException) = 0;
229 
230  /// Open a directory.
231  /// @param inode The inode of the directory.
232  /// @return An opaque pointer to a directory.
233  virtual IDirectory* openDir(ino_t inode) throw (DmException) = 0;
234 
235  /// Close a directory.
236  /// @param dir The opaque structure to close.
237  virtual void closeDir(IDirectory* dir) throw (DmException) = 0;
238 
239  /// Read the next entry.
240  /// @param dir The opaque structure of a directory.
241  /// @return NULL when finished. Extended stat of the next entry otherwise.
242  virtual ExtendedStat* readDirx(IDirectory* dir) throw (DmException) = 0;
243 
244  /// Read the next entry.
245  /// @param dir The opaque structure of a directory.
246  /// @return NULL when finished. Extended stat of the next entry otherwise.
247  virtual struct dirent* readDir (IDirectory* dir) throw (DmException) = 0;
248  };
249 
250  /// INodeFactory
251  class INodeFactory: public virtual BaseFactory {
252  public:
253  /// Destructor
254  virtual ~INodeFactory();
255 
256  protected:
257  // Stack instance is allowed to instantiate INodes
258  friend class StackInstance;
259 
260  /// Children of INodeFactory are allowed to instantiate too (decorator)
261  static INode* createINode(INodeFactory* factory,
262  PluginManager* pm) throw (DmException);
263 
264  /// Instantiate a implementation of INode
265  virtual INode* createINode(PluginManager* pm) throw (DmException) = 0;
266  };
267 
268 };
269 
270 #endif // DMLITE_CPP_INODE_H