Ticket #8415: amigaos4-fs.cpp

File amigaos4-fs.cpp, 8.3 KB (added by SF/capehill, 15 years ago)

AmigaOS 4 filesystem backend

Line 
1/* ScummVM - Scumm Interpreter
2 * Copyright (C) 2005 The ScummVM project, contribution by Hans-Jörg Frieden and Juha Niemimäki
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * $Header$
19 */
20
21#if defined(__amigaos4__)
22#ifdef __USE_INLINE__
23#undef __USE_INLINE__
24#endif
25
26#include <proto/exec.h>
27#include <proto/dos.h>
28#include <stdio.h>
29
30#ifndef USE_NEWLIB
31#include <strings.h>
32#endif
33
34#include <stdafx.h>
35
36#include "util.h"
37
38#include "base/engine.h"
39#include "../fs.h"
40
41#define ENTER() /* debug(6, "Enter\n") */
42#define LEAVE() /* debug(6, "Leave\n") */
43
44
45const uint32 ExAllBufferSize = 40960;
46
47class AmigaOSFilesystemNode : public AbstractFilesystemNode {
48 protected:
49 BPTR _pFileLock;
50 String _sDisplayName;
51 bool _bIsDirectory;
52 bool _bIsValid;
53 String _sPath;
54
55 public:
56 AmigaOSFilesystemNode();
57 AmigaOSFilesystemNode(const AmigaOSFilesystemNode *pNode);
58 AmigaOSFilesystemNode(BPTR pLock, const char *pDisplayName = 0);
59 AmigaOSFilesystemNode(const String &p);
60
61 ~AmigaOSFilesystemNode();
62
63 virtual String displayName() const { return _sDisplayName; };
64 virtual bool isValid() const { return _bIsValid; };
65 virtual bool isDirectory() const { return _bIsDirectory; };
66 virtual String path() const { return _sPath; };
67
68 virtual FSList listDir(ListMode mode = kListDirectoriesOnly) const;
69 virtual FSList listVolumes(void) const;
70 virtual AbstractFilesystemNode *parent() const;
71 virtual AbstractFilesystemNode *clone() const { return new AmigaOSFilesystemNode(this); };
72};
73
74AbstractFilesystemNode *FilesystemNode::getRoot() {
75 return new AmigaOSFilesystemNode();
76}
77
78AbstractFilesystemNode *FilesystemNode::getNodeForPath(const String &path) {
79 return new AmigaOSFilesystemNode(path);
80}
81
82AmigaOSFilesystemNode::AmigaOSFilesystemNode() {
83 ENTER();
84 _sDisplayName = "Available Disks";
85 _bIsValid = true;
86 _bIsDirectory = true;
87 _sPath = "";
88 _pFileLock = 0;
89 LEAVE();
90}
91
92
93AmigaOSFilesystemNode::AmigaOSFilesystemNode(const String &p) {
94 ENTER();
95
96 int len = 0, offset = p.size();
97
98 assert(offset > 0);
99
100 _sPath = p;
101
102 // Extract last component from path
103 const char *str = p.c_str();
104
105 while (offset > 0 && (str[offset-1] == '/' || str[offset-1] == ':'))
106 offset--;
107
108 while (offset > 0 && (str[offset-1] != '/' && str[offset-1] != ':')) {
109 len++;
110 offset--;
111 }
112
113 _sDisplayName = String(str + offset, len);
114
115 // Check whether it is a directory, and whether the file actually exists
116
117 struct FileInfoBlock *fib = (struct FileInfoBlock *)IDOS->AllocDosObject(DOS_FIB, NULL);
118 if (!fib) {
119 debug(6, "fib == 0\n");
120 LEAVE();
121 return;
122 }
123
124 BPTR pLock = IDOS->Lock( (char *)_sPath.c_str(), SHARED_LOCK);
125 if (pLock) {
126 if (IDOS->Examine(pLock, fib) != DOSFALSE) {
127 if (fib->fib_EntryType > 0)
128 _bIsDirectory = true;
129 else
130 _bIsDirectory = false;
131
132 if (_bIsDirectory) {
133 if (fib->fib_EntryType != ST_ROOT)
134 _sPath += "/";
135
136 _pFileLock = IDOS->DupLock(pLock);
137 _bIsValid = (_pFileLock != 0);
138 }
139 else _bIsValid = true;
140 }
141 }
142
143 IDOS->FreeDosObject(DOS_FIB, fib);
144 LEAVE();
145}
146
147AmigaOSFilesystemNode::AmigaOSFilesystemNode(BPTR pLock, const char *pDisplayName) {
148 ENTER();
149 int bufsize = 256;
150 _pFileLock = 0;
151
152 while (1) {
153 char *name = new char[bufsize];
154 if (IDOS->NameFromLock(pLock, name, bufsize) != DOSFALSE) {
155 _sPath = name;
156 _sDisplayName = pDisplayName ? pDisplayName : IDOS->FilePart(name);
157 delete name;
158 break;
159 }
160
161 if (IDOS->IoErr() != ERROR_LINE_TOO_LONG) {
162 _bIsValid = false;
163 debug(6, "Error\n");
164 LEAVE();
165 delete name;
166 return;
167 }
168 bufsize *= 2;
169 delete name;
170 }
171
172 _bIsValid = false;
173
174 struct FileInfoBlock *fib = (struct FileInfoBlock *)IDOS->AllocDosObject(DOS_FIB, NULL);
175 if (!fib) {
176 debug(6, "fib == 0\n");
177 LEAVE();
178 return;
179 }
180
181 if (IDOS->Examine(pLock, fib) != DOSFALSE) {
182 if (fib->fib_EntryType > 0)
183 _bIsDirectory = true;
184 else
185 _bIsDirectory = false;
186
187 if (_bIsDirectory) {
188 if (fib->fib_EntryType != ST_ROOT)
189 _sPath += "/";
190
191 _pFileLock = IDOS->DupLock(pLock);
192 _bIsValid = (_pFileLock != 0);
193 }
194 else _bIsValid = true;
195 }
196
197 IDOS->FreeDosObject(DOS_FIB, fib);
198 LEAVE();
199}
200
201AmigaOSFilesystemNode::AmigaOSFilesystemNode(const AmigaOSFilesystemNode *node) {
202 ENTER();
203 _sDisplayName = node->_sDisplayName;
204 _bIsValid = node->_bIsValid;
205 _bIsDirectory = node->_bIsDirectory;
206 _sPath = node->_sPath;
207 _pFileLock = IDOS->DupLock(node->_pFileLock);
208 LEAVE();
209}
210
211AmigaOSFilesystemNode::~AmigaOSFilesystemNode() {
212 ENTER();
213 if (_pFileLock)
214 IDOS->UnLock(_pFileLock);
215 LEAVE();
216}
217
218FSList AmigaOSFilesystemNode::listDir(ListMode mode) const {
219 ENTER();
220
221 if (!_bIsValid) {
222 debug(6, "Invalid node\n");
223 LEAVE();
224 //return 0;
225 }
226
227 if (!_bIsDirectory) {
228 debug(6, "Not a directory\n");
229 LEAVE();
230 //return 0;
231 }
232
233 if (_pFileLock == 0) {
234 debug(6, "Root node\n");
235 LEAVE();
236 return listVolumes();
237 }
238
239 //FSList *myList = new FSList();
240 FSList myList;
241
242 struct ExAllControl *eac;
243 struct ExAllData *data, *ead;
244 BOOL bExMore;
245
246 eac = (struct ExAllControl *)IDOS->AllocDosObject(DOS_EXALLCONTROL, 0);
247 if (eac) {
248 data = (struct ExAllData *)IExec->AllocVec(ExAllBufferSize, MEMF_ANY);
249 if (data) {
250 eac->eac_LastKey = 0;
251 do {
252 bExMore = IDOS->ExAll(_pFileLock, data, ExAllBufferSize,
253 ED_TYPE, eac);
254
255 LONG error = IDOS->IoErr();
256 if (!bExMore && error != ERROR_NO_MORE_ENTRIES)
257 break;
258
259 if (eac->eac_Entries == 0)
260 continue;
261
262 ead = data;
263 do {
264 AmigaOSFilesystemNode *entry;
265 String full_path;
266 BPTR lock;
267
268 if ((ead->ed_Type > 0 && (mode & kListDirectoriesOnly)) ||
269 (ead->ed_Type < 0 && (mode & kListFilesOnly))) {
270 full_path = _sPath;
271 full_path += (char*)ead->ed_Name;
272 lock = IDOS->Lock((char *)full_path.c_str(), SHARED_LOCK);
273 if (lock) {
274 entry = new AmigaOSFilesystemNode(lock, (char *)ead->ed_Name);
275 if (entry) {
276 if (entry->isValid())
277 myList.push_back(wrap(entry));
278 else
279 delete entry;
280 }
281 IDOS->UnLock(lock);
282 }
283 }
284 ead = ead->ed_Next;
285 } while (ead);
286 } while (bExMore);
287
288 IExec->FreeVec(data);
289 }
290
291 IDOS->FreeDosObject(DOS_EXALLCONTROL, eac);
292 }
293 LEAVE();
294 return myList;
295}
296
297AbstractFilesystemNode *AmigaOSFilesystemNode::parent() const {
298 ENTER();
299 AmigaOSFilesystemNode *node;
300
301 if (!_bIsDirectory) {
302 debug(6, "No directory\n");
303 LEAVE();
304 return 0;
305 }
306
307 if (_pFileLock == 0) {
308 debug(6, "Root node\n");
309 LEAVE();
310 return clone();
311 }
312
313 BPTR parent = IDOS->ParentDir(_pFileLock);
314 if (parent) {
315 node = new AmigaOSFilesystemNode(parent);
316 IDOS->UnLock(parent);
317 }
318 else
319 node = new AmigaOSFilesystemNode();
320
321 LEAVE();
322 return node;
323}
324
325FSList AmigaOSFilesystemNode::listVolumes(void) const {
326 ENTER();
327 //FSList *myList = new FSList();
328 FSList myList;
329
330 struct DosList *dosList;
331
332 const uint32 lockFlags = LDF_READ | LDF_VOLUMES;
333 char name[256];
334
335 dosList = IDOS->LockDosList(lockFlags);
336 if (!dosList) {
337 debug(6, "Cannot lock dos list\n");
338 LEAVE();
339 return myList;
340 }
341
342
343 dosList = IDOS->NextDosEntry(dosList, LDF_VOLUMES);
344 while (dosList) {
345 if (dosList->dol_Type == DLT_VOLUME &&
346 dosList->dol_Name &&
347 dosList->dol_Task) {
348 AmigaOSFilesystemNode *entry;
349 const char *volname = (const char *)BADDR(dosList->dol_Name)+1;
350 const char *devname = (const char *)((struct Task *)dosList->dol_Task->mp_SigTask)->tc_Node.ln_Name;
351
352 strcpy(name, volname);
353 strcat(name, ":");
354
355 BPTR volume_lock = IDOS->Lock(name, SHARED_LOCK);
356 if (volume_lock) {
357 sprintf(name, "%s (%s)", volname, devname);
358 entry = new AmigaOSFilesystemNode(volume_lock, name);
359 if (entry) {
360 if (entry->isValid())
361 myList.push_back(wrap(entry));
362 else
363 delete entry;
364 }
365 IDOS->UnLock(volume_lock);
366 }
367 }
368 dosList = IDOS->NextDosEntry(dosList, LDF_VOLUMES);
369 }
370
371 IDOS->UnLockDosList(lockFlags);
372
373 LEAVE();
374 return myList;
375}
376
377#endif