aboutsummaryrefslogtreecommitdiff
path: root/backends/fs/ps2/ps2-fs.cpp
blob: eba740e9487240676840f11fd671bf7ab515b443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#if defined(__PLAYSTATION2__)

// Disable symbol overrides so that we can use "FILE"
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
#define FORBIDDEN_SYMBOL_EXCEPTION_printf

#include "backends/fs/ps2/ps2-fs.h"

#include <kernel.h>
#include <stdio.h>
#include <stdlib.h>
#include "backends/platform/ps2/asyncfio.h"
#include "backends/platform/ps2/fileio.h"
#include "backends/platform/ps2/systemps2.h"
#include "backends/platform/ps2/ps2debug.h"

#include <fileXio_rpc.h>

#include "backends/platform/ps2/ps2temp.h"

#define DEFAULT_MODE (FIO_S_IRUSR | FIO_S_IWUSR | FIO_S_IRGRP | FIO_S_IWGRP | FIO_S_IROTH | FIO_S_IWOTH)

extern AsyncFio fio;
extern OSystem_PS2 *g_systemPs2;

const char *_lastPathComponent(const Common::String &str) {
	if (str.empty())
		return "";

	const char *start = str.c_str();
	const char *cur = start + str.size() - 2;

	while (cur >= start && *cur != '/' && *cur != ':') {
		--cur;
	}

	cur++;

	// dbg_printf("lastPathComponent path=%s token=%s\n", start, cur);

	return cur;
}

Ps2FilesystemNode::Ps2FilesystemNode() {
	dbg_printf("NEW FSNODE()\n");

	_isHere = true;
	_isDirectory = true;
	_isRoot = true;
	_verified = false;
	_displayName = Common::String("PlayStation 2");
	_path = "";
}

Ps2FilesystemNode::Ps2FilesystemNode(const Common::String &path) {
	dbg_printf("NEW FSNODE(%s)\n", path.c_str());

	_path = path;

	if (path.empty()) {
		_isHere = true;
		_isDirectory = true; /* root is always a dir */
		_isRoot = true;
		_displayName = Common::String("PlayStation 2");
		_verified = true;
	} else if (path.lastChar() == ':') {
		_isHere = true;
		_isDirectory = true; /* devs are always a dir */
		_isRoot = false;
		_displayName = getDeviceDescription();
		_verified = true;
	} else {
		_verified = false;
		doverify();
		if (!_isHere)
			return;

		_displayName = _lastPathComponent(_path);

		if (_isDirectory && _path.lastChar() != '/')
			_path+= '/';

		_isRoot = false;
	}
}

Ps2FilesystemNode::Ps2FilesystemNode(const Common::String &path, bool verify) {
	dbg_printf("NEW FSNODE(%s, %d)\n", path.c_str(), verify);

	_path = path;

	if (path.empty()) {
		_isHere = true;
		_isDirectory = true; /* root is always a dir */
		_isRoot = true;
		_displayName = Common::String("PlayStation 2");
		_verified = true;
	} else if (path.lastChar() == ':') {
		_isHere = true;
		_isDirectory = true; /* devs are always a dir */
		_isRoot = false;
		_displayName = getDeviceDescription();
		_verified = true;
	} else {
		_verified = false;
		if (verify) {
			doverify();

			if (!_isHere)
				return;

		} else {
			_verified = false;
			_isDirectory = false;
			_isHere = false; // true
		}

		_displayName = _lastPathComponent(_path);

		if (_isDirectory && _path.lastChar() != '/')
			_path+= '/';

		_isRoot = false;
	}
}

Ps2FilesystemNode::Ps2FilesystemNode(const Ps2FilesystemNode *node) {
	_displayName = node->_displayName;
	_isDirectory = node->_isDirectory;
	_path = node->_path;
	_isRoot = node->_isRoot;
	_isHere = node->_isHere;
	_verified = node->_verified;
}

