blob: 2afc88ba0d4d8bbc806b1e1e70a22f70d1a01af2 [file] [log] [blame]
Ben Murdochca12bfa2013-07-23 11:17:05 +01001// Copyright (c) 2012 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
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00005#ifndef LIBRARIES_NACL_IO_MOUNT_NODE_H_
6#define LIBRARIES_NACL_IO_MOUNT_NODE_H_
7
8#include <string>
9
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010010#include "nacl_io/error.h"
Ben Murdochfb250652013-07-31 11:42:55 +010011#include "nacl_io/event_listener.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010012#include "nacl_io/osdirent.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000013#include "nacl_io/osstat.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010014#include "nacl_io/ostermios.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010015
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010016#include "sdk_util/ref_object.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010017#include "sdk_util/scoped_ref.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010018#include "sdk_util/simple_lock.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000019
Ben Murdochca12bfa2013-07-23 11:17:05 +010020namespace nacl_io {
Ben Murdocheb525c52013-07-10 11:40:50 +010021
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000022class Mount;
Ben Murdocheb525c52013-07-10 11:40:50 +010023class MountNode;
24
Ben Murdochca12bfa2013-07-23 11:17:05 +010025typedef sdk_util::ScopedRef<MountNode> ScopedMountNode;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000026
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010027// NOTE: The KernelProxy is the only class that should be setting errno. All
28// other classes should return Error (as defined by nacl_io/error.h).
Ben Murdochfb250652013-07-31 11:42:55 +010029class MountNode : public EventListener {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000030 protected:
31 explicit MountNode(Mount* mount);
32 virtual ~MountNode();
33
34 protected:
35 // Initialize with node specific flags, in this case stat permissions.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010036 virtual Error Init(int flags);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000037 virtual void Destroy();
38
39 public:
Ben Murdochfb250652013-07-31 11:42:55 +010040 // Declared in EventEmitter. defaults to signalled.
41 virtual uint32_t GetEventStatus();
42
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000043 // Normal OS operations on a node (file), can be called by the kernel
44 // directly so it must lock and unlock appropriately. These functions
45 // must not be called by the mount.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010046 virtual Error FSync();
47 // It is expected that the derived MountNode will fill with 0 when growing
48 // the file.
49 virtual Error FTruncate(off_t length);
50 // Assume that |out_bytes| is non-NULL.
51 virtual Error GetDents(size_t offs,
52 struct dirent* pdir,
53 size_t count,
54 int* out_bytes);
55 // Assume that |stat| is non-NULL.
56 virtual Error GetStat(struct stat* stat);
57 // Assume that |arg| is non-NULL.
58 virtual Error Ioctl(int request, char* arg);
59 // Assume that |buf| and |out_bytes| are non-NULL.
60 virtual Error Read(size_t offs, void* buf, size_t count, int* out_bytes);
61 // Assume that |buf| and |out_bytes| are non-NULL.
62 virtual Error Write(size_t offs,
63 const void* buf,
64 size_t count,
65 int* out_bytes);
66 // Assume that |addr| and |out_addr| are non-NULL.
67 virtual Error MMap(void* addr,
68 size_t length,
69 int prot,
70 int flags,
71 size_t offset,
72 void** out_addr);
Ben Murdochbb1529c2013-08-08 10:24:53 +010073 virtual Error Tcflush(int queue_selector);
74 virtual Error Tcgetattr(struct termios* termios_p);
75 virtual Error Tcsetattr(int optional_actions,
76 const struct termios *termios_p);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000077
78 virtual int GetLinks();
79 virtual int GetMode();
80 virtual int GetType();
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010081 // Assume that |out_size| is non-NULL.
Ben Murdocheb525c52013-07-10 11:40:50 +010082 virtual Error GetSize(size_t* out_size);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083 virtual bool IsaDir();
84 virtual bool IsaFile();
85 virtual bool IsaTTY();
86
Ben Murdochbb1529c2013-08-08 10:24:53 +010087
Ben Murdocheb525c52013-07-10 11:40:50 +010088 // Number of children for this node (directory)
89 virtual int ChildCount();
90
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000091 protected:
92 // Directory operations on the node are done by the Mount. The mount's lock
93 // must be held while these calls are made.
94
95 // Adds or removes a directory entry updating the link numbers and refcount
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010096 // Assumes that |node| is non-NULL.
Ben Murdocheb525c52013-07-10 11:40:50 +010097 virtual Error AddChild(const std::string& name, const ScopedMountNode& node);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010098 virtual Error RemoveChild(const std::string& name);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000099
100 // Find a child and return it without updating the refcount
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100101 // Assumes that |out_node| is non-NULL.
Ben Murdocheb525c52013-07-10 11:40:50 +0100102 virtual Error FindChild(const std::string& name, ScopedMountNode* out_node);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000103
104 // Update the link count
105 virtual void Link();
106 virtual void Unlink();
107
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100108 protected:
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000109 struct stat stat_;
Ben Murdochca12bfa2013-07-23 11:17:05 +0100110 sdk_util::SimpleLock node_lock_;
Ben Murdocheb525c52013-07-10 11:40:50 +0100111
112 // We use a pointer directly to avoid cycles in the ref count.
113 // TODO(noelallen) We should change this so it's unnecessary for the node
114 // to track it's parent. When a node is unlinked, the mount should do
115 // any cleanup it needs.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000116 Mount* mount_;
117
118 friend class Mount;
119 friend class MountDev;
120 friend class MountHtml5Fs;
121 friend class MountHttp;
122 friend class MountMem;
123 friend class MountNodeDir;
124};
125
Ben Murdochca12bfa2013-07-23 11:17:05 +0100126} // namespace nacl_io
127
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000128#endif // LIBRARIES_NACL_IO_MOUNT_NODE_H_