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