headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / boot / loader / RootFileSystem.cpp
blob1153397683add019ae00d8dfb3f32ab4f7e125b3
1 /*
2 * Copyright 2003-2013, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "RootFileSystem.h"
9 #include <OS.h>
11 #include <string.h>
12 #include <fcntl.h>
15 RootFileSystem::RootFileSystem()
20 RootFileSystem::~RootFileSystem()
22 struct entry *entry = NULL;
24 while ((entry = fList.RemoveHead()) != NULL) {
25 entry->root->Release();
26 delete entry;
31 status_t
32 RootFileSystem::Open(void **_cookie, int mode)
34 EntryIterator *iterator = new (std::nothrow) EntryIterator(&fList);
35 if (iterator == NULL)
36 return B_NO_MEMORY;
38 *_cookie = iterator;
40 return B_OK;
44 status_t
45 RootFileSystem::Close(void *cookie)
47 delete (EntryIterator *)cookie;
48 return B_OK;
52 Node*
53 RootFileSystem::LookupDontTraverse(const char* name)
55 EntryIterator iterator = fLinks.GetIterator();
56 struct entry *entry;
58 // first check the links
60 while ((entry = iterator.Next()) != NULL) {
61 if (!strcmp(name, entry->name)) {
62 entry->root->Acquire();
63 return entry->root;
67 // then all mounted file systems
69 iterator = fList.GetIterator();
71 while ((entry = iterator.Next()) != NULL) {
72 char entryName[B_OS_NAME_LENGTH];
73 if (entry->root->GetName(entryName, sizeof(entryName)) != B_OK)
74 continue;
76 if (!strcmp(entryName, name)) {
77 entry->root->Acquire();
78 return entry->root;
82 return NULL;
86 status_t
87 RootFileSystem::GetNextEntry(void *_cookie, char *name, size_t size)
89 EntryIterator *iterator = (EntryIterator *)_cookie;
90 struct entry *entry;
92 entry = iterator->Next();
93 if (entry != NULL)
94 return entry->root->GetName(name, size);
96 return B_ENTRY_NOT_FOUND;
100 status_t
101 RootFileSystem::GetNextNode(void *_cookie, Node **_node)
103 EntryIterator *iterator = (EntryIterator *)_cookie;
104 struct entry *entry;
106 entry = iterator->Next();
107 if (entry != NULL) {
108 *_node = entry->root;
109 return B_OK;
111 return B_ENTRY_NOT_FOUND;
115 status_t
116 RootFileSystem::Rewind(void *_cookie)
118 EntryIterator *iterator = (EntryIterator *)_cookie;
120 iterator->Rewind();
121 return B_OK;
125 bool
126 RootFileSystem::IsEmpty()
128 return fList.IsEmpty();
132 status_t
133 RootFileSystem::AddVolume(Directory *volume, Partition *partition)
135 struct entry *entry = new (std::nothrow) RootFileSystem::entry();
136 if (entry == NULL)
137 return B_NO_MEMORY;
139 volume->Acquire();
140 entry->name = NULL;
141 entry->root = volume;
142 entry->partition = partition;
144 fList.Add(entry);
146 return B_OK;
150 status_t
151 RootFileSystem::AddLink(const char *name, Directory *target)
153 struct entry *entry = new (std::nothrow) RootFileSystem::entry();
154 if (entry == NULL)
155 return B_NO_MEMORY;
157 target->Acquire();
158 entry->name = name;
159 entry->root = target;
161 fLinks.Add(entry);
163 return B_OK;
167 status_t
168 RootFileSystem::GetPartitionFor(Directory *volume, Partition **_partition)
170 EntryIterator iterator = fList.GetIterator();
171 struct entry *entry;
173 while ((entry = iterator.Next()) != NULL) {
174 if (entry->root == volume) {
175 *_partition = entry->partition;
176 return B_OK;
180 return B_ENTRY_NOT_FOUND;