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/log.h"
14 #include "nacl_io/osdirent.h"
15 #include "nacl_io/osstat.h"
16 #include "nacl_io/ostermios.h"
18 #include "sdk_util/ref_object.h"
19 #include "sdk_util/scoped_ref.h"
20 #include "sdk_util/simple_lock.h"
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)
32 typedef sdk_util::ScopedRef<Node> ScopedNode;
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 {
38 explicit Node(Filesystem* filesystem);
42 virtual Error Init(int open_flags);
43 virtual void Destroy();
46 // Return true if the node permissions match the given open mode.
47 virtual bool CanOpen(int open_flags);
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();
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
60 virtual Error FTruncate(off_t length);
61 // Assume that |out_bytes| is non-NULL.
62 virtual Error GetDents(size_t offs,
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,
76 // Assume that |buf| and |out_bytes| are non-NULL.
77 virtual Error Write(const HandleAttr& attr,
81 // Assume that |addr| and |out_addr| are non-NULL.
82 virtual Error MMap(void* 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);
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();
105 virtual bool IsaDir();
106 virtual bool IsaFile();
107 virtual bool IsaSock();
109 // Number of children for this node (directory)
110 virtual int ChildCount();
113 // Directory operations on the node are done by the Filesystem. The
114 // filesystem's lock must be held while these calls are made.
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);
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);
125 // Update the link count
127 virtual void Unlink();
131 sdk_util::SimpleLock node_lock_;
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_;
140 friend class DirNode;
141 friend class Filesystem;
143 friend class Html5Fs;
148 } // namespace nacl_io
150 #endif // LIBRARIES_NACL_IO_NODE_H_