1 /* Copyright (C) 2010-2020 The RetroArch team
3 * ---------------------------------------------------------------------------------------
4 * The following license statement only applies to this file (nbio_orbis.c).
5 * ---------------------------------------------------------------------------------------
7 * Permission is hereby granted, free of charge,
8 * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <file/nbio.h>
28 #include <orbisFile.h>
30 #include <sys/fcntl.h>
41 * NBIO_READ, NBIO_WRITE - obvious
42 * -1 - currently doing nothing
43 * -2 - the pointer was reallocated since the last operation
48 static void *nbio_orbis_open(const char * filename, unsigned int mode)
50 static const int o_flags[] = { O_RDONLY, O_RDWR | O_CREAT | O_TRUNC,
51 O_RDWR, O_RDONLY, O_RDWR | O_CREAT | O_TRUNC };
53 struct nbio_orbis_t* handle = NULL;
55 int fd = orbisOpen(filename, o_flags[mode], 0644);
59 handle = (struct nbio_orbis_t*)malloc(sizeof(struct nbio_orbis_t));
72 len=orbisLseek(handle->fd, 0, SEEK_END);
73 orbisLseek(handle->fd, 0, SEEK_SET);
87 handle->progress = handle->len;
99 static void nbio_orbis_begin_read(void *data)
102 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
109 orbisLseek(handle->fd, 0, SEEK_SET);
111 handle->op = NBIO_READ;
112 handle->progress = 0;
115 static void nbio_orbis_begin_write(void *data)
117 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
124 orbisLseek(handle->fd, 0, SEEK_SET);
125 handle->op = NBIO_WRITE;
126 handle->progress = 0;
129 static bool nbio_orbis_iterate(void *data)
131 size_t amount = 65536;
132 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
137 if (amount > handle->len - handle->progress)
138 amount = handle->len - handle->progress;
143 if (handle->mode == BIO_READ)
144 amount = handle->len;
147 if (handle->mode == BIO_WRITE)
150 amount = handle->len;
151 written = orbisWrite(handle->fd, (char*)handle->data, amount);
153 if (written != amount)
159 handle->progress += amount;
161 if (handle->progress == handle->len)
163 return (handle->op < 0);
166 static void nbio_orbis_resize(void *data, size_t len)
168 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
174 if (len < handle->len)
178 handle->data = realloc(handle->data, handle->len);
180 handle->progress = handle->len;
183 static void *nbio_orbis_get_ptr(void *data, size_t* len)
185 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
190 if (handle->op == -1)
195 static void nbio_orbis_cancel(void *data)
197 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
201 handle->progress = handle->len;
204 static void nbio_orbis_free(void *data)
206 struct nbio_orbis_t *handle = (struct nbio_orbis_t*)data;
213 orbisClose(handle->fd);
220 nbio_intf_t nbio_orbis = {
222 nbio_orbis_begin_read,
223 nbio_orbis_begin_write,