xref: /haiku/src/add-ons/kernel/file_systems/packagefs/nodes/Directory.h (revision 0059775c1d6454bba41a9f0defebc4b6151838fa)
1 /*
2  * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef DIRECTORY_H
6 #define DIRECTORY_H
7 
8 
9 #include "Node.h"
10 
11 #include <lock.h>
12 #include <AutoLocker.h>
13 
14 
15 struct DirectoryIterator : DoublyLinkedListLinkImpl<DirectoryIterator> {
16 	Node*	node;
17 
DirectoryIteratorDirectoryIterator18 	DirectoryIterator()
19 		:
20 		node(NULL)
21 	{
22 	}
23 };
24 
25 typedef DoublyLinkedList<DirectoryIterator> DirectoryIteratorList;
26 
27 
28 class Directory : public Node {
29 public:
30 								Directory(ino_t id);
31 	virtual						~Directory();
32 
33 	inline	bool				ReadLock();
34 	inline	void				ReadUnlock();
35 	inline	bool				WriteLock();
36 	inline	void				WriteUnlock();
37 
38 	virtual	status_t			Init(const String& name);
39 
40 	virtual	mode_t				Mode() const;
41 	virtual	off_t				FileSize() const;
42 
43 	virtual	status_t			Read(off_t offset, void* buffer,
44 									size_t* bufferSize);
45 	virtual	status_t			Read(io_request* request);
46 
47 	virtual	status_t			ReadSymlink(void* buffer, size_t* bufferSize);
48 
49 			void				AddChild(Node* node);
50 			void				RemoveChild(Node* node);
51 			Node*				FindChild(const StringKey& name);
52 
53 	inline	Node*				FirstChild() const;
54 	inline	Node*				NextChild(Node* node) const;
55 
56 			void				AddDirectoryIterator(
57 									DirectoryIterator* iterator);
58 			void				RemoveDirectoryIterator(
59 									DirectoryIterator* iterator);
60 
61 protected:
62 			rw_lock				fLock;
63 
64 private:
65 			NodeNameHashTable	fChildTable;
66 			NodeList			fChildList;
67 			DirectoryIteratorList fIterators;
68 };
69 
70 
71 bool
ReadLock()72 Directory::ReadLock()
73 {
74 	return rw_lock_read_lock(&fLock) == B_OK;
75 }
76 
77 
78 void
ReadUnlock()79 Directory::ReadUnlock()
80 {
81 	rw_lock_read_unlock(&fLock);
82 }
83 
84 
85 bool
WriteLock()86 Directory::WriteLock()
87 {
88 	return rw_lock_write_lock(&fLock) == B_OK;
89 }
90 
91 
92 void
WriteUnlock()93 Directory::WriteUnlock()
94 {
95 	rw_lock_write_unlock(&fLock);
96 }
97 
98 
99 Node*
FirstChild()100 Directory::FirstChild() const
101 {
102 	return fChildList.First();
103 }
104 
105 
106 Node*
NextChild(Node * node)107 Directory::NextChild(Node* node) const
108 {
109 	return fChildList.GetNext(node);
110 }
111 
112 
113 typedef AutoLocker<Directory, AutoLockerReadLocking<Directory> > DirectoryReadLocker;
114 typedef AutoLocker<Directory, AutoLockerWriteLocking<Directory> > DirectoryWriteLocker;
115 
116 
117 #endif	// DIRECTORY_H
118