node.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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
26namespace nacl_io {
27
28class Filesystem;
29class Node;
30struct HandleAttr;
31
32typedef 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).
36class 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
93  virtual int GetLinks();
94  virtual int GetMode();
95  virtual int GetType();
96  virtual void SetType(int type);
97  // Assume that |out_size| is non-NULL.
98  virtual Error GetSize(off_t* out_size);
99  // Returns 0 if node is a TTY
100  virtual Error Isatty();
101
102  virtual bool IsaDir();
103  virtual bool IsaFile();
104  virtual bool IsaSock();
105
106  // Number of children for this node (directory)
107  virtual int ChildCount();
108
109 protected:
110  // Directory operations on the node are done by the Filesystem. The
111  // filesystem's lock must be held while these calls are made.
112
113  // Adds or removes a directory entry updating the link numbers and refcount
114  // Assumes that |node| is non-NULL.
115  virtual Error AddChild(const std::string& name, const ScopedNode& node);
116  virtual Error RemoveChild(const std::string& name);
117
118  // Find a child and return it without updating the refcount
119  // Assumes that |out_node| is non-NULL.
120  virtual Error FindChild(const std::string& name, ScopedNode* out_node);
121
122  // Update the link count
123  virtual void Link();
124  virtual void Unlink();
125
126 protected:
127  struct stat stat_;
128  sdk_util::SimpleLock node_lock_;
129
130  // We use a pointer directly to avoid cycles in the ref count.
131  // TODO(noelallen) We should change this so it's unnecessary for the node
132  // to track it's parent.  When a node is unlinked, the filesystem should do
133  // any cleanup it needs.
134  Filesystem* filesystem_;
135
136  friend class DevFs;
137  friend class DirNode;
138  friend class Filesystem;
139  friend class FuseFs;
140  friend class Html5Fs;
141  friend class HttpFs;
142  friend class MemFs;
143};
144
145}  // namespace nacl_io
146
147#endif  // LIBRARIES_NACL_IO_NODE_H_
148