headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / boot / loader / FileMapDisk.cpp
blob06b2cd67c9433f37dd70000b897bf390ab1d37c8
1 /*
2 * Copyright 2005, ?.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
7 #include <boot/FileMapDisk.h>
8 #include <boot_item.h>
10 #include <new>
12 #include <endian.h>
13 #include <stdio.h>
14 #include <string.h>
16 #include <OS.h>
17 #include <SupportDefs.h>
20 //#define TRACE_FILEMAPDISK
21 #ifdef TRACE_FILEMAPDISK
22 # define TRACE(x) dprintf x
23 #else
24 # define TRACE(x) ;
25 #endif
28 using std::nothrow;
31 // constructor
32 FileMapDisk::FileMapDisk()
34 fNode(NULL)
38 // destructor
39 FileMapDisk::~FileMapDisk()
43 // Init
44 status_t
45 FileMapDisk::Init(Node *node/*, Partition *partition, FileMap *map, off_t imageSize*/)
47 TRACE(("FileMapDisk::FileMapDisk(%p)\n", node));
48 fNode = node;
50 fPartition = partition;
51 fMap = map;
52 fImageSize = imageSize;
54 // create and bind socket
55 fSocket = new(nothrow) UDPSocket;
56 if (!fSocket)
57 return B_NO_MEMORY;
59 status_t error = fSocket->Bind(INADDR_ANY, 6666);
60 if (error != B_OK)
61 return error;
64 return B_OK;
68 status_t
69 FileMapDisk::Open(void **_cookie, int mode)
71 TRACE(("FileMapDisk::Open(, 0x%08x)\n", mode));
72 if (!fNode)
73 return B_NO_INIT;
75 return fNode->Open(_cookie, mode);
79 status_t
80 FileMapDisk::Close(void *cookie)
82 TRACE(("FileMapDisk::Close(%p)\n", cookie));
83 if (!fNode)
84 return B_NO_INIT;
86 return fNode->Close(cookie);
90 // ReadAt
91 ssize_t
92 FileMapDisk::ReadAt(void *cookie, off_t pos, void *_buffer,
93 size_t bufferSize)
95 TRACE(("FileMapDisk::ReadAt(%p, %lld, , %ld)\n", cookie, pos, bufferSize));
96 if (!fNode)
97 return B_NO_INIT;
99 return fNode->ReadAt(cookie, pos, _buffer, bufferSize);
103 // WriteAt
104 ssize_t
105 FileMapDisk::WriteAt(void */*cookie*/, off_t pos, const void *buffer,
106 size_t bufferSize)
108 // Not needed in the boot loader.
109 return B_PERMISSION_DENIED;
112 // GetName
113 status_t
114 FileMapDisk::GetName(char *nameBuffer, size_t bufferSize) const
116 const char *prefix = "FileMapDisk:";
117 if (!nameBuffer)
118 return B_BAD_VALUE;
120 snprintf(nameBuffer, bufferSize, prefix);
121 if (bufferSize > strlen(prefix) && fNode)
122 return fNode->GetName(nameBuffer + strlen(prefix),
123 bufferSize - strlen(prefix));
125 return B_OK;
129 status_t
130 FileMapDisk::GetFileMap(struct file_map_run *runs, int32 *count)
132 return fNode->GetFileMap(runs, count);
136 off_t
137 FileMapDisk::Size() const
139 if (!fNode)
140 return B_NO_INIT;
141 return fNode->Size();
145 FileMapDisk *
146 FileMapDisk::FindAnyFileMapDisk(Directory *volume)
148 TRACE(("FileMapDisk::FindAnyFileMapDisk(%p)\n", volume));
149 Node *node;
150 status_t error;
152 if (!volume)
153 return NULL;
155 //XXX: check lower/mixed case as well
156 Node *dirnode;
157 Directory *dir;
158 dirnode = volume->Lookup(FMAP_FOLDER_NAME, true);
159 if (!dirnode || !S_ISDIR(dirnode->Type()))
160 return NULL;
161 dir = (Directory *)dirnode;
162 node = dir->Lookup(FMAP_IMAGE_NAME, true);
163 if (!node)
164 return NULL;
166 // create a FileMapDisk object
167 FileMapDisk *disk = new(nothrow) FileMapDisk;
168 if (disk) {
169 error = disk->Init(node);
170 if (error != B_OK) {
171 delete disk;
172 disk = NULL;
176 return disk;
180 status_t
181 FileMapDisk::RegisterFileMapBootItem()
183 return B_ERROR;
184 #if 0
185 struct file_map_boot_item *item;
186 item = (struct file_map_boot_item *)malloc(sizeof(struct file_map_boot_item));
187 item->num_runs = FMAP_MAX_RUNS;
188 status_t err;
189 err = GetFileMap(item->runs, &item->num_runs);
190 if (err < B_OK)
191 return err;
192 // err = add_boot_item("file_map_disk", item, sizeof(struct file_map_boot_item));
193 err = B_ERROR;
194 return err;
195 #endif