Home | History | Annotate | Download | only in nacl_io
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef LIBRARIES_NACL_IO_MOUNT_H_
      6 #define LIBRARIES_NACL_IO_MOUNT_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "nacl_io/error.h"
     12 #include "nacl_io/inode_pool.h"
     13 #include "nacl_io/mount_node.h"
     14 #include "nacl_io/path.h"
     15 
     16 #include "sdk_util/macros.h"
     17 #include "sdk_util/ref_object.h"
     18 #include "sdk_util/scoped_ref.h"
     19 
     20 struct fuse_operations;
     21 
     22 namespace nacl_io {
     23 
     24 class Mount;
     25 class MountNode;
     26 class PepperInterface;
     27 
     28 typedef sdk_util::ScopedRef<Mount> ScopedMount;
     29 typedef std::map<std::string, std::string> StringMap_t;
     30 
     31 // This structure is passed to all mounts via the Mount::Init virtual function.
     32 // With it, we can add or remove initialization values without changing the
     33 // function signature.
     34 struct MountInitArgs {
     35   MountInitArgs() : dev(0), ppapi(NULL), fuse_ops(NULL) {}
     36   explicit MountInitArgs(int dev) : dev(dev), ppapi(NULL), fuse_ops(NULL) {}
     37 
     38   // Device number of the new filesystem.
     39   int dev;
     40   StringMap_t string_map;
     41   PepperInterface* ppapi;
     42   fuse_operations* fuse_ops;
     43 };
     44 
     45 // NOTE: The KernelProxy is the only class that should be setting errno. All
     46 // other classes should return Error (as defined by nacl_io/error.h).
     47 class Mount : public sdk_util::RefObject {
     48  protected:
     49   // The protected functions are only used internally and will not
     50   // acquire or release the mount's lock.
     51   Mount();
     52   virtual ~Mount();
     53 
     54   // Init must be called by the factory before the mount is used.
     55   // |ppapi| can be NULL. If so, this mount cannot make any pepper calls.
     56   virtual Error Init(const MountInitArgs& args);
     57   virtual void Destroy();
     58 
     59  public:
     60   PepperInterface* ppapi() { return ppapi_; }
     61 
     62   // All paths in functions below are expected to containing a leading "/".
     63 
     64   // Test whether a file or directory at a given path can be accessed.
     65   // Returns 0 on success, or an appropriate errno value on failure.
     66   virtual Error Access(const Path& path, int a_mode) = 0;
     67 
     68   // Open a node at |path| with the specified open flags. The resulting
     69   // MountNode is created with a ref count of 1.
     70   // Assumes that |out_node| is non-NULL.
     71   virtual Error Open(const Path& path,
     72                      int open_flags,
     73                      ScopedMountNode* out_node) = 0;
     74 
     75   // OpenResource is only used to read files from the NaCl NMF file. No mount
     76   // except MountPassthrough should implement it.
     77   // Assumes that |out_node| is non-NULL.
     78   virtual Error OpenResource(const Path& path, ScopedMountNode* out_node);
     79 
     80   // Unlink, Mkdir, Rmdir will affect the both the RefCount
     81   // and the nlink number in the stat object.
     82   virtual Error Unlink(const Path& path) = 0;
     83   virtual Error Mkdir(const Path& path, int permissions) = 0;
     84   virtual Error Rmdir(const Path& path) = 0;
     85   virtual Error Remove(const Path& path) = 0;
     86   virtual Error Rename(const Path& path, const Path& newpath) = 0;
     87 
     88   // Assumes that |node| is non-NULL.
     89   void OnNodeCreated(MountNode* node);
     90 
     91   // Assumes that |node| is non-NULL.
     92   void OnNodeDestroyed(MountNode* node);
     93 
     94  protected:
     95   // Device number for the mount.
     96   int dev_;
     97   PepperInterface* ppapi_;  // Weak reference.
     98   INodePool inode_pool_;
     99 
    100  private:
    101   // May only be called by the KernelProxy when the Kernel's
    102   // lock is held, so we make it private.
    103   friend class KernelObject;
    104   friend class KernelProxy;
    105   DISALLOW_COPY_AND_ASSIGN(Mount);
    106 };
    107 
    108 }  // namespace nacl_io
    109 
    110 #endif  // LIBRARIES_NACL_IO_MOUNT_H_
    111