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);
26
27struct VFile* VFileOpenSce(const char* path, int flags, SceMode mode) {
28 struct VFileSce* vfsce = malloc(sizeof(struct VFileSce));
29 if (!vfsce) {
30 return 0;
31 }
32
33 vfsce->fd = sceIoOpen(path, flags, mode);
34 if (vfsce->fd < 0) {
35 free(vfsce);
36 return 0;
37 }
38
39 vfsce->d.close = _vfsceClose;
40 vfsce->d.seek = _vfsceSeek;
41 vfsce->d.read = _vfsceRead;
42 vfsce->d.readline = 0;
43 vfsce->d.write = _vfsceWrite;
44 vfsce->d.map = _vfsceMap;
45 vfsce->d.unmap = _vfsceUnmap;
46 vfsce->d.truncate = _vfsceTruncate;
47 vfsce->d.size = _vfsceSize;
48
49 return &vfsce->d;
50}
51
52bool _vfsceClose(struct VFile* vf) {
53 struct VFileSce* vfsce = (struct VFileSce*) vf;
54
55 return sceIoClose(vfsce->fd) >= 0;
56}
57
58off_t _vfsceSeek(struct VFile* vf, off_t offset, int whence) {
59 struct VFileSce* vfsce = (struct VFileSce*) vf;
60 return sceIoLseek(vfsce->fd, offset, whence);
61}
62
63ssize_t _vfsceRead(struct VFile* vf, void* buffer, size_t size) {
64 struct VFileSce* vfsce = (struct VFileSce*) vf;
65 return sceIoRead(vfsce->fd, buffer, size);
66}
67
68ssize_t _vfsceWrite(struct VFile* vf, const void* buffer, size_t size) {
69 struct VFileSce* vfsce = (struct VFileSce*) vf;
70 return sceIoWrite(vfsce->fd, buffer, size);
71}
72
73static void* _vfsceMap(struct VFile* vf, size_t size, int flags) {
74 struct VFileSce* vfsce = (struct VFileSce*) vf;
75 UNUSED(flags);
76 void* buffer = anonymousMemoryMap(size);
77 if (buffer) {
78 sceIoRead(vfsce->fd, buffer, size);
79 }
80 return buffer;
81}
82
83static void _vfsceUnmap(struct VFile* vf, void* memory, size_t size) {
84 UNUSED(vf);
85 mappedMemoryFree(memory, size);
86}
87
88static void _vfsceTruncate(struct VFile* vf, size_t size) {
89 struct VFileSce* vfsce = (struct VFileSce*) vf;
90 // TODO
91}
92
93ssize_t _vfsceSize(struct VFile* vf) {
94 struct VFileSce* vfsce = (struct VFileSce*) vf;
95 SceOff cur = sceIoLseek(vfsce->fd, 0, SEEK_CUR);
96 SceOff end = sceIoLseek(vfsce->fd, 0, SEEK_END);
97 sceIoLseek(vfsce->fd, cur, SEEK_SET);
98 return end;
99}