all repos — mgba @ 08c6943c93a54c87f02fef408d560a3d96c5b3f1

mGBA Game Boy Advance Emulator

src/platform/psp2/sce-vfs.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "sce-vfs.h"
  7
  8#include "util/vfs.h"
  9#include "util/memory.h"
 10
 11
 12struct VFileSce {
 13	struct VFile d;
 14
 15	SceUID fd;
 16};
 17
 18static bool _vfsceClose(struct VFile* vf);
 19static off_t _vfsceSeek(struct VFile* vf, off_t offset, int whence);
 20static ssize_t _vfsceRead(struct VFile* vf, void* buffer, size_t size);
 21static ssize_t _vfsceWrite(struct VFile* vf, const void* buffer, size_t size);
 22static void* _vfsceMap(struct VFile* vf, size_t size, int flags);
 23static void _vfsceUnmap(struct VFile* vf, void* memory, size_t size);
 24static void _vfsceTruncate(struct VFile* vf, size_t size);
 25static ssize_t _vfsceSize(struct VFile* vf);
 26static bool _vfsceSync(struct VFile* vf, const void* memory, size_t size);
 27
 28struct VFile* VFileOpenSce(const char* path, int flags, SceMode mode) {
 29	struct VFileSce* vfsce = malloc(sizeof(struct VFileSce));
 30	if (!vfsce) {
 31		return 0;
 32	}
 33
 34	vfsce->fd = sceIoOpen(path, flags, mode);
 35	if (vfsce->fd < 0) {
 36		free(vfsce);
 37		return 0;
 38	}
 39
 40	vfsce->d.close = _vfsceClose;
 41	vfsce->d.seek = _vfsceSeek;
 42	vfsce->d.read = _vfsceRead;
 43	vfsce->d.readline = 0;
 44	vfsce->d.write = _vfsceWrite;
 45	vfsce->d.map = _vfsceMap;
 46	vfsce->d.unmap = _vfsceUnmap;
 47	vfsce->d.truncate = _vfsceTruncate;
 48	vfsce->d.size = _vfsceSize;
 49	vfsce->d.sync = _vfsceSync;
 50
 51	return &vfsce->d;
 52}
 53
 54bool _vfsceClose(struct VFile* vf) {
 55	struct VFileSce* vfsce = (struct VFileSce*) vf;
 56
 57	return sceIoClose(vfsce->fd) >= 0;
 58}
 59
 60off_t _vfsceSeek(struct VFile* vf, off_t offset, int whence) {
 61	struct VFileSce* vfsce = (struct VFileSce*) vf;
 62	return sceIoLseek(vfsce->fd, offset, whence);
 63}
 64
 65ssize_t _vfsceRead(struct VFile* vf, void* buffer, size_t size) {
 66	struct VFileSce* vfsce = (struct VFileSce*) vf;
 67	return sceIoRead(vfsce->fd, buffer, size);
 68}
 69
 70ssize_t _vfsceWrite(struct VFile* vf, const void* buffer, size_t size) {
 71	struct VFileSce* vfsce = (struct VFileSce*) vf;
 72	return sceIoWrite(vfsce->fd, buffer, size);
 73}
 74
 75static void* _vfsceMap(struct VFile* vf, size_t size, int flags) {
 76	struct VFileSce* vfsce = (struct VFileSce*) vf;
 77	UNUSED(flags);
 78	void* buffer = anonymousMemoryMap(size);
 79	if (buffer) {
 80		sceIoRead(vfsce->fd, buffer, size);
 81	}
 82	return buffer;
 83}
 84
 85static void _vfsceUnmap(struct VFile* vf, void* memory, size_t size) {
 86	UNUSED(vf);
 87	mappedMemoryFree(memory, size);
 88}
 89
 90static void _vfsceTruncate(struct VFile* vf, size_t size) {
 91	struct VFileSce* vfsce = (struct VFileSce*) vf;
 92	// TODO
 93}
 94
 95ssize_t _vfsceSize(struct VFile* vf) {
 96	struct VFileSce* vfsce = (struct VFileSce*) vf;
 97	SceOff cur = sceIoLseek(vfsce->fd, 0, SEEK_CUR);
 98	SceOff end = sceIoLseek(vfsce->fd, 0, SEEK_END);
 99	sceIoLseek(vfsce->fd, cur, SEEK_SET);
100	return end;
101}
102
103bool _vfsceSync(struct VFile* vf, const void* buffer, size_t size) {
104	struct VFileSce* vfsce = (struct VFileSce*) vf;
105	if (buffer && size) {
106		SceOff cur = sceIoLseek(vfsce->fd, 0, SEEK_CUR);
107		sceIoLseek(vfsce->fd, 0, SEEK_SET);
108		sceIoWrite(vfsce->fd, buffer, size);
109		sceIoLseek(vfsce->fd, cur, SEEK_SET);
110	}
111	// TODO: Get the right device
112	return sceIoSync("cache0:", 0) >= 0;
113}
114
115struct VDir* VDirOpen(const char* path) {
116	// TODO
117	return 0;
118}