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
20namespace 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
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:
77 kToBeDeleted = 'D'
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.
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)
303 PluginManager* pm) ;
304
305 /// Instantiate a implementation of INode
307 };
308
309
310
311
312 /// Convenience class that releases a resource on destruction
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
Base interfaces.
Definition security.h:52
Base class for factories.
Definition base.h:48
Base class for interfaces.
Definition base.h:18
Definition status.h:17
File/directory metadata.
Definition inode.h:29
std::string name
Definition inode.h:39
int getchecksum(std::string &cktype, std::string &ckvalue)
struct stat stat
Definition inode.h:37
FileStatus
Definition inode.h:31
@ kMigrated
Definition inode.h:32
@ kOnline
Definition inode.h:31
@ kDeleted
Definition inode.h:33
bool operator>(const ExtendedStat &) const
bool operator<(const ExtendedStat &) const
Acl acl
Definition inode.h:43
std::string csumtype
Definition inode.h:41
bool operator==(const ExtendedStat &) const
bool operator!=(const ExtendedStat &) const
std::string csumvalue
Definition inode.h:42
ino_t parent
Definition inode.h:36
FileStatus status
Definition inode.h:38
std::string guid
Definition inode.h:40
Helpful typedef for KeyValue containers.
Definition extensible.h:20
INodeFactory.
Definition inode.h:292
static INode * createINode(INodeFactory *factory, PluginManager *pm)
Children of INodeFactory are allowed to instantiate too (decorator)
virtual INode * createINode(PluginManager *pm)
Instantiate a implementation of INode.
virtual ~INodeFactory()
Destructor.
Definition inode.h:113
virtual void begin(void)
Start a transaction.
virtual DmStatus extendedStat(ExtendedStat &xstat, ino_t inode)
virtual SymLink readLink(ino_t inode)
virtual void move(ino_t inode, ino_t dest)
virtual void utime(ino_t inode, const struct utimbuf *buf)
virtual ~INode()
Destructor.
virtual void unlink(ino_t inode)
virtual DmStatus extendedStat(ExtendedStat &xstat, ino_t parent, const std::string &name)
virtual void addReplica(const Replica &replica)
virtual Replica getReplica(const std::string &rfn)
virtual ExtendedStat extendedStat(ino_t inode)
virtual std::string getComment(ino_t inode)
virtual void rollback(void)
Rollback changes.
virtual IDirectory * openDir(ino_t inode)
virtual void rename(ino_t inode, const std::string &name)
virtual ExtendedStat create(const ExtendedStat &f)
virtual ExtendedStat extendedStat(ino_t parent, const std::string &name)
virtual std::vector< Replica > getReplicas(ino_t inode)
virtual void closeDir(IDirectory *dir)
virtual Replica getReplica(int64_t rid)
virtual void commit(void)
Commit a transaction.
virtual void setComment(ino_t inode, const std::string &comment)
virtual void symlink(ino_t inode, const std::string &link)
virtual struct dirent * readDir(IDirectory *dir)
virtual void setChecksum(ino_t inode, const std::string &csumtype, const std::string &csumvalue)
virtual void deleteComment(ino_t inode)
virtual ExtendedStat * readDirx(IDirectory *dir)
virtual void setGuid(ino_t inode, const std::string &guid)
virtual void setMode(ino_t inode, uid_t uid, gid_t gid, mode_t mode, const Acl &acl)
virtual void updateExtendedAttributes(ino_t inode, const Extensible &attr)
virtual void updateReplica(const Replica &replica)
virtual ExtendedStat extendedStat(const std::string &guid)
virtual void setSize(ino_t inode, size_t size)
virtual void deleteReplica(const Replica &replica)
Convenience class that releases a resource on destruction.
Definition inode.h:313
InodeTrans(INode *o)
Definition inode.h:315
void Commit()
Definition inode.h:326
INode * obj
Definition inode.h:332
~InodeTrans()
Definition inode.h:321
CatalogInterface can only be instantiated through this class.
Definition dmlite.h:42
File replica metadata.
Definition inode.h:73
std::string rfn
Definition inode.h:103
bool operator<(const Replica &) const
std::string setname
Definition inode.h:100
ReplicaStatus status
Definition inode.h:94
int64_t replicaid
Definition inode.h:86
time_t ptime
Definition inode.h:91
ReplicaPS
Definition inode.h:82
@ kSecondary
Definition inode.h:83
@ kPrimary
Definition inode.h:82
bool operator>(const Replica &) const
ReplicaPS rtype
Definition inode.h:96
time_t ltime
Definition inode.h:92
std::string server
Definition inode.h:102
ReplicaType
Definition inode.h:79
@ kPermanent
Definition inode.h:80
@ kVolatile
Definition inode.h:79
int64_t fileid
Definition inode.h:87
ReplicaStatus
Definition inode.h:75
@ kAvailable
Definition inode.h:75
@ kToBeDeleted
Definition inode.h:77
@ kBeingPopulated
Definition inode.h:76
int64_t nbaccesses
Definition inode.h:89
bool operator==(const Replica &) const
bool operator!=(const Replica &) const
time_t atime
Definition inode.h:90
ReplicaType type
Definition inode.h:95
Definition dmlite.h:161
Utility methods for checksum handling.
Exceptions used by the API.
Extensible types (hold metadata).
Namespace for the dmlite C++ API.
Definition authn.h:16
Security functionality shared between modules.
Status objects used by the API.
Typedef for directories.
Definition inode.h:26
virtual ~IDirectory()