void Ps2FilesystemNode::doverify(void) {
	PS2Device medium;
	int fd;

	if (_verified)
		return;

	_verified = true;

	dbg_printf(" verify: %s -> ", _path.c_str());

#if 0
	if (_path.empty()) {
		dbg_printf("PlayStation 2 Root !\n");
		_verified = true;
		return;
	}

	if (_path.lastChar() == ':') {
		dbg_printf("Dev: %s\n", _path.c_str());
		_verified = true;
		return;
	}
#endif

	if (_path[3] != ':' && _path[4] != ':') {
		dbg_printf("relative path !\n");
		_isHere = false;
		_isDirectory = false;
		return;
	}

	medium = _getDev(_path);
	if (medium == ERR_DEV) {
		_isHere = false;
		_isDirectory = false;
		return;
	}

	switch (medium) {
#if 0
	case HD_DEV: /*stat*/
	case USB_DEV:
		iox_stat_t stat;

		fileXioGetStat(_path.c_str(), &stat);
		fileXioWaitAsync(FXIO_WAIT, &fd);

		if (!fd) {
			dbg_printf("  yes [stat]\n");
			return true;
		}
	break;
#endif

	case CD_DEV: /*no stat*/
	case HD_DEV:
	case USB_DEV:
	case HOST_DEV:
	case MC_DEV:
#if 1
	fd = fio.open(_path.c_str(), O_RDONLY);

	dbg_printf("_path = %s -- fio.open -> %d\n", _path.c_str(), fd);

	if (fd >=0) {
		fio.close(fd);
		dbg_printf("  yes [open]\n");
		_isHere = true;
		if (medium==MC_DEV && _path.lastChar()=='/')
			_isDirectory = true;
		else
			_isDirectory = false;
		return;
	}

	fd = fio.dopen(_path.c_str());
	if (fd >=0) {
		fio.dclose(fd);
		dbg_printf("  yes [dopen]\n");
		_isHere = true;
		_isDirectory = true;
		return;
	}

#else
	fileXioOpen(_path.c_str(), O_RDONLY, DEFAULT_MODE);
	fileXioWaitAsync(FXIO_WAIT, &fd);
	if (fd>=0) {
		fileXioClose(fd);
		fileXioWaitAsync(FXIO_WAIT, &fd);
		return true;
	}

	fileXioDopen(_path.c_str());
	fileXioWaitAsync(FXIO_WAIT, &fd);
	if (fd>=0) {
		fileXioDclose(fd);
		fileXioWaitAsync(FXIO_WAIT, &fd);
		return true;
	}
#endif
	break;
	case ERR_DEV:
		_isHere = false;
		_isDirectory = false;
	break;
	}

	_isHere = false;
	_isDirectory = false;

	dbg_printf("  no\n");
	return;
}

AbstractFSNode *Ps2FilesystemNode::getChild(const Common::String &n) const {

	dbg_printf("getChild : %s\n", n.c_str());

	if (!_isDirectory)
		return NULL;

	if (_isRoot) {
		if (n.lastChar() == ':')
			return new Ps2FilesystemNode(n);
		else
			return NULL;
	}

	return new Ps2FilesystemNode(_path+n, 1);

/*
	int fd;

	if (_path == "pfs0:")
		fd = fio.dopen("pfs0:/");
	else
		fd = fio.dopen(_path.c_str());

	if (fd >= 0) {
		iox_dirent_t dirent;

		while (fio.dread(fd, &dirent) > 0) {
			if (strcmp(n.c_str(), dirent.name) == 0) {
				Ps2FilesystemNode *dirEntry = new Ps2FilesystemNode();

				dirEntry->_isHere = true;
				dirEntry->_isDirectory = (bool)(dirent.stat.mode & FIO_S_IFDIR);
				dirEntry->_isRoot = false;

				dirEntry->_path = _path;
				dirEntry->_path += dirent.name;
				if (dirEntry->_isDirectory && dirEntry->_path.lastChar() != '/')
					dirEntry->_path += '/';
				dirEntry->_displayName = dirent.name;

				dirEntry->_verified = true;

				fio.dclose(fd);
				return dirEntry;
			}
		}
		fio.dclose(fd);
	}

	return NULL;
*/
}

