Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / node.h
blob917aee423959dc0ea0ee65fe705cc8cac0652dde
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_
8 #include <stdarg.h>
9 #include <string>
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)
25 #define S_MODEBITS 07777
27 namespace nacl_io {
29 class Filesystem;
30 class Node;
31 struct HandleAttr;
33 typedef sdk_util::ScopedRef<Node> ScopedNode;
35 // NOTE: The KernelProxy is the only class that should be setting errno. All
36 // other classes should return Error (as defined by nacl_io/error.h).
37 class Node : public sdk_util::RefObject {
38 protected:
39 explicit Node(Filesystem* filesystem);
40 virtual ~Node();
42 protected:
43 virtual Error Init(int open_flags);
44 virtual void Destroy();
46 public:
47 // Return true if the node permissions match the given open mode.
48 virtual bool CanOpen(int open_flags);
50 // Returns the emitter for this Node if it has one, if not, assume this
51 // object can not block.
52 virtual EventEmitter* GetEventEmitter();
53 virtual uint32_t GetEventStatus();
55 // Normal OS operations on a node (file), can be called by the kernel
56 // directly so it must lock and unlock appropriately. These functions
57 // must not be called by the filesystem.
58 virtual Error FSync();
59 // It is expected that the derived Node will fill with 0 when growing
60 // the file.
61 virtual Error FTruncate(off_t length);
62 // Assume that |out_bytes| is non-NULL.
63 virtual Error GetDents(size_t offs,
64 struct dirent* pdir,
65 size_t count,
66 int* out_bytes);
67 // Assume that |stat| is non-NULL.
68 virtual Error GetStat(struct stat* stat);
69 // Assume that |arg| is non-NULL.
70 Error Ioctl(int request, ...);
71 virtual Error VIoctl(int request, va_list args);
72 // Assume that |buf| and |out_bytes| are non-NULL.
73 virtual Error Read(const HandleAttr& attr,
74 void* buf,
75 size_t count,
76 int* out_bytes);
77 // Assume that |buf| and |out_bytes| are non-NULL.
78 virtual Error Write(const HandleAttr& attr,
79 const void* buf,
80 size_t count,
81 int* out_bytes);
82 // Assume that |addr| and |out_addr| are non-NULL.
83 virtual Error MMap(void* addr,
84 size_t length,
85 int prot,
86 int flags,
87 size_t offset,
88 void** out_addr);
89 virtual Error Tcflush(int queue_selector);
90 virtual Error Tcgetattr(struct termios* termios_p);
91 virtual Error Tcsetattr(int optional_actions,
92 const struct termios* termios_p);
93 virtual Error Futimens(const struct timespec times[2]);
94 virtual Error Fchmod(mode_t mode);
96 virtual int GetLinks();
97 virtual int GetMode();
98 virtual void SetMode(int mode);
99 virtual int GetType();
100 virtual void SetType(int type);
101 // Assume that |out_size| is non-NULL.
102 virtual Error GetSize(off_t* out_size);
103 // Returns 0 if node is a TTY
104 virtual Error Isatty();
106 virtual bool IsaDir();
107 virtual bool IsaFile();
108 virtual bool IsaSock();
109 virtual bool IsSeekable();
111 // Number of children for this node (directory)
112 virtual int ChildCount();
114 protected:
115 // Directory operations on the node are done by the Filesystem. The
116 // filesystem's lock must be held while these calls are made.
118 // Adds or removes a directory entry updating the link numbers and refcount
119 // Assumes that |node| is non-NULL.
120 virtual Error AddChild(const std::string& name, const ScopedNode& node);
121 virtual Error RemoveChild(const std::string& name);
123 // Find a child and return it without updating the refcount
124 // Assumes that |out_node| is non-NULL.
125 virtual Error FindChild(const std::string& name, ScopedNode* out_node);
127 // Update the link count
128 virtual void Link();
129 virtual void Unlink();
131 // Update the a/m/c time in stat_.
132 enum {
133 UPDATE_ATIME = 1,
134 UPDATE_MTIME = 2,
135 UPDATE_CTIME = 4,
137 void UpdateTime(int update_bits);
139 protected:
140 struct stat stat_;
141 sdk_util::SimpleLock node_lock_;
143 // We use a pointer directly to avoid cycles in the ref count.
144 // TODO(bradnelson) We should change this so it's unnecessary for the node
145 // to track it's parent. When a node is unlinked, the filesystem should do
146 // any cleanup it needs.
147 Filesystem* filesystem_;
149 friend class DevFs;
150 friend class DirNode;
151 friend class Filesystem;
152 friend class FuseFs;
153 friend class Html5Fs;
154 friend class HttpFs;
155 friend class MemFs;
158 } // namespace nacl_io
160 #endif // LIBRARIES_NACL_IO_NODE_H_