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 --- firmware/common/file.c | 1637 +++++++++++++++++++++++++++++------------------- 1 file changed, 1010 insertions(+), 627 deletions(-) (limited to 'firmware/common/file.c') diff --git a/firmware/common/file.c b/firmware/common/file.c index 920eada84e..7d3b5092ae 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c @@ -8,6 +8,7 @@ * $Id$ * * Copyright (C) 2002 by Björn Stenberg + * 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 @@ -18,819 +19,1201 @@ * KIND, either express or implied. * ****************************************************************************/ +#define RB_FILESYSTEM_OS +#include "config.h" +#include "system.h" #include #include -#include -#include "file.h" -#include "fat.h" -#include "dir_uncached.h" #include "debug.h" -#include "dircache.h" -#include "filefuncs.h" -#include "system.h" +#include "file.h" +#include "fileobj_mgr.h" +#include "disk_cache.h" +#include "dircache_redirect.h" +#include "string-extra.h" + +/** + * These functions provide a roughly POSIX-compatible file I/O API. + */ + +/* structure used for open file descriptors */ +static struct filestr_desc +{ + struct filestr_base stream; /* basic stream info (first!) */ + file_size_t offset; /* current offset for stream */ + file_size_t *sizep; /* shortcut to file size in fileobj */ +} open_streams[MAX_OPEN_FILES]; -/* - These functions provide a roughly POSIX-compatible file IO API. +/* check and return a struct filestr_desc* from a file descriptor number */ +static struct filestr_desc * get_filestr(int fildes) +{ + struct filestr_desc *file = &open_streams[fildes]; - Since the fat32 driver only manages sectors, we maintain a one-sector - cache for each open file. This way we can provide byte access without - having to re-read the sector each time. - The penalty is the RAM used for the cache and slightly more complex code. -*/ + if ((unsigned int)fildes >= MAX_OPEN_FILES) + file = NULL; + else if (file->stream.flags & FDO_BUSY) + return file; -struct filedesc { - unsigned char cache[SECTOR_SIZE] CACHEALIGN_ATTR; - int cacheoffset; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */ - long fileoffset; - long size; - int attr; - struct fat_file fatfile; - bool busy; - bool write; - bool dirty; - bool trunc; -} CACHEALIGN_ATTR; + DEBUGF("fildes %d: bad file number\n", fildes); + errno = (file && file->stream.flags == FV_NONEXIST) ? ENXIO : EBADF; + return NULL; +} -static struct filedesc openfiles[MAX_OPEN_FILES] CACHEALIGN_ATTR; +#define GET_FILESTR(type, fildes) \ + ({ \ + file_internal_lock_##type(); \ + struct filestr_desc * _file = get_filestr(fildes); \ + if (_file) \ + FILESTR_LOCK(type, &_file->stream); \ + else \ + file_internal_unlock_##type(); \ + _file; \ + }) + +/* release the lock on the filestr_desc* */ +#define RELEASE_FILESTR(type, file) \ + ({ \ + FILESTR_UNLOCK(type, &(file)->stream); \ + file_internal_unlock_##type(); \ + }) + +/* find a free file descriptor */ +static int alloc_filestr(struct filestr_desc **filep) +{ + for (int fildes = 0; fildes < MAX_OPEN_FILES; fildes++) + { + struct filestr_desc *file = &open_streams[fildes]; + if (!file->stream.flags) + { + *filep = file; + return fildes; + } + } -static int flush_cache(int fd); + DEBUGF("Too many files open\n"); + return -1; +} -int file_creat(const char *pathname) +/* return the file size in sectors */ +static inline unsigned long filesize_sectors(file_size_t size) { - return open(pathname, O_WRONLY|O_CREAT|O_TRUNC, 0666); + /* overflow proof whereas "(x + y - 1) / y" is not */ + unsigned long numsectors = size / SECTOR_SIZE; + + if (size % SECTOR_SIZE) + numsectors++; + + return numsectors; } -static int open_internal(const char* pathname, int flags, bool use_cache) +/* flush a dirty cache buffer */ +static int flush_cache(struct filestr_desc *file) { - DIR_UNCACHED* dir; - struct dirent_uncached* entry; - int fd; - int pathnamesize = strlen(pathname) + 1; - char pathnamecopy[pathnamesize]; - char* name; - struct filedesc* file = NULL; int rc; -#ifndef HAVE_DIRCACHE - (void)use_cache; -#endif + struct filestr_cache *cachep = file->stream.cachep; - LDEBUGF("open(\"%s\",%d)\n",pathname,flags); + DEBUGF("Flushing dirty sector cache (%lu)\n", cachep->sector); - if ( pathname[0] != '/' ) { - DEBUGF("'%s' is not an absolute path.\n",pathname); - DEBUGF("Only absolute pathnames supported at the moment\n"); - errno = EINVAL; - return -1; + if (fat_query_sectornum(&file->stream.fatstr) != cachep->sector) + { + /* get on the correct sector */ + rc = fat_seek(&file->stream.fatstr, cachep->sector); + if (rc < 0) + FILE_ERROR(EIO, rc * 10 - 1); } - /* find a free file descriptor */ - for ( fd=0; fdstream.fatstr, 1, cachep->buffer, true); + if (rc < 0) + { + if (rc == FAT_RC_ENOSPC) + FILE_ERROR(ENOSPC, RC); + else + FILE_ERROR(EIO, rc * 10 - 2); } - file = &openfiles[fd]; - memset(file, 0, sizeof(struct filedesc)); + cachep->flags = 0; + return 1; +file_error: + DEBUGF("Failed flushing cache: %d\n", rc); + return rc; +} + +static void discard_cache(struct filestr_desc *file) +{ + struct filestr_cache *const cachep = file->stream.cachep; + cachep->flags = 0; +} - if (flags & (O_RDWR | O_WRONLY)) { - file->write = true; +/* set the file pointer */ +static off_t lseek_internal(struct filestr_desc *file, off_t offset, + int whence) +{ + off_t rc; + file_size_t pos; - if (flags & O_TRUNC) - file->trunc = true; - } - file->busy = true; + file_size_t size = MIN(*file->sizep, FILE_SIZE_MAX); -#ifdef HAVE_DIRCACHE - if (dircache_is_enabled() && !file->write && use_cache) + switch (whence) { -# ifdef HAVE_MULTIVOLUME - int volume = strip_volume(pathname, pathnamecopy); -# endif + case SEEK_SET: + if (offset < 0 || (file_size_t)offset > size) + FILE_ERROR(EINVAL, -1); - int ce = dircache_get_entry_id(pathname); - if (ce < 0) - { - errno = ENOENT; - file->busy = false; - return -7; - } + pos = offset; + break; - long startcluster = _dircache_get_entry_startcluster(ce); - fat_open(IF_MV(volume,) - startcluster, - &(file->fatfile), - NULL); - struct dirinfo *info = _dircache_get_entry_dirinfo(ce); - file->size = info->size; - file->attr = info->attribute; - file->cacheoffset = -1; - file->fileoffset = 0; + case SEEK_CUR: + if ((offset < 0 && (file_size_t)-offset > file->offset) || + (offset > 0 && (file_size_t)offset > size - file->offset)) + FILE_ERROR(EINVAL, -1); - return fd; - } -#endif + pos = file->offset + offset; + break; - strlcpy(pathnamecopy, pathname, pathnamesize); + case SEEK_END: + if (offset > 0 || (file_size_t)-offset > size) + FILE_ERROR(EINVAL, -1); - /* locate filename */ - name=strrchr(pathnamecopy+1,'/'); - if ( name ) { - *name = 0; - dir = opendir_uncached(pathnamecopy); - *name = '/'; - name++; - } - else { - dir = opendir_uncached("/"); - name = pathnamecopy+1; - } - if (!dir) { - DEBUGF("Failed opening dir\n"); - errno = EIO; - file->busy = false; - return -4; - } - - if(name[0] == 0) { - DEBUGF("Empty file name\n"); - errno = EINVAL; - file->busy = false; - closedir_uncached(dir); - return -5; - } - - /* scan dir for name */ - while ((entry = readdir_uncached(dir))) { - if ( !strcasecmp(name, entry->d_name) ) { - fat_open(IF_MV(dir->fatdir.file.volume,) - entry->startcluster, - &(file->fatfile), - &(dir->fatdir)); - file->size = file->trunc ? 0 : entry->info.size; - file->attr = entry->info.attribute; - break; - } + pos = size + offset; + break; + + default: + FILE_ERROR(EINVAL, -1); } - if ( !entry ) { - LDEBUGF("Didn't find file %s\n",name); - if ( file->write && (flags & O_CREAT) ) { - rc = fat_create_file(name, - &(file->fatfile), - &(dir->fatdir)); - if (rc < 0) { - DEBUGF("Couldn't create %s in %s\n",name,pathnamecopy); - errno = EIO; - file->busy = false; - closedir_uncached(dir); - return rc * 10 - 6; + file->offset = pos; + + return pos; +file_error: + return rc; +} + +/* callback for each file stream to make sure all data is in sync with new + size */ +void ftruncate_internal_callback(struct filestr_base *stream, + struct filestr_base *s) +{ + struct filestr_desc *file = (struct filestr_desc *)s; + file_size_t size = *file->sizep; + + /* caches with data beyond new extents are invalid */ + unsigned long sector = file->stream.cachep->sector; + if (sector != INVALID_SECNUM && sector >= filesize_sectors(size)) + filestr_discard_cache(&file->stream); + + /* keep all positions within bounds */ + if (file->offset > size) + file->offset = size; + + (void)stream; +} + +/* truncate the file to the specified length */ +static int ftruncate_internal(struct filestr_desc *file, file_size_t size, + bool write_now) +{ + int rc = 0, rc2 = 1; + + file_size_t cursize = *file->sizep; + file_size_t truncsize = MIN(size, cursize); + + if (write_now) + { + unsigned long sector = filesize_sectors(truncsize); + struct filestr_cache *const cachep = file->stream.cachep; + + if (cachep->flags == (FSC_NEW|FSC_DIRTY) && + cachep->sector + 1 == sector) + { + /* sector created but may have never been added to the cluster + chain; flush it now or the subsequent may fail */ + rc2 = flush_cache(file); + if (rc2 == FAT_RC_ENOSPC) + { + /* no space left on device; further truncation needed */ + discard_cache(file); + truncsize = ALIGN_DOWN(truncsize - 1, SECTOR_SIZE); + sector--; + rc = rc2; } -#ifdef HAVE_DIRCACHE - dircache_add_file(pathname, file->fatfile.firstcluster); -#endif - file->size = 0; - file->attr = 0; - } - else { - DEBUGF("Couldn't find %s in %s\n",name,pathnamecopy); - errno = ENOENT; - file->busy = false; - closedir_uncached(dir); - return -7; - } - } else { - if(file->write && (file->attr & FAT_ATTR_DIRECTORY)) { - errno = EISDIR; - file->busy = false; - closedir_uncached(dir); - return -8; + else if (rc2 < 0) + FILE_ERROR(ERRNO, rc2 * 10 - 1); } - } - closedir_uncached(dir); - file->cacheoffset = -1; - file->fileoffset = 0; + rc2 = fat_seek(&file->stream.fatstr, sector); + if (rc2 < 0) + FILE_ERROR(EIO, rc2 * 10 - 2); - if (file->write && (flags & O_APPEND)) { - rc = lseek(fd,0,SEEK_END); - if (rc < 0 ) - return rc * 10 - 9; + rc2 = fat_truncate(&file->stream.fatstr); + if (rc2 < 0) + FILE_ERROR(EIO, rc2 * 10 - 3); } + /* else just change the cached file size */ -#ifdef HAVE_DIRCACHE - if (file->write) - dircache_bind(fd, pathname); -#endif + if (truncsize < cursize) + { + *file->sizep = truncsize; + fileop_ontruncate_internal(&file->stream); + } - return fd; -} + /* if truncation was partially successful, it effectively destroyed + everything after the truncation point; still, indicate failure + after adjusting size */ + if (rc2 == 0) + FILE_ERROR(EIO, -4); + else if (rc2 < 0) + FILE_ERROR(ERRNO, rc2); -int file_open(const char* pathname, int flags) -{ - /* By default, use the dircache if available. */ - return open_internal(pathname, flags, true); +file_error: + return rc; } -int close(int fd) +/* flush back all outstanding writes to the file */ +static int fsync_internal(struct filestr_desc *file) { - struct filedesc* file = &openfiles[fd]; + /* call only when holding WRITER lock (updates directory entries) */ int rc = 0; - LDEBUGF("close(%d)\n", fd); + file_size_t size = *file->sizep; + unsigned int foflags = fileobj_get_flags(&file->stream); + + /* flush sector cache? */ + struct filestr_cache *const cachep = file->stream.cachep; + if (cachep->flags & FSC_DIRTY) + { + int rc2 = flush_cache(file); + if (rc2 == FAT_RC_ENOSPC && (cachep->flags & FSC_NEW)) + { + /* no space left on device so this must be dropped */ + discard_cache(file); + size = ALIGN_DOWN(size - 1, SECTOR_SIZE); + foflags |= FO_TRUNC; + rc = rc2; + } + else if (rc2 < 0) + FILE_ERROR(ERRNO, rc2 * 10 - 1); + } + + /* truncate? */ + if (foflags & FO_TRUNC) + { + int rc2 = ftruncate_internal(file, size, rc == 0); + if (rc2 < 0) + FILE_ERROR(ERRNO, rc2 * 10 - 2); - if (fd < 0 || fd > MAX_OPEN_FILES-1) { - errno = EINVAL; - return -1; + /* never needs to be done this way again since any data beyond the + cached size is now gone */ + fileobj_change_flags(&file->stream, 0, FO_TRUNC); } - if (!file->busy) { - errno = EBADF; - return -2; + +file_error:; + /* tie up all loose ends (try to close the file even if failing) */ + int rc2 = fat_closewrite(&file->stream.fatstr, size, + get_dir_fatent_dircache()); + if (rc2 >= 0) + fileop_onsync_internal(&file->stream); /* dir_fatent is implicit arg */ + + if (rc2 < 0 && rc >= 0) + { + errno = EIO; + rc = rc2 * 10 - 3; } - if (file->write) { - rc = fsync(fd); + + return rc; +} + +/* finish with the file and free resources */ +static int close_internal(struct filestr_desc *file) +{ + /* call only when holding WRITER lock (updates directory entries) */ + int rc; + + if ((file->stream.flags & FD_WRITE) && + !(fileobj_get_flags(&file->stream) & FO_REMOVED)) + { + rc = fsync_internal(file); if (rc < 0) - return rc * 10 - 3; -#ifdef HAVE_DIRCACHE - dircache_update_filesize(fd, file->size, file->fatfile.firstcluster); - dircache_update_filetime(fd); -#endif + FILE_ERROR(ERRNO, rc * 10 - 1); } - file->busy = false; - return 0; + rc = 0; +file_error:; + int rc2 = close_stream_internal(&file->stream); + if (rc2 < 0 && rc >= 0) + rc = rc2 * 10 - 2; + return rc; } -int fsync(int fd) +/* actually do the open gruntwork */ +static int open_internal_inner2(const char *path, + struct filestr_desc *file, + unsigned int callflags) { - struct filedesc* file = &openfiles[fd]; - int rc = 0; - - LDEBUGF("fsync(%d)\n", fd); + int rc; - if (fd < 0 || fd > MAX_OPEN_FILES-1) { - errno = EINVAL; - return -1; - } - if (!file->busy) { - errno = EBADF; - return -2; + struct path_component_info compinfo; + rc = open_stream_internal(path, callflags, &file->stream, &compinfo); + if (rc < 0) + { + DEBUGF("Open failed: %d\n", rc); + FILE_ERROR_RETURN(ERRNO, rc * 10 - 1); } - if (file->write) { - /* flush sector cache */ - if ( file->dirty ) { - rc = flush_cache(fd); - if (rc < 0) - { - /* when failing, try to close the file anyway */ - fat_closewrite(&(file->fatfile), file->size, file->attr); - return rc * 10 - 3; - } + + bool created = false; + + if (rc > 0) + { + if (callflags & FF_EXCL) + { + DEBUGF("File exists\n"); + FILE_ERROR(EEXIST, -2); } - /* truncate? */ - if (file->trunc) { - rc = ftruncate(fd, file->size); - if (rc < 0) + if (compinfo.attr & ATTR_DIRECTORY) + { + if ((callflags & FD_WRITE) || !(callflags & FF_ANYTYPE)) { - /* when failing, try to close the file anyway */ - fat_closewrite(&(file->fatfile), file->size, file->attr); - return rc * 10 - 4; + DEBUGF("File is a directory\n"); + FILE_ERROR(EISDIR, -3); } + + compinfo.filesize = MAX_DIRECTORY_SIZE; /* allow file ops */ + } + } + else if (callflags & FF_CREAT) + { + if (compinfo.attr & ATTR_DIRECTORY) + { + DEBUGF("File is a directory\n"); + FILE_ERROR(EISDIR, -5); } - /* tie up all loose ends */ - rc = fat_closewrite(&(file->fatfile), file->size, file->attr); + /* not found; try to create it */ + + callflags &= ~FO_TRUNC; + rc = create_stream_internal(&compinfo.parentinfo, compinfo.name, + compinfo.length, ATTR_NEW_FILE, callflags, + &file->stream); if (rc < 0) - return rc * 10 - 5; - } - return 0; -} + FILE_ERROR(ERRNO, rc * 10 - 6); -int remove(const char* name) -{ - int rc; - struct filedesc* file; - /* Can't use dircache now, because we need to access the fat structures. */ - int fd = open_internal(name, O_WRONLY, false); - if ( fd < 0 ) - return fd * 10 - 1; - - file = &openfiles[fd]; -#ifdef HAVE_DIRCACHE - dircache_remove(name); -#endif - rc = fat_remove(&(file->fatfile)); - if ( rc < 0 ) { - DEBUGF("Failed removing file: %d\n", rc); - errno = EIO; - return rc * 10 - 3; + created = true; } + else + { + DEBUGF("File not found\n"); + FILE_ERROR(ENOENT, -7); + } + + fat_rewind(&file->stream.fatstr); + file->sizep = fileobj_get_sizep(&file->stream); + file->offset = 0; + + if (!created) + { + /* size from storage applies to first stream only otherwise it's + already up to date */ + const bool first = fileobj_get_flags(&file->stream) & FO_SINGLE; + if (first) + *file->sizep = compinfo.filesize; - file->size = 0; + if (callflags & FO_TRUNC) + { + /* if the file is kind of "big" then free some space now */ + rc = ftruncate_internal(file, 0, *file->sizep >= O_TRUNC_THRESH); + if (rc < 0) + { + DEBUGF("O_TRUNC failed: %d\n", rc); + FILE_ERROR(ERRNO, rc * 10 - 4); + } + } + } - rc = close(fd); - if (rc<0) - return rc * 10 - 4; + rc = 0; +file_error: + if (rc < 0) + close_stream_internal(&file->stream); - return 0; + return rc; } -int rename(const char* path, const char* newpath) +/* allocate a file descriptor, if needed, assemble stream flags and open + a new stream */ +static int open_internal_inner1(const char *path, int oflag, + unsigned int callflags) { - int rc, fd; - DIR_UNCACHED* dir; - char* nameptr; - char* dirptr; - struct filedesc* file; - char newpath2[MAX_PATH]; + DEBUGF("%s(path=\"%s\",oflag=%X,callflags=%X)\n", __func__, + path, oflag, callflags); - /* verify new path does not already exist */ - /* If it is a directory, errno == EISDIR if the name exists */ - fd = open(newpath, O_RDONLY); - if ( fd >= 0 || errno == EISDIR) { - close(fd); - errno = EBUSY; - return -1; - } - close(fd); + int rc; - fd = open_internal(path, O_RDONLY, false); - if ( fd < 0 ) { - errno = EIO; - return fd * 10 - 2; - } + struct filestr_desc *file; + int fildes = alloc_filestr(&file); + if (fildes < 0) + FILE_ERROR(EMFILE, -1); - /* extract new file name */ - nameptr = strrchr(newpath,'/'); - if (nameptr) - nameptr++; - else { - close(fd); - return - 3; - } + callflags &= ~FDO_MASK; - /* Extract new path */ - strcpy(newpath2, newpath); + if (oflag & O_ACCMODE) + { + callflags |= FD_WRITE; - dirptr = strrchr(newpath2,'/'); - if(dirptr) - *dirptr = 0; - else { - close(fd); - return - 4; - } + if ((oflag & O_ACCMODE) == O_WRONLY) + callflags |= FD_WRONLY; - dirptr = newpath2; + if (oflag & O_APPEND) + callflags |= FD_APPEND; - if(strlen(dirptr) == 0) { - dirptr = "/"; + if (oflag & O_TRUNC) + callflags |= FO_TRUNC; } - - dir = opendir_uncached(dirptr); - if(!dir) { - close(fd); - return - 5; + else if (oflag & O_TRUNC) + { + /* O_TRUNC requires write mode */ + DEBUGF("No write mode but have O_TRUNC\n"); + FILE_ERROR(EINVAL, -2); } - file = &openfiles[fd]; + /* O_CREAT and O_APPEND are fine without write mode + * for the former, an empty file is created but no data may be written + * for the latter, no append will be allowed anyway */ + if (oflag & O_CREAT) + { + callflags |= FF_CREAT; - rc = fat_rename(&file->fatfile, &dir->fatdir, nameptr, - file->size, file->attr); -#ifdef HAVE_MULTIVOLUME - if ( rc == -1) { - close(fd); - closedir_uncached(dir); - DEBUGF("Failed renaming file across volumnes: %d\n", rc); - errno = EXDEV; - return -6; - } -#endif - if ( rc < 0 ) { - close(fd); - closedir_uncached(dir); - DEBUGF("Failed renaming file: %d\n", rc); - errno = EIO; - return rc * 10 - 7; + if (oflag & O_EXCL) + callflags |= FF_EXCL; } -#ifdef HAVE_DIRCACHE - dircache_rename(path, newpath); -#endif + rc = open_internal_inner2(path, file, callflags); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 3); - rc = close(fd); - if (rc<0) { - closedir_uncached(dir); - errno = EIO; - return rc * 10 - 8; - } + return fildes; - rc = closedir_uncached(dir); - if (rc<0) { - errno = EIO; - return rc * 10 - 9; - } +file_error: + return rc; +} - return 0; +static int open_internal_locked(const char *path, int oflag, + unsigned int callflags) +{ + file_internal_lock_WRITER(); + int rc = open_internal_inner1(path, oflag, callflags); + file_internal_unlock_WRITER(); + return rc; } -int ftruncate(int fd, off_t size) +/* fill a cache buffer with a new sector */ +static int readwrite_fill_cache(struct filestr_desc *file, unsigned long sector, + unsigned long filesectors, bool write) { - int rc, sector; - struct filedesc* file = &openfiles[fd]; + /* sector != cachep->sector should have been checked by now */ - sector = size / SECTOR_SIZE; - if (size % SECTOR_SIZE) - sector++; + int rc; + struct filestr_cache *cachep = filestr_get_cache(&file->stream); - rc = fat_seek(&(file->fatfile), sector); - if (rc < 0) { - errno = EIO; - return rc * 10 - 1; + if (cachep->flags & FSC_DIRTY) + { + rc = flush_cache(file); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 1); } - rc = fat_truncate(&(file->fatfile)); - if (rc < 0) { - errno = EIO; - return rc * 10 - 2; + if (fat_query_sectornum(&file->stream.fatstr) != sector) + { + /* get on the correct sector */ + rc = fat_seek(&file->stream.fatstr, sector); + if (rc < 0) + FILE_ERROR(EIO, rc * 10 - 2); } - file->size = size; -#ifdef HAVE_DIRCACHE - dircache_update_filesize(fd, size, file->fatfile.firstcluster); -#endif + if (!write || sector < filesectors) + { + /* only reading or this sector would have been flushed if the cache + was previously needed for a different sector */ + rc = fat_readwrite(&file->stream.fatstr, 1, cachep->buffer, false); + if (rc < 0) + FILE_ERROR(rc == FAT_RC_ENOSPC ? ENOSPC : EIO, rc * 10 - 3); + } + else + { + /* create a fresh, shiny, new sector with that new sector smell */ + cachep->flags = FSC_NEW; + } - return 0; + cachep->sector = sector; + return 1; +file_error: + DEBUGF("Failed caching sector: %d\n", rc); + return rc; } -static int flush_cache(int fd) +/* read or write to part or all of the cache buffer */ +static inline void readwrite_cache(struct filestr_cache *cachep, void *buf, + unsigned long secoffset, size_t nbyte, + bool write) { - int rc; - struct filedesc* file = &openfiles[fd]; - long sector = file->fileoffset / SECTOR_SIZE; - - DEBUGF("Flushing dirty sector cache\n"); + void *dst, *cbufp = cachep->buffer + secoffset; - /* make sure we are on correct sector */ - rc = fat_seek(&(file->fatfile), sector); - if ( rc < 0 ) - return rc * 10 - 3; - - rc = fat_readwrite(&(file->fatfile), 1, file->cache, true ); + if (write) + { + dst = cbufp; + cachep->flags |= FSC_DIRTY; + } + else + { + dst = buf; + buf = cbufp; + } - if ( rc < 0 ) { - if(file->fatfile.eof) - errno = ENOSPC; + memcpy(dst, buf, nbyte); +} - return rc * 10 - 2; +/* read or write a partial sector using the file's cache */ +static inline ssize_t readwrite_partial(struct filestr_desc *file, + struct filestr_cache *cachep, + unsigned long sector, + unsigned long secoffset, + void *buf, + size_t nbyte, + unsigned long filesectors, + unsigned int flags) +{ + if (sector != cachep->sector) + { + /* wrong sector in buffer */ + int rc = readwrite_fill_cache(file, sector, filesectors, flags); + if (rc <= 0) + return rc; } - file->dirty = false; - - return 0; + readwrite_cache(cachep, buf, secoffset, nbyte, flags); + return nbyte; } -static int readwrite(int fd, void* buf, long count, bool write) +/* read from or write to the file; back end to read() and write() */ +static ssize_t readwrite(struct filestr_desc *file, void *buf, size_t nbyte, + bool write) { - long sectors; - long nread=0; - struct filedesc* file; - int rc; -#ifdef STORAGE_NEEDS_ALIGN - long i; - int rc2; -#endif + DEBUGF("readwrite(%p,%lx,%ld,%s)\n", + file, (long)buf, nbyte, write ? "write" : "read"); - if (fd < 0 || fd > MAX_OPEN_FILES-1) { - errno = EINVAL; - return -1; - } + const file_size_t size = *file->sizep; + file_size_t filerem; - file = &openfiles[fd]; + if (write) + { + /* if opened in append mode, move pointer to end */ + if (file->stream.flags & FD_APPEND) + file->offset = MIN(size, FILE_SIZE_MAX); - if ( !file->busy ) { - errno = EBADF; - return -1; + filerem = FILE_SIZE_MAX - file->offset; + } + else + { + /* limit to maximum possible offset (EOF or FILE_SIZE_MAX) */ + filerem = MIN(size, FILE_SIZE_MAX) - file->offset; } - if(file->attr & FAT_ATTR_DIRECTORY) { - errno = EISDIR; - return -1; + if (nbyte > filerem) + { + nbyte = filerem; + if (nbyte > 0) + {} + else if (write) + FILE_ERROR_RETURN(EFBIG, -1); /* would get too large */ + else if (file->offset >= FILE_SIZE_MAX) + FILE_ERROR_RETURN(EOVERFLOW, -2); /* can't read here */ } - LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n", - fd,(long)buf,count,write?"write":"read"); + if (nbyte == 0) + return 0; - /* attempt to read past EOF? */ - if (!write && count > file->size - file->fileoffset) - count = file->size - file->fileoffset; + int rc = 0; + + struct filestr_cache * const cachep = file->stream.cachep; + void * const bufstart = buf; + + const unsigned long filesectors = filesize_sectors(size); + unsigned long sector = file->offset / SECTOR_SIZE; + unsigned long sectoroffs = file->offset % SECTOR_SIZE; /* any head bytes? */ - if ( file->cacheoffset != -1 ) { - int offs = file->cacheoffset; - int headbytes = MIN(count, SECTOR_SIZE - offs); + if (sectoroffs) + { + size_t headbytes = MIN(nbyte, SECTOR_SIZE - sectoroffs); + rc = readwrite_partial(file, cachep, sector, sectoroffs, buf, headbytes, + filesectors, write); + if (rc <= 0) + { + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 3); - if (write) { - memcpy( file->cache + offs, buf, headbytes ); - file->dirty = true; + nbyte = 0; /* eof, skip the rest */ } - else { - memcpy( buf, file->cache + offs, headbytes ); + else + { + buf += rc; + nbyte -= rc; + sector++; /* if nbyte goes to 0, the rest is skipped anyway */ } + } - if (offs + headbytes == SECTOR_SIZE) { - if (file->dirty) { - rc = flush_cache(fd); - if ( rc < 0 ) { - errno = EIO; - return rc * 10 - 2; - } - } - file->cacheoffset = -1; - } - else { - file->cacheoffset += headbytes; - } + /* read/write whole sectors right into/from the supplied buffer */ + unsigned long sectorcount = nbyte / SECTOR_SIZE; - nread = headbytes; - count -= headbytes; - } + while (sectorcount) + { + unsigned long runlen = sectorcount; - /* If the buffer has been modified, either it has been flushed already - * (if (offs+headbytes == SECTOR_SIZE)...) or does not need to be (no - * more data to follow in this call). Do NOT flush here. */ + /* if a cached sector is inside the transfer range, split the transfer + into two parts and use the cache for that sector to keep it coherent + without writeback */ + if (UNLIKELY(cachep->sector >= sector && + cachep->sector < sector + sectorcount)) + { + runlen = cachep->sector - sector; + } - /* read/write whole sectors right into/from the supplied buffer */ - sectors = count / SECTOR_SIZE; - rc = 0; - if ( sectors ) { -#ifdef STORAGE_NEEDS_ALIGN - if (((uint32_t)buf + nread) & (CACHEALIGN_SIZE - 1)) - for (i = 0; i < sectors; i++) + if (runlen) + { + if (fat_query_sectornum(&file->stream.fatstr) != sector) { - if (write) memcpy(file->cache, buf+nread+i*SECTOR_SIZE, SECTOR_SIZE); - rc2 = fat_readwrite(&(file->fatfile), 1, file->cache, write ); - if (rc2 < 0) + /* get on the correct sector */ + rc = 0; + + /* If the dirty bit isn't set, we're somehow beyond the file + size and you can't explain _that_ */ + if (sector >= filesectors && cachep->flags == (FSC_NEW|FSC_DIRTY)) { - rc = rc2; - break; + rc = flush_cache(file); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 4); + + if (cachep->sector + 1 == sector) + rc = 1; /* if now ok, don't seek */ + } + + if (rc == 0) + { + rc = fat_seek(&file->stream.fatstr, sector); + if (rc < 0) + FILE_ERROR(EIO, rc * 10 - 5); } - else rc += rc2; - if (!write) memcpy(buf+nread+i*SECTOR_SIZE, file->cache, SECTOR_SIZE); - } - else -#endif - rc = fat_readwrite(&(file->fatfile), sectors, (unsigned char*)buf+nread, write ); - if ( rc < 0 ) { - DEBUGF("Failed read/writing %ld sectors\n",sectors); - errno = EIO; - if(write && file->fatfile.eof) { - DEBUGF("No space left on device\n"); - errno = ENOSPC; - } else { - file->fileoffset += nread; } - file->cacheoffset = -1; - /* adjust file size to length written */ - if ( write && file->fileoffset > file->size ) + + rc = fat_readwrite(&file->stream.fatstr, runlen, buf, write); + if (rc < 0) { - file->size = file->fileoffset; -#ifdef HAVE_DIRCACHE - dircache_update_filesize(fd, file->size, file->fatfile.firstcluster); -#endif + DEBUGF("I/O error %sing %ld sectors\n", sectors, + write ? "writ" : "read"); + FILE_ERROR(rc == FAT_RC_ENOSPC ? ENOSPC : EIO, + rc * 10 - 6); } - return nread ? nread : rc * 10 - 4; - } - else { - if ( rc > 0 ) { - nread += rc * SECTOR_SIZE; - count -= sectors * SECTOR_SIZE; + else + { + buf += rc * SECTOR_SIZE; + nbyte -= rc * SECTOR_SIZE; + sector += rc; + sectorcount -= rc; /* if eof, skip tail bytes */ - if ( rc < sectors ) - count = 0; - } - else { - /* eof */ - count=0; + if ((unsigned long)rc < runlen) + nbyte = 0; + + if (!nbyte) + break; } + } - file->cacheoffset = -1; + if (UNLIKELY(sectorcount && sector == cachep->sector)) + { + /* do this one sector with the cache */ + readwrite_cache(cachep, buf, 0, SECTOR_SIZE, write); + buf += SECTOR_SIZE; + nbyte -= SECTOR_SIZE; + sector++; + sectorcount--; } } /* any tail bytes? */ - if ( count ) { - if (write) { - if ( file->fileoffset + nread < file->size ) { - /* sector is only partially filled. copy-back from disk */ - LDEBUGF("Copy-back tail cache\n"); - rc = fat_readwrite(&(file->fatfile), 1, file->cache, false ); - if ( rc < 0 ) { - DEBUGF("Failed writing\n"); - errno = EIO; - file->fileoffset += nread; - file->cacheoffset = -1; - /* adjust file size to length written */ - if ( file->fileoffset > file->size ) - { - file->size = file->fileoffset; -#ifdef HAVE_DIRCACHE - dircache_update_filesize(fd, file->size, file->fatfile.firstcluster); -#endif - } - return nread ? nread : rc * 10 - 5; - } - /* seek back one sector to put file position right */ - rc = fat_seek(&(file->fatfile), - (file->fileoffset + nread) / - SECTOR_SIZE); - if ( rc < 0 ) { - DEBUGF("fat_seek() failed\n"); - errno = EIO; - file->fileoffset += nread; - file->cacheoffset = -1; - /* adjust file size to length written */ - if ( file->fileoffset > file->size ) - { - file->size = file->fileoffset; -#ifdef HAVE_DIRCACHE - dircache_update_filesize(fd, file->size, file->fatfile.firstcluster); -#endif - } - return nread ? nread : rc * 10 - 6; - } - } - memcpy( file->cache, (unsigned char*)buf + nread, count ); - file->dirty = true; - } - else { - rc = fat_readwrite(&(file->fatfile), 1, file->cache,false); - if (rc < 1 ) { - DEBUGF("Failed caching sector\n"); - errno = EIO; - file->fileoffset += nread; - file->cacheoffset = -1; - return nread ? nread : rc * 10 - 7; - } - memcpy( (unsigned char*)buf + nread, file->cache, count ); - } + if (nbyte) + { + /* tail bytes always start at sector offset 0 */ + rc = readwrite_partial(file, cachep, sector, 0, buf, nbyte, + filesectors, write); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 7); - nread += count; - file->cacheoffset = count; + buf += rc; } - file->fileoffset += nread; - LDEBUGF("fileoffset: %ld\n", file->fileoffset); +file_error:; +#ifdef DEBUG + if (errno == ENOSPC) + DEBUGF("No space left on device\n"); +#endif - /* adjust file size to length written */ - if ( write && file->fileoffset > file->size ) + size_t done = buf - bufstart; + if (done) { - file->size = file->fileoffset; -#ifdef HAVE_DIRCACHE - dircache_update_filesize(fd, file->size, file->fatfile.firstcluster); -#endif + /* error or not, update the file offset and size if anything was + transferred */ + file->offset += done; + DEBUGF("file offset: %ld\n", file->offset); + + /* adjust file size to length written */ + if (write && file->offset > size) + *file->sizep = file->offset; + + if (rc > 0) + return done; } - return nread; + return rc; } -ssize_t write(int fd, const void* buf, size_t count) + +/** Internal interface **/ + +/* open a file without codepage conversion during the directory search; + required to avoid any reentrancy when opening codepages and when scanning + directories internally, which could infinitely recurse and would corrupt + the static data */ +int open_noiso_internal(const char *path, int oflag) { - if (!openfiles[fd].write) { - errno = EACCES; - return -1; - } - return readwrite(fd, (void *)buf, count, true); + return open_internal_locked(path, oflag, FF_ANYTYPE | FF_NOISO); } -ssize_t read(int fd, void* buf, size_t count) + +/** POSIX **/ + +/* open a file */ +int open(const char *path, int oflag) { - return readwrite(fd, buf, count, false); + DEBUGF("open(path=\"%s\",oflag=%X)\n", path, (unsigned)oflag); + return open_internal_locked(path, oflag, FF_ANYTYPE); } +/* create a new file or rewrite an existing one */ +int creat(const char *path) +{ + DEBUGF("creat(path=\"%s\")\n", path); + return open_internal_locked(path, O_WRONLY|O_CREAT|O_TRUNC, FF_ANYTYPE); +} -off_t lseek(int fd, off_t offset, int whence) +/* close a file descriptor */ +int close(int fildes) { - off_t pos; - long newsector; - long oldsector; - int sectoroffset; + DEBUGF("close(fd=%d)\n", fildes); + int rc; - struct filedesc* file = &openfiles[fd]; - LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence); + file_internal_lock_WRITER(); - if (fd < 0 || fd > MAX_OPEN_FILES-1) { - errno = EINVAL; - return -1; - } - if ( !file->busy ) { - errno = EBADF; - return -1; + /* needs to work even if marked "nonexistant" */ + struct filestr_desc *file = &open_streams[fildes]; + if ((unsigned int)fildes >= MAX_OPEN_FILES || !file->stream.flags) + { + DEBUGF("filedes %d not open\n", fildes); + FILE_ERROR(EBADF, -2); } - switch ( whence ) { - case SEEK_SET: - pos = offset; - break; + rc = close_internal(file); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 3); - case SEEK_CUR: - pos = file->fileoffset + offset; - break; +file_error: + file_internal_unlock_WRITER(); + return rc; +} + +/* truncate a file to a specified length */ +int ftruncate(int fildes, off_t length) +{ + DEBUGF("ftruncate(fd=%d,len=%ld)\n", fildes, (long)length); - case SEEK_END: - pos = file->size + offset; - break; + struct filestr_desc * const file = GET_FILESTR(READER, fildes); + if (!file) + FILE_ERROR_RETURN(ERRNO, -1); - default: - errno = EINVAL; - return -2; + int rc; + + if (!(file->stream.flags & FD_WRITE)) + { + DEBUGF("Descriptor is read-only mode\n"); + FILE_ERROR(EBADF, -2); } - if ((pos < 0) || (pos > file->size)) { - errno = EINVAL; - return -3; + + if (length < 0) + { + DEBUGF("Length %ld is invalid\n", (long)length); + FILE_ERROR(EINVAL, -3); } - /* new sector? */ - newsector = pos / SECTOR_SIZE; - oldsector = file->fileoffset / SECTOR_SIZE; - sectoroffset = pos % SECTOR_SIZE; + rc = ftruncate_internal(file, length, true); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 4); - if ( (newsector != oldsector) || - ((file->cacheoffset==-1) && sectoroffset) ) { +file_error: + RELEASE_FILESTR(READER, file); + return rc; +} - if ( newsector != oldsector ) { - if (file->dirty) { - rc = flush_cache(fd); - if (rc < 0) - return rc * 10 - 5; - } +/* synchronize changes to a file */ +int fsync(int fildes) +{ + DEBUGF("fsync(fd=%d)\n", fildes); - rc = fat_seek(&(file->fatfile), newsector); - if ( rc < 0 ) { - errno = EIO; - return rc * 10 - 4; - } - } - if ( sectoroffset ) { - rc = fat_readwrite(&(file->fatfile), 1, file->cache ,false); - if ( rc < 0 ) { - errno = EIO; - return rc * 10 - 6; - } - file->cacheoffset = sectoroffset; - } - else - file->cacheoffset = -1; + struct filestr_desc * const file = GET_FILESTR(WRITER, fildes); + if (!file) + FILE_ERROR_RETURN(ERRNO, -1); + + int rc; + + if (!(file->stream.flags & FD_WRITE)) + { + DEBUGF("Descriptor is read-only mode\n", fd); + FILE_ERROR(EINVAL, -2); } - else - if ( file->cacheoffset != -1 ) - file->cacheoffset = sectoroffset; - file->fileoffset = pos; + rc = fsync_internal(file); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 3); - return pos; +file_error: + RELEASE_FILESTR(WRITER, file); + return rc; } -off_t filesize(int fd) +/* move the read/write file offset */ +off_t lseek(int fildes, off_t offset, int whence) { - struct filedesc* file = &openfiles[fd]; + DEBUGF("lseek(fd=%d,ofs=%ld,wh=%d)\n", fildes, (long)offset, whence); - if (fd < 0 || fd > MAX_OPEN_FILES-1) { - errno = EINVAL; - return -1; + struct filestr_desc * const file = GET_FILESTR(READER, fildes); + if (!file) + FILE_ERROR_RETURN(ERRNO, -1); + + off_t rc = lseek_internal(file, offset, whence); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 2); + +file_error: + RELEASE_FILESTR(READER, file); + return rc; +} + +/* read from a file */ +ssize_t read(int fildes, void *buf, size_t nbyte) +{ + struct filestr_desc * const file = GET_FILESTR(READER, fildes); + if (!file) + FILE_ERROR_RETURN(ERRNO, -1); + + ssize_t rc; + + if (file->stream.flags & FD_WRONLY) + { + DEBUGF("read(fd=%d,buf=%p,nb=%lu) - " + "descriptor is write-only mode\n", fildes, buf, nbyte); + FILE_ERROR(EBADF, -2); } - if ( !file->busy ) { - errno = EBADF; - return -1; + + rc = readwrite(file, buf, nbyte, false); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 3); + +file_error: + RELEASE_FILESTR(READER, file); + return rc; +} + +/* write on a file */ +ssize_t write(int fildes, const void *buf, size_t nbyte) +{ + struct filestr_desc * const file = GET_FILESTR(READER, fildes); + if (!file) + FILE_ERROR_RETURN(ERRNO, -1); + + ssize_t rc; + + if (!(file->stream.flags & FD_WRITE)) + { + DEBUGF("write(fd=%d,buf=%p,nb=%lu) - " + "descriptor is read-only mode\n", fildes, buf, nbyte); + FILE_ERROR(EBADF, -2); } - return file->size; + rc = readwrite(file, (void *)buf, nbyte, true); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 3); + +file_error: + RELEASE_FILESTR(READER, file); + return rc; } +/* remove a file */ +int remove(const char *path) +{ + DEBUGF("remove(path=\"%s\")\n", path); + + file_internal_lock_WRITER(); + int rc = remove_stream_internal(path, NULL, FF_FILE); + file_internal_unlock_WRITER(); + return rc; +} -/* release all file handles on a given volume "by force", to avoid leaks */ -int release_files(int volume) +/* rename a file */ +int rename(const char *old, const char *new) { - struct filedesc* pfile = openfiles; - int fd; - int closed = 0; - for ( fd=0; fdfatfile.volume == volume) -#else - (void)volume; -#endif + if (oldinfo.parentinfo.volume != newinfo.parentinfo.volume) + { + DEBUGF("Cross-device link\n"); + FILE_ERROR(EXDEV, -3); + } +#endif /* HAVE_MULTIVOLUME */ + + /* if the parent is changing then this is a move, not a simple rename */ + const bool is_move = !fat_file_is_same(&oldinfo.parentinfo.fatfile, + &newinfo.parentinfo.fatfile); + /* prefix found and moving? */ + if (is_move && (newinfo.attr & ATTR_PREFIX)) + { + DEBUGF("New contains prefix that names old\n"); + FILE_ERROR(EINVAL, -4); + } + + const char * const oldname = strmemdupa(oldinfo.name, oldinfo.length); + const char * const newname = strmemdupa(newinfo.name, newinfo.length); + bool is_overwrite = false; + + if (open2rc > 0) + { + /* new name exists in parent; check if 'old' is overwriting 'new'; + if it's the very same file, then it's just a rename */ + is_overwrite = oldstr.bindp != newstr.bindp; + + if (is_overwrite) + { + if (are_dirs) + { + /* the directory to be overwritten must be empty */ + rc = test_dir_empty_internal(&newstr); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 5); + } + } + else if (!strcmp(newname, oldname)) /* case-only is ok */ + { + DEBUGF("No name change (success)\n"); + rc = 0; + FILE_ERROR(ERRNO, RC); + } + } + else if (!are_dirs && (newinfo.attr & ATTR_DIRECTORY)) + { + /* even if new doesn't exist, canonical path type must match + (ie. a directory path such as "/foo/bar/" when old names a file) */ + DEBUGF("New path is a directory\n"); + FILE_ERROR(EISDIR, -6); + } + + /* first, create the new entry so that there's never a time that the + victim's data has no reference in the directory tree, that is, until + everything else first succeeds */ + struct file_base_info old_fileinfo = *oldstr.infop; + rc = fat_rename(&newinfo.parentinfo.fatfile, &oldstr.infop->fatfile, + newname); + if (rc < 0) + { + DEBUGF("I/O error renaming file: %d\n", rc); + FILE_ERROR(rc == FAT_RC_ENOSPC ? ENOSPC : EIO, rc * 10 - 7); + } + + if (is_overwrite) + { + /* 'new' would have been assigned its own directory entry and + succeeded so at this point it is treated like a remove() call + on the victim which preserves data until the last reference is + closed */ + rc = remove_stream_internal(NULL, &newstr, callflags); + if (rc < 0) + FILE_ERROR(ERRNO, rc * 10 - 8); + } + + fileop_onrename_internal(&oldstr, is_move ? &old_fileinfo : NULL, + &newinfo.parentinfo, newname); + +file_error: + /* for now, there is nothing to fail upon closing the old stream */ + if (open1rc >= 0) + close_stream_internal(&oldstr); + + /* the 'new' stream could fail to close cleanly because it became + impossible to remove its data if this was an overwrite operation */ + if (open2rc >= 0) + { + int rc2 = close_stream_internal(&newstr); + if (rc2 < 0 && rc >= 0) { - pfile->busy = false; /* mark as available, no further action */ - closed++; + DEBUGF("Success but failed closing new: %d\n", rc2); + rc = rc2 * 10 - 9; } } - return closed; /* return how many we did */ + + file_internal_unlock_WRITER(); + return rc; +} + + +/** Extensions **/ + +/* get the binary size of a file (in bytes) */ +off_t filesize(int fildes) +{ + struct filestr_desc * const file = GET_FILESTR(READER, fildes); + if (!file) + FILE_ERROR_RETURN(ERRNO, -1); + + off_t rc; + file_size_t size = *file->sizep; + + if (size > FILE_SIZE_MAX) + FILE_ERROR(EOVERFLOW, -2); + + rc = (off_t)size; +file_error: + RELEASE_FILESTR(READER, file); + return rc; +} + +/* test if two file descriptors refer to the same file */ +int fsamefile(int fildes1, int fildes2) +{ + struct filestr_desc * const file1 = GET_FILESTR(WRITER, fildes1); + if (!file1) + FILE_ERROR_RETURN(ERRNO, -1); + + int rc = -2; + + struct filestr_desc * const file2 = get_filestr(fildes2); + if (file2) + rc = file1->stream.bindp == file2->stream.bindp ? 1 : 0; + + RELEASE_FILESTR(WRITER, file1); + return rc; +} + +/* tell the relationship of path1 to path2 */ +int relate(const char *path1, const char *path2) +{ + /* this is basically what rename() does but reduced to the relationship + determination */ + DEBUGF("relate(path1=\"%s\",path2=\"%s\")\n", path1, path2); + + int rc, open1rc = -1, open2rc = -1; + struct filestr_base str1, str2; + struct path_component_info info1, info2; + + file_internal_lock_WRITER(); + + open1rc = open_stream_internal(path1, FF_ANYTYPE, &str1, &info1); + if (open1rc <= 0) + { + DEBUGF("Failed opening path1: %d\n", rc); + if (open1rc < 0) + FILE_ERROR(ERRNO, open1rc * 10 - 1); + else + FILE_ERROR(ENOENT, -1); + } + + info2.prefixp = str1.infop; + open2rc = open_stream_internal(path2, FF_ANYTYPE | FF_CHECKPREFIX, + &str2, &info2); + if (open2rc < 0) + { + DEBUGF("Failed opening path2: %d\n", rc); + FILE_ERROR(ERRNO, open2rc * 10 - 2); + } + + rc = RELATE_DIFFERENT; + + if (open2rc > 0) + { + if (str1.bindp == str2.bindp) + rc = RELATE_SAME; + else if (info2.attr & ATTR_PREFIX) + rc = RELATE_PREFIX; + } + else /* open2rc == 0 */ + { + /* path1 existing and path2's final part not can only be a prefix or + different */ + if (info2.attr & ATTR_PREFIX) + rc = RELATE_PREFIX; + } + +file_error: + if (open1rc >= 0) + close_stream_internal(&str1); + + if (open2rc >= 0) + close_stream_internal(&str2); + + file_internal_unlock_WRITER(); + return rc; +} + +/* test file or directory existence */ +bool file_exists(const char *path) +{ + file_internal_lock_WRITER(); + bool rc = test_stream_exists_internal(path, FF_ANYTYPE) > 0; + file_internal_unlock_WRITER(); + return rc; } -- cgit v1.2.3