include/dmlite/cpp/inode.h

Go to the documentation of this file.
00001 /// @file   include/dmlite/cpp/inode.h
00002 /// @brief  Low-level access API.
00003 /// @author Alejandro Álvarez Ayllón <aalvarez@cern.ch>
00004 #ifndef DMLITE_CPP_INODE_H
00005 #define DMLITE_CPP_INODE_H
00006 
00007 #include "dmlite/common/config.h"
00008 #include "base.h"
00009 #include "exceptions.h"
00010 #include "utils/extensible.h"
00011 #include "utils/security.h"
00012 #include "utils/checksums.h"
00013 
00014 #include <dirent.h>
00015 #include <utime.h>
00016 #include <string>
00017 #include <vector>
00018 
00019 namespace dmlite {
00020   
00021   // Forward declarations.
00022   class StackInstance;  
00023   
00024   /// Typedef for directories.
00025   struct IDirectory { virtual ~IDirectory(); };
00026   
00027   /// File/directory metadata.
00028     struct ExtendedStat: public Extensible {
00029       enum FileStatus { kOnline = '-',
00030                         kMigrated = 'm'
00031                       };
00032       
00033       ino_t         parent;
00034       struct stat stat;
00035       FileStatus    status;
00036       std::string   name;
00037       std::string   guid;
00038       std::string   csumtype;
00039       std::string   csumvalue;
00040       Acl           acl;
00041       
00042       bool operator == (const ExtendedStat&) const;
00043       bool operator != (const ExtendedStat&) const;
00044       bool operator <  (const ExtendedStat&) const;
00045       bool operator >  (const ExtendedStat&) const;
00046       
00047       void fixchecksums() {
00048         // If legacy fields are empty then fill them with a suitable chksum from the xattrs
00049         if (!csumtype.length() || !csumvalue.length()) {
00050           std::string shortCsumType;
00051           std::vector<std::string> keys = getKeys();
00052 
00053           for (unsigned i = 0; i < keys.size(); ++i) {
00054             if (checksums::isChecksumFullName(keys[i])) {
00055               std::string csumXattr = keys[i];
00056               shortCsumType = checksums::shortChecksumName(csumXattr.substr(9));
00057               if (!shortCsumType.empty() && shortCsumType.length() <= 2) {
00058                 csumvalue     = getString(csumXattr);
00059                 csumtype      = shortCsumType;
00060                 break;
00061               }
00062             }
00063           }
00064 
00065         }
00066         else {
00067           // If legacy fields are not empty make sure that the same chksum is presented as an xattr too
00068           checksums::fillChecksumInXattr(*this);
00069         }
00070       }
00071       
00072       
00073     };
00074   
00075   /// Symbolic link
00076   struct SymLink: public Extensible {
00077     ino_t       inode;
00078     std::string link;
00079     
00080     bool operator == (const SymLink&) const;
00081     bool operator != (const SymLink&) const;
00082     bool operator <  (const SymLink&) const;
00083     bool operator >  (const SymLink&) const;
00084   };
00085   
00086   /// File replica metadata
00087   struct Replica: public Extensible {
00088     enum ReplicaStatus { kAvailable      = '-',
00089                          kBeingPopulated = 'P',
00090                          kToBeDeleted    = 'D'
00091                        };
00092     enum ReplicaType   { kVolatile  = 'V',
00093                          kPermanent = 'P'
00094                        };
00095     
00096     int64_t    replicaid;
00097     int64_t    fileid;
00098     
00099     int64_t    nbaccesses;
00100     time_t     atime;
00101     time_t     ptime;
00102     time_t     ltime;
00103     
00104     ReplicaStatus status;
00105     ReplicaType   type;
00106     
00107     std::string server;
00108     std::string rfn;
00109     
00110     bool operator == (const Replica&) const;
00111     bool operator != (const Replica&) const;
00112     bool operator <  (const Replica&) const;
00113     bool operator >  (const Replica&) const;
00114   };
00115 
00116   /// Low-level interface. Based on i-nodes.
00117   /// @note Security checks NOT done on this level.
00118   class INode: public virtual BaseInterface {
00119    public:
00120     /// Destructor
00121     virtual ~INode();
00122 
00123     /// Start a transaction
00124     virtual void begin(void) throw (DmException);
00125 
00126     /// Commit a transaction
00127     virtual void commit(void) throw (DmException);
00128 
00129     /// Rollback changes
00130     virtual void rollback(void) throw (DmException);
00131     
00132     /// Create a new file or directory
00133     /// @param f  The file that will be inserted. Its fields must be initialized.
00134     /// @return   An stat of the created file.
00135     virtual ExtendedStat create(const ExtendedStat& f) throw (DmException);
00136 
00137     /// Create or modify the file inode to point to another file.
00138     /// @param inode The file to modify.
00139     /// @param link  The new symbolic link.
00140     /// @note This does NOT create the file. Use create first.
00141     virtual void symlink(ino_t inode, const std::string &link) throw (DmException);
00142 
00143     /// Remove a file or directory. It will fail if it is a directory and it is not empty,
00144     /// or if it a file and it has replicas.
00145     /// @param inode The inode of the file.
00146     /// @note This will check for non empty directories.
00147     /// @note This will remove associated comments and replicas.
00148     virtual void unlink(ino_t inode) throw (DmException);
00149 
00150     /// Move a file between two directories.
00151     /// @param inode  File to be moved.
00152     /// @param dest   The new parent.
00153     virtual void move(ino_t inode, ino_t dest) throw (DmException);
00154 
00155     /// Change the name of a file.
00156     /// @param inode The inode of the file.
00157     /// @param name  New name.
00158     virtual void rename(ino_t inode, const std::string& name) throw (DmException);
00159 
00160     /// Do an extended stat of en entry using its inode.
00161     /// @param inode The inode of the file.
00162     /// @return      The extended status of the file.
00163     virtual ExtendedStat extendedStat(ino_t inode) throw (DmException);
00164 
00165     /// Do an extended stat of an entry using the parent inode and the name.
00166     /// @param parent The parent inode.
00167     /// @param name   The file or directory name.
00168     /// @note         No security check will be done.
00169     virtual ExtendedStat extendedStat(ino_t parent,
00170                                       const std::string& name) throw (DmException);
00171 
00172     /// Do an extended stat using the GUID.
00173     /// @param guid The file GUID.
00174     virtual ExtendedStat extendedStat(const std::string& guid) throw (DmException);
00175 
00176     /// Get the symlink associated with a inode.
00177     /// @param inode The inode of the file.
00178     /// @return      A SymLink struct.
00179     /// @note        If inode is not a symlink, an exception will be thrown.
00180     virtual SymLink readLink(ino_t inode) throw (DmException);
00181 
00182     /// Add a new replica for a file.
00183     /// @param replica Stores the data that is going to be added. fileid must
00184     ///                point to the id of the logical file in the catalog.
00185     virtual void addReplica(const Replica& replica) throw (DmException);
00186 
00187     /// Delete a replica.
00188     /// @param replica The replica to remove.
00189     virtual void deleteReplica(const Replica& replica) throw (DmException);
00190 
00191     /// Get a replica using the replica ID.
00192     /// @param rid The replica ID.
00193     virtual Replica getReplica(int64_t rid) throw (DmException);
00194 
00195     /// Get a replica.
00196     /// @param rfn The replica to retrieve.
00197     virtual Replica getReplica(const std::string& rfn) throw (DmException);
00198 
00199     /// Modify a replica.
00200     /// @param replica The replica data.
00201     virtual void updateReplica(const Replica& replica) throw (DmException);
00202 
00203     /// Get replicas for a file.
00204     /// @param inode The entry inode.
00205     virtual std::vector<Replica> getReplicas(ino_t inode) throw (DmException);
00206 
00207     /// Change access and/or modification time.
00208     /// @param inode The inode of the file.
00209     /// @param buf   A struct holding the new times.
00210     virtual void utime(ino_t inode,
00211                        const struct utimbuf* buf) throw (DmException);
00212 
00213     /// Set the mode of a file.
00214     /// @param inode The inode of the file.
00215     /// @param uid   The owner. If -1, not changed.
00216     /// @param gid   The group. If -1, not changed.
00217     /// @param mode  The new mode. S_IFMT bits are cleared, and kept as they
00218     ///              are in the DB.
00219     /// @param acl   The new ACL. If empty, not changed.
00220     virtual void setMode(ino_t inode, uid_t uid, gid_t gid, mode_t mode,
00221                          const Acl& acl) throw (DmException);
00222 
00223     /// Set the size of a file.
00224     /// @param inode The inode of the file.
00225     /// @param size  The new size.
00226     virtual void setSize(ino_t inode, size_t size) throw (DmException);
00227 
00228     /// Set the checksum of a file.
00229     /// @param inode     The inode of the file.
00230     /// @param csumtype  The checksum type.
00231     /// @param csumvalue The checksum value.
00232     virtual void setChecksum(ino_t inode, const std::string& csumtype,
00233                              const std::string& csumvalue) throw (DmException);
00234 
00235     /// Get the comment associated to a file.
00236     /// @param inode The inode of the file.
00237     /// @return The comment.
00238     virtual std::string getComment(ino_t inode) throw (DmException);
00239 
00240     /// Set the comment associated to a file.
00241     /// @param inode   The inode of the file.
00242     /// @param comment The new comment.
00243     virtual void setComment(ino_t inode,
00244                             const std::string& comment) throw (DmException);
00245 
00246     /// Remove the associated comment.
00247     /// @param inode The file whose comment will be removed.
00248     virtual void deleteComment(ino_t inode) throw (DmException);
00249 
00250     /// Set the GUID of a file.
00251     /// @param inode The inode of the file.
00252     /// @param guid  The new GUID.
00253     virtual void setGuid(ino_t inode,
00254                          const std::string& guid) throw (DmException);
00255     
00256     /// Update extended metadata on the catalog.
00257     /// @param attr The extended attributes struct.
00258     virtual void updateExtendedAttributes(ino_t inode,
00259                                           const Extensible& attr) throw (DmException);
00260 
00261     /// Open a directory.
00262     /// @param inode The inode of the directory.
00263     /// @return An opaque pointer to a directory.
00264     virtual IDirectory* openDir(ino_t inode) throw (DmException);
00265 
00266     /// Close a directory.
00267     /// @param dir The opaque structure to close.
00268     virtual void closeDir(IDirectory* dir) throw (DmException);
00269 
00270     /// Read the next entry.
00271     /// @param dir The opaque structure of a directory.
00272     /// @return NULL when finished. Extended stat of the next entry otherwise.
00273     virtual ExtendedStat* readDirx(IDirectory* dir) throw (DmException);
00274 
00275     /// Read the next entry.
00276     /// @param dir The opaque structure of a directory.
00277     /// @return NULL when finished. Extended stat of the next entry otherwise.
00278     virtual struct dirent* readDir (IDirectory* dir) throw (DmException);
00279   };
00280 
00281   /// INodeFactory
00282   class INodeFactory: public virtual BaseFactory {
00283    public:
00284     /// Destructor
00285     virtual ~INodeFactory();
00286 
00287    protected:
00288     // Stack instance is allowed to instantiate INodes
00289     friend class StackInstance;  
00290 
00291     /// Children of INodeFactory are allowed to instantiate too (decorator)
00292     static INode* createINode(INodeFactory* factory,
00293                               PluginManager* pm) throw (DmException);
00294 
00295     /// Instantiate a implementation of INode
00296     virtual INode* createINode(PluginManager* pm) throw (DmException);
00297   };
00298   
00299 };
00300 
00301 #endif // DMLITE_CPP_INODE_H

Generated on 4 May 2016 for dmlite by  doxygen 1.4.7