2002-05-07 16:01:53 +00:00
|
|
|
|
/***************************************************************************
|
|
|
|
|
* __________ __ ___.
|
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2002 by Bj<EFBFBD>rn Stenberg
|
|
|
|
|
*
|
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
|
*
|
|
|
|
|
* 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"
|
|
|
|
|
#include "dir.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2003-07-01 21:05:43 +00:00
|
|
|
|
#define MAX_OPEN_FILES 8
|
2002-05-07 16:01:53 +00:00
|
|
|
|
|
|
|
|
|
struct filedesc {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
unsigned char cache[SECTOR_SIZE];
|
|
|
|
|
int cacheoffset;
|
2002-11-11 13:57:58 +00:00
|
|
|
|
int fileoffset;
|
2002-05-08 12:10:30 +00:00
|
|
|
|
int 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);
|
|
|
|
|
|
2003-12-08 21:58:38 +00:00
|
|
|
|
int creat(const char *pathname, mode_t mode)
|
2002-10-20 22:50:58 +00:00
|
|
|
|
{
|
|
|
|
|
(void)mode;
|
2002-11-13 23:25:46 +00:00
|
|
|
|
return open(pathname, O_WRONLY|O_CREAT|O_TRUNC);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int open(const char* pathname, int flags)
|
2002-05-07 16:01:53 +00:00
|
|
|
|
{
|
|
|
|
|
DIR* dir;
|
|
|
|
|
struct dirent* entry;
|
|
|
|
|
int fd;
|
|
|
|
|
char* name;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
struct filedesc* file = NULL;
|
2003-02-26 02:08:52 +00:00
|
|
|
|
int rc;
|
2002-05-07 16:01:53 +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;
|
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
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
|
/* locate filename */
|
|
|
|
|
name=strrchr(pathname+1,'/');
|
|
|
|
|
if ( name ) {
|
|
|
|
|
*name = 0;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
dir = opendir((char*)pathname);
|
2002-05-07 16:01:53 +00:00
|
|
|
|
*name = '/';
|
|
|
|
|
name++;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dir = opendir("/");
|
2002-10-20 22:50:58 +00:00
|
|
|
|
name = (char*)pathname+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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* scan dir for name */
|
|
|
|
|
while ((entry = readdir(dir))) {
|
2002-08-22 20:13:21 +00:00
|
|
|
|
if ( !strcasecmp(name, entry->d_name) ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
|
fat_open(entry->startcluster,
|
2002-11-11 16:13:45 +00:00
|
|
|
|
&(file->fatfile),
|
2002-10-20 22:50:58 +00:00
|
|
|
|
&(dir->fatdir));
|
2002-11-11 16:13:45 +00:00
|
|
|
|
file->size = entry->size;
|
2003-01-27 09:32:17 +00:00
|
|
|
|
file->attr = entry->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) {
|
2002-10-20 22:50:58 +00:00
|
|
|
|
DEBUGF("Couldn't create %s in %s\n",name,pathname);
|
|
|
|
|
errno = EIO;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
file->busy = false;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
closedir(dir);
|
2003-02-26 02:08:52 +00:00
|
|
|
|
return rc * 10 - 5;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
}
|
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 {
|
|
|
|
|
DEBUGF("Couldn't find %s in %s\n",name,pathname);
|
|
|
|
|
errno = ENOENT;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
file->busy = false;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
closedir(dir);
|
2002-10-20 22:50:58 +00:00
|
|
|
|
return -6;
|
|
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
2002-11-19 12:48:50 +00:00
|
|
|
|
closedir(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 )
|
|
|
|
|
return rc * 10 - 7;
|
2002-11-11 15:45:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
return rc * 10 - 3;
|
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) {
|
2003-02-26 02:08:52 +00:00
|
|
|
|
rc = ftruncate(fd, file->fileoffset);
|
|
|
|
|
if (rc < 0)
|
|
|
|
|
return rc * 10 - 4;
|
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;
|
2002-11-01 15:26:06 +00:00
|
|
|
|
int fd = open(name, O_WRONLY);
|
|
|
|
|
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];
|
|
|
|
|
rc = fat_truncate(&(file->fatfile));
|
2002-11-11 10:21:51 +00:00
|
|
|
|
if ( rc < 0 ) {
|
2002-11-19 12:48:50 +00:00
|
|
|
|
DEBUGF("Failed truncating 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 - 2;
|
2002-11-11 10:21:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
char* nameptr;
|
|
|
|
|
struct filedesc* file;
|
|
|
|
|
|
|
|
|
|
/* verify new path does not already exist */
|
|
|
|
|
fd = open(newpath, O_RDONLY);
|
|
|
|
|
if ( fd >= 0 ) {
|
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);
|
|
|
|
|
|
2002-11-19 12:48:50 +00:00
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* strip path */
|
|
|
|
|
nameptr = strrchr(newpath,'/');
|
|
|
|
|
if (nameptr)
|
|
|
|
|
nameptr++;
|
|
|
|
|
else
|
|
|
|
|
nameptr = (char*)newpath;
|
|
|
|
|
|
|
|
|
|
file = &openfiles[fd];
|
2003-01-27 09:32:17 +00:00
|
|
|
|
rc = fat_rename(&file->fatfile, nameptr, file->size, file->attr);
|
2002-11-19 12:48:50 +00:00
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
|
DEBUGF("Failed renaming file: %d\n", rc);
|
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
|
return rc * 10 - 3;
|
2002-11-19 12:48:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = close(fd);
|
|
|
|
|
if (rc<0) {
|
|
|
|
|
errno = EIO;
|
2003-02-26 02:08:52 +00:00
|
|
|
|
return rc * 10 - 4;
|
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
|
|
|
|
|
|
|
|
|
sector = size / SECTOR_SIZE;
|
|
|
|
|
if (size % SECTOR_SIZE)
|
|
|
|
|
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;
|
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];
|
2003-03-15 21:13:35 +00:00
|
|
|
|
int sector = file->fileoffset / SECTOR_SIZE;
|
|
|
|
|
|
2003-03-10 21:55:59 +00:00
|
|
|
|
DEBUGF("Flushing dirty sector cache\n");
|
|
|
|
|
|
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;
|
|
|
|
|
|
2002-11-11 16:13:45 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
static int readwrite(int fd, void* buf, int count, bool write)
|
2002-05-07 16:01:53 +00:00
|
|
|
|
{
|
|
|
|
|
int sectors;
|
|
|
|
|
int nread=0;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
struct filedesc* file = &openfiles[fd];
|
2003-02-26 02:08:52 +00:00
|
|
|
|
int rc;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
|
2002-11-11 16:13:45 +00:00
|
|
|
|
if ( !file->busy ) {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
errno = EBADF;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
LDEBUGF( "readwrite(%d,%x,%d,%s)\n",
|
|
|
|
|
fd,buf,count,write?"write":"read");
|
|
|
|
|
|
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 ) {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
int headbytes;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
int offs = file->cacheoffset;
|
|
|
|
|
if ( count <= SECTOR_SIZE - file->cacheoffset ) {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
headbytes = count;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
file->cacheoffset += count;
|
|
|
|
|
if ( file->cacheoffset >= SECTOR_SIZE )
|
|
|
|
|
file->cacheoffset = -1;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2002-11-11 16:13:45 +00:00
|
|
|
|
headbytes = SECTOR_SIZE - file->cacheoffset;
|
|
|
|
|
file->cacheoffset = -1;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
2002-05-08 12:10:30 +00:00
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
if (write) {
|
2002-11-11 16:13:45 +00:00
|
|
|
|
memcpy( file->cache + offs, buf, headbytes );
|
2002-10-20 22:50:58 +00:00
|
|
|
|
if (offs+headbytes == SECTOR_SIZE) {
|
2002-12-19 00:51:39 +00:00
|
|
|
|
int 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
|
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
|
file->cacheoffset = -1;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
}
|
2002-11-11 13:57:58 +00:00
|
|
|
|
else
|
2002-11-11 16:13:45 +00:00
|
|
|
|
file->dirty = true;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2002-11-11 16:13:45 +00:00
|
|
|
|
memcpy( buf, file->cache + offs, 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
|
|
|
|
}
|
|
|
|
|
|
2002-11-11 13:57:58 +00:00
|
|
|
|
/* if buffer has been modified, write it back to disk */
|
2003-03-12 15:15:46 +00:00
|
|
|
|
if (nread && file->dirty) {
|
2003-02-26 02:08:52 +00:00
|
|
|
|
rc = flush_cache(fd);
|
|
|
|
|
if (rc < 0)
|
2003-03-12 15:15:46 +00:00
|
|
|
|
return rc * 10 - 3;
|
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 */
|
2002-05-07 16:01:53 +00:00
|
|
|
|
sectors = count / SECTOR_SIZE;
|
|
|
|
|
if ( sectors ) {
|
2003-04-15 08:07:50 +00:00
|
|
|
|
int rc = fat_readwrite(&(file->fatfile), sectors, buf+nread, write );
|
2002-05-08 12:10:30 +00:00
|
|
|
|
if ( rc < 0 ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
|
DEBUGF("Failed read/writing %d 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;
|
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 ) {
|
2002-10-31 19:05:25 +00:00
|
|
|
|
nread += rc * SECTOR_SIZE;
|
2002-05-08 12:10:30 +00:00
|
|
|
|
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 */
|
|
|
|
|
int rc;
|
|
|
|
|
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;
|
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),
|
|
|
|
|
(file->fileoffset + nread) /
|
2002-11-11 11:16:49 +00:00
|
|
|
|
SECTOR_SIZE);
|
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
|
DEBUGF("fat_seek() failed\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 - 6;
|
2002-11-11 11:16:49 +00:00
|
|
|
|
}
|
2002-11-11 10:21:51 +00:00
|
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
|
memcpy( file->cache, buf + nread, count );
|
|
|
|
|
file->dirty = true;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
|
else {
|
2003-02-26 02:08:52 +00:00
|
|
|
|
rc = fat_readwrite(&(file->fatfile), 1, &(file->cache),false);
|
|
|
|
|
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
|
|
|
|
}
|
2002-11-11 16:13:45 +00:00
|
|
|
|
memcpy( buf + nread, file->cache, count );
|
2002-10-20 22:50:58 +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;
|
|
|
|
|
LDEBUGF("fileoffset: %d\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 )
|
|
|
|
|
file->size = file->fileoffset;
|
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
|
|
|
|
{
|
|
|
|
|
int pos;
|
|
|
|
|
int newsector;
|
|
|
|
|
int oldsector;
|
|
|
|
|
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
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
LDEBUGF("lseek(%d,%d,%d)\n",fd,offset,whence);
|
|
|
|
|
|
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? */
|
|
|
|
|
newsector = pos / SECTOR_SIZE;
|
2002-11-11 16:13:45 +00:00
|
|
|
|
oldsector = file->fileoffset / SECTOR_SIZE;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
sectoroffset = pos % SECTOR_SIZE;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
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 ) {
|
2002-11-11 16:13:45 +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
|
|
|
|
|
|
|
|
|
int filesize(int fd)
|
|
|
|
|
{
|
|
|
|
|
struct filedesc* file = &openfiles[fd];
|
|
|
|
|
|
|
|
|
|
if ( !file->busy ) {
|
|
|
|
|
errno = EBADF;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file->size;
|
|
|
|
|
}
|