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_NODE_H_
      6 #define LIBRARIES_NACL_IO_MOUNT_NODE_H_
      7 
      8 #include <stdarg.h>
      9 #include <string>
     10 
     11 #include "nacl_io/error.h"
     12 #include "nacl_io/event_listener.h"
     13 #include "nacl_io/osdirent.h"
     14 #include "nacl_io/osstat.h"
     15 #include "nacl_io/ostermios.h"
     16 
     17 #include "sdk_util/ref_object.h"
     18 #include "sdk_util/scoped_ref.h"
     19 #include "sdk_util/simple_lock.h"
     20 
     21 #define S_IRALL (S_IRUSR | S_IRGRP | S_IROTH)
     22 #define S_IWALL (S_IWUSR | S_IWGRP | S_IWOTH)
     23 #define S_IXALL (S_IXUSR | S_IXGRP | S_IXOTH)
     24 
     25 namespace nacl_io {
     26 
     27 class Mount;
     28 class MountNode;
     29 struct HandleAttr;
     30 
     31 typedef sdk_util::ScopedRef<MountNode> ScopedMountNode;
     32 
     33 // NOTE: The KernelProxy is the only class that should be setting errno. All
     34 // other classes should return Error (as defined by nacl_io/error.h).
     35 class MountNode : public sdk_util::RefObject {
     36  protected:
     37   explicit MountNode(Mount* mount);
     38   virtual ~MountNode();
     39 
     40  protected:
     41   virtual Error Init(int open_flags);
     42   virtual void Destroy();
     43 
     44  public:
     45   // Return true if the node permissions match the given open mode.
     46   virtual bool CanOpen(int open_flags);
     47 
     48   // Returns the emitter for this Node if it has one, if not, assume this
     49   // object can not block.
     50   virtual EventEmitter* GetEventEmitter();
     51   virtual uint32_t GetEventStatus();
     52 
     53   // Normal OS operations on a node (file), can be called by the kernel
     54   // directly so it must lock and unlock appropriately.  These functions
     55   // must not be called by the mount.
     56   virtual Error FSync();
     57   // It is expected that the derived MountNode will fill with 0 when growing
     58   // the file.
     59   virtual Error FTruncate(off_t length);
     60   // Assume that |out_bytes| is non-NULL.
     61   virtual Error GetDents(size_t offs,
     62                          struct dirent* pdir,
     63                          size_t count,
     64                          int* out_bytes);
     65   // Assume that |stat| is non-NULL.
     66   virtual Error GetStat(struct stat* stat);
     67   // Assume that |arg| is non-NULL.
     68   Error Ioctl(int request, ...);
     69   virtual Error VIoctl(int request, va_list args);
     70   // Assume that |buf| and |out_bytes| are non-NULL.
     71   virtual Error Read(const HandleAttr& attr,
     72                      void* buf,
     73                      size_t count,
     74                      int* out_bytes);
     75   // Assume that |buf| and |out_bytes| are non-NULL.
     76   virtual Error Write(const HandleAttr& attr,
     77                       const void* buf,
     78                       size_t count,
     79                       int* out_bytes);
     80   // Assume that |addr| and |out_addr| are non-NULL.
     81   virtual Error MMap(void* addr,
     82                      size_t length,
     83                      int prot,
     84                      int flags,
     85                      size_t offset,
     86                      void** out_addr);
     87   virtual Error Tcflush(int queue_selector);
     88   virtual Error Tcgetattr(struct termios* termios_p);
     89   virtual Error Tcsetattr(int optional_actions,
     90                           const struct termios *termios_p);
     91 
     92   virtual int GetLinks();
     93   virtual int GetMode();
     94   virtual int GetType();
     95   virtual void SetType(int type);
     96   // Assume that |out_size| is non-NULL.
     97   virtual Error GetSize(size_t* out_size);
     98   virtual bool IsaDir();
     99   virtual bool IsaFile();
    100   virtual bool IsaSock();
    101   virtual bool IsaTTY();
    102 
    103   // Number of children for this node (directory)
    104   virtual int ChildCount();
    105 
    106  protected:
    107   // Directory operations on the node are done by the Mount. The mount's lock
    108   // must be held while these calls are made.
    109 
    110   // Adds or removes a directory entry updating the link numbers and refcount
    111   // Assumes that |node| is non-NULL.
    112   virtual Error AddChild(const std::string& name, const ScopedMountNode& node);
    113   virtual Error RemoveChild(const std::string& name);
    114 
    115   // Find a child and return it without updating the refcount
    116   // Assumes that |out_node| is non-NULL.
    117   virtual Error FindChild(const std::string& name, ScopedMountNode* out_node);
    118 
    119   // Update the link count
    120   virtual void Link();
    121   virtual void Unlink();
    122 
    123  protected:
    124   struct stat stat_;
    125   sdk_util::SimpleLock node_lock_;
    126 
    127   // We use a pointer directly to avoid cycles in the ref count.
    128   // TODO(noelallen) We should change this so it's unnecessary for the node
    129   // to track it's parent.  When a node is unlinked, the mount should do
    130   // any cleanup it needs.
    131   Mount* mount_;
    132 
    133   friend class Mount;
    134   friend class MountDev;
    135   friend class MountFuse;
    136   friend class MountHtml5Fs;
    137   friend class MountHttp;
    138   friend class MountMem;
    139   friend class MountNodeDir;
    140 };
    141 
    142 }  // namespace nacl_io
    143 
    144 #endif  // LIBRARIES_NACL_IO_MOUNT_NODE_H_
    145