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.
5 #ifndef LIBRARIES_NACL_IO_NODE_H_
6 #define LIBRARIES_NACL_IO_NODE_H_
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"
17 #include "sdk_util/ref_object.h"
18 #include "sdk_util/scoped_ref.h"
19 #include "sdk_util/simple_lock.h"
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)
31 typedef sdk_util::ScopedRef<Node> ScopedNode;
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 Node : public sdk_util::RefObject {
37 explicit Node(Filesystem* filesystem);
41 virtual Error Init(int open_flags);
42 virtual void Destroy();
45 // Return true if the node permissions match the given open mode.
46 virtual bool CanOpen(int open_flags);
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();
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 filesystem.
56 virtual Error FSync();
57 // It is expected that the derived Node will fill with 0 when growing
59 virtual Error FTruncate(off_t length);
60 // Assume that |out_bytes| is non-NULL.
61 virtual Error GetDents(size_t offs,
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,
75 // Assume that |buf| and |out_bytes| are non-NULL.
76 virtual Error Write(const HandleAttr& attr,
80 // Assume that |addr| and |out_addr| are non-NULL.
81 virtual Error MMap(void* 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);
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(off_t* out_size);
98 // Returns 0 if node is a TTY
99 virtual Error Isatty();
101 virtual bool IsaDir();
102 virtual bool IsaFile();
103 virtual bool IsaSock();
105 // Number of children for this node (directory)
106 virtual int ChildCount();
109 // Directory operations on the node are done by the Filesystem. The
110 // filesystem's lock must be held while these calls are made.
112 // Adds or removes a directory entry updating the link numbers and refcount
113 // Assumes that |node| is non-NULL.
114 virtual Error AddChild(const std::string& name, const ScopedNode& node);
115 virtual Error RemoveChild(const std::string& name);
117 // Find a child and return it without updating the refcount
118 // Assumes that |out_node| is non-NULL.
119 virtual Error FindChild(const std::string& name, ScopedNode* out_node);
121 // Update the link count
123 virtual void Unlink();
127 sdk_util::SimpleLock node_lock_;
129 // We use a pointer directly to avoid cycles in the ref count.
130 // TODO(noelallen) We should change this so it's unnecessary for the node
131 // to track it's parent. When a node is unlinked, the filesystem should do
132 // any cleanup it needs.
133 Filesystem* filesystem_;
136 friend class DirNode;
137 friend class Filesystem;
139 friend class Html5Fs;
144 } // namespace nacl_io
146 #endif // LIBRARIES_NACL_IO_NODE_H_