Redo previous commit to not break android builds.
[maemo-rb.git] / uisimulator / common / io.c
blobb367eb14a1df7173fb8863fbf7a414c7b0e310fd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <sys/stat.h>
27 #include <time.h>
28 #include "config.h"
30 #define HAVE_STATVFS (0 == (CONFIG_PLATFORM & PLATFORM_ANDROID) && !defined(WIN32))
32 #if HAVE_STATVFS
33 #include <sys/statvfs.h>
34 #endif
36 #ifdef WIN32
37 #include <windows.h>
38 #endif
40 #ifndef _MSC_VER
41 #include <dirent.h>
42 #include <unistd.h>
43 #else
44 #include "dir-win32.h"
45 #endif
47 #include <fcntl.h>
48 #if (CONFIG_PLATFORM & PLATFORM_SDL)
49 #include <SDL.h>
50 #include <SDL_thread.h>
51 #include "thread-sdl.h"
52 #else
53 #define sim_thread_unlock() NULL
54 #define sim_thread_lock(a)
55 #endif
56 #include "thread.h"
57 #include "kernel.h"
58 #include "debug.h"
59 #include "ata.h" /* for IF_MV2 et al. */
60 #include "rbpaths.h"
61 #include "load_code.h"
63 /* keep this in sync with file.h! */
64 #undef MAX_PATH /* this avoids problems when building simulator */
65 #define MAX_PATH 260
66 #define MAX_OPEN_FILES 11
68 /* Windows (and potentially other OSes) distinguish binary and text files.
69 * Define a dummy for the others. */
70 #ifndef O_BINARY
71 #define O_BINARY 0
72 #endif
74 /* Unicode compatibility for win32 */
75 #if defined __MINGW32__
76 /* Rockbox unicode functions */
77 extern const unsigned char* utf8decode(const unsigned char *utf8,
78 unsigned short *ucs);
79 extern unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8);
81 /* Static buffers for the conversion results. This isn't thread safe,
82 * but it's sufficient for rockbox. */
83 static unsigned char convbuf1[3*MAX_PATH];
84 static unsigned char convbuf2[3*MAX_PATH];
86 static wchar_t* utf8_to_ucs2(const unsigned char *utf8, void *buffer)
88 wchar_t *ucs = buffer;
90 while (*utf8)
91 utf8 = utf8decode(utf8, ucs++);
93 *ucs = 0;
94 return buffer;
96 static unsigned char *ucs2_to_utf8(const wchar_t *ucs, unsigned char *buffer)
98 unsigned char *utf8 = buffer;
100 while (*ucs)
101 utf8 = utf8encode(*ucs++, utf8);
103 *utf8 = 0;
104 return buffer;
107 #define UTF8_TO_OS(a) utf8_to_ucs2(a,convbuf1)
108 #define OS_TO_UTF8(a) ucs2_to_utf8(a,convbuf1)
109 #define DIR_T _WDIR
110 #define DIRENT_T struct _wdirent
111 #define STAT_T struct _stat
112 extern int _wmkdir(const wchar_t*);
113 extern int _wrmdir(const wchar_t*);
114 #define MKDIR(a,b) (_wmkdir)(UTF8_TO_OS(a))
115 #define RMDIR(a) (_wrmdir)(UTF8_TO_OS(a))
116 #define OPENDIR(a) (_wopendir)(UTF8_TO_OS(a))
117 #define READDIR(a) (_wreaddir)(a)
118 #define CLOSEDIR(a) (_wclosedir)(a)
119 #define STAT(a,b) (_wstat)(UTF8_TO_OS(a),b)
120 /* empty variable parameter list doesn't work for variadic macros,
121 * so pretend the second parameter is variable too */
122 #define OPEN(a,...) (_wopen)(UTF8_TO_OS(a), __VA_ARGS__)
123 #define CLOSE(a) (close)(a)
124 #define REMOVE(a) (_wremove)(UTF8_TO_OS(a))
125 #define RENAME(a,b) (_wrename)(UTF8_TO_OS(a),utf8_to_ucs2(b,convbuf2))
127 #else /* !__MINGW32__ */
129 #define UTF8_TO_OS(a) (a)
130 #define OS_TO_UTF8(a) (a)
131 #define DIR_T DIR
132 #define DIRENT_T struct dirent
133 #define STAT_T struct stat
134 #define MKDIR(a,b) (mkdir)(a,b)
135 #define RMDIR(a) (rmdir)(a)
136 #define OPENDIR(a) (opendir)(a)
137 #define READDIR(a) (readdir)(a)
138 #define CLOSEDIR(a) (closedir)(a)
139 #define STAT(a,b) (stat)(a,b)
140 /* empty variable parameter list doesn't work for variadic macros,
141 * so pretend the second parameter is variable too */
142 #define OPEN(a, ...) (open)(a, __VA_ARGS__)
143 #define CLOSE(x) (close)(x)
144 #define REMOVE(a) (remove)(a)
145 #define RENAME(a,b) (rename)(a,b)
147 #endif /* !__MINGW32__ */
150 #ifdef HAVE_DIRCACHE
151 void dircache_remove(const char *name);
152 void dircache_rename(const char *oldname, const char *newname);
153 #endif
156 #define SIMULATOR_DEFAULT_ROOT "simdisk"
157 extern const char *sim_root_dir;
159 static int num_openfiles = 0;
161 /* from dir.h */
162 struct dirinfo {
163 int attribute;
164 long size;
165 unsigned short wrtdate;
166 unsigned short wrttime;
169 struct sim_dirent {
170 unsigned char d_name[MAX_PATH];
171 struct dirinfo info;
172 long startcluster;
175 struct dirstruct {
176 void *dir; /* actually a DIR* dir */
177 char *name;
178 } SIM_DIR;
180 struct mydir {
181 DIR_T *dir;
182 char *name;
185 typedef struct mydir MYDIR;
187 static unsigned int rockbox2sim(int opt)
189 #if 0
190 /* this shouldn't be needed since we use the host's versions */
191 int newopt = O_BINARY;
193 if(opt & 1)
194 newopt |= O_WRONLY;
195 if(opt & 2)
196 newopt |= O_RDWR;
197 if(opt & 4)
198 newopt |= O_CREAT;
199 if(opt & 8)
200 newopt |= O_APPEND;
201 if(opt & 0x10)
202 newopt |= O_TRUNC;
204 return newopt;
205 #else
206 return opt|O_BINARY;
207 #endif
210 /** Simulator I/O engine routines **/
211 #define IO_YIELD_THRESHOLD 512
213 enum io_dir
215 IO_READ,
216 IO_WRITE,
219 struct sim_io
221 struct mutex sim_mutex; /* Rockbox mutex */
222 int cmd; /* The command to perform */
223 int ready; /* I/O ready flag - 1= ready */
224 int fd; /* The file to read/write */
225 void *buf; /* The buffer to read/write */
226 size_t count; /* Number of bytes to read/write */
227 size_t accum; /* Acculated bytes transferred */
230 static struct sim_io io;
232 int ata_init(void)
234 /* Initialize the rockbox kernel objects on a rockbox thread */
235 mutex_init(&io.sim_mutex);
236 io.accum = 0;
237 return 1;
240 int ata_spinup_time(void)
242 return HZ;
245 static ssize_t io_trigger_and_wait(enum io_dir cmd)
247 void *mythread = NULL;
248 ssize_t result;
250 if (io.count > IO_YIELD_THRESHOLD ||
251 (io.accum += io.count) >= IO_YIELD_THRESHOLD)
253 /* Allow other rockbox threads to run */
254 io.accum = 0;
255 mythread = sim_thread_unlock();
258 switch (cmd)
260 case IO_READ:
261 result = read(io.fd, io.buf, io.count);
262 break;
263 case IO_WRITE:
264 result = write(io.fd, io.buf, io.count);
265 break;
266 /* shut up gcc */
267 default:
268 result = -1;
271 /* Regain our status as current */
272 if (mythread != NULL)
274 sim_thread_lock(mythread);
277 return result;
280 #if !defined(__PCTOOL__) && !defined(APPLICATION)
281 static const char *get_sim_pathname(const char *name)
283 static char buffer[MAX_PATH]; /* sufficiently big */
285 if(name[0] == '/')
287 snprintf(buffer, sizeof(buffer), "%s%s",
288 sim_root_dir != NULL ? sim_root_dir : SIMULATOR_DEFAULT_ROOT, name);
289 return buffer;
291 fprintf(stderr, "WARNING, bad file name lacks slash: %s\n", name);
292 return name;
294 #else
295 #define get_sim_pathname(name) name
296 #endif
298 MYDIR *sim_opendir(const char *name)
300 DIR_T *dir;
302 dir = (DIR_T *) OPENDIR(get_sim_pathname(name));
304 if (dir)
306 MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR));
307 my->dir = dir;
308 my->name = (char *)malloc(strlen(name)+1);
309 strcpy(my->name, name);
311 return my;
313 /* failed open, return NULL */
314 return (MYDIR *)0;
317 struct sim_dirent *sim_readdir(MYDIR *dir)
319 char buffer[MAX_PATH]; /* sufficiently big */
320 static struct sim_dirent secret;
321 STAT_T s;
322 DIRENT_T *x11 = READDIR(dir->dir);
323 struct tm* tm;
325 if(!x11)
326 return (struct sim_dirent *)0;
328 strcpy((char *)secret.d_name, OS_TO_UTF8(x11->d_name));
330 /* build file name */
331 snprintf(buffer, sizeof(buffer), "%s/%s",
332 get_sim_pathname(dir->name), secret.d_name);
333 STAT(buffer, &s); /* get info */
335 #define ATTR_DIRECTORY 0x10
337 secret.info.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0;
338 secret.info.size = s.st_size;
340 tm = localtime(&(s.st_mtime));
341 secret.info.wrtdate = ((tm->tm_year - 80) << 9) |
342 ((tm->tm_mon + 1) << 5) |
343 tm->tm_mday;
344 secret.info.wrttime = (tm->tm_hour << 11) |
345 (tm->tm_min << 5) |
346 (tm->tm_sec >> 1);
347 return &secret;
350 struct dirinfo dir_get_info(DIR* parent, struct sim_dirent *entry)
352 (void)parent;
353 return entry->info;
357 void sim_closedir(MYDIR *dir)
359 free(dir->name);
360 CLOSEDIR(dir->dir);
362 free(dir);
365 int sim_open(const char *name, int o, ...)
367 int opts = rockbox2sim(o);
368 int ret;
369 if (num_openfiles >= MAX_OPEN_FILES)
370 return -2;
372 if (opts & O_CREAT)
374 va_list ap;
375 va_start(ap, o);
376 mode_t mode = va_arg(ap, unsigned int);
377 ret = OPEN(get_sim_pathname(name), opts, mode);
378 va_end(ap);
380 else
381 ret = OPEN(get_sim_pathname(name), opts);
383 if (ret >= 0)
384 num_openfiles++;
385 return ret;
388 int sim_close(int fd)
390 int ret;
391 ret = CLOSE(fd);
392 if (ret == 0)
393 num_openfiles--;
394 return ret;
397 int sim_creat(const char *name, mode_t mode)
399 return OPEN(get_sim_pathname(name), O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, mode);
402 ssize_t sim_read(int fd, void *buf, size_t count)
404 ssize_t result;
406 mutex_lock(&io.sim_mutex);
408 /* Setup parameters */
409 io.fd = fd;
410 io.buf = buf;
411 io.count = count;
413 result = io_trigger_and_wait(IO_READ);
415 mutex_unlock(&io.sim_mutex);
417 return result;
420 ssize_t sim_write(int fd, const void *buf, size_t count)
422 ssize_t result;
424 mutex_lock(&io.sim_mutex);
426 io.fd = fd;
427 io.buf = (void*)buf;
428 io.count = count;
430 result = io_trigger_and_wait(IO_WRITE);
432 mutex_unlock(&io.sim_mutex);
434 return result;
437 int sim_mkdir(const char *name)
439 return MKDIR(get_sim_pathname(name), 0777);
442 int sim_rmdir(const char *name)
444 return RMDIR(get_sim_pathname(name));
447 int sim_remove(const char *name)
449 #ifdef HAVE_DIRCACHE
450 dircache_remove(name);
451 #endif
452 return REMOVE(get_sim_pathname(name));
455 int sim_rename(const char *oldname, const char *newname)
457 char sim_old[MAX_PATH];
458 char sim_new[MAX_PATH];
459 #ifdef HAVE_DIRCACHE
460 dircache_rename(oldname, newname);
461 #endif
462 // This is needed as get_sim_pathname() has a static buffer
463 strncpy(sim_old, get_sim_pathname(oldname), MAX_PATH);
464 strncpy(sim_new, get_sim_pathname(newname), MAX_PATH);
465 return RENAME(sim_old, sim_new);
468 /* rockbox off_t may be different from system off_t */
469 long sim_lseek(int fildes, long offset, int whence)
471 return lseek(fildes, offset, whence);
474 long sim_filesize(int fd)
476 #ifdef WIN32
477 return _filelength(fd);
478 #else
479 struct stat buf;
481 if (!fstat(fd, &buf))
482 return buf.st_size;
483 else
484 return -1;
485 #endif
488 void fat_size(IF_MV2(int volume,) unsigned long* size, unsigned long* free)
490 #ifdef HAVE_MULTIVOLUME
491 if (volume != 0) {
492 /* debugf("io.c: fat_size(volume=%d); simulator only supports volume 0\n",volume); */
494 if (size) *size = 0;
495 if (free) *free = 0;
496 return;
498 #endif
500 #ifdef WIN32
501 long secperclus, bytespersec, free_clusters, num_clusters;
503 if (GetDiskFreeSpace(NULL, &secperclus, &bytespersec, &free_clusters,
504 &num_clusters)) {
505 if (size)
506 *size = num_clusters * secperclus / 2 * (bytespersec / 512);
507 if (free)
508 *free = free_clusters * secperclus / 2 * (bytespersec / 512);
510 #elif HAVE_STATVFS
511 struct statvfs vfs;
513 if (!statvfs(".", &vfs)) {
514 DEBUGF("statvfs: frsize=%d blocks=%ld free=%ld\n",
515 (int)vfs.f_frsize, (long)vfs.f_blocks, (long)vfs.f_bfree);
516 if (size)
517 *size = vfs.f_blocks / 2 * (vfs.f_frsize / 512);
518 if (free)
519 *free = vfs.f_bfree / 2 * (vfs.f_frsize / 512);
520 } else
521 #endif
523 if (size)
524 *size = 0;
525 if (free)
526 *free = 0;
530 int sim_fsync(int fd)
532 #ifdef WIN32
533 return _commit(fd);
534 #else
535 return fsync(fd);
536 #endif
540 #ifndef __PCTOOL__
541 #ifdef WIN32
542 /* sim-win32 */
543 #define dlopen(_x_, _y_) LoadLibraryW(UTF8_TO_OS(_x_))
544 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
545 #define dlclose(_x_) FreeLibrary(_x_)
546 #else
547 /* sim-x11 */
548 #include <dlfcn.h>
549 #endif
552 void *lc_open(const char *filename, char *buf, size_t buf_size)
554 const char *sim_path = get_sim_pathname(filename);
555 void *handle = _lc_open(UTF8_TO_OS(sim_path), buf, buf_size);
557 if (handle == NULL)
559 DEBUGF("failed to load %s\n", filename);
560 DEBUGF("lc_open(%s): %s\n", filename, lc_last_error());
562 return handle;
565 void *lc_get_header(void *handle)
567 return _lc_get_header(handle);
570 void lc_close(void *handle)
572 _lc_close(handle);
575 #endif /* __PCTOOL__ */
576 #ifdef WIN32
577 static unsigned old_cp;
579 void debug_exit(void)
581 /* Reset console output codepage */
582 SetConsoleOutputCP(old_cp);
585 void debug_init(void)
587 old_cp = GetConsoleOutputCP();
588 /* Set console output codepage to UTF8. Only works
589 * correctly when the console uses a truetype font. */
590 SetConsoleOutputCP(65001);
591 atexit(debug_exit);
593 #else
594 void debug_init(void)
596 /* nothing to be done */
598 #endif
600 void debugf(const char *fmt, ...)
602 va_list ap;
603 va_start( ap, fmt );
604 vfprintf( stderr, fmt, ap );
605 va_end( ap );
608 void ldebugf(const char* file, int line, const char *fmt, ...)
610 va_list ap;
611 va_start( ap, fmt );
612 fprintf( stderr, "%s:%d ", file, line );
613 vfprintf( stderr, fmt, ap );
614 va_end( ap );
617 /* rockbox off_t may be different from system off_t */
618 int sim_ftruncate(int fd, long length)
620 #ifdef WIN32
621 return _chsize(fd, length);
622 #else
623 return ftruncate(fd, length);
624 #endif