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 namespace nacl_io {
     21 
     22 class Mount;
     23 class MountNode;
     24 class PepperInterface;
     25 
     26 typedef sdk_util::ScopedRef<Mount> ScopedMount;
     27 typedef std::map<std::string, std::string> StringMap_t;
     28 
     29 // NOTE: The KernelProxy is the only class that should be setting errno. All
     30 // other classes should return Error (as defined by nacl_io/error.h).
     31 class Mount : public sdk_util::RefObject {
     32  protected:
     33   // The protected functions are only used internally and will not
     34   // acquire or release the mount's lock.
     35   Mount();
     36   virtual ~Mount();
     37 
     38   // Init must be called by the factory before the mount is used.
     39   // This function must assign a root node, or replace FindNode.
     40   // |ppapi| can be NULL. If so, this mount cannot make any pepper calls.
     41   virtual Error Init(int dev, StringMap_t& args, PepperInterface* ppapi);
     42   virtual void Destroy();
     43 
     44  public:
     45   PepperInterface* ppapi() { return ppapi_; }
     46 
     47   // All paths in functions below are expected to containing a leading "/".
     48 
     49   // Test whether a file or directory at a given path can be accessed.
     50   // Returns 0 on success, or an appropriate errno value on failure.
     51   virtual Error Access(const Path& path, int a_mode) = 0;
     52 
     53   // Open a node at |path| with the specified open flags. The resulting
     54   // MountNode is created with a ref count of 1.
     55   // Assumes that |out_node| is non-NULL.
     56   virtual Error Open(const Path& path,
     57                      int o_flags,
     58                      ScopedMountNode* out_node) = 0;
     59 
     60   // OpenResource is only used to read files from the NaCl NMF file. No mount
     61   // except MountPassthrough should implement it.
     62   // Assumes that |out_node| is non-NULL.
     63   virtual Error OpenResource(const Path& path, ScopedMountNode* out_node);
     64 
     65   // Unlink, Mkdir, Rmdir will affect the both the RefCount
     66   // and the nlink number in the stat object.
     67   virtual Error Unlink(const Path& path) = 0;
     68   virtual Error Mkdir(const Path& path, int permissions) = 0;
     69   virtual Error Rmdir(const Path& path) = 0;
     70   virtual Error Remove(const Path& path) = 0;
     71 
     72   // Convert from R,W,R/W open flags to STAT permission flags
     73   static int OpenModeToPermission(int mode);
     74 
     75   // Assumes that |node| is non-NULL.
     76   void OnNodeCreated(MountNode* node);
     77 
     78   // Assumes that |node| is non-NULL.
     79   void OnNodeDestroyed(MountNode* node);
     80 
     81  protected:
     82   // Device number for the mount.
     83   int dev_;
     84   PepperInterface* ppapi_;  // Weak reference.
     85   INodePool inode_pool_;
     86 
     87  private:
     88   // May only be called by the KernelProxy when the Kernel's
     89   // lock is held, so we make it private.
     90   friend class KernelObject;
     91   friend class KernelProxy;
     92   DISALLOW_COPY_AND_ASSIGN(Mount);
     93 };
     94 
     95 }  // namespace nacl_io
     96 
     97 #endif  // LIBRARIES_NACL_IO_MOUNT_H_
     98