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