bool Ps2FilesystemNode::getChildren(AbstractFSList &list, ListMode mode, bool hidden) const {
	//TODO: honor the hidden flag

	// dbg_printf("getChildren\n");

	if (!_isDirectory)
		return false;

	if (_isRoot) {
		if (g_systemPs2->cdPresent())
			list.push_back(new Ps2FilesystemNode("cdfs:"));

		if (g_systemPs2->hddPresent())
			list.push_back(new Ps2FilesystemNode("pfs0:"));

		if (g_systemPs2->usbMassPresent())
			list.push_back(new Ps2FilesystemNode("mass:"));

		if (g_systemPs2->netPresent())
			list.push_back(new Ps2FilesystemNode("host:"));

		if (g_systemPs2->mcPresent())
			list.push_back(new Ps2FilesystemNode("mc0:"));

		return true;
	} else {
		int fd;

		if (_path == "pfs0:")
			fd = fio.dopen("pfs0:/");
		else
			fd = fio.dopen(_path.c_str());

		// dbg_printf("dopen = %d\n", fd);

		if (fd >= 0) {
			iox_dirent_t dirent;
			Ps2FilesystemNode dirEntry;
			int dreadRes;
			while ((dreadRes = fio.dread(fd, &dirent)) > 0) {

				if (dirent.name[0] == '.')
					continue; // ignore '.' and '..'

				if ( (mode == Common::FSNode::kListAll) ||

					((mode == Common::FSNode::kListDirectoriesOnly) &&
					 (dirent.stat.mode & FIO_S_IFDIR)) ||

				    ((mode == Common::FSNode::kListFilesOnly) &&
					 !(dirent.stat.mode & FIO_S_IFDIR)) ) {

					dirEntry._isHere = true;
					dirEntry._isDirectory = (bool)(dirent.stat.mode & FIO_S_IFDIR);
					dirEntry._isRoot = false;

					dirEntry._path = _path;
					dirEntry._path += dirent.name;
					if (dirEntry._isDirectory && dirEntry._path.lastChar() != '/')
						dirEntry._path += '/';
					dirEntry._displayName = dirent.name;

					dirEntry._verified = true;

					list.push_back(new Ps2FilesystemNode(&dirEntry));
				}
			}
			fio.dclose(fd);
			return true;
		}
	}
	return false;
}

AbstractFSNode *Ps2FilesystemNode::getParent() const {
	// dbg_printf("Ps2FilesystemNode::getParent : path = %s\n", _path.c_str());

	if (_isRoot)
		return new Ps2FilesystemNode(this); // FIXME : 0 ???

	if (_path.lastChar() == ':') // devs
		return new Ps2FilesystemNode(); // N: default is root

	const char *start = _path.c_str();
	const char *end = _lastPathComponent(_path);

	Common::String str(start, end - start);
	// dbg_printf("  parent = %s\n", str.c_str());

	return new Ps2FilesystemNode(str, true);
}

const char *Ps2FilesystemNode::getDeviceDescription() const {
	if (strncmp(_path.c_str(), "cdfs", 4) == 0)
		return "DVD Drive";
	else if (strncmp(_path.c_str(), "pfs0", 4) == 0)
		return "Harddisk";
	else if (strncmp(_path.c_str(), "mass", 4) == 0)
		return "USB Mass Storage";
	else if (strncmp(_path.c_str(), "host", 4) == 0)
		return "Host";
	else if (strncmp(_path.c_str(), "mc0", 3) == 0)
		return "Memory Card";
	else
		return "WTF ???";
}

Common::SeekableReadStream *Ps2FilesystemNode::createReadStream() {
	Common::SeekableReadStream *ss = PS2FileStream::makeFromPath(getPath(), false);
	return ss;
}

Common::WriteStream *Ps2FilesystemNode::createWriteStream() {
	return PS2FileStream::makeFromPath(getPath(), true);
}

bool Ps2FilesystemNode::create(bool isDirectoryFlag) {
	error("Not supported");
	return false;
}

#endif