1/*
2 * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef NODE_H
6#define NODE_H
7
8
9#include <sys/types.h>
10
11#include <AutoLocker.h>
12
13#include <lock.h>
14
15#include "checksumfs.h"
16
17
18class Transaction;
19class Volume;
20
21
22enum {
23	NODE_ACCESSED,
24	NODE_STAT_CHANGED,
25	NODE_MODIFIED
26};
27
28
29class Node {
30public:
31								Node(Volume* volume, uint64 blockIndex,
32									const checksumfs_node& nodeData);
33								Node(Volume* volume, mode_t mode);
34	virtual						~Node();
35
36			void				SetBlockIndex(uint64 blockIndex);
37
38	virtual	status_t			InitForVFS();
39	virtual	void				DeletingNode();
40
41	virtual	status_t			Resize(uint64 newSize, bool fillWithZeroes,
42									Transaction& transaction);
43	virtual	status_t			Read(off_t pos, void* buffer, size_t size,
44									size_t& _bytesRead);
45	virtual	status_t			Write(off_t pos, const void* buffer,
46									size_t size, size_t& _bytesWritten,
47									bool& _sizeChanged);
48	virtual	status_t			Sync();
49
50	inline	const checksumfs_node& NodeData() const	{ return fNode; }
51	inline	Volume*				GetVolume() const	{ return fVolume; }
52	inline	uint64				BlockIndex() const	{ return fBlockIndex; }
53	inline	uint32				Mode() const		{ return fNode.mode; }
54	inline	uint32				AttributeType() const;
55	inline	uint64				ParentDirectory() const;
56	inline	uint64				AttributeDirectory() const;
57	inline	uint32				HardLinks() const	{ return fNode.hardLinks; }
58	inline	uint32				UID() const			{ return fNode.uid; }
59	inline	uint32				GID() const			{ return fNode.gid; }
60	inline	uint64				Size() const		{ return fNode.size; }
61	inline	uint64				AccessedTime() const { return fAccessedTime; }
62	inline	uint64				CreationTime() const;
63	inline	uint64				ModificationTime() const;
64	inline	uint64				ChangeTime() const;
65
66			void				SetMode(uint32 mode);
67			void				SetAttributeType(uint32 type);
68			void				SetParentDirectory(uint32 blockIndex);
69			void				SetAttributeDirectory(uint32 blockIndex);
70			void				SetHardLinks(uint32 value);
71			void				SetUID(uint32 uid);
72			void				SetGID(uint32 gid);
73			void				SetSize(uint64 size);
74			void				SetAccessedTime(uint64 time);
75			void				SetCreationTime(uint64 time);
76			void				SetModificationTime(uint64 time);
77			void				SetChangeTime(uint64 time);
78
79			void				Touched(int32 mode);
80
81	inline	bool				ReadLock();
82	inline	bool				ReadLockWithTimeout(uint32 timeoutFlags,
83									bigtime_t timeout);
84	inline	void				ReadUnlock();
85	inline	bool				WriteLock();
86	inline	void				WriteUnlock();
87
88	virtual	void				RevertNodeData(const checksumfs_node& nodeData);
89
90			status_t			Flush(Transaction& transaction);
91
92private:
93			void				_Init();
94
95private:
96			rw_lock				fLock;
97			Volume*				fVolume;
98			uint64				fBlockIndex;
99			uint64				fAccessedTime;
100			checksumfs_node		fNode;
101			bool				fNodeDataDirty;
102};
103
104
105uint32
106Node::AttributeType() const
107{
108	return fNode.attributeType;
109}
110
111
112uint64
113Node::ParentDirectory() const
114{
115	return fNode.parentDirectory;
116}
117
118
119uint64
120Node::AttributeDirectory() const
121{
122	return fNode.attributeDirectory;
123}
124
125
126uint64
127Node::CreationTime() const
128{
129	return fNode.creationTime;
130}
131
132
133uint64
134Node::ModificationTime() const
135{
136	return fNode.modificationTime;
137}
138
139
140uint64
141Node::ChangeTime() const
142{
143	return fNode.changeTime;
144}
145
146
147bool
148Node::ReadLock()
149{
150	return rw_lock_read_lock(&fLock) == B_OK;
151}
152
153
154bool
155Node::ReadLockWithTimeout(uint32 timeoutFlags, bigtime_t timeout)
156{
157	return rw_lock_read_lock_with_timeout(&fLock, timeoutFlags, timeout)
158		== B_OK;
159}
160
161
162void
163Node::ReadUnlock()
164{
165	rw_lock_read_unlock(&fLock);
166}
167
168
169bool
170Node::WriteLock()
171{
172	return rw_lock_write_lock(&fLock) == B_OK;
173}
174
175
176void
177Node::WriteUnlock()
178{
179	rw_lock_write_unlock(&fLock);
180}
181
182
183typedef AutoLocker<Node, AutoLockerReadLocking<Node> > NodeReadLocker;
184typedef AutoLocker<Node, AutoLockerWriteLocking<Node> > NodeWriteLocker;
185
186
187#endif	// NODE_H
188