blob: 0d680e0e8a7d55b4737dae34768ea1c69e139885 [file] [log] [blame]
Björn Stenberg32125d42002-05-13 15:23:30 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 Daniel Stenberg
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Björn Stenberg32125d42002-05-13 15:23:30 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Daniel Stenberg7b3abdc2002-04-30 13:14:59 +000021
Daniel Stenberged6c7e42002-06-14 11:00:13 +000022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Daniel Stenberg22b77012005-02-22 12:19:12 +000025#include <stdarg.h>
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +000026#include <sys/stat.h>
Peter D'Hoyef0ed5942007-04-06 23:54:21 +000027#include <time.h>
Thomas Martitz240923a2010-08-02 20:34:47 +000028#include "config.h"
29
Thomas Martitze22aa752010-09-08 20:39:55 +000030#define HAVE_STATVFS (!defined(WIN32))
Thomas Martitz87c8be42010-12-23 19:02:18 +000031#define HAVE_LSTAT (!defined(WIN32))
Thomas Martitz240923a2010-08-02 20:34:47 +000032
33#if HAVE_STATVFS
Jens Arnold181e0e02010-03-20 12:45:54 +000034#include <sys/statvfs.h>
Björn Stenbergf6ed9702003-07-04 08:30:01 +000035#endif
Hardeep Sidhufb26bfb2004-06-14 07:00:50 +000036
Daniel Stenberg22b77012005-02-22 12:19:12 +000037#ifdef WIN32
38#include <windows.h>
39#endif
40
Hardeep Sidhufb26bfb2004-06-14 07:00:50 +000041#ifndef _MSC_VER
Daniel Stenberge3a12d32002-05-07 12:25:30 +000042#include <dirent.h>
Björn Stenberg2ba4fed2003-01-28 23:14:41 +000043#include <unistd.h>
Hardeep Sidhufb26bfb2004-06-14 07:00:50 +000044#else
45#include "dir-win32.h"
46#endif
Daniel Stenberg7b3abdc2002-04-30 13:14:59 +000047
Daniel Stenberged6c7e42002-06-14 11:00:13 +000048#include <fcntl.h>
Thomas Martitz6d85de32011-02-18 22:46:01 +000049#ifdef HAVE_SDL_THREADS
Thomas Martitz240923a2010-08-02 20:34:47 +000050#include "thread-sdl.h"
Thomas Martitz6d85de32011-02-18 22:46:01 +000051#else
52#define sim_thread_unlock() NULL
53#define sim_thread_lock(a)
54#endif
Michael Sevakisf64ebb12007-09-08 12:20:53 +000055#include "thread.h"
56#include "kernel.h"
Daniel Stenberged6c7e42002-06-14 11:00:13 +000057#include "debug.h"
Jens Arnold51280802007-09-13 20:53:32 +000058#include "ata.h" /* for IF_MV2 et al. */
Thomas Martitz9c0b2472010-08-01 16:15:27 +000059#include "rbpaths.h"
Thomas Martitz194174a2010-08-27 00:29:50 +000060#include "load_code.h"
Michael Sevakisa4d19b72007-12-03 14:01:12 +000061
Thomas Martitz9c0b2472010-08-01 16:15:27 +000062/* keep this in sync with file.h! */
63#undef MAX_PATH /* this avoids problems when building simulator */
64#define MAX_PATH 260
65#define MAX_OPEN_FILES 11
Miika Pekkarinend70bd0e2006-08-26 14:19:18 +000066
Jens Arnold67eb1542007-02-01 23:08:15 +000067/* Windows (and potentially other OSes) distinguish binary and text files.
68 * Define a dummy for the others. */
69#ifndef O_BINARY
70#define O_BINARY 0
71#endif
72
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000073/* Unicode compatibility for win32 */
74#if defined __MINGW32__
75/* Rockbox unicode functions */
76extern const unsigned char* utf8decode(const unsigned char *utf8,
77 unsigned short *ucs);
78extern unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8);
79
Jens Arnold49be3fa2007-04-20 17:35:05 +000080/* Static buffers for the conversion results. This isn't thread safe,
81 * but it's sufficient for rockbox. */
82static unsigned char convbuf1[3*MAX_PATH];
83static unsigned char convbuf2[3*MAX_PATH];
84
85static wchar_t* utf8_to_ucs2(const unsigned char *utf8, void *buffer)
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000086{
Jens Arnold49be3fa2007-04-20 17:35:05 +000087 wchar_t *ucs = buffer;
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000088
89 while (*utf8)
90 utf8 = utf8decode(utf8, ucs++);
Steve Bavinf87eb602007-11-08 12:11:34 +000091
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000092 *ucs = 0;
Jens Arnold49be3fa2007-04-20 17:35:05 +000093 return buffer;
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000094}
Jens Arnold49be3fa2007-04-20 17:35:05 +000095static unsigned char *ucs2_to_utf8(const wchar_t *ucs, unsigned char *buffer)
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000096{
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000097 unsigned char *utf8 = buffer;
Jens Arnold49be3fa2007-04-20 17:35:05 +000098
Jens Arnold2fd7c3c2007-04-19 20:17:24 +000099 while (*ucs)
100 utf8 = utf8encode(*ucs++, utf8);
Steve Bavinf87eb602007-11-08 12:11:34 +0000101
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000102 *utf8 = 0;
103 return buffer;
104}
105
Jens Arnold49be3fa2007-04-20 17:35:05 +0000106#define UTF8_TO_OS(a) utf8_to_ucs2(a,convbuf1)
107#define OS_TO_UTF8(a) ucs2_to_utf8(a,convbuf1)
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000108#define DIR_T _WDIR
109#define DIRENT_T struct _wdirent
110#define STAT_T struct _stat
111extern int _wmkdir(const wchar_t*);
112extern int _wrmdir(const wchar_t*);
113#define MKDIR(a,b) (_wmkdir)(UTF8_TO_OS(a))
114#define RMDIR(a) (_wrmdir)(UTF8_TO_OS(a))
115#define OPENDIR(a) (_wopendir)(UTF8_TO_OS(a))
116#define READDIR(a) (_wreaddir)(a)
117#define CLOSEDIR(a) (_wclosedir)(a)
118#define STAT(a,b) (_wstat)(UTF8_TO_OS(a),b)
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000119/* empty variable parameter list doesn't work for variadic macros,
120 * so pretend the second parameter is variable too */
121#define OPEN(a,...) (_wopen)(UTF8_TO_OS(a), __VA_ARGS__)
Jonas Häggqvistb24631c2007-10-09 17:51:02 +0000122#define CLOSE(a) (close)(a)
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000123#define REMOVE(a) (_wremove)(UTF8_TO_OS(a))
Jens Arnold49be3fa2007-04-20 17:35:05 +0000124#define RENAME(a,b) (_wrename)(UTF8_TO_OS(a),utf8_to_ucs2(b,convbuf2))
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000125
126#else /* !__MINGW32__ */
127
128#define UTF8_TO_OS(a) (a)
129#define OS_TO_UTF8(a) (a)
130#define DIR_T DIR
131#define DIRENT_T struct dirent
132#define STAT_T struct stat
133#define MKDIR(a,b) (mkdir)(a,b)
134#define RMDIR(a) (rmdir)(a)
135#define OPENDIR(a) (opendir)(a)
136#define READDIR(a) (readdir)(a)
137#define CLOSEDIR(a) (closedir)(a)
138#define STAT(a,b) (stat)(a,b)
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000139/* empty variable parameter list doesn't work for variadic macros,
140 * so pretend the second parameter is variable too */
141#define OPEN(a, ...) (open)(a, __VA_ARGS__)
Nicolas Pennequinef9abe42007-10-09 15:15:00 +0000142#define CLOSE(x) (close)(x)
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000143#define REMOVE(a) (remove)(a)
144#define RENAME(a,b) (rename)(a,b)
145
146#endif /* !__MINGW32__ */
147
148
Miika Pekkarinend70bd0e2006-08-26 14:19:18 +0000149#ifdef HAVE_DIRCACHE
150void dircache_remove(const char *name);
Steve Bavinb61f0c62009-02-27 21:25:17 +0000151void dircache_rename(const char *oldname, const char *newname);
Miika Pekkarinend70bd0e2006-08-26 14:19:18 +0000152#endif
Daniel Stenberged6c7e42002-06-14 11:00:13 +0000153
Steve Bavin9e05cc52007-11-08 10:15:44 +0000154
Björn Stenberg819378b2008-11-24 20:32:57 +0000155#define SIMULATOR_DEFAULT_ROOT "simdisk"
Steve Bavin9e05cc52007-11-08 10:15:44 +0000156extern const char *sim_root_dir;
Daniel Stenberge3a12d32002-05-07 12:25:30 +0000157
Nicolas Pennequinef9abe42007-10-09 15:15:00 +0000158static int num_openfiles = 0;
159
Thomas Martitz6eaab4d2010-09-01 21:29:34 +0000160/* from dir.h */
161struct dirinfo {
Daniel Stenberg22b77012005-02-22 12:19:12 +0000162 int attribute;
Jens Arnoldd6cb7162007-04-07 00:14:16 +0000163 long size;
Thomas Martitz6eaab4d2010-09-01 21:29:34 +0000164 unsigned short wrtdate;
165 unsigned short wrttime;
166};
167
168struct sim_dirent {
169 unsigned char d_name[MAX_PATH];
170 struct dirinfo info;
Jens Arnoldd6cb7162007-04-07 00:14:16 +0000171 long startcluster;
Daniel Stenberg22b77012005-02-22 12:19:12 +0000172};
173
174struct dirstruct {
175 void *dir; /* actually a DIR* dir */
Steve Bavinf87eb602007-11-08 12:11:34 +0000176 char *name;
Daniel Stenberg22b77012005-02-22 12:19:12 +0000177} SIM_DIR;
178
Daniel Stenberge3a12d32002-05-07 12:25:30 +0000179struct mydir {
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000180 DIR_T *dir;
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000181 char *name;
Daniel Stenberge3a12d32002-05-07 12:25:30 +0000182};
183
184typedef struct mydir MYDIR;
Daniel Stenberg7b3abdc2002-04-30 13:14:59 +0000185
Daniel Stenberg36c9a952004-06-14 22:22:49 +0000186static unsigned int rockbox2sim(int opt)
187{
Thomas Martitz2c2e2612010-08-27 12:38:25 +0000188#if 0
189/* this shouldn't be needed since we use the host's versions */
Linus Nielsen Feltzingfc72c532006-02-03 15:19:58 +0000190 int newopt = O_BINARY;
Jens Arnold67eb1542007-02-01 23:08:15 +0000191
Daniel Stenberg36c9a952004-06-14 22:22:49 +0000192 if(opt & 1)
193 newopt |= O_WRONLY;
194 if(opt & 2)
195 newopt |= O_RDWR;
196 if(opt & 4)
197 newopt |= O_CREAT;
198 if(opt & 8)
199 newopt |= O_APPEND;
200 if(opt & 0x10)
201 newopt |= O_TRUNC;
202
203 return newopt;
Thomas Martitz2c2e2612010-08-27 12:38:25 +0000204#else
205 return opt|O_BINARY;
Linus Nielsen Feltzingb6f67a82004-07-08 05:50:20 +0000206#endif
Thomas Martitz2c2e2612010-08-27 12:38:25 +0000207}
Daniel Stenberg36c9a952004-06-14 22:22:49 +0000208
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000209/** Simulator I/O engine routines **/
Michael Sevakis6a058832007-12-03 15:33:12 +0000210#define IO_YIELD_THRESHOLD 512
211
Thomas Martitz240923a2010-08-02 20:34:47 +0000212enum io_dir
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000213{
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000214 IO_READ,
215 IO_WRITE,
216};
217
218struct sim_io
219{
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000220 struct mutex sim_mutex; /* Rockbox mutex */
Michael Sevakis6a058832007-12-03 15:33:12 +0000221 int cmd; /* The command to perform */
222 int ready; /* I/O ready flag - 1= ready */
223 int fd; /* The file to read/write */
224 void *buf; /* The buffer to read/write */
225 size_t count; /* Number of bytes to read/write */
226 size_t accum; /* Acculated bytes transferred */
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000227};
228
229static struct sim_io io;
230
Michael Sevakisc4a76312007-09-10 03:49:12 +0000231int ata_init(void)
232{
233 /* Initialize the rockbox kernel objects on a rockbox thread */
234 mutex_init(&io.sim_mutex);
Michael Sevakis6a058832007-12-03 15:33:12 +0000235 io.accum = 0;
Michael Sevakisc4a76312007-09-10 03:49:12 +0000236 return 1;
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000237}
238
Björn Stenbergff498512009-01-10 21:38:56 +0000239int ata_spinup_time(void)
240{
241 return HZ;
242}
243
Thomas Martitz240923a2010-08-02 20:34:47 +0000244static ssize_t io_trigger_and_wait(enum io_dir cmd)
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000245{
Michael Sevakis6a058832007-12-03 15:33:12 +0000246 void *mythread = NULL;
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000247 ssize_t result;
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000248
Michael Sevakis6a058832007-12-03 15:33:12 +0000249 if (io.count > IO_YIELD_THRESHOLD ||
250 (io.accum += io.count) >= IO_YIELD_THRESHOLD)
251 {
252 /* Allow other rockbox threads to run */
253 io.accum = 0;
Thomas Martitz3d0cee82010-05-15 21:02:47 +0000254 mythread = sim_thread_unlock();
Michael Sevakis6a058832007-12-03 15:33:12 +0000255 }
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000256
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000257 switch (cmd)
258 {
259 case IO_READ:
260 result = read(io.fd, io.buf, io.count);
261 break;
262 case IO_WRITE:
263 result = write(io.fd, io.buf, io.count);
264 break;
Thomas Martitz240923a2010-08-02 20:34:47 +0000265 /* shut up gcc */
266 default:
267 result = -1;
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000268 }
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000269
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000270 /* Regain our status as current */
Michael Sevakis6a058832007-12-03 15:33:12 +0000271 if (mythread != NULL)
272 {
Thomas Martitz3d0cee82010-05-15 21:02:47 +0000273 sim_thread_lock(mythread);
Michael Sevakis6a058832007-12-03 15:33:12 +0000274 }
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000275
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000276 return result;
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000277}
278
Thomas Martitz9c0b2472010-08-01 16:15:27 +0000279#if !defined(__PCTOOL__) && !defined(APPLICATION)
Steve Bavin6eea66f2009-02-27 21:15:52 +0000280static const char *get_sim_pathname(const char *name)
Steve Bavin9e05cc52007-11-08 10:15:44 +0000281{
Steve Bavin6eea66f2009-02-27 21:15:52 +0000282 static char buffer[MAX_PATH]; /* sufficiently big */
283
284 if(name[0] == '/')
285 {
Steve Bavinb61f0c62009-02-27 21:25:17 +0000286 snprintf(buffer, sizeof(buffer), "%s%s",
Steve Bavin6eea66f2009-02-27 21:15:52 +0000287 sim_root_dir != NULL ? sim_root_dir : SIMULATOR_DEFAULT_ROOT, name);
288 return buffer;
289 }
Steve Bavinb61f0c62009-02-27 21:25:17 +0000290 fprintf(stderr, "WARNING, bad file name lacks slash: %s\n", name);
Steve Bavin6eea66f2009-02-27 21:15:52 +0000291 return name;
Steve Bavin9e05cc52007-11-08 10:15:44 +0000292}
Steve Bavin6eea66f2009-02-27 21:15:52 +0000293#else
294#define get_sim_pathname(name) name
Björn Stenbergee46a3d2008-12-15 23:42:19 +0000295#endif
Steve Bavin9e05cc52007-11-08 10:15:44 +0000296
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000297MYDIR *sim_opendir(const char *name)
Daniel Stenberg7b3abdc2002-04-30 13:14:59 +0000298{
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000299 DIR_T *dir;
Steve Bavin6eea66f2009-02-27 21:15:52 +0000300 dir = (DIR_T *) OPENDIR(get_sim_pathname(name));
Björn Stenbergee46a3d2008-12-15 23:42:19 +0000301
Steve Bavin6eea66f2009-02-27 21:15:52 +0000302 if (dir)
Miika Pekkarinen0dd7ea22006-11-10 08:03:33 +0000303 {
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000304 MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR));
305 my->dir = dir;
Björn Stenbergee46a3d2008-12-15 23:42:19 +0000306 my->name = (char *)malloc(strlen(name)+1);
307 strcpy(my->name, name);
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +0000308
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000309 return my;
310 }
311 /* failed open, return NULL */
312 return (MYDIR *)0;
Daniel Stenberg7b3abdc2002-04-30 13:14:59 +0000313}
Daniel Stenberg91f165e2002-05-05 10:28:23 +0000314
Andree Buschmann992f8972010-12-25 22:17:02 +0000315#if defined(WIN32)
316static inline struct tm* localtime_r (const time_t *clock, struct tm *result) {
317 if (!clock || !result) return NULL;
318 memcpy(result,localtime(clock),sizeof(*result));
319 return result;
320}
321#endif
322
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000323struct sim_dirent *sim_readdir(MYDIR *dir)
Daniel Stenberg159d4482002-05-07 11:35:03 +0000324{
Steve Bavinf87eb602007-11-08 12:11:34 +0000325 char buffer[MAX_PATH]; /* sufficiently big */
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000326 static struct sim_dirent secret;
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000327 STAT_T s;
328 DIRENT_T *x11 = READDIR(dir->dir);
Thomas Martitz87c8be42010-12-23 19:02:18 +0000329 struct tm tm;
Daniel Stenberg159d4482002-05-07 11:35:03 +0000330
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000331 if(!x11)
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000332 return (struct sim_dirent *)0;
Daniel Stenberg159d4482002-05-07 11:35:03 +0000333
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000334 strcpy((char *)secret.d_name, OS_TO_UTF8(x11->d_name));
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +0000335
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000336 /* build file name */
Steve Bavinb61f0c62009-02-27 21:25:17 +0000337 snprintf(buffer, sizeof(buffer), "%s/%s",
Steve Bavin6eea66f2009-02-27 21:15:52 +0000338 get_sim_pathname(dir->name), secret.d_name);
Thomas Martitz87c8be42010-12-23 19:02:18 +0000339 if (STAT(buffer, &s)) /* get info */
340 return NULL;
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +0000341
Daniel Stenberg22b77012005-02-22 12:19:12 +0000342#define ATTR_DIRECTORY 0x10
Steve Bavinf87eb602007-11-08 12:11:34 +0000343
Thomas Martitz87c8be42010-12-23 19:02:18 +0000344 secret.info.attribute = 0;
Daniel Stenberg159d4482002-05-07 11:35:03 +0000345
Thomas Martitz87c8be42010-12-23 19:02:18 +0000346 if (S_ISDIR(s.st_mode))
347 secret.info.attribute = ATTR_DIRECTORY;
348
349 secret.info.size = s.st_size;
350
351 if (localtime_r(&(s.st_mtime), &tm) == NULL)
352 return NULL;
353 secret.info.wrtdate = ((tm.tm_year - 80) << 9) |
354 ((tm.tm_mon + 1) << 5) |
355 tm.tm_mday;
356 secret.info.wrttime = (tm.tm_hour << 11) |
357 (tm.tm_min << 5) |
358 (tm.tm_sec >> 1);
359
Andree Buschmann992f8972010-12-25 22:17:02 +0000360#if HAVE_LSTAT
Thomas Martitz87c8be42010-12-23 19:02:18 +0000361#define ATTR_LINK 0x80
362 if (!lstat(buffer, &s) && S_ISLNK(s.st_mode))
363 {
364 secret.info.attribute |= ATTR_LINK;
365 }
366#endif
367
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000368 return &secret;
Daniel Stenberg159d4482002-05-07 11:35:03 +0000369}
370
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000371void sim_closedir(MYDIR *dir)
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +0000372{
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000373 free(dir->name);
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000374 CLOSEDIR(dir->dir);
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +0000375
Daniel Stenberg11d9ecb2003-03-05 22:59:36 +0000376 free(dir);
Daniel Stenberg27dfc7c2002-05-07 12:06:32 +0000377}
378
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000379int sim_open(const char *name, int o, ...)
Daniel Stenberg91f165e2002-05-05 10:28:23 +0000380{
Daniel Stenberg36c9a952004-06-14 22:22:49 +0000381 int opts = rockbox2sim(o);
Nicolas Pennequinef9abe42007-10-09 15:15:00 +0000382 int ret;
Nicolas Pennequinef9abe42007-10-09 15:15:00 +0000383 if (num_openfiles >= MAX_OPEN_FILES)
384 return -2;
Daniel Stenberg91f165e2002-05-05 10:28:23 +0000385
Thomas Martitze919b5d2010-05-07 16:56:40 +0000386 if (opts & O_CREAT)
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000387 {
388 va_list ap;
389 va_start(ap, o);
Thomas Martitze919b5d2010-05-07 16:56:40 +0000390 mode_t mode = va_arg(ap, unsigned int);
391 ret = OPEN(get_sim_pathname(name), opts, mode);
Thomas Martitz0a1d7c22010-05-06 17:35:13 +0000392 va_end(ap);
393 }
394 else
395 ret = OPEN(get_sim_pathname(name), opts);
396
Björn Stenbergee46a3d2008-12-15 23:42:19 +0000397 if (ret >= 0)
398 num_openfiles++;
399 return ret;
Nicolas Pennequinef9abe42007-10-09 15:15:00 +0000400}
401
402int sim_close(int fd)
403{
404 int ret;
405 ret = CLOSE(fd);
Steve Bavinb61f0c62009-02-27 21:25:17 +0000406 if (ret == 0)
407 num_openfiles--;
Nicolas Pennequinef9abe42007-10-09 15:15:00 +0000408 return ret;
Daniel Stenberg91f165e2002-05-05 10:28:23 +0000409}
Björn Stenberg6fb512a2002-11-12 11:32:26 +0000410
Thomas Martitzc61e89c2010-05-06 17:35:04 +0000411int sim_creat(const char *name, mode_t mode)
Björn Stenbergc78e1b02003-01-09 00:55:00 +0000412{
Thomas Martitzc61e89c2010-05-06 17:35:04 +0000413 return OPEN(get_sim_pathname(name), O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, mode);
Steve Bavinf87eb602007-11-08 12:11:34 +0000414}
Björn Stenbergc78e1b02003-01-09 00:55:00 +0000415
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000416ssize_t sim_read(int fd, void *buf, size_t count)
417{
418 ssize_t result;
419
420 mutex_lock(&io.sim_mutex);
421
422 /* Setup parameters */
423 io.fd = fd;
424 io.buf = buf;
425 io.count = count;
426
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000427 result = io_trigger_and_wait(IO_READ);
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000428
429 mutex_unlock(&io.sim_mutex);
430
431 return result;
432}
433
434ssize_t sim_write(int fd, const void *buf, size_t count)
435{
436 ssize_t result;
437
438 mutex_lock(&io.sim_mutex);
439
440 io.fd = fd;
441 io.buf = (void*)buf;
442 io.count = count;
443
Michael Sevakisa4d19b72007-12-03 14:01:12 +0000444 result = io_trigger_and_wait(IO_WRITE);
Michael Sevakisf64ebb12007-09-08 12:20:53 +0000445
446 mutex_unlock(&io.sim_mutex);
447
448 return result;
449}
450
Jens Arnold6b0fdae2007-02-03 10:28:55 +0000451int sim_mkdir(const char *name)
Linus Nielsen Feltzingef7293f2004-01-21 14:58:40 +0000452{
Steve Bavinb61f0c62009-02-27 21:25:17 +0000453 return MKDIR(get_sim_pathname(name), 0777);
Linus Nielsen Feltzingef7293f2004-01-21 14:58:40 +0000454}
455
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000456int sim_rmdir(const char *name)
Linus Nielsen Feltzing8fa821d2004-04-16 09:24:38 +0000457{
Steve Bavinb61f0c62009-02-27 21:25:17 +0000458 return RMDIR(get_sim_pathname(name));
Linus Nielsen Feltzing8fa821d2004-04-16 09:24:38 +0000459}
460
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000461int sim_remove(const char *name)
Björn Stenbergc78e1b02003-01-09 00:55:00 +0000462{
Miika Pekkarinend70bd0e2006-08-26 14:19:18 +0000463#ifdef HAVE_DIRCACHE
464 dircache_remove(name);
465#endif
Steve Bavinb61f0c62009-02-27 21:25:17 +0000466 return REMOVE(get_sim_pathname(name));
Björn Stenbergc78e1b02003-01-09 00:55:00 +0000467}
468
Steve Bavinb61f0c62009-02-27 21:25:17 +0000469int sim_rename(const char *oldname, const char *newname)
Björn Stenberg7d80ba02003-01-15 11:38:03 +0000470{
Amaury Poulye9b3c902010-02-01 22:45:27 +0000471 char sim_old[MAX_PATH];
472 char sim_new[MAX_PATH];
Miika Pekkarinend70bd0e2006-08-26 14:19:18 +0000473#ifdef HAVE_DIRCACHE
Steve Bavinb61f0c62009-02-27 21:25:17 +0000474 dircache_rename(oldname, newname);
Miika Pekkarinend70bd0e2006-08-26 14:19:18 +0000475#endif
Amaury Poulye9b3c902010-02-01 22:45:27 +0000476 // This is needed as get_sim_pathname() has a static buffer
477 strncpy(sim_old, get_sim_pathname(oldname), MAX_PATH);
478 strncpy(sim_new, get_sim_pathname(newname), MAX_PATH);
479 return RENAME(sim_old, sim_new);
Björn Stenberg7d80ba02003-01-15 11:38:03 +0000480}
481
Jens Arnold399c0812005-02-28 18:32:57 +0000482/* rockbox off_t may be different from system off_t */
483long sim_lseek(int fildes, long offset, int whence)
Linus Nielsen Feltzinge98bad52003-03-18 00:39:57 +0000484{
Jens Arnold399c0812005-02-28 18:32:57 +0000485 return lseek(fildes, offset, whence);
486}
487
488long sim_filesize(int fd)
489{
Jens Arnoldbb9dfa02005-03-09 19:26:59 +0000490#ifdef WIN32
491 return _filelength(fd);
492#else
493 struct stat buf;
Steve Bavinf87eb602007-11-08 12:11:34 +0000494
Jens Arnoldbb9dfa02005-03-09 19:26:59 +0000495 if (!fstat(fd, &buf))
496 return buf.st_size;
497 else
498 return -1;
499#endif
Linus Nielsen Feltzinge98bad52003-03-18 00:39:57 +0000500}
501
Jens Arnold51280802007-09-13 20:53:32 +0000502void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free)
Björn Stenberg6fb512a2002-11-12 11:32:26 +0000503{
Jens Arnold51280802007-09-13 20:53:32 +0000504#ifdef HAVE_MULTIVOLUME
505 if (volume != 0) {
Steve Bavinac313ab2008-04-07 17:27:56 +0000506 /* debugf("io.c: fat_size(volume=%d); simulator only supports volume 0\n",volume); */
Steve Bavinf87eb602007-11-08 12:11:34 +0000507
Jens Arnold51280802007-09-13 20:53:32 +0000508 if (size) *size = 0;
509 if (free) *free = 0;
510 return;
511 }
512#endif
513
Linus Nielsen Feltzinga6142ab2004-06-10 13:29:52 +0000514#ifdef WIN32
Jens Arnold78724262005-02-22 21:55:48 +0000515 long secperclus, bytespersec, free_clusters, num_clusters;
516
517 if (GetDiskFreeSpace(NULL, &secperclus, &bytespersec, &free_clusters,
518 &num_clusters)) {
519 if (size)
520 *size = num_clusters * secperclus / 2 * (bytespersec / 512);
521 if (free)
522 *free = free_clusters * secperclus / 2 * (bytespersec / 512);
523 }
Thomas Martitz240923a2010-08-02 20:34:47 +0000524#elif HAVE_STATVFS
Jens Arnold181e0e02010-03-20 12:45:54 +0000525 struct statvfs vfs;
Björn Stenberg6fb512a2002-11-12 11:32:26 +0000526
Jens Arnold181e0e02010-03-20 12:45:54 +0000527 if (!statvfs(".", &vfs)) {
528 DEBUGF("statvfs: frsize=%d blocks=%ld free=%ld\n",
529 (int)vfs.f_frsize, (long)vfs.f_blocks, (long)vfs.f_bfree);
Björn Stenberg6fb512a2002-11-12 11:32:26 +0000530 if (size)
Jens Arnold181e0e02010-03-20 12:45:54 +0000531 *size = vfs.f_blocks / 2 * (vfs.f_frsize / 512);
Björn Stenberg6fb512a2002-11-12 11:32:26 +0000532 if (free)
Jens Arnold181e0e02010-03-20 12:45:54 +0000533 *free = vfs.f_bfree / 2 * (vfs.f_frsize / 512);
Thomas Martitz240923a2010-08-02 20:34:47 +0000534 } else
Jens Arnold78724262005-02-22 21:55:48 +0000535#endif
Thomas Martitz240923a2010-08-02 20:34:47 +0000536 {
Björn Stenberg6fb512a2002-11-12 11:32:26 +0000537 if (size)
538 *size = 0;
539 if (free)
540 *free = 0;
541 }
542}
Daniel Stenberg22b77012005-02-22 12:19:12 +0000543
544int sim_fsync(int fd)
545{
546#ifdef WIN32
547 return _commit(fd);
548#else
549 return fsync(fd);
550#endif
551}
552
Thomas Martitz89dcb1c2010-09-08 20:42:34 +0000553#ifndef __PCTOOL__
Thomas Martitz194174a2010-08-27 00:29:50 +0000554
Thomas Martitz0d4585b2010-09-09 16:17:21 +0000555void *lc_open(const char *filename, unsigned char *buf, size_t buf_size)
Miika Pekkarinen645a2e12005-07-10 16:33:03 +0000556{
Thomas Martitz194174a2010-08-27 00:29:50 +0000557 const char *sim_path = get_sim_pathname(filename);
Thomas Martitz2c2e2612010-08-27 12:38:25 +0000558 void *handle = _lc_open(UTF8_TO_OS(sim_path), buf, buf_size);
Miika Pekkarinen645a2e12005-07-10 16:33:03 +0000559
Thomas Martitz194174a2010-08-27 00:29:50 +0000560 if (handle == NULL)
Steve Bavin8c9d5f32006-09-28 08:46:28 +0000561 {
Thomas Martitz194174a2010-08-27 00:29:50 +0000562 DEBUGF("failed to load %s\n", filename);
563 DEBUGF("lc_open(%s): %s\n", filename, lc_last_error());
Steve Bavin8c9d5f32006-09-28 08:46:28 +0000564 }
Thomas Martitz194174a2010-08-27 00:29:50 +0000565 return handle;
Miika Pekkarinen645a2e12005-07-10 16:33:03 +0000566}
567
Thomas Martitz194174a2010-08-27 00:29:50 +0000568void *lc_get_header(void *handle)
Miika Pekkarinen645a2e12005-07-10 16:33:03 +0000569{
Thomas Martitz194174a2010-08-27 00:29:50 +0000570 return _lc_get_header(handle);
Miika Pekkarinen645a2e12005-07-10 16:33:03 +0000571}
572
Thomas Martitz194174a2010-08-27 00:29:50 +0000573void lc_close(void *handle)
Daniel Stenberg22b77012005-02-22 12:19:12 +0000574{
Thomas Martitz194174a2010-08-27 00:29:50 +0000575 _lc_close(handle);
Thomas Martitz97d2a6e2010-08-27 00:16:26 +0000576}
577
Thomas Martitz194174a2010-08-27 00:29:50 +0000578#endif /* __PCTOOL__ */
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000579#ifdef WIN32
580static unsigned old_cp;
Daniel Stenberg22b77012005-02-22 12:19:12 +0000581
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000582void debug_exit(void)
583{
584 /* Reset console output codepage */
585 SetConsoleOutputCP(old_cp);
586}
587
588void debug_init(void)
589{
590 old_cp = GetConsoleOutputCP();
591 /* Set console output codepage to UTF8. Only works
592 * correctly when the console uses a truetype font. */
593 SetConsoleOutputCP(65001);
594 atexit(debug_exit);
595}
596#else
Daniel Stenberg22b77012005-02-22 12:19:12 +0000597void debug_init(void)
598{
599 /* nothing to be done */
600}
Jens Arnold2fd7c3c2007-04-19 20:17:24 +0000601#endif
Daniel Stenberg22b77012005-02-22 12:19:12 +0000602
603void debugf(const char *fmt, ...)
604{
605 va_list ap;
606 va_start( ap, fmt );
607 vfprintf( stderr, fmt, ap );
608 va_end( ap );
609}
610
611void ldebugf(const char* file, int line, const char *fmt, ...)
612{
613 va_list ap;
614 va_start( ap, fmt );
615 fprintf( stderr, "%s:%d ", file, line );
616 vfprintf( stderr, fmt, ap );
617 va_end( ap );
618}
619
Jens Arnold576d0292005-03-05 21:48:58 +0000620/* rockbox off_t may be different from system off_t */
621int sim_ftruncate(int fd, long length)
Daniel Stenberg22b77012005-02-22 12:19:12 +0000622{
623#ifdef WIN32
624 return _chsize(fd, length);
625#else
626 return ftruncate(fd, length);
627#endif
628}