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