From 7d1a47cf13726c95ac46027156cc12dd9da5b855 Mon Sep 17 00:00:00 2001 From: Michael Sevakis Date: Mon, 5 Aug 2013 22:02:45 -0400 Subject: Rewrite filesystem code (WIP) This patch redoes the filesystem code from the FAT driver up to the clipboard code in onplay.c. Not every aspect of this is finished therefore it is still "WIP". I don't wish to do too much at once (haha!). What is left to do is get dircache back in the sim and find an implementation for the dircache indicies in the tagcache and playlist code or do something else that has the same benefit. Leaving these out for now does not make anything unusable. All the basics are done. Phone app code should probably get vetted (and app path handling just plain rewritten as environment expansions); the SDL app and Android run well. Main things addressed: 1) Thread safety: There is none right now in the trunk code. Most of what currently works is luck when multiple threads are involved or multiple descriptors to the same file are open. 2) POSIX compliance: Many of the functions behave nothing like their counterparts on a host system. This leads to inconsistent code or very different behavior from native to hosted. One huge offender was rename(). Going point by point would fill a book. 3) Actual running RAM usage: Many targets will use less RAM and less stack space (some more RAM because I upped the number of cache buffers for large memory). There's very little memory lying fallow in rarely-used areas (see 'Key core changes' below). Also, all targets may open the same number of directory streams whereas before those with less than 8MB RAM were limited to 8, not 12 implying those targets will save slightly less. 4) Performance: The test_disk plugin shows markedly improved performance, particularly in the area of (uncached) directory scanning, due partly to more optimal directory reading and to a better sector cache algorithm. Uncached times tend to be better while there is a bit of a slowdown in dircache due to it being a bit heavier of an implementation. It's not noticeable by a human as far as I can say. Key core changes: 1) Files and directories share core code and data structures. 2) The filesystem code knows which descriptors refer to same file. This ensures that changes from one stream are appropriately reflected in every open descriptor for that file (fileobj_mgr.c). 3) File and directory cache buffers are borrowed from the main sector cache. This means that when they are not in use by a file, they are not wasted, but used for the cache. Most of the time, only a few of them are needed. It also means that adding more file and directory handles is less expensive. All one must do in ensure a large enough cache to borrow from. 4) Relative path components are supported and the namespace is unified. It does not support full relative paths to an implied current directory; what is does support is use of "." and "..". Adding the former would not be very difficult. The namespace is unified in the sense that volumes may be specified several times along with relative parts, e.g.: "/<0>/foo/../../<1>/bar" :<=> "/<1>/bar". 5) Stack usage is down due to sharing of data, static allocation and less duplication of strings on the stack. This requires more serialization than I would like but since the number of threads is limited to a low number, the tradoff in favor of the stack seems reasonable. 6) Separates and heirarchicalizes (sic) the SIM and APP filesystem code. SIM path and volume handling is just like the target. Some aspects of the APP file code get more straightforward (e.g. no path hashing is needed). Dircache: Deserves its own section. Dircache is new but pays homage to the old. The old one was not compatible and so it, since it got redone, does all the stuff it always should have done such as: 1) It may be update and used at any time during the build process. No longer has one to wait for it to finish building to do basic file management (create, remove, rename, etc.). 2) It does not need to be either fully scanned or completely disabled; it can be incomplete (i.e. overfilled, missing paths), still be of benefit and be correct. 3) Handles mounting and dismounting of individual volumes which means a full rebuild is not needed just because you pop a new SD card in the slot. Now, because it reuses its freed entry data, may rebuild only that volume. 4) Much more fundamental to the file code. When it is built, it is the keeper of the master file list whether enabled or not ("disabled" is just a state of the cache). Its must always to ready to be started and bind all streams opened prior to being enabled. 5) Maintains any short filenames in OEM format which means that it does not need to be rebuilt when changing the default codepage. Miscellaneous Compatibility: 1) Update any other code that would otherwise not work such as the hotswap mounting code in various card drivers. 2) File management: Clipboard needed updating because of the behavioral changes. Still needs a little more work on some finer points. 3) Remove now-obsolete functionality such as the mutex's "no preempt" flag (which was only for the prior FAT driver). 4) struct dirinfo uses time_t rather than raw FAT directory entry time fields. I plan to follow up on genericizing everything there (i.e. no FAT attributes). 5) unicode.c needed some redoing so that the file code does not try try to load codepages during a scan, which is actually a problem with the current code. The default codepage, if any is required, is now kept in RAM separarately (bufalloced) from codepages specified to iso_decode() (which must not be bufalloced because the conversion may be done by playback threads). Brings with it some additional reusable core code: 1) Revised file functions: Reusable code that does things such as safe path concatenation and parsing without buffer limitations or data duplication. Variants that copy or alter the input path may be based off these. To do: 1) Put dircache functionality back in the sim. Treating it internally as a different kind of file system seems the best approach at this time. 2) Restore use of dircache indexes in the playlist and database or something effectively the same. Since the cache doesn't have to be complete in order to be used, not getting a hit on the cache doesn't unambiguously say if the path exists or not. Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8 Reviewed-on: http://gerrit.rockbox.org/566 Reviewed-by: Michael Sevakis Tested: Michael Sevakis --- uisimulator/common/SOURCES | 17 +- uisimulator/common/dummylib.c | 1 + uisimulator/common/filesystem-sim.c | 833 ++++++++++++++++++++++++++++++++++++ uisimulator/common/filesystem-sim.h | 108 +++++ uisimulator/common/io.c | 729 ------------------------------- uisimulator/common/load_code-sim.c | 72 ++++ uisimulator/common/sim_tasks.c | 34 +- uisimulator/common/stubs.c | 5 +- uisimulator/common/time-win32.c | 61 +++ 9 files changed, 1120 insertions(+), 740 deletions(-) create mode 100644 uisimulator/common/dummylib.c create mode 100644 uisimulator/common/filesystem-sim.c create mode 100644 uisimulator/common/filesystem-sim.h delete mode 100644 uisimulator/common/io.c create mode 100644 uisimulator/common/load_code-sim.c create mode 100644 uisimulator/common/time-win32.c (limited to 'uisimulator') diff --git a/uisimulator/common/SOURCES b/uisimulator/common/SOURCES index 939f1638c3..9833753236 100644 --- a/uisimulator/common/SOURCES +++ b/uisimulator/common/SOURCES @@ -1,4 +1,5 @@ #ifdef SIMULATOR + lcd-common.c #ifdef HAVE_LCD_CHARCELLS font-player.c @@ -8,8 +9,20 @@ sim_icons.c sim_tasks.c fmradio.c backlight-sim.c -stubs.c powermgmt-sim.c +filesystem-sim.c + +#ifdef WIN32 +time-win32.c +#endif + +#ifndef __PCTOOL__ +load_code-sim.c #endif -io.c +stubs.c + +#else +dummylib.c /* for now, so the lib actually builds */ +#endif /* SIMULATOR */ + diff --git a/uisimulator/common/dummylib.c b/uisimulator/common/dummylib.c new file mode 100644 index 0000000000..9747edf3a1 --- /dev/null +++ b/uisimulator/common/dummylib.c @@ -0,0 +1 @@ +/* this exists to get libuisimulator to make so that the sdl app may link */ diff --git a/uisimulator/common/filesystem-sim.c b/uisimulator/common/filesystem-sim.c new file mode 100644 index 0000000000..766beb3fda --- /dev/null +++ b/uisimulator/common/filesystem-sim.c @@ -0,0 +1,833 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 Daniel Stenberg + * Copyright (C) 2014 Michael Sevakis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#define RB_FILESYSTEM_OS +#include +#include +#include +#include +#include +#include +#include "config.h" +#include "system.h" +#include "file.h" +#include "dir.h" +#include "file_internal.h" +#include "pathfuncs.h" +#include "string-extra.h" +#include "debug.h" + +#ifndef os_fstatat +#define USE_OSDIRNAME /* we need to remember the open directory path */ +#endif + +extern const char *sim_root_dir; + +/* Windows (and potentially other OSes) distinguish binary and text files. + * Define a dummy for the others. */ +#ifndef O_BINARY +#define O_BINARY 0 +#endif + +struct filestr_desc +{ + int osfd; /* The host OS file descriptor */ + bool mounted; /* Is host volume still mounted? */ +#ifdef HAVE_MULTIVOLUME + int volume; /* The virtual volume number */ +#endif +} openfiles[MAX_OPEN_FILES] = +{ + [0 ... MAX_OPEN_FILES-1] = { .osfd = -1 } +}; + +static struct filestr_desc * alloc_filestr(int *fildesp) +{ + for (unsigned int i = 0; i < MAX_OPEN_FILES; i++) + { + struct filestr_desc *filestr = &openfiles[i]; + if (filestr->osfd < 0) + { + *fildesp = i; + return filestr; + } + } + + return NULL; +} + +static struct filestr_desc * get_filestr(int fildes) +{ + struct filestr_desc *filestr = &openfiles[fildes]; + + if ((unsigned int)fildes >= MAX_OPEN_FILES || filestr->osfd < 0) + filestr = NULL; + else if (filestr->mounted) + return filestr; + + errno = filestr ? ENXIO : EBADF; + DEBUGF("fildes %d: %s\n", fildes, strerror(errno)); + return NULL; +} + +struct dirstr_desc +{ + int osfd; /* Host OS directory file descriptor */ + bool osfd_opened; /* Host fd is another open file */ + OS_DIR_T *osdirp; /* Host OS directory stream */ +#ifdef USE_OSDIRNAME + char *osdirname; /* Host OS directory path */ +#endif + struct sim_dirent entry; /* Rockbox directory entry */ +#ifdef HAVE_MULTIVOLUME + int volume; /* Virtual volume number */ + int volumecounter; /* Counter for root volume entries */ +#endif + bool mounted; /* Is the virtual volume still mounted? */ +} opendirs[MAX_OPEN_DIRS]; + +static struct dirstr_desc * alloc_dirstr(void) +{ + for (unsigned int i = 0; i < MAX_OPEN_DIRS; i++) + { + struct dirstr_desc *dirstr = &opendirs[i]; + if (dirstr->osdirp == NULL) + return dirstr; + } + + return NULL; +} + +static struct dirstr_desc * get_dirstr(DIR *dirp) +{ + struct dirstr_desc *dirstr = (struct dirstr_desc *)dirp; + + if (!PTR_IN_ARRAY(opendirs, dirstr, MAX_OPEN_DIRS) || !dirstr->osdirp) + dirstr = NULL; + else if (dirstr->mounted) + return dirstr; + + errno = dirstr ? ENXIO : EBADF; + DEBUGF("dir #%d: %s\n", (int)(dirstr - opendirs), strerror(errno)); + return NULL; +} + +static int close_dirstr(struct dirstr_desc *dirstr) +{ + OS_DIR_T *osdirp = dirstr->osdirp; + + dirstr->mounted = false; + +#ifdef USE_OSDIRNAME + free(dirstr->osdirname); +#endif + if (dirstr->osfd_opened) + { + os_close(dirstr->osfd); + dirstr->osfd_opened = false; + } + + int rc = os_closedir(osdirp); + dirstr->osdirp = NULL; + + return rc; +} + +#ifdef HAVE_MULTIVOLUME +static int readdir_volume_inner(struct dirstr_desc *dirstr, + struct sim_dirent *entry) +{ + /* Volumes (secondary file systems) get inserted into the system root + * directory. If the path specified volume 0, enumeration will not + * include other volumes, but just its own files and directories. + * + * Fake special directories, which don't really exist, that will get + * redirected upon opendir() + */ + while (++dirstr->volumecounter < NUM_VOLUMES) + { + /* on the system root */ + if (!volume_present(dirstr->volumecounter)) + continue; + + get_volume_name(dirstr->volumecounter, entry->d_name); + return 1; + } + + /* do normal directory entry fetching */ + return 0; +} +#endif /* HAVE_MULTIVOLUME */ + +static inline int readdir_volume(struct dirstr_desc *dirstr, + struct sim_dirent *entry) +{ +#ifdef HAVE_MULTIVOLUME + if (dirstr->volumecounter < NUM_VOLUMES) + return readdir_volume_inner(dirstr, entry); +#endif /* HAVE_MULTIVOLUME */ + + /* do normal directory entry fetching */ + return 0; + (void)dirstr; (void)entry; +} + + +/** Internal functions **/ + +#ifdef HAVE_MULTIDRIVE +/** + * Handle drive extraction by pretending the files' volumes no longer exist + * and invalidating their I/O for the remainder of their lifetimes as would + * happen on a native target + */ +void sim_ext_extracted(int drive) +{ + for (unsigned int i = 0; i < MAX_OPEN_FILES; i++) + { + struct filestr_desc *filestr = &openfiles[i]; + if (filestr->osfd >= 0 && volume_drive(filestr->volume) == drive) + filestr->mounted = false; + } + + for (unsigned int i = 0; i < MAX_OPEN_DIRS; i++) + { + struct dirstr_desc *dirstr = &opendirs[i]; + if (dirstr->osdirp && volume_drive(dirstr->volume) == drive) + dirstr->mounted = false; + } + + (void)drive; +} +#endif /* HAVE_MULTIDRIVE */ + +/** + * Provides target-like path parsing behavior with single and multiple volumes + * while performing minimal transforming of the input. + * + * Paths are sandboxed to the simulated namespace: + * e.g. "{simdir}/foo/../../../../bar" becomes "{simdir}/foo/../bar" + */ +int sim_get_os_path(char *buffer, const char *path, size_t bufsize) +{ + #define ADVBUF(amt) \ + ({ buffer += (amt); bufsize -= (amt); }) + + #define PPP_SHOWPATHS 0 + + if (!path_is_absolute(path)) + { + DEBUGF("ERROR: path is not absolute: \"%s\"\n", path); + errno = ENOENT; + return -1; + } + +#if PPP_SHOWPATHS + const char *const buffer0 = buffer; + DEBUGF("PPP (pre): \"%s\"\n", path); +#endif + + /* Prepend sim root */ + size_t size = strlcpy(buffer, sim_root_dir, bufsize); + if (size >= bufsize) + { + errno = ENAMETOOLONG; + return -1; + } + ADVBUF(size); + +#ifdef HAVE_MULTIVOLUME + /* Track the last valid volume spec encountered */ + int volume = -1; + bool sysroot = true; + + /* Basename of sim dir to switch back to simdisk from ext */ + #define DIRBASE_FMT ".." PATH_SEPSTR "%s" + ssize_t dirbase_len = 0; + char dirbase[size + 3 + 1]; + + /* Basename of ext directory to switch to alternate volume */ + #define SIMEXT_FMT ".." PATH_SEPSTR "simext%d" + char extbuf[sizeof (SIMEXT_FMT) + 20 + 1]; +#endif /* HAVE_MULTIVOLUME */ + + int level = 0; + bool done = false; + while (!done) + { + const char *p; + ssize_t len = parse_path_component(&path, &p); + + + switch (len) + { + case 0: + done = true; + if (path[-1] != PATH_SEPCH) + continue; + + /* Path ends with a separator; preserve that */ + p = &path[-1]; + len = 1; + break; + + case 1: + case 2: + if (p[0] == '.') + { + if (len == 1) + break; + + if (p[1] == '.') + goto is_dot_dot; + } + + default: + level++; + + #ifdef HAVE_MULTIVOLUME + if (level != 1) + break; /* Volume spec only valid @ root level */ + + const char *next; + volume = path_strip_volume(p, &next, true); + + if (next > p) + { + #ifdef HAVE_MULTIDRIVE + /* Feign failure if the volume isn't "mounted" */ + if (!volume_present(volume)) + { + errno = ENXIO; + return -1; + } + #endif /* HAVE_MULTIDRIVE */ + + sysroot = false; + + if (volume == 0) + continue; + + p = extbuf; + len = snprintf(extbuf, sizeof (extbuf), SIMEXT_FMT, volume); + } + #endif /* HAVE_MULTIVOLUME */ + break; + + is_dot_dot: + if (level <= 0) + continue; /* Can't go above root; erase */ + + level--; + + #ifdef HAVE_MULTIVOLUME + if (level == 0) + { + int v = volume; + bool sr = sysroot; + volume = -1; + sysroot = true; + + if (v <= 0) + { + if (sr) + break; + + continue; + } + + /* Going up from a volume root and back down to the sys root */ + if (dirbase_len == 0) + { + /* Get the main simdisk directory so it can be reentered */ + char tmpbuf[sizeof (dirbase)]; + #ifdef WIN32 + path_correct_separators(tmpbuf, sim_root_dir); + path_strip_drive(tmpbuf, &p, false); + #else + p = tmpbuf; + strcpy(tmpbuf, sim_root_dir); + #endif + size = path_basename(p, &p); + ((char *)p)[size] = '\0'; + + if (size == 0 || is_dotdir_name(p)) + { + /* This is nonsense and won't work */ + DEBUGF("ERROR: sim root dir basname is dotdir or" + " empty: \"%s\"\n", sim_root_dir); + errno = ENOENT; + return -1; + } + + dirbase_len = snprintf(dirbase, sizeof (dirbase), + DIRBASE_FMT, p); + } + + p = dirbase; + len = dirbase_len; + break; + } + #endif /* HAVE_MULTIVOLUME */ + break; + } /* end switch */ + + char compname[len + 1]; + strmemcpy(compname, p, len); + + size = path_append(buffer, PA_SEP_HARD, compname, bufsize); + if (size >= bufsize) + { + errno = ENAMETOOLONG; + return -1; + } + ADVBUF(size); + } + +#if PPP_SHOWPATHS + DEBUGF("PPP (post): \"%s\"" IF_MV(" vol:%d") "\n", + buffer0 IF_MV(, volume)); +#endif + + return IF_MV(volume) +1; +} + + +/** File functions **/ + +int sim_open(const char *path, int oflag, ...) +{ + int fildes; + struct filestr_desc *filestr = alloc_filestr(&fildes); + if (!filestr) + { + errno = EMFILE; + return -1; + } + + char ospath[SIM_TMPBUF_MAX_PATH]; + int pprc = sim_get_os_path(ospath, path, sizeof (ospath)); + if (pprc < 0) + return -2; + + filestr->osfd = os_open(ospath, oflag | O_BINARY __OPEN_MODE_ARG); + if (filestr->osfd < 0) + return -3; + +#ifdef HAVE_MULTIVOLUME + filestr->volume = MAX(pprc - 1, 0); +#endif + filestr->mounted = true; + return fildes; +} + +int sim_creat(const char *path, mode_t mode) +{ + return sim_open(path, O_WRONLY|O_CREAT|O_TRUNC, mode); +} + +int sim_close(int fildes) +{ + struct filestr_desc *filestr = &openfiles[fildes]; + if ((unsigned int)fildes >= MAX_OPEN_FILES || filestr->osfd < 0) + { + errno = EBADF; + return -1; + } + + int osfd = filestr->osfd; + filestr->osfd = -1; + return os_close(osfd); +} + +int sim_ftruncate(int fildes, off_t length) +{ + struct filestr_desc *filestr = get_filestr(fildes); + if (!filestr) + return -1; + + off_t size = os_filesize(filestr->osfd); + if (size < 0) + return -1; + + if (length >= size) + return 0; + + int rc = os_ftruncate(filestr->osfd, length); + +#ifdef HAVE_DIRCACHE + if (rc >= 0) + dircache_ftruncate(xxxxxx); +#endif + + return rc; +} + +int sim_fsync(int fildes) +{ + struct filestr_desc *filestr = get_filestr(fildes); + if (!filestr) + return -1; + + int rc = os_fsync(filestr->osfd); + +#ifdef HAVE_DIRCACHE + if (rc >= 0) + dircache_fsync(xxxxxx); +#endif + + return rc; +} + +off_t sim_lseek(int fildes, off_t offset, int whence) +{ + struct filestr_desc *filestr = get_filestr(fildes); + if (!filestr) + return -1; + + return os_lseek(filestr->osfd, offset, whence); +} + +ssize_t sim_read(int fildes, void *buf, size_t nbyte) +{ + struct filestr_desc *filestr = get_filestr(fildes); + if (!filestr) + return -1; + + return os_read(filestr->osfd, buf, nbyte); +} + +ssize_t sim_write(int fildes, const void *buf, size_t nbyte) +{ + struct filestr_desc *filestr = get_filestr(fildes); + if (!filestr) + return -1; + + return os_write(filestr->osfd, buf, nbyte); +} + +int sim_remove(const char *path) +{ + char ospath[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(ospath, path, sizeof (ospath)) < 0) + return -1; + + int rc = os_remove(ospath); + +#ifdef HAVE_DIRCACHE + if (rc >= 0) + dircache_remove(xxxxxx); +#endif + + return rc; +} + +int sim_rename(const char *old, const char *new) +{ + char osold[SIM_TMPBUF_MAX_PATH]; + int pprc1 = sim_get_os_path(osold, old, sizeof (osold)); + if (pprc1 < 0) + return -1; + + char osnew[SIM_TMPBUF_MAX_PATH]; + int pprc2 = sim_get_os_path(osnew, new, sizeof (osnew)); + if (pprc2 < 0) + return -1; + + if (MAX(pprc1 - 1, 0) != MAX(pprc2 - 1, 0)) + { + /* Pretend they're different devices */ + errno = EXDEV; + return -1; + } + + int rc = os_rename(osold, osnew); + +#ifdef HAVE_DIRCACHE + if (rc >= 0) + dircache_rename(xxxxxx); +#endif + + return rc; +} + +off_t sim_filesize(int fildes) +{ + struct filestr_desc *filestr = get_filestr(fildes); + if (!filestr) + return -1; + + return os_filesize(filestr->osfd); +} + +int sim_fsamefile(int fildes1, int fildes2) +{ + struct filestr_desc *filestr1 = get_filestr(fildes1); + if (!filestr1) + return -1; + + struct filestr_desc *filestr2 = get_filestr(fildes2); + if (!filestr2) + return -1; + + if (filestr1 == filestr2) + return 1; + + return os_fsamefile(filestr1->osfd, filestr2->osfd); +} + +int sim_relate(const char *path1, const char *path2) +{ + char ospath1[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(ospath1, path1, sizeof (ospath1)) < 0) + return -1; + + char ospath2[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(ospath2, path2, sizeof (ospath2)) < 0) + return -1; + + return os_relate(ospath1, ospath2); +} + +bool sim_file_exists(const char *path) +{ + char ospath[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(ospath, path, sizeof (ospath)) < 0) + return false; + + return os_file_exists(ospath); +} + + +/** Directory functions **/ +DIR * sim_opendir(const char *dirname) +{ + struct dirstr_desc *dirstr = alloc_dirstr(); + if (!dirstr) + { + errno = EMFILE; + return NULL; + } + + char osdirname[SIM_TMPBUF_MAX_PATH]; + int pprc = sim_get_os_path(osdirname, dirname, sizeof (osdirname)); + if (pprc < 0) + return NULL; + + int rc = os_opendir_and_fd(osdirname, &dirstr->osdirp, &dirstr->osfd); + if (rc < 0) + return NULL; + + dirstr->osfd_opened = rc > 0; + +#ifdef USE_OSDIRNAME + dirstr->osdirname = strdup(osdirname); + if (!dirstr->osdirname) + { + close_dirstr(dirstr); + return NULL; + } +#endif + + dirstr->entry.d_name[0] = 0; /* Mark as invalid */ +#ifdef HAVE_MULTIVOLUME + dirstr->volume = MAX(pprc - 1, 0); + dirstr->volumecounter = pprc == 0 ? 0 : INT_MAX; +#endif + dirstr->mounted = true; + return (DIR *)dirstr; /* A-Okay */ +} + +int sim_closedir(DIR *dirp) +{ + struct dirstr_desc *dirstr = (struct dirstr_desc *)dirp; + if (!PTR_IN_ARRAY(opendirs, dirstr, MAX_OPEN_DIRS) || !dirstr->osdirp) + { + errno = EBADF; + return -1; + } + + return close_dirstr(dirstr); +} + +struct sim_dirent * sim_readdir(DIR *dirp) +{ + struct dirstr_desc *dirstr = get_dirstr(dirp); + if (!dirstr) + return NULL; + + struct sim_dirent *entry = &dirstr->entry; + entry->info.osdirent = NULL; + + if (readdir_volume(dirstr, entry)) + return entry; + + OS_DIRENT_T *osdirent = os_readdir(dirstr->osdirp); + if (!osdirent) + return NULL; + + size_t size = sizeof (entry->d_name); + if (strlcpy_from_os(entry->d_name, osdirent->d_name, size) >= size) + FILE_ERROR_RETURN(ENAMETOOLONG, NULL); + + entry->info.osdirent = osdirent; + return entry; +} + +int sim_mkdir(const char *path) +{ + char ospath[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(ospath, path, sizeof (ospath)) < 0) + return -1; + + int rc = os_mkdir(ospath __MKDIR_MODE_ARG); + +#ifdef HAVE_DIRCACHE + if (rc >= 0) + dircache_mkdir(xxxxxx); +#endif + + return rc; +} + +int sim_rmdir(const char *path) +{ + char ospath[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(ospath, path, sizeof (ospath)) < 0) + return -1; + + int rc = os_rmdir(ospath); + +#ifdef HAVE_DIRCACHE + if (rc >= 0) + dircache_rmdir(xxxxxx); +#endif + + return rc; +} + +int sim_samedir(DIR *dirp1, DIR *dirp2) +{ + struct dirstr_desc *dirstr1 = get_dirstr(dirp1); + if (!dirstr1) + return -1; + + struct dirstr_desc *dirstr2 = get_dirstr(dirp2); + if (!dirstr2) + return -1; + + return os_fsamefile(dirstr1->osfd, dirstr2->osfd); +} + +bool sim_dir_exists(const char *dirname) +{ + char osdirname[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(osdirname, dirname, sizeof (osdirname)) < 0) + return false; + + OS_DIR_T *dirp = os_opendir(osdirname); + if (!dirp) + return false; + + os_closedir(dirp); + return true; +} + +struct dirinfo dir_get_info(DIR *dirp, struct sim_dirent *entry) +{ + int rc; + struct dirstr_desc *dirstr = get_dirstr(dirp); + if (!dirstr) + FILE_ERROR(ERRNO, RC); + + if (entry->d_name[0] == 0) + FILE_ERROR(ENOENT, RC); + + OS_DIRENT_T *osdirent = entry->info.osdirent; + if (osdirent == NULL) + return (struct dirinfo){ .attribute = ATTR_MOUNT_POINT }; + + struct dirinfo info; + info.attribute = 0; + + OS_STAT_T s; + +#ifdef os_fstatat + if (os_fstatat(dirstr->osfd, entry->d_name, &s, 0)) +#else /* no fstatat; build file name for stat() */ + char statpath[SIM_TMPBUF_MAX_PATH]; + if (path_append(statpath, dirstr->osdirname, entry->d_name, + sizeof (statpath)) >= sizeof (statpath)) + { + FILE_ERROR(ENAMETOOLONG, RC); + } + + if (os_stat(statpath, &s)) /* get info */ +#endif /* os_fstatat */ + { + /* File size larger than 2 GB? */ + #ifdef EOVERFLOW + if (errno == EOVERFLOW) + DEBUGF("stat overflow for \"%s\"\n", entry->d_name); + #endif + FILE_ERROR(ERRNO, RC); + } + + if (S_ISDIR(s.st_mode)) + info.attribute |= ATTR_DIRECTORY; + + info.size = s.st_size; + + struct tm tm; + if (localtime_r(&s.st_mtime, &tm) == NULL) + FILE_ERROR(ERRNO, RC); + + info.mtime = mktime(&tm); + + return info; + +file_error: + return (struct dirinfo){ .size = 0 }; +} + +int os_volume_path(IF_MV(int volume, ) char *buffer, size_t bufsize) +{ + if (!buffer || !bufsize IF_MV( || !volume_present(volume) )) + return -1; + + char tmpbuf[SIM_TMPBUF_MAX_PATH]; + tmpbuf[0] = '\0'; + +#ifdef HAVE_MULTIVOLUME + char volname[VOL_MAX_LEN + 1]; + get_volume_name(volume, volname); + + if (path_append(tmpbuf, PA_SEP_HARD, volname, sizeof (volname)) + >= sizeof (volname)) + return -1; +#endif /* HAVE_MULTIVOLUME */ + + if (path_append(tmpbuf, PA_SEP_HARD, ".", sizeof (tmpbuf)) + >= sizeof (tmpbuf)) + return -1; + + return sim_get_os_path(buffer, tmpbuf, bufsize); +} diff --git a/uisimulator/common/filesystem-sim.h b/uisimulator/common/filesystem-sim.h new file mode 100644 index 0000000000..7c46c449d8 --- /dev/null +++ b/uisimulator/common/filesystem-sim.h @@ -0,0 +1,108 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2014 by Michael Sevakis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef _FILESYSTEM_SIM_H_ +#define _FILESYSTEM_SIM_H_ + +#if defined(PLUGIN) || defined(CODEC) +/* Prevent often-problematic plugin namespace pollution */ +#define FILEFUNCTIONS_DECLARED +#define FILEFUNCTIONS_DEFINED +#define DIRFUNCTIONS_DECLARED +#define DIRFUNCTIONS_DEFINED +#define OSFUNCTIONS_DECLARED +#endif /* PLUGIN || CODEC */ + +#ifndef OSFUNCTIONS_DECLARED +#define FS_PREFIX(_x_) sim_ ## _x_ + +/* path sandboxing and volume redirector */ +int sim_get_os_path(char *buffer, const char *path, size_t bufsize); +#define SIM_TMPBUF_MAX_PATH (MAX_PATH+1) + +#endif /* !OSFUNCTIONS_DECLARED */ + +#endif /* _FILESYSTEM_SIM_H_ */ + +#include "filesystem-sdl.h" +#ifdef WIN32 +#include "filesystem-win32.h" +#else /* !WIN32 */ +#include "filesystem-unix.h" +#endif /* WIN32 */ +#include "filesystem-hosted.h" + +#ifdef _FILE_H_ +#ifndef _FILESYSTEM_SIM_H__FILE_H_ +#define _FILESYSTEM_SIM_H__FILE_H_ + +#ifdef RB_FILESYSTEM_OS +#define FILEFUNCTIONS_DEFINED +#endif + +#ifndef FILEFUNCTIONS_DECLARED +int sim_open(const char *name, int oflag, ...); +int sim_creat(const char *name, mode_t mode); +int sim_close(int fildes); +int sim_ftruncate(int fildes, off_t length); +int sim_fsync(int fildes); +off_t sim_lseek(int fildes, off_t offset, int whence); +ssize_t sim_read(int fildes, void *buf, size_t nbyte); +ssize_t sim_write(int fildes, const void *buf, size_t nbyte); +int sim_remove(const char *path); +int sim_rename(const char *old, const char *new); +off_t sim_filesize(int fildes); +int sim_fsamefile(int fildes1, int fildes2); +int sim_relate(const char *path1, const char *path2); +bool sim_file_exists(const char *path); +#endif /* !FILEFUNCTIONS_DECLARED */ + +#endif /* _FILESYSTEM_SIM_H__FILE_H_ */ +#endif /* _FILE_H_ */ + +#ifdef _DIR_H_ +#ifndef _FILESYSTEM_SIM_H__DIR_H_ +#define _FILESYSTEM_SIM_H__DIR_H_ + +#ifdef RB_FILESYSTEM_OS +#define DIRFUNCTIONS_DEFINED +#else +#define dirent DIRENT +#endif + +#define DIRENT sim_dirent + +struct dirinfo_native +{ + void *osdirent; +}; + +#ifndef DIRFUNCTIONS_DECLARED +DIR * sim_opendir(const char *dirname); +struct sim_dirent * sim_readdir(DIR *dirp); +int sim_closedir(DIR *dirp); +int sim_mkdir(const char *path); +int sim_rmdir(const char *path); +int sim_samedir(DIR *dirp1, DIR *dirp2); +bool sim_dir_exists(const char *dirname); +#endif /* !DIRFUNCTIONS_DECLARED */ + +#endif /* _FILESYSTEM_SIM_H__DIR_H_ */ +#endif /* _DIR_H_ */ diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c deleted file mode 100644 index 6662e9ffda..0000000000 --- a/uisimulator/common/io.c +++ /dev/null @@ -1,729 +0,0 @@ -/*************************************************************************** - * __________ __ ___. - * Open \______ \ ____ ____ | | _\_ |__ _______ ___ - * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / - * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < - * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ - * \/ \/ \/ \/ \/ - * $Id$ - * - * Copyright (C) 2002 Daniel Stenberg - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY - * KIND, either express or implied. - * - ****************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include "config.h" -#include "system.h" -#include "ata_idle_notify.h" -#include "mv.h" - -#define HAVE_STATVFS (!defined(WIN32)) -#define HAVE_LSTAT (!defined(WIN32)) - -#if HAVE_STATVFS -#include -#endif - -#ifdef WIN32 -#include -#endif - -#ifndef _MSC_VER -#include -#include -#else -#include "dir-win32.h" -#endif - -#include -#ifdef HAVE_SDL_THREADS -#include "thread-sdl.h" -#else -#define sim_thread_unlock() NULL -#define sim_thread_lock(a) -#endif -#include "thread.h" -#include "kernel.h" -#include "debug.h" -#include "ata.h" /* for IF_MV et al. */ -#include "rbpaths.h" -#include "load_code.h" - -/* keep this in sync with file.h! */ -#undef MAX_PATH /* this avoids problems when building simulator */ -#define MAX_PATH 260 -#define MAX_OPEN_FILES 11 - -/* Windows (and potentially other OSes) distinguish binary and text files. - * Define a dummy for the others. */ -#ifndef O_BINARY -#define O_BINARY 0 -#endif - -/* Unicode compatibility for win32 */ -#if defined __MINGW32__ -/* Rockbox unicode functions */ -extern const unsigned char* utf8decode(const unsigned char *utf8, - unsigned short *ucs); -extern unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8); - -/* Static buffers for the conversion results. This isn't thread safe, - * but it's sufficient for rockbox. */ -static unsigned char convbuf1[3*MAX_PATH]; -static unsigned char convbuf2[3*MAX_PATH]; - -static wchar_t* utf8_to_ucs2(const unsigned char *utf8, void *buffer) -{ - wchar_t *ucs = buffer; - - while (*utf8) - utf8 = utf8decode(utf8, ucs++); - - *ucs = 0; - return buffer; -} -static unsigned char *ucs2_to_utf8(const wchar_t *ucs, unsigned char *buffer) -{ - unsigned char *utf8 = buffer; - - while (*ucs) - utf8 = utf8encode(*ucs++, utf8); - - *utf8 = 0; - return buffer; -} - -#define UTF8_TO_OS(a) utf8_to_ucs2(a,convbuf1) -#define OS_TO_UTF8(a) ucs2_to_utf8(a,convbuf1) -#define DIR_T _WDIR -#define DIRENT_T struct _wdirent -#define STAT_T struct _stat -extern int _wmkdir(const wchar_t*); -extern int _wrmdir(const wchar_t*); -#define MKDIR(a,b) (_wmkdir)(UTF8_TO_OS(a)) -#define RMDIR(a) (_wrmdir)(UTF8_TO_OS(a)) -#define OPENDIR(a) (_wopendir)(UTF8_TO_OS(a)) -#define READDIR(a) (_wreaddir)(a) -#define CLOSEDIR(a) (_wclosedir)(a) -#define STAT(a,b) (_wstat)(UTF8_TO_OS(a),b) -/* empty variable parameter list doesn't work for variadic macros, - * so pretend the second parameter is variable too */ -#define OPEN(a,...) (_wopen)(UTF8_TO_OS(a), __VA_ARGS__) -#define CLOSE(a) (close)(a) -#define REMOVE(a) (_wremove)(UTF8_TO_OS(a)) -#define RENAME(a,b) (_wrename)(UTF8_TO_OS(a),utf8_to_ucs2(b,convbuf2)) -/* readlink isn't used in the sim yet (FIXME) */ -#define READLINK(a,b,c) ({ fprintf(stderr, "no readlink on windows yet"); abort(); }) -#else /* !__MINGW32__ */ - -#define UTF8_TO_OS(a) (a) -#define OS_TO_UTF8(a) (a) -#define DIR_T DIR -#define DIRENT_T struct dirent -#define STAT_T struct stat -#define MKDIR(a,b) (mkdir)(a,b) -#define RMDIR(a) (rmdir)(a) -#define OPENDIR(a) (opendir)(a) -#define READDIR(a) (readdir)(a) -#define CLOSEDIR(a) (closedir)(a) -#define STAT(a,b) (stat)(a,b) -/* empty variable parameter list doesn't work for variadic macros, - * so pretend the second parameter is variable too */ -#define OPEN(a, ...) (open)(a, __VA_ARGS__) -#define CLOSE(x) (close)(x) -#define REMOVE(a) (remove)(a) -#define RENAME(a,b) (rename)(a,b) -#define READLINK(a,b,c) (readlink)(a,b,c) - -#endif /* !__MINGW32__ */ - - -#ifdef HAVE_DIRCACHE -int dircache_get_entry_id(const char *filename); -void dircache_add_file(const char *name, long startcluster); -void dircache_remove(const char *name); -void dircache_rename(const char *oldname, const char *newname); -#endif - -#ifndef APPLICATION - -#define SIMULATOR_DEFAULT_ROOT "simdisk" -extern const char *sim_root_dir; - -static int num_openfiles = 0; - -/* from dir.h */ -struct dirinfo { - int attribute; - long size; - unsigned short wrtdate; - unsigned short wrttime; -}; - -struct sim_dirent { - unsigned char d_name[MAX_PATH]; - struct dirinfo info; - long startcluster; -}; - -struct dirstruct { - void *dir; /* actually a DIR* dir */ - char *name; -} SIM_DIR; - -struct mydir { - DIR_T *dir; - IF_MV(int volumes_returned); - char *name; -}; - -typedef struct mydir MYDIR; - -static unsigned int rockbox2sim(int opt) -{ -#if 0 -/* this shouldn't be needed since we use the host's versions */ - int newopt = O_BINARY; - - if(opt & 1) - newopt |= O_WRONLY; - if(opt & 2) - newopt |= O_RDWR; - if(opt & 4) - newopt |= O_CREAT; - if(opt & 8) - newopt |= O_APPEND; - if(opt & 0x10) - newopt |= O_TRUNC; - - return newopt; -#else - return opt|O_BINARY; -#endif -} - -#endif /* APPLICATION */ - -/** Simulator I/O engine routines **/ -#define IO_YIELD_THRESHOLD 512 - -enum io_dir -{ - IO_READ, - IO_WRITE, -}; - -struct sim_io -{ - struct mutex sim_mutex; /* Rockbox mutex */ - int cmd; /* The command to perform */ - int ready; /* I/O ready flag - 1= ready */ - int fd; /* The file to read/write */ - void *buf; /* The buffer to read/write */ - size_t count; /* Number of bytes to read/write */ - size_t accum; /* Acculated bytes transferred */ -}; - -static struct sim_io io; - -int ata_init(void) -{ - /* Initialize the rockbox kernel objects on a rockbox thread */ - mutex_init(&io.sim_mutex); - io.accum = 0; - return 1; -} - -int ata_spinup_time(void) -{ - return HZ; -} - -static ssize_t io_trigger_and_wait(enum io_dir cmd) -{ - void *mythread = NULL; - ssize_t result; - - if (io.count > IO_YIELD_THRESHOLD || - (io.accum += io.count) >= IO_YIELD_THRESHOLD) - { - /* Allow other rockbox threads to run */ - io.accum = 0; - mythread = sim_thread_unlock(); - } - - switch (cmd) - { - case IO_READ: - result = read(io.fd, io.buf, io.count); - break; - case IO_WRITE: - result = write(io.fd, io.buf, io.count); - break; - /* shut up gcc */ - default: - result = -1; - } - - call_storage_idle_notifys(false); - - /* Regain our status as current */ - if (mythread != NULL) - { - sim_thread_lock(mythread); - } - - return result; -} - - -ssize_t sim_read(int fd, void *buf, size_t count) -{ - ssize_t result; - - mutex_lock(&io.sim_mutex); - - /* Setup parameters */ - io.fd = fd; - io.buf = buf; - io.count = count; - - result = io_trigger_and_wait(IO_READ); - - mutex_unlock(&io.sim_mutex); - - return result; -} - - -ssize_t sim_write(int fd, const void *buf, size_t count) -{ - ssize_t result; - - mutex_lock(&io.sim_mutex); - - io.fd = fd; - io.buf = (void*)buf; - io.count = count; - - result = io_trigger_and_wait(IO_WRITE); - - mutex_unlock(&io.sim_mutex); - - return result; -} - -#if !defined(APPLICATION) - -static const char *handle_special_links(const char* link) -{ -#ifdef HAVE_MULTIDRIVE - static char buffer[MAX_PATH]; /* sufficiently big */ - char vol_string[VOL_ENUM_POS + 8]; - int len = sprintf(vol_string, VOL_NAMES, 1); - - /* link might be passed with or without HOME_DIR expanded. To handle - * both perform substring matching (VOL_NAMES is unique enough) */ - const char *begin = strstr(link, vol_string); - if (begin) - { - /* begin now points to the start of vol_string within link, - * we want to copy the remainder of the paths, prefixed by - * the actual mount point (the remainder might be "") */ - snprintf(buffer, sizeof(buffer), "%s/../simext/%s", - sim_root_dir ?: SIMULATOR_DEFAULT_ROOT, begin + len); - return buffer; - } - else -#endif - return link; -} - - -static const char *get_sim_pathname(const char *name) -{ - static char buffer[MAX_PATH]; /* sufficiently big */ - - if(name[0] == '/') - { - snprintf(buffer, sizeof(buffer), "%s%s", - sim_root_dir ?: SIMULATOR_DEFAULT_ROOT, name); - return handle_special_links(buffer); - } - fprintf(stderr, "WARNING, bad file name lacks slash: %s\n", name); - return name; -} - - -MYDIR *sim_opendir(const char *name) -{ - DIR_T *dir; - dir = (DIR_T *) OPENDIR(get_sim_pathname(name)); - - if (dir) - { - MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR)); - my->dir = dir; - my->name = (char *)malloc(strlen(name)+1); - strcpy(my->name, name); - IF_MV(my->volumes_returned = 0); - - return my; - } - /* failed open, return NULL */ - return (MYDIR *)0; -} - -#if defined(WIN32) -static inline struct tm* localtime_r (const time_t *clock, struct tm *result) { - if (!clock || !result) return NULL; - memcpy(result,localtime(clock),sizeof(*result)); - return result; -} -#endif - -struct sim_dirent *sim_readdir(MYDIR *dir) -{ - char buffer[MAX_PATH]; /* sufficiently big */ - static struct sim_dirent secret; - STAT_T s; - struct tm tm; - DIRENT_T *x11; - -#ifdef EOVERFLOW -read_next: -#endif - -#define ATTR_LINK 0x80 /* see dir.h */ - - secret.info.attribute = 0; -#ifdef HAVE_MULTIVOLUME - if (dir->name[0] == '/' && dir->name[1] == '\0' - && dir->volumes_returned++ < (NUM_VOLUMES-1) - && volume_present(dir->volumes_returned)) - { - sprintf((char *)secret.d_name, VOL_NAMES, dir->volumes_returned); - secret.info.attribute = ATTR_LINK; - /* build file name for stat() which is the actual mount point */ - snprintf(buffer, sizeof(buffer), "%s/../simext", - sim_root_dir ?: SIMULATOR_DEFAULT_ROOT); - } - else -#endif - { - x11 = READDIR(dir->dir); - - if(!x11) - return (struct sim_dirent *)0; - - strcpy((char *)secret.d_name, OS_TO_UTF8(x11->d_name)); - /* build file name for stat() */ - snprintf(buffer, sizeof(buffer), "%s/%s", - get_sim_pathname(dir->name), secret.d_name); - } - - if (STAT(buffer, &s)) /* get info */ - { -#ifdef EOVERFLOW - /* File size larger than 2 GB? */ - if (errno == EOVERFLOW) - { - DEBUGF("stat() overflow for %s. Skipping\n", buffer); - goto read_next; - } -#endif - - return NULL; - } - -#define ATTR_DIRECTORY 0x10 - - if (S_ISDIR(s.st_mode)) - secret.info.attribute = ATTR_DIRECTORY; - - secret.info.size = s.st_size; - - if (localtime_r(&(s.st_mtime), &tm) == NULL) - return NULL; - secret.info.wrtdate = ((tm.tm_year - 80) << 9) | - ((tm.tm_mon + 1) << 5) | - tm.tm_mday; - secret.info.wrttime = (tm.tm_hour << 11) | - (tm.tm_min << 5) | - (tm.tm_sec >> 1); - -#if HAVE_LSTAT - if (!lstat(buffer, &s) && S_ISLNK(s.st_mode)) - { - secret.info.attribute |= ATTR_LINK; - } -#endif - - return &secret; -} - -void sim_closedir(MYDIR *dir) -{ - free(dir->name); - CLOSEDIR(dir->dir); - - free(dir); -} - -int sim_open(const char *name, int o, ...) -{ - int opts = rockbox2sim(o); - int ret; - if (num_openfiles >= MAX_OPEN_FILES) - return -2; - - if (opts & O_CREAT) - { - va_list ap; - va_start(ap, o); - mode_t mode = va_arg(ap, unsigned int); - ret = OPEN(get_sim_pathname(name), opts, mode); -#ifdef HAVE_DIRCACHE - if (ret >= 0 && (dircache_get_entry_id(name) < 0)) - dircache_add_file(name, 0); -#endif - va_end(ap); - } - else - ret = OPEN(get_sim_pathname(name), opts); - - if (ret >= 0) - num_openfiles++; - return ret; -} - -int sim_close(int fd) -{ - int ret; - ret = CLOSE(fd); - if (ret == 0) - num_openfiles--; - return ret; -} - -int sim_creat(const char *name, mode_t mode) -{ - int ret = OPEN(get_sim_pathname(name), - O_BINARY | O_WRONLY | O_CREAT | O_TRUNC, mode); -#ifdef HAVE_DIRCACHE - if (ret >= 0 && (dircache_get_entry_id(name) < 0)) - dircache_add_file(name, 0); -#endif - return ret; -} - -int sim_mkdir(const char *name) -{ - return MKDIR(get_sim_pathname(name), 0777); -} - -int sim_rmdir(const char *name) -{ - return RMDIR(get_sim_pathname(name)); -} - -int sim_remove(const char *name) -{ - int ret = REMOVE(get_sim_pathname(name)); -#ifdef HAVE_DIRCACHE - if (ret >= 0) - dircache_remove(name); -#endif - return ret; -} - -int sim_rename(const char *oldname, const char *newname) -{ - char sim_old[MAX_PATH]; - char sim_new[MAX_PATH]; -#ifdef HAVE_DIRCACHE - dircache_rename(oldname, newname); -#endif - // This is needed as get_sim_pathname() has a static buffer - strncpy(sim_old, get_sim_pathname(oldname), MAX_PATH); - strncpy(sim_new, get_sim_pathname(newname), MAX_PATH); - return RENAME(sim_old, sim_new); -} - -/* rockbox off_t may be different from system off_t */ -long sim_lseek(int fildes, long offset, int whence) -{ - return lseek(fildes, offset, whence); -} - -#else -#define get_sim_pathname(x) x -#endif - -long filesize(int fd) -{ -#ifdef WIN32 - return _filelength(fd); -#else - struct stat buf; - - if (!fstat(fd, &buf)) - return buf.st_size; - else - return -1; -#endif -} - -void fat_size(IF_MV(int volume,) unsigned long* size, unsigned long* free) -{ -#ifdef HAVE_MULTIVOLUME - if (volume != 0) { - /* debugf("io.c: fat_size(volume=%d); simulator only supports volume 0\n",volume); */ - - if (size) *size = 0; - if (free) *free = 0; - return; - } -#endif - -#ifdef WIN32 - long secperclus, bytespersec, free_clusters, num_clusters; - - if (GetDiskFreeSpace(NULL, &secperclus, &bytespersec, &free_clusters, - &num_clusters)) { - if (size) - *size = num_clusters * secperclus / 2 * (bytespersec / 512); - if (free) - *free = free_clusters * secperclus / 2 * (bytespersec / 512); - } else -#elif HAVE_STATVFS - struct statvfs vfs; - - if (!statvfs(".", &vfs)) { - DEBUGF("statvfs: frsize=%d blocks=%ld free=%ld\n", - (int)vfs.f_frsize, (long)vfs.f_blocks, (long)vfs.f_bfree); - if (size) - *size = vfs.f_blocks / 2 * (vfs.f_frsize / 512); - if (free) - *free = vfs.f_bfree / 2 * (vfs.f_frsize / 512); - } else -#endif - { - if (size) - *size = 0; - if (free) - *free = 0; - } -} - -int sim_fsync(int fd) -{ -#ifdef WIN32 - return _commit(fd); -#else - return fsync(fd); -#endif -} - -#ifndef __PCTOOL__ - -#include -void *lc_open(const char *filename, unsigned char *buf, size_t buf_size) -{ - (void)buf; - (void)buf_size; - void *handle = SDL_LoadObject(get_sim_pathname(filename)); - if (handle == NULL) - { - DEBUGF("failed to load %s\n", filename); - DEBUGF("lc_open(%s): %s\n", filename, SDL_GetError()); - } - return handle; -} - -void *lc_get_header(void *handle) -{ - char *ret = SDL_LoadFunction(handle, "__header"); - if (ret == NULL) - ret = SDL_LoadFunction(handle, "___header"); - - return ret; -} - -void lc_close(void *handle) -{ - SDL_UnloadObject(handle); -} - -void *lc_open_from_mem(void *addr, size_t blob_size) -{ -#ifndef SIMULATOR - (void)addr; - (void)blob_size; - /* we don't support loading code from memory on application builds, - * it doesn't make sense (since it means writing the blob to disk again and - * then falling back to load from disk) and requires the ability to write - * to an executable directory */ - return NULL; -#else - /* support it in the sim for the sake of simulating */ - int fd, i; - char temp_filename[MAX_PATH]; - - /* We have to create the dynamic link library file from ram so we - can simulate the codec loading. With voice and crossfade, - multiple codecs may be loaded at the same time, so we need - to find an unused filename */ - for (i = 0; i < 10; i++) - { - snprintf(temp_filename, sizeof(temp_filename), - ROCKBOX_DIR "/libtemp_binary_%d.dll", i); - fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700); - if (fd >= 0) - break; /* Created a file ok */ - } - - if (fd < 0) - { - DEBUGF("open failed\n"); - return NULL; - } - - if (write(fd, addr, blob_size) < (ssize_t)blob_size) - { - DEBUGF("Write failed\n"); - close(fd); - remove(temp_filename); - return NULL; - } - - close(fd); - return lc_open(temp_filename, NULL, 0); -#endif -} - -#endif /* __PCTOOL__ */ - -/* rockbox off_t may be different from system off_t */ -int sim_ftruncate(int fd, long length) -{ -#ifdef WIN32 - return _chsize(fd, length); -#else - return ftruncate(fd, length); -#endif -} diff --git a/uisimulator/common/load_code-sim.c b/uisimulator/common/load_code-sim.c new file mode 100644 index 0000000000..59ca97a259 --- /dev/null +++ b/uisimulator/common/load_code-sim.c @@ -0,0 +1,72 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2002 Daniel Stenberg + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#define RB_FILESYSTEM_OS +#include +#include "config.h" +#include "system.h" +#include "file.h" +#include "load_code.h" +#include "rbpaths.h" +#include "debug.h" + +void * lc_open(const char *filename, unsigned char *buf, size_t buf_size) +{ + char osfilename[SIM_TMPBUF_MAX_PATH]; + if (sim_get_os_path(osfilename, filename, sizeof (osfilename)) < 0) + return NULL; + + return os_lc_open(osfilename); + (void)buf; (void)buf_size; +} + +void * lc_open_from_mem(void *addr, size_t blob_size) +{ + /* We have to create the dynamic link library file from ram so we can + simulate code loading. Multiple binaries may be loaded at the same + time, so we need to find an unused filename. */ + int fd; + char tempfile[SIM_TMPBUF_MAX_PATH]; + for (unsigned int i = 0; i < 10; i++) + { + snprintf(tempfile, sizeof(tempfile), + ROCKBOX_DIR "/libtemp_binary_%d.dll", i); + fd = sim_open(tempfile, O_WRONLY|O_CREAT|O_TRUNC, 0700); + if (fd >= 0) + break; /* Created a file ok */ + } + + if (fd < 0) + { + DEBUGF("open failed\n"); + return NULL; + } + + if (sim_write(fd, addr, blob_size) != (ssize_t)blob_size) + { + DEBUGF("Write failed\n"); + sim_close(fd); + sim_remove(tempfile); + return NULL; + } + + sim_close(fd); + return lc_open(tempfile, NULL, 0); +} diff --git a/uisimulator/common/sim_tasks.c b/uisimulator/common/sim_tasks.c index 1299a69302..003b993740 100644 --- a/uisimulator/common/sim_tasks.c +++ b/uisimulator/common/sim_tasks.c @@ -28,6 +28,11 @@ #include "thread.h" #include "debug.h" #include "usb.h" +#include "mv.h" +#include "ata_idle_notify.h" +#ifdef WIN32 +#include +#endif static void sim_thread(void); static long sim_thread_stack[DEFAULT_STACK_SIZE/sizeof(long)]; @@ -46,6 +51,10 @@ enum { #endif }; +#ifdef HAVE_MULTIDRIVE +extern void sim_ext_extracted(int drive); +#endif + void sim_thread(void) { struct queue_event ev; @@ -54,9 +63,13 @@ void sim_thread(void) while (1) { - queue_wait(&sim_queue, &ev); + queue_wait_w_tmo(&sim_queue, &ev, 5*HZ); switch(ev.id) { + case SYS_TIMEOUT: + call_storage_idle_notifys(false); + break; + case SIM_SCREENDUMP: screen_dump(); #ifdef HAVE_REMOTE_LCD @@ -102,6 +115,7 @@ void sim_thread(void) #ifdef HAVE_MULTIDRIVE case SIM_EXT_INSERTED: case SIM_EXT_EXTRACTED: + sim_ext_extracted(ev.data); queue_broadcast(ev.id == SIM_EXT_INSERTED ? SYS_HOTSWAP_INSERTED : SYS_HOTSWAP_EXTRACTED, 0); sleep(HZ/20); @@ -174,11 +188,13 @@ static bool is_ext_inserted; void sim_trigger_external(bool inserted) { + is_ext_inserted = inserted; + + int drive = 1; /* Can do others! */ if (inserted) - queue_post(&sim_queue, SIM_EXT_INSERTED, 0); + queue_post(&sim_queue, SIM_EXT_INSERTED, drive); else - queue_post(&sim_queue, SIM_EXT_EXTRACTED, 0); - is_ext_inserted = inserted; + queue_post(&sim_queue, SIM_EXT_EXTRACTED, drive); } bool hostfs_present(int drive) @@ -203,7 +219,13 @@ bool volume_present(int volume) /* volume == drive for now */ return hostfs_present(volume); } -#endif + +int volume_drive(int volume) +{ + /* volume == drive for now */ + return volume; +} +#endif /* HAVE_MULTIVOLUME */ #if (CONFIG_STORAGE & STORAGE_MMC) bool mmc_touched(void) @@ -212,4 +234,4 @@ bool mmc_touched(void) } #endif -#endif +#endif /* CONFIG_STORAGE & STORAGE_MMC */ diff --git a/uisimulator/common/stubs.c b/uisimulator/common/stubs.c index 18f60ce6b3..922b9e8da0 100644 --- a/uisimulator/common/stubs.c +++ b/uisimulator/common/stubs.c @@ -153,10 +153,9 @@ int fat_startsector(void) return 63; } -bool fat_ismounted(int volume) +int ata_spinup_time(void) { - (void)volume; - return true; + return 100; } int storage_spinup_time(void) diff --git a/uisimulator/common/time-win32.c b/uisimulator/common/time-win32.c new file mode 100644 index 0000000000..1de69c35e6 --- /dev/null +++ b/uisimulator/common/time-win32.c @@ -0,0 +1,61 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2014 by Michael Sevakis + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include +#include "system.h" +#include "debug.h" + +/* These functions we like but Windows doesn't because it implements the + non-"_r" versions with thread-local storage in its multithreaded libs */ + +struct tm * localtime_r(const time_t *restrict timer, + struct tm *restrict result) +{ + if (!timer || !result) + { + errno = EINVAL; + return NULL; + } + + struct tm *tm = localtime(timer); + if (!tm) + return NULL; + + *result = *tm; + return result; +} + +struct tm * gmtime_r(const time_t *restrict timer, + struct tm *restrict result) +{ + if (!timer || !result) + { + errno = EINVAL; + return NULL; + } + + struct tm *tm = gmtime(timer); + if (!tm) + return NULL; + + *result = *tm; + return result; +} -- cgit