Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / node.h
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/osdirent.h"
14 #include "nacl_io/osstat.h"
15 #include "nacl_io/ostermios.h"
16
17 #include "sdk_util/ref_object.h"
18 #include "sdk_util/scoped_ref.h"
19 #include "sdk_util/simple_lock.h"
20
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)
24
25 namespace nacl_io {
26
27 class Filesystem;
28 class Node;
29 struct HandleAttr;
30
31 typedef sdk_util::ScopedRef<Node> ScopedNode;
32
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 {
36  protected:
37   explicit Node(Filesystem* filesystem);
38   virtual ~Node();
39
40  protected:
41   virtual Error Init(int open_flags);
42   virtual void Destroy();
43
44  public:
45   // Return true if the node permissions match the given open mode.
46   virtual bool CanOpen(int open_flags);
47
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();
52
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
58   // the file.
59   virtual Error FTruncate(off_t length);
60   // Assume that |out_bytes| is non-NULL.
61   virtual Error GetDents(size_t offs,
62                          struct dirent* pdir,
63                          size_t count,
64                          int* out_bytes);
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,
72                      void* buf,
73                      size_t count,
74                      int* out_bytes);
75   // Assume that |buf| and |out_bytes| are non-NULL.
76   virtual Error Write(const HandleAttr& attr,
77                       const void* buf,
78                       size_t count,
79                       int* out_bytes);
80   // Assume that |addr| and |out_addr| are non-NULL.
81   virtual Error MMap(void* addr,
82                      size_t length,
83                      int prot,
84                      int flags,
85                      size_t offset,
86                      void** out_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);
91
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();
100
101   virtual bool IsaDir();
102   virtual bool IsaFile();
103   virtual bool IsaSock();
104
105   // Number of children for this node (directory)
106   virtual int ChildCount();
107
108  protected:
109   // Directory operations on the node are done by the Filesystem. The
110   // filesystem's lock must be held while these calls are made.
111
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);
116
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);
120
121   // Update the link count
122   virtual void Link();
123   virtual void Unlink();
124
125  protected:
126   struct stat stat_;
127   sdk_util::SimpleLock node_lock_;
128
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_;
134
135   friend class DevFs;
136   friend class DirNode;
137   friend class Filesystem;
138   friend class FuseFs;
139   friend class Html5Fs;
140   friend class HttpFs;
141   friend class MemFs;
142 };
143
144 }  // namespace nacl_io
145
146 #endif  // LIBRARIES_NACL_IO_NODE_H_