2002-05-07 16:01:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-05-05 10:32:46 +00:00
|
|
|
* Copyright (C) 2002 by Björn Stenberg
|
2002-05-07 16:01:53 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* 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.
|
2002-05-07 16:01:53 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include <string.h>
|
2002-05-08 12:10:30 +00:00
|
|
|
#include <errno.h>
|
2002-05-13 12:29:34 +00:00
|
|
|
#include <stdbool.h>
|
2002-05-07 16:01:53 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "fat.h"
|
2007-07-20 17:06:55 +00:00
|
|
|
#include "dir_uncached.h"
|
2002-05-07 16:01:53 +00:00
|
|
|
#include "debug.h"
|
2005-10-07 17:38:05 +00:00
|
|
|
#include "dircache.h"
|
2009-11-26 22:34:08 +00:00
|
|
|
#include "filefuncs.h"
|
2005-12-17 12:17:11 +00:00
|
|
|
#include "system.h"
|
2002-05-07 16:01:53 +00:00
|
|
|
|
2002-05-08 15:27:21 +00:00
|
|
|
/*
|
|
|
|
These functions provide a roughly POSIX-compatible file IO API.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
struct filedesc {
|
2006-12-04 21:37:22 +00:00
|
|
|
unsigned char cache[SECTOR_SIZE];
|
2005-01-23 23:29:35 +00:00
|
|
|
int cacheoffset; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
|
|
|
|
long fileoffset;
|
|
|
|
long size;
|
2003-01-27 09:32:17 +00:00
|
|
|
int attr;
|
2002-05-07 16:01:53 +00:00
|
|
|
struct fat_file fatfile;
|
|
|
|
bool busy;
|
2002-10-20 22:50:58 +00:00
|
|
|
bool write;
|
2002-11-11 13:57:58 +00:00
|
|
|
bool dirty;
|
2002-11-11 15:45:43 +00:00
|
|
|
bool trunc;
|
2002-05-07 16:01:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct filedesc openfiles[MAX_OPEN_FILES];
|
|
|
|
|
2002-11-11 15:45:43 +00:00
|
|
|
static int flush_cache(int fd);
|
|
|
|
|
2010-05-06 17:35:04 +00:00
|
|
|
int file_creat(const char *pathname)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
2010-05-06 17:35:13 +00:00
|
|
|
return open(pathname, O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2006-04-13 21:14:13 +00:00
|
|
|
static int open_internal(const char* pathname, int flags, bool use_cache)
|
2002-05-07 16:01:53 +00:00
|
|
|
{
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED* dir;
|
|
|
|
struct dirent_uncached* entry;
|
2002-05-07 16:01:53 +00:00
|
|
|
int fd;
|
2010-10-25 12:36:57 +00:00
|
|
|
int pathnamesize = strlen(pathname) + 1;
|
|
|
|
char pathnamecopy[pathnamesize];
|
2002-05-07 16:01:53 +00:00
|
|
|
char* name;
|
2002-11-11 16:13:45 +00:00
|
|
|
struct filedesc* file = NULL;
|
2003-02-26 02:08:52 +00:00
|
|
|
int rc;
|
2006-04-13 21:14:13 +00:00
|
|
|
#ifndef HAVE_DIRCACHE
|
|
|
|
(void)use_cache;
|
|
|
|
#endif
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
LDEBUGF("open(\"%s\",%d)\n",pathname,flags);
|
2002-06-07 15:15:10 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
if ( pathname[0] != '/' ) {
|
|
|
|
DEBUGF("'%s' is not an absolute path.\n",pathname);
|
|
|
|
DEBUGF("Only absolute pathnames supported at the moment\n");
|
2002-05-08 12:10:30 +00:00
|
|
|
errno = EINVAL;
|
2002-05-07 16:01:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find a free file descriptor */
|
|
|
|
for ( fd=0; fd<MAX_OPEN_FILES; fd++ )
|
|
|
|
if ( !openfiles[fd].busy )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if ( fd == MAX_OPEN_FILES ) {
|
|
|
|
DEBUGF("Too many files open\n");
|
2002-05-08 12:10:30 +00:00
|
|
|
errno = EMFILE;
|
2002-10-20 22:50:58 +00:00
|
|
|
return -2;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
|
|
|
|
file = &openfiles[fd];
|
|
|
|
memset(file, 0, sizeof(struct filedesc));
|
2002-05-07 16:01:53 +00:00
|
|
|
|
2002-11-11 16:08:28 +00:00
|
|
|
if (flags & (O_RDWR | O_WRONLY)) {
|
2002-11-11 16:13:45 +00:00
|
|
|
file->write = true;
|
2004-07-19 21:08:44 +00:00
|
|
|
|
2002-11-11 16:08:28 +00:00
|
|
|
if (flags & O_TRUNC)
|
2002-11-11 16:13:45 +00:00
|
|
|
file->trunc = true;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
file->busy = true;
|
2002-05-27 09:13:56 +00:00
|
|
|
|
2006-03-28 11:51:12 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2006-04-13 21:14:13 +00:00
|
|
|
if (dircache_is_enabled() && !file->write && use_cache)
|
2006-03-28 11:51:12 +00:00
|
|
|
{
|
|
|
|
const struct dircache_entry *ce;
|
2008-03-11 19:52:07 +00:00
|
|
|
# ifdef HAVE_MULTIVOLUME
|
|
|
|
int volume = strip_volume(pathname, pathnamecopy);
|
|
|
|
# endif
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2006-03-28 11:51:12 +00:00
|
|
|
ce = dircache_get_entry_ptr(pathname);
|
|
|
|
if (!ce)
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
file->busy = false;
|
|
|
|
return -7;
|
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2008-03-11 19:52:07 +00:00
|
|
|
fat_open(IF_MV2(volume,)
|
2006-03-28 11:51:12 +00:00
|
|
|
ce->startcluster,
|
|
|
|
&(file->fatfile),
|
|
|
|
NULL);
|
2010-09-01 21:29:34 +00:00
|
|
|
file->size = ce->info.size;
|
|
|
|
file->attr = ce->info.attribute;
|
2006-03-28 11:51:12 +00:00
|
|
|
file->cacheoffset = -1;
|
|
|
|
file->fileoffset = 0;
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2006-03-28 11:51:12 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#endif
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2010-10-25 12:36:57 +00:00
|
|
|
strlcpy(pathnamecopy, pathname, pathnamesize);
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
/* locate filename */
|
2004-07-19 21:08:44 +00:00
|
|
|
name=strrchr(pathnamecopy+1,'/');
|
2002-05-07 16:01:53 +00:00
|
|
|
if ( name ) {
|
2004-07-19 21:08:44 +00:00
|
|
|
*name = 0;
|
2007-07-20 17:06:55 +00:00
|
|
|
dir = opendir_uncached(pathnamecopy);
|
2002-05-07 16:01:53 +00:00
|
|
|
*name = '/';
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
else {
|
2007-07-20 17:06:55 +00:00
|
|
|
dir = opendir_uncached("/");
|
2004-07-19 21:08:44 +00:00
|
|
|
name = pathnamecopy+1;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
|
|
|
if (!dir) {
|
|
|
|
DEBUGF("Failed opening dir\n");
|
2002-05-08 12:10:30 +00:00
|
|
|
errno = EIO;
|
2002-11-11 16:13:45 +00:00
|
|
|
file->busy = false;
|
2002-10-20 22:50:58 +00:00
|
|
|
return -4;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2004-02-11 14:37:16 +00:00
|
|
|
if(name[0] == 0) {
|
|
|
|
DEBUGF("Empty file name\n");
|
|
|
|
errno = EINVAL;
|
|
|
|
file->busy = false;
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-02-11 14:37:16 +00:00
|
|
|
return -5;
|
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
/* scan dir for name */
|
2007-07-20 17:06:55 +00:00
|
|
|
while ((entry = readdir_uncached(dir))) {
|
2002-08-22 20:13:21 +00:00
|
|
|
if ( !strcasecmp(name, entry->d_name) ) {
|
2004-12-28 22:16:07 +00:00
|
|
|
fat_open(IF_MV2(dir->fatdir.file.volume,)
|
|
|
|
entry->startcluster,
|
2002-11-11 16:13:45 +00:00
|
|
|
&(file->fatfile),
|
2002-10-20 22:50:58 +00:00
|
|
|
&(dir->fatdir));
|
2010-09-01 21:29:34 +00:00
|
|
|
file->size = file->trunc ? 0 : entry->info.size;
|
|
|
|
file->attr = entry->info.attribute;
|
2002-05-07 16:01:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-11-19 12:48:50 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
if ( !entry ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
LDEBUGF("Didn't find file %s\n",name);
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( file->write && (flags & O_CREAT) ) {
|
2003-02-26 02:08:52 +00:00
|
|
|
rc = fat_create_file(name,
|
|
|
|
&(file->fatfile),
|
|
|
|
&(dir->fatdir));
|
|
|
|
if (rc < 0) {
|
2004-07-19 21:08:44 +00:00
|
|
|
DEBUGF("Couldn't create %s in %s\n",name,pathnamecopy);
|
2002-10-20 22:50:58 +00:00
|
|
|
errno = EIO;
|
2002-11-11 16:13:45 +00:00
|
|
|
file->busy = false;
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-02-11 14:37:16 +00:00
|
|
|
return rc * 10 - 6;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2005-10-07 17:38:05 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2006-03-28 11:51:12 +00:00
|
|
|
dircache_add_file(pathname, file->fatfile.firstcluster);
|
2005-10-07 17:38:05 +00:00
|
|
|
#endif
|
2002-11-11 16:13:45 +00:00
|
|
|
file->size = 0;
|
2003-01-27 09:32:17 +00:00
|
|
|
file->attr = 0;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2004-07-19 21:08:44 +00:00
|
|
|
DEBUGF("Couldn't find %s in %s\n",name,pathnamecopy);
|
2002-10-20 22:50:58 +00:00
|
|
|
errno = ENOENT;
|
2002-11-11 16:13:45 +00:00
|
|
|
file->busy = false;
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-02-11 14:37:16 +00:00
|
|
|
return -7;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2004-03-10 19:47:59 +00:00
|
|
|
} else {
|
|
|
|
if(file->write && (file->attr & FAT_ATTR_DIRECTORY)) {
|
|
|
|
errno = EISDIR;
|
|
|
|
file->busy = false;
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2004-03-10 19:47:59 +00:00
|
|
|
return -8;
|
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2007-07-20 17:06:55 +00:00
|
|
|
closedir_uncached(dir);
|
2002-05-07 16:01:53 +00:00
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->cacheoffset = -1;
|
|
|
|
file->fileoffset = 0;
|
2002-11-11 15:45:43 +00:00
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
if (file->write && (flags & O_APPEND)) {
|
2003-02-26 02:08:52 +00:00
|
|
|
rc = lseek(fd,0,SEEK_END);
|
|
|
|
if (rc < 0 )
|
2004-03-10 19:47:59 +00:00
|
|
|
return rc * 10 - 9;
|
2002-11-11 15:45:43 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 17:38:05 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
if (file->write)
|
|
|
|
dircache_bind(fd, pathname);
|
|
|
|
#endif
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2010-05-06 17:35:13 +00:00
|
|
|
int file_open(const char* pathname, int flags)
|
2006-04-13 21:14:13 +00:00
|
|
|
{
|
|
|
|
/* By default, use the dircache if available. */
|
|
|
|
return open_internal(pathname, flags, true);
|
|
|
|
}
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
int close(int fd)
|
|
|
|
{
|
2002-11-11 16:13:45 +00:00
|
|
|
struct filedesc* file = &openfiles[fd];
|
2002-10-20 22:50:58 +00:00
|
|
|
int rc = 0;
|
|
|
|
|
2003-02-26 02:08:52 +00:00
|
|
|
LDEBUGF("close(%d)\n", fd);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2003-03-10 17:10:46 +00:00
|
|
|
if (fd < 0 || fd > MAX_OPEN_FILES-1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!file->busy) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (file->write) {
|
2003-06-29 13:17:19 +00:00
|
|
|
rc = fsync(fd);
|
2003-03-10 17:10:46 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 3;
|
2006-05-16 06:53:41 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
|
2007-09-02 13:24:51 +00:00
|
|
|
dircache_update_filetime(fd);
|
2006-05-16 06:53:41 +00:00
|
|
|
#endif
|
2003-03-10 17:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file->busy = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-29 13:17:19 +00:00
|
|
|
int fsync(int fd)
|
2003-03-10 17:10:46 +00:00
|
|
|
{
|
|
|
|
struct filedesc* file = &openfiles[fd];
|
|
|
|
int rc = 0;
|
|
|
|
|
2003-06-29 13:17:19 +00:00
|
|
|
LDEBUGF("fsync(%d)\n", fd);
|
2003-03-10 17:10:46 +00:00
|
|
|
|
2002-08-24 09:47:54 +00:00
|
|
|
if (fd < 0 || fd > MAX_OPEN_FILES-1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
if (!file->busy) {
|
2002-08-24 09:47:54 +00:00
|
|
|
errno = EBADF;
|
2002-10-20 22:50:58 +00:00
|
|
|
return -2;
|
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
if (file->write) {
|
2002-10-20 22:50:58 +00:00
|
|
|
/* flush sector cache */
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( file->dirty ) {
|
2003-02-26 02:08:52 +00:00
|
|
|
rc = flush_cache(fd);
|
|
|
|
if (rc < 0)
|
2007-10-10 23:26:17 +00:00
|
|
|
{
|
|
|
|
/* when failing, try to close the file anyway */
|
|
|
|
fat_closewrite(&(file->fatfile), file->size, file->attr);
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 3;
|
2007-10-10 23:26:17 +00:00
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2002-11-11 15:45:43 +00:00
|
|
|
|
2002-11-14 15:50:07 +00:00
|
|
|
/* truncate? */
|
|
|
|
if (file->trunc) {
|
2004-09-06 23:33:21 +00:00
|
|
|
rc = ftruncate(fd, file->size);
|
2003-02-26 02:08:52 +00:00
|
|
|
if (rc < 0)
|
2007-10-10 23:26:17 +00:00
|
|
|
{
|
|
|
|
/* when failing, try to close the file anyway */
|
|
|
|
fat_closewrite(&(file->fatfile), file->size, file->attr);
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 4;
|
2007-10-10 23:26:17 +00:00
|
|
|
}
|
2002-11-14 15:50:07 +00:00
|
|
|
}
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
/* tie up all loose ends */
|
2003-02-26 02:08:52 +00:00
|
|
|
rc = fat_closewrite(&(file->fatfile), file->size, file->attr);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 5;
|
2002-08-24 09:47:54 +00:00
|
|
|
}
|
2003-02-26 02:08:52 +00:00
|
|
|
return 0;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
2002-11-01 15:26:06 +00:00
|
|
|
int remove(const char* name)
|
|
|
|
{
|
|
|
|
int rc;
|
2002-11-11 16:13:45 +00:00
|
|
|
struct filedesc* file;
|
2006-04-13 21:14:13 +00:00
|
|
|
/* Can't use dircache now, because we need to access the fat structures. */
|
|
|
|
int fd = open_internal(name, O_WRONLY, false);
|
2002-11-01 15:26:06 +00:00
|
|
|
if ( fd < 0 )
|
2003-02-26 02:08:52 +00:00
|
|
|
return fd * 10 - 1;
|
2002-11-01 15:26:06 +00:00
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file = &openfiles[fd];
|
2006-03-30 10:20:44 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
dircache_remove(name);
|
|
|
|
#endif
|
2002-11-11 16:13:45 +00:00
|
|
|
rc = fat_remove(&(file->fatfile));
|
2002-11-11 10:21:51 +00:00
|
|
|
if ( rc < 0 ) {
|
2002-11-19 12:48:50 +00:00
|
|
|
DEBUGF("Failed removing file: %d\n", rc);
|
2002-11-11 10:21:51 +00:00
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 3;
|
2002-11-11 10:21:51 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->size = 0;
|
2002-11-01 15:26:06 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
rc = close(fd);
|
|
|
|
if (rc<0)
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 4;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rename(const char* path, const char* newpath)
|
|
|
|
{
|
|
|
|
int rc, fd;
|
2007-07-20 17:06:55 +00:00
|
|
|
DIR_UNCACHED* dir;
|
2002-11-19 12:48:50 +00:00
|
|
|
char* nameptr;
|
2004-08-22 11:28:24 +00:00
|
|
|
char* dirptr;
|
2002-11-19 12:48:50 +00:00
|
|
|
struct filedesc* file;
|
2004-08-22 11:28:24 +00:00
|
|
|
char newpath2[MAX_PATH];
|
2002-11-19 12:48:50 +00:00
|
|
|
|
|
|
|
/* verify new path does not already exist */
|
2004-08-22 11:28:24 +00:00
|
|
|
/* If it is a directory, errno == EISDIR if the name exists */
|
2002-11-19 12:48:50 +00:00
|
|
|
fd = open(newpath, O_RDONLY);
|
2004-08-22 11:28:24 +00:00
|
|
|
if ( fd >= 0 || errno == EISDIR) {
|
2003-02-26 02:08:52 +00:00
|
|
|
close(fd);
|
2002-11-19 12:48:50 +00:00
|
|
|
errno = EBUSY;
|
2003-02-26 02:08:52 +00:00
|
|
|
return -1;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
2002-11-01 15:26:06 +00:00
|
|
|
close(fd);
|
|
|
|
|
2006-04-13 21:14:13 +00:00
|
|
|
fd = open_internal(path, O_RDONLY, false);
|
2002-11-19 12:48:50 +00:00
|
|
|
if ( fd < 0 ) {
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return fd * 10 - 2;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
|
|
|
|
2004-08-22 11:28:24 +00:00
|
|
|
/* extract new file name */
|
2002-11-19 12:48:50 +00:00
|
|
|
nameptr = strrchr(newpath,'/');
|
|
|
|
if (nameptr)
|
|
|
|
nameptr++;
|
2010-01-01 20:41:29 +00:00
|
|
|
else {
|
|
|
|
close(fd);
|
2004-08-22 11:28:24 +00:00
|
|
|
return - 3;
|
2010-01-01 20:41:29 +00:00
|
|
|
}
|
2004-08-22 11:28:24 +00:00
|
|
|
|
|
|
|
/* Extract new path */
|
|
|
|
strcpy(newpath2, newpath);
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2004-08-22 11:28:24 +00:00
|
|
|
dirptr = strrchr(newpath2,'/');
|
|
|
|
if(dirptr)
|
|
|
|
*dirptr = 0;
|
2010-01-01 20:41:29 +00:00
|
|
|
else {
|
|
|
|
close(fd);
|
2004-08-22 11:28:24 +00:00
|
|
|
return - 4;
|
2010-01-01 20:41:29 +00:00
|
|
|
}
|
2002-11-19 12:48:50 +00:00
|
|
|
|
2004-08-22 11:28:24 +00:00
|
|
|
dirptr = newpath2;
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2004-08-22 11:28:24 +00:00
|
|
|
if(strlen(dirptr) == 0) {
|
|
|
|
dirptr = "/";
|
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
dir = opendir_uncached(dirptr);
|
2010-01-01 20:41:29 +00:00
|
|
|
if(!dir) {
|
|
|
|
close(fd);
|
2004-08-22 11:28:24 +00:00
|
|
|
return - 5;
|
2010-01-01 20:41:29 +00:00
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
file = &openfiles[fd];
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2004-08-22 11:28:24 +00:00
|
|
|
rc = fat_rename(&file->fatfile, &dir->fatdir, nameptr,
|
|
|
|
file->size, file->attr);
|
2006-03-02 11:03:34 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
|
|
|
if ( rc == -1) {
|
2010-01-01 20:41:29 +00:00
|
|
|
close(fd);
|
|
|
|
closedir_uncached(dir);
|
2006-03-02 22:29:53 +00:00
|
|
|
DEBUGF("Failed renaming file across volumnes: %d\n", rc);
|
|
|
|
errno = EXDEV;
|
|
|
|
return -6;
|
2006-03-02 11:03:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-11-19 12:48:50 +00:00
|
|
|
if ( rc < 0 ) {
|
2010-01-01 20:41:29 +00:00
|
|
|
close(fd);
|
|
|
|
closedir_uncached(dir);
|
2002-11-19 12:48:50 +00:00
|
|
|
DEBUGF("Failed renaming file: %d\n", rc);
|
|
|
|
errno = EIO;
|
2006-03-02 11:03:34 +00:00
|
|
|
return rc * 10 - 7;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
|
|
|
|
2008-02-07 22:15:13 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
dircache_rename(path, newpath);
|
|
|
|
#endif
|
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
rc = close(fd);
|
|
|
|
if (rc<0) {
|
2010-01-01 20:41:29 +00:00
|
|
|
closedir_uncached(dir);
|
2002-11-19 12:48:50 +00:00
|
|
|
errno = EIO;
|
2006-03-02 11:03:34 +00:00
|
|
|
return rc * 10 - 8;
|
2004-08-22 11:28:24 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 17:06:55 +00:00
|
|
|
rc = closedir_uncached(dir);
|
2004-08-22 11:28:24 +00:00
|
|
|
if (rc<0) {
|
|
|
|
errno = EIO;
|
2006-03-02 11:03:34 +00:00
|
|
|
return rc * 10 - 9;
|
2002-11-19 12:48:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2002-11-01 15:26:06 +00:00
|
|
|
}
|
|
|
|
|
2003-12-08 21:58:38 +00:00
|
|
|
int ftruncate(int fd, off_t size)
|
2002-11-11 14:40:18 +00:00
|
|
|
{
|
|
|
|
int rc, sector;
|
2002-11-11 16:13:45 +00:00
|
|
|
struct filedesc* file = &openfiles[fd];
|
2002-11-11 14:40:18 +00:00
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
sector = size / SECTOR_SIZE;
|
|
|
|
if (size % SECTOR_SIZE)
|
2002-11-11 14:40:18 +00:00
|
|
|
sector++;
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
rc = fat_seek(&(file->fatfile), sector);
|
2002-11-11 14:40:18 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 1;
|
2002-11-11 14:40:18 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
rc = fat_truncate(&(file->fatfile));
|
2002-11-11 14:40:18 +00:00
|
|
|
if (rc < 0) {
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 2;
|
2002-11-11 14:40:18 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->size = size;
|
2006-03-28 11:51:12 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
dircache_update_filesize(fd, size, file->fatfile.firstcluster);
|
|
|
|
#endif
|
2002-11-11 14:40:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-11 15:45:43 +00:00
|
|
|
static int flush_cache(int fd)
|
|
|
|
{
|
|
|
|
int rc;
|
2002-11-11 16:13:45 +00:00
|
|
|
struct filedesc* file = &openfiles[fd];
|
2006-12-04 21:37:22 +00:00
|
|
|
long sector = file->fileoffset / SECTOR_SIZE;
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2003-03-10 21:55:59 +00:00
|
|
|
DEBUGF("Flushing dirty sector cache\n");
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2003-03-15 21:13:35 +00:00
|
|
|
/* make sure we are on correct sector */
|
|
|
|
rc = fat_seek(&(file->fatfile), sector);
|
|
|
|
if ( rc < 0 )
|
|
|
|
return rc * 10 - 3;
|
|
|
|
|
2007-10-29 00:04:35 +00:00
|
|
|
rc = fat_readwrite(&(file->fatfile), 1, file->cache, true );
|
2003-06-19 12:08:22 +00:00
|
|
|
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
if(file->fatfile.eof)
|
|
|
|
errno = ENOSPC;
|
|
|
|
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 2;
|
2003-06-19 12:08:22 +00:00
|
|
|
}
|
2002-11-11 15:45:43 +00:00
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->dirty = false;
|
2002-11-11 15:45:43 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-23 23:29:35 +00:00
|
|
|
static int readwrite(int fd, void* buf, long count, bool write)
|
2002-05-07 16:01:53 +00:00
|
|
|
{
|
2005-01-23 23:29:35 +00:00
|
|
|
long sectors;
|
|
|
|
long nread=0;
|
2009-12-23 21:51:29 +00:00
|
|
|
struct filedesc* file;
|
2003-02-26 02:08:52 +00:00
|
|
|
int rc;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
2007-10-10 23:26:17 +00:00
|
|
|
if (fd < 0 || fd > MAX_OPEN_FILES-1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-23 21:51:29 +00:00
|
|
|
|
|
|
|
file = &openfiles[fd];
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( !file->busy ) {
|
2002-05-08 12:10:30 +00:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-23 22:13:37 +00:00
|
|
|
if(file->attr & FAT_ATTR_DIRECTORY) {
|
|
|
|
errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-01-23 23:29:35 +00:00
|
|
|
LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n",
|
|
|
|
fd,(long)buf,count,write?"write":"read");
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2002-10-31 19:05:25 +00:00
|
|
|
/* attempt to read past EOF? */
|
2002-11-11 16:13:45 +00:00
|
|
|
if (!write && count > file->size - file->fileoffset)
|
|
|
|
count = file->size - file->fileoffset;
|
2002-05-08 12:10:30 +00:00
|
|
|
|
|
|
|
/* any head bytes? */
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( file->cacheoffset != -1 ) {
|
|
|
|
int offs = file->cacheoffset;
|
2006-12-04 21:37:22 +00:00
|
|
|
int headbytes = MIN(count, SECTOR_SIZE - offs);
|
2005-12-17 12:17:11 +00:00
|
|
|
|
|
|
|
if (write) {
|
|
|
|
memcpy( file->cache + offs, buf, headbytes );
|
|
|
|
file->dirty = true;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-12-17 12:17:11 +00:00
|
|
|
memcpy( buf, file->cache + offs, headbytes );
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2002-05-08 12:10:30 +00:00
|
|
|
|
2006-12-04 21:37:22 +00:00
|
|
|
if (offs + headbytes == SECTOR_SIZE) {
|
2005-12-17 12:17:11 +00:00
|
|
|
if (file->dirty) {
|
2007-04-26 10:29:41 +00:00
|
|
|
rc = flush_cache(fd);
|
2002-10-20 22:50:58 +00:00
|
|
|
if ( rc < 0 ) {
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 2;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
2005-12-17 12:17:11 +00:00
|
|
|
file->cacheoffset = -1;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
else {
|
2005-12-17 12:17:11 +00:00
|
|
|
file->cacheoffset += headbytes;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2002-05-08 12:10:30 +00:00
|
|
|
|
|
|
|
nread = headbytes;
|
|
|
|
count -= headbytes;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2004-11-17 02:34:17 +00:00
|
|
|
/* 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. */
|
2002-11-11 13:57:58 +00:00
|
|
|
|
2003-06-19 12:08:22 +00:00
|
|
|
/* read/write whole sectors right into/from the supplied buffer */
|
2006-12-04 21:37:22 +00:00
|
|
|
sectors = count / SECTOR_SIZE;
|
2002-05-07 16:01:53 +00:00
|
|
|
if ( sectors ) {
|
2007-04-26 10:29:41 +00:00
|
|
|
rc = fat_readwrite(&(file->fatfile), sectors,
|
2004-10-01 19:45:51 +00:00
|
|
|
(unsigned char*)buf+nread, write );
|
2002-05-08 12:10:30 +00:00
|
|
|
if ( rc < 0 ) {
|
2005-01-23 23:29:35 +00:00
|
|
|
DEBUGF("Failed read/writing %ld sectors\n",sectors);
|
2002-05-08 12:10:30 +00:00
|
|
|
errno = EIO;
|
2003-06-19 12:08:22 +00:00
|
|
|
if(write && file->fatfile.eof) {
|
|
|
|
DEBUGF("No space left on device\n");
|
|
|
|
errno = ENOSPC;
|
|
|
|
} else {
|
|
|
|
file->fileoffset += nread;
|
|
|
|
}
|
2003-04-15 08:07:50 +00:00
|
|
|
file->cacheoffset = -1;
|
2007-12-13 23:36:22 +00:00
|
|
|
/* adjust file size to length written */
|
|
|
|
if ( write && file->fileoffset > file->size )
|
|
|
|
{
|
|
|
|
file->size = file->fileoffset;
|
|
|
|
#ifdef HAVE_DIRCACHE
|
|
|
|
dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
|
|
|
|
#endif
|
|
|
|
}
|
2003-03-12 15:06:57 +00:00
|
|
|
return nread ? nread : rc * 10 - 4;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2002-05-08 12:10:30 +00:00
|
|
|
else {
|
|
|
|
if ( rc > 0 ) {
|
2006-12-04 21:37:22 +00:00
|
|
|
nread += rc * SECTOR_SIZE;
|
|
|
|
count -= sectors * SECTOR_SIZE;
|
2002-10-31 19:05:25 +00:00
|
|
|
|
|
|
|
/* if eof, skip tail bytes */
|
|
|
|
if ( rc < sectors )
|
|
|
|
count = 0;
|
2002-05-08 12:10:30 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* eof */
|
|
|
|
count=0;
|
|
|
|
}
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->cacheoffset = -1;
|
2002-05-08 12:10:30 +00:00
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
2002-05-08 12:10:30 +00:00
|
|
|
/* any tail bytes? */
|
2002-05-07 16:01:53 +00:00
|
|
|
if ( count ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
if (write) {
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( file->fileoffset + nread < file->size ) {
|
2002-11-11 11:16:49 +00:00
|
|
|
/* sector is only partially filled. copy-back from disk */
|
|
|
|
LDEBUGF("Copy-back tail cache\n");
|
2003-04-15 08:07:50 +00:00
|
|
|
rc = fat_readwrite(&(file->fatfile), 1, file->cache, false );
|
2002-11-11 11:16:49 +00:00
|
|
|
if ( rc < 0 ) {
|
2003-02-22 01:54:03 +00:00
|
|
|
DEBUGF("Failed writing\n");
|
2002-11-11 11:16:49 +00:00
|
|
|
errno = EIO;
|
2003-04-15 08:07:50 +00:00
|
|
|
file->fileoffset += nread;
|
|
|
|
file->cacheoffset = -1;
|
2007-12-13 23:36:22 +00:00
|
|
|
/* 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
|
|
|
|
}
|
2003-03-12 15:06:57 +00:00
|
|
|
return nread ? nread : rc * 10 - 5;
|
2002-11-11 11:16:49 +00:00
|
|
|
}
|
|
|
|
/* seek back one sector to put file position right */
|
2002-11-11 16:13:45 +00:00
|
|
|
rc = fat_seek(&(file->fatfile),
|
2007-10-29 00:04:35 +00:00
|
|
|
(file->fileoffset + nread) /
|
2006-12-04 21:37:22 +00:00
|
|
|
SECTOR_SIZE);
|
2002-11-11 11:16:49 +00:00
|
|
|
if ( rc < 0 ) {
|
|
|
|
DEBUGF("fat_seek() failed\n");
|
|
|
|
errno = EIO;
|
2003-04-15 08:07:50 +00:00
|
|
|
file->fileoffset += nread;
|
|
|
|
file->cacheoffset = -1;
|
2007-12-13 23:36:22 +00:00
|
|
|
/* 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
|
|
|
|
}
|
2003-03-12 15:06:57 +00:00
|
|
|
return nread ? nread : rc * 10 - 6;
|
2002-11-11 11:16:49 +00:00
|
|
|
}
|
2002-11-11 10:21:51 +00:00
|
|
|
}
|
2004-10-01 19:45:51 +00:00
|
|
|
memcpy( file->cache, (unsigned char*)buf + nread, count );
|
2002-11-11 16:13:45 +00:00
|
|
|
file->dirty = true;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
else {
|
2010-05-20 12:59:12 +00:00
|
|
|
rc = fat_readwrite(&(file->fatfile), 1, file->cache,false);
|
2003-02-26 02:08:52 +00:00
|
|
|
if (rc < 1 ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
DEBUGF("Failed caching sector\n");
|
|
|
|
errno = EIO;
|
2003-04-15 08:07:50 +00:00
|
|
|
file->fileoffset += nread;
|
|
|
|
file->cacheoffset = -1;
|
2003-03-12 15:06:57 +00:00
|
|
|
return nread ? nread : rc * 10 - 7;
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2004-10-01 19:45:51 +00:00
|
|
|
memcpy( (unsigned char*)buf + nread, file->cache, count );
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2002-05-08 12:10:30 +00:00
|
|
|
nread += count;
|
2002-11-11 16:13:45 +00:00
|
|
|
file->cacheoffset = count;
|
2002-05-07 16:01:53 +00:00
|
|
|
}
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->fileoffset += nread;
|
2005-01-23 23:29:35 +00:00
|
|
|
LDEBUGF("fileoffset: %ld\n", file->fileoffset);
|
2002-10-31 19:05:25 +00:00
|
|
|
|
|
|
|
/* adjust file size to length written */
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( write && file->fileoffset > file->size )
|
2005-10-07 17:38:05 +00:00
|
|
|
{
|
2002-11-11 16:13:45 +00:00
|
|
|
file->size = file->fileoffset;
|
2005-10-07 17:38:05 +00:00
|
|
|
#ifdef HAVE_DIRCACHE
|
2006-03-28 11:51:12 +00:00
|
|
|
dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
|
2005-10-07 17:38:05 +00:00
|
|
|
#endif
|
|
|
|
}
|
2002-10-31 19:05:25 +00:00
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
2003-12-08 21:58:38 +00:00
|
|
|
ssize_t write(int fd, const void* buf, size_t count)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
2002-11-11 13:57:58 +00:00
|
|
|
if (!openfiles[fd].write) {
|
|
|
|
errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
2003-12-08 21:58:38 +00:00
|
|
|
return readwrite(fd, (void *)buf, count, true);
|
2002-10-20 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2003-12-08 21:58:38 +00:00
|
|
|
ssize_t read(int fd, void* buf, size_t count)
|
2002-10-20 22:50:58 +00:00
|
|
|
{
|
|
|
|
return readwrite(fd, buf, count, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-12-08 21:58:38 +00:00
|
|
|
off_t lseek(int fd, off_t offset, int whence)
|
2002-05-08 15:16:02 +00:00
|
|
|
{
|
2005-01-23 23:29:35 +00:00
|
|
|
off_t pos;
|
|
|
|
long newsector;
|
|
|
|
long oldsector;
|
2002-05-08 15:16:02 +00:00
|
|
|
int sectoroffset;
|
|
|
|
int rc;
|
2002-11-11 16:13:45 +00:00
|
|
|
struct filedesc* file = &openfiles[fd];
|
2002-05-08 15:16:02 +00:00
|
|
|
|
2005-01-23 23:29:35 +00:00
|
|
|
LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
2007-10-10 23:26:17 +00:00
|
|
|
if (fd < 0 || fd > MAX_OPEN_FILES-1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( !file->busy ) {
|
2002-05-08 15:16:02 +00:00
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( whence ) {
|
|
|
|
case SEEK_SET:
|
|
|
|
pos = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
2002-11-11 16:13:45 +00:00
|
|
|
pos = file->fileoffset + offset;
|
2002-05-08 15:16:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
2002-11-11 16:13:45 +00:00
|
|
|
pos = file->size + offset;
|
2002-05-08 15:16:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
2002-10-20 22:50:58 +00:00
|
|
|
return -2;
|
2002-05-08 15:16:02 +00:00
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
if ((pos < 0) || (pos > file->size)) {
|
2002-05-08 15:16:02 +00:00
|
|
|
errno = EINVAL;
|
2002-10-20 22:50:58 +00:00
|
|
|
return -3;
|
2002-05-08 15:16:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* new sector? */
|
2006-12-04 21:37:22 +00:00
|
|
|
newsector = pos / SECTOR_SIZE;
|
|
|
|
oldsector = file->fileoffset / SECTOR_SIZE;
|
|
|
|
sectoroffset = pos % SECTOR_SIZE;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
|
|
|
if ( (newsector != oldsector) ||
|
2002-11-11 16:13:45 +00:00
|
|
|
((file->cacheoffset==-1) && sectoroffset) ) {
|
2002-11-11 15:45:43 +00:00
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
if ( newsector != oldsector ) {
|
2002-11-11 16:13:45 +00:00
|
|
|
if (file->dirty) {
|
2003-02-26 02:08:52 +00:00
|
|
|
rc = flush_cache(fd);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc * 10 - 5;
|
2002-11-11 15:45:43 +00:00
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
rc = fat_seek(&(file->fatfile), newsector);
|
2002-05-08 15:16:02 +00:00
|
|
|
if ( rc < 0 ) {
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 4;
|
2002-05-08 15:16:02 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-31 20:41:36 +00:00
|
|
|
if ( sectoroffset ) {
|
2010-05-20 12:59:12 +00:00
|
|
|
rc = fat_readwrite(&(file->fatfile), 1, file->cache ,false);
|
2002-10-31 20:41:36 +00:00
|
|
|
if ( rc < 0 ) {
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
return rc * 10 - 6;
|
2002-10-31 20:41:36 +00:00
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
file->cacheoffset = sectoroffset;
|
2002-05-08 15:16:02 +00:00
|
|
|
}
|
2002-11-04 14:59:46 +00:00
|
|
|
else
|
2002-11-11 16:13:45 +00:00
|
|
|
file->cacheoffset = -1;
|
2002-05-08 15:16:02 +00:00
|
|
|
}
|
2002-06-26 14:21:25 +00:00
|
|
|
else
|
2002-11-11 16:13:45 +00:00
|
|
|
if ( file->cacheoffset != -1 )
|
|
|
|
file->cacheoffset = sectoroffset;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
file->fileoffset = pos;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2003-03-18 00:39:57 +00:00
|
|
|
|
2005-01-23 23:29:35 +00:00
|
|
|
off_t filesize(int fd)
|
2003-03-18 00:39:57 +00:00
|
|
|
{
|
|
|
|
struct filedesc* file = &openfiles[fd];
|
|
|
|
|
2007-10-10 23:26:17 +00:00
|
|
|
if (fd < 0 || fd > MAX_OPEN_FILES-1) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2003-03-18 00:39:57 +00:00
|
|
|
if ( !file->busy ) {
|
|
|
|
errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2007-10-29 00:04:35 +00:00
|
|
|
|
2003-03-18 00:39:57 +00:00
|
|
|
return file->size;
|
|
|
|
}
|
2005-01-28 21:32:16 +00:00
|
|
|
|
|
|
|
|
2007-10-29 00:04:35 +00:00
|
|
|
/* release all file handles on a given volume "by force", to avoid leaks */
|
2005-01-28 21:32:16 +00:00
|
|
|
int release_files(int volume)
|
|
|
|
{
|
|
|
|
struct filedesc* pfile = openfiles;
|
|
|
|
int fd;
|
|
|
|
int closed = 0;
|
|
|
|
for ( fd=0; fd<MAX_OPEN_FILES; fd++, pfile++)
|
|
|
|
{
|
2009-11-26 16:57:30 +00:00
|
|
|
#ifdef HAVE_MULTIVOLUME
|
2005-01-28 21:32:16 +00:00
|
|
|
if (pfile->fatfile.volume == volume)
|
2009-11-26 16:57:30 +00:00
|
|
|
#else
|
|
|
|
(void)volume;
|
|
|
|
#endif
|
2005-01-28 21:32:16 +00:00
|
|
|
{
|
|
|
|
pfile->busy = false; /* mark as available, no further action */
|
|
|
|
closed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return closed; /* return how many we did */
|
|
|
|
}
|