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