1 /* 2 ** Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 ** Distributed under the terms of the OpenBeOS License. 4 */ 5 6 7 #include "RootFileSystem.h" 8 9 #include <OS.h> 10 #include <util/kernel_cpp.h> 11 12 #include <string.h> 13 #include <fcntl.h> 14 15 16 RootFileSystem::RootFileSystem() 17 { 18 } 19 20 21 RootFileSystem::~RootFileSystem() 22 { 23 struct entry *entry = NULL; 24 25 while ((entry = fList.RemoveHead()) != NULL) { 26 entry->root->Release(); 27 delete entry; 28 } 29 } 30 31 32 status_t 33 RootFileSystem::Open(void **_cookie, int mode) 34 { 35 EntryIterator *iterator = new EntryIterator(fList.Iterator()); 36 if (iterator == NULL) 37 return B_NO_MEMORY; 38 39 *_cookie = iterator; 40 41 return B_OK; 42 } 43 44 45 status_t 46 RootFileSystem::Close(void *cookie) 47 { 48 delete (EntryIterator *)cookie; 49 return B_OK; 50 } 51 52 53 Node * 54 RootFileSystem::Lookup(const char *name, bool /*traverseLinks*/) 55 { 56 EntryIterator iterator = fLinks.Iterator(); 57 struct entry *entry; 58 59 // first check the links 60 61 while ((entry = iterator.Next()) != NULL) { 62 if (!strcmp(name, entry->name)) { 63 entry->root->Acquire(); 64 return entry->root; 65 } 66 } 67 68 // then all mounted file systems 69 70 iterator = fList.Iterator(); 71 72 while ((entry = iterator.Next()) != NULL) { 73 char entryName[B_OS_NAME_LENGTH]; 74 if (entry->root->GetName(entryName, sizeof(entryName)) != B_OK) 75 continue; 76 77 if (!strcmp(entryName, name)) { 78 entry->root->Acquire(); 79 return entry->root; 80 } 81 } 82 83 return NULL; 84 } 85 86 87 status_t 88 RootFileSystem::GetNextEntry(void *_cookie, char *name, size_t size) 89 { 90 EntryIterator *iterator = (EntryIterator *)_cookie; 91 struct entry *entry; 92 93 entry = iterator->Next(); 94 if (entry != NULL) 95 return entry->root->GetName(name, size); 96 97 return B_ENTRY_NOT_FOUND; 98 } 99 100 101 status_t 102 RootFileSystem::GetNextNode(void *_cookie, Node **_node) 103 { 104 EntryIterator *iterator = (EntryIterator *)_cookie; 105 struct entry *entry; 106 107 entry = iterator->Next(); 108 if (entry != NULL) { 109 *_node = entry->root; 110 return B_OK; 111 } 112 return B_ENTRY_NOT_FOUND; 113 } 114 115 116 status_t 117 RootFileSystem::Rewind(void *_cookie) 118 { 119 // ToDo: implement 120 return B_OK; 121 } 122 123 124 bool 125 RootFileSystem::IsEmpty() 126 { 127 return fList.IsEmpty(); 128 } 129 130 131 status_t 132 RootFileSystem::AddVolume(Directory *volume, Partition *partition) 133 { 134 struct entry *entry = new RootFileSystem::entry(); 135 if (entry == NULL) 136 return B_NO_MEMORY; 137 138 volume->Acquire(); 139 entry->name = NULL; 140 entry->root = volume; 141 entry->partition = partition; 142 143 fList.Add(entry); 144 145 return B_OK; 146 } 147 148 149 status_t 150 RootFileSystem::AddLink(const char *name, Directory *target) 151 { 152 struct entry *entry = new RootFileSystem::entry(); 153 if (entry == NULL) 154 return B_NO_MEMORY; 155 156 target->Acquire(); 157 entry->name = name; 158 entry->root = target; 159 160 fLinks.Add(entry); 161 162 return B_OK; 163 } 164 165 166 status_t 167 RootFileSystem::GetPartitionFor(Directory *volume, Partition **_partition) 168 { 169 EntryIterator iterator = fList.Iterator(); 170 struct entry *entry; 171 172 while ((entry = iterator.Next()) != NULL) { 173 if (entry->root == volume) { 174 *_partition = entry->partition; 175 return B_OK; 176 } 177 } 178 179 return B_ENTRY_NOT_FOUND; 180 } 181 182