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.
|
|
|
|
|
*/
|
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
|
#define MAX_OPEN_FILES 4
|
|
|
|
|
|
|
|
|
|
struct filedesc {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
unsigned char cache[SECTOR_SIZE];
|
|
|
|
|
int cacheoffset;
|
|
|
|
|
int fileoffset;
|
|
|
|
|
int size;
|
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-05-07 16:01:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct filedesc openfiles[MAX_OPEN_FILES];
|
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
int creat(const char *pathname, int mode)
|
|
|
|
|
{
|
|
|
|
|
(void)mode;
|
|
|
|
|
return open(pathname, O_WRONLY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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-10-20 22:50:58 +00:00
|
|
|
|
switch ( flags ) {
|
|
|
|
|
case O_RDONLY:
|
|
|
|
|
openfiles[fd].write = false;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case O_WRONLY:
|
|
|
|
|
openfiles[fd].write = true;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
DEBUGF("Only O_RDONLY and O_WRONLY is supported\n");
|
|
|
|
|
errno = EROFS;
|
|
|
|
|
return -3;
|
|
|
|
|
}
|
2002-05-27 09:13:56 +00:00
|
|
|
|
openfiles[fd].busy = true;
|
|
|
|
|
|
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-05-27 09:13:56 +00:00
|
|
|
|
openfiles[fd].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,
|
|
|
|
|
&(openfiles[fd].fatfile),
|
|
|
|
|
&(dir->fatdir));
|
2002-05-08 12:10:30 +00:00
|
|
|
|
openfiles[fd].size = entry->size;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
closedir(dir);
|
|
|
|
|
if ( !entry ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
|
LDEBUGF("Didn't find file %s\n",name);
|
|
|
|
|
if ( openfiles[fd].write ) {
|
|
|
|
|
if (fat_create_file(name,
|
|
|
|
|
&(openfiles[fd].fatfile),
|
|
|
|
|
&(dir->fatdir)) < 0) {
|
|
|
|
|
DEBUGF("Couldn't create %s in %s\n",name,pathname);
|
|
|
|
|
errno = EIO;
|
|
|
|
|
openfiles[fd].busy = false;
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
|
|
|
|
openfiles[fd].size = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
DEBUGF("Couldn't find %s in %s\n",name,pathname);
|
|
|
|
|
errno = ENOENT;
|
|
|
|
|
openfiles[fd].busy = false;
|
|
|
|
|
return -6;
|
|
|
|
|
}
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
|
openfiles[fd].cacheoffset = -1;
|
2002-05-08 12:10:30 +00:00
|
|
|
|
openfiles[fd].fileoffset = 0;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int close(int fd)
|
|
|
|
|
{
|
2002-10-20 22:50:58 +00:00
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
|
|
LDEBUGF("close(%d)\n",fd);
|
|
|
|
|
|
2002-08-24 09:47:54 +00:00
|
|
|
|
if (fd < 0 || fd > MAX_OPEN_FILES-1) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (!openfiles[fd].busy) {
|
|
|
|
|
errno = EBADF;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
if (openfiles[fd].write) {
|
|
|
|
|
/* flush sector cache */
|
|
|
|
|
if ( openfiles[fd].cacheoffset != -1 ) {
|
|
|
|
|
if ( fat_readwrite(&(openfiles[fd].fatfile), 1,
|
|
|
|
|
&(openfiles[fd].cache),true) < 0 ) {
|
|
|
|
|
DEBUGF("Failed flushing cache\n");
|
|
|
|
|
errno = EIO;
|
|
|
|
|
rc = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* tie up all loose ends */
|
2002-10-22 15:06:08 +00:00
|
|
|
|
fat_closewrite(&(openfiles[fd].fatfile), openfiles[fd].fileoffset);
|
2002-08-24 09:47:54 +00:00
|
|
|
|
}
|
2002-05-13 12:29:34 +00:00
|
|
|
|
openfiles[fd].busy = false;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
return rc;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-05-08 12:10:30 +00:00
|
|
|
|
if ( !openfiles[fd].busy ) {
|
|
|
|
|
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? */
|
|
|
|
|
if (!write && count > openfiles[fd].size - openfiles[fd].fileoffset)
|
|
|
|
|
count = openfiles[fd].size - openfiles[fd].fileoffset;
|
2002-05-08 12:10:30 +00:00
|
|
|
|
|
|
|
|
|
/* any head bytes? */
|
2002-05-08 15:16:02 +00:00
|
|
|
|
if ( openfiles[fd].cacheoffset != -1 ) {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
int headbytes;
|
|
|
|
|
int offs = openfiles[fd].cacheoffset;
|
|
|
|
|
if ( count <= SECTOR_SIZE - openfiles[fd].cacheoffset ) {
|
|
|
|
|
headbytes = count;
|
|
|
|
|
openfiles[fd].cacheoffset += count;
|
|
|
|
|
if ( openfiles[fd].cacheoffset >= SECTOR_SIZE )
|
2002-06-24 07:46:19 +00:00
|
|
|
|
openfiles[fd].cacheoffset = -1;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2002-05-08 12:10:30 +00:00
|
|
|
|
headbytes = SECTOR_SIZE - openfiles[fd].cacheoffset;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
openfiles[fd].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) {
|
|
|
|
|
memcpy( openfiles[fd].cache + offs, buf, headbytes );
|
|
|
|
|
if (offs+headbytes == SECTOR_SIZE) {
|
|
|
|
|
int rc = fat_readwrite(&(openfiles[fd].fatfile), 1,
|
|
|
|
|
openfiles[fd].cache, true );
|
|
|
|
|
if ( rc < 0 ) {
|
2002-10-21 07:26:12 +00:00
|
|
|
|
DEBUGF("Failed read/writing\n");
|
2002-10-20 22:50:58 +00:00
|
|
|
|
errno = EIO;
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
openfiles[fd].cacheoffset = -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
memcpy( buf, openfiles[fd].cache + offs, headbytes );
|
|
|
|
|
}
|
2002-05-08 12:10:30 +00:00
|
|
|
|
|
|
|
|
|
nread = headbytes;
|
|
|
|
|
count -= headbytes;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* read whole sectors right into the supplied buffer */
|
|
|
|
|
sectors = count / SECTOR_SIZE;
|
|
|
|
|
if ( sectors ) {
|
2002-10-20 22:50:58 +00:00
|
|
|
|
int rc = fat_readwrite(&(openfiles[fd].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;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
return -2;
|
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-05-08 15:16:02 +00:00
|
|
|
|
openfiles[fd].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) {
|
|
|
|
|
memcpy( openfiles[fd].cache, buf + nread, count );
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
2002-10-20 22:50:58 +00:00
|
|
|
|
else {
|
|
|
|
|
if ( fat_readwrite(&(openfiles[fd].fatfile), 1,
|
|
|
|
|
&(openfiles[fd].cache),false) < 0 ) {
|
|
|
|
|
DEBUGF("Failed caching sector\n");
|
|
|
|
|
errno = EIO;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
memcpy( buf + nread, openfiles[fd].cache, count );
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-08 12:10:30 +00:00
|
|
|
|
nread += count;
|
|
|
|
|
openfiles[fd].cacheoffset = count;
|
2002-05-07 16:01:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-05-08 12:10:30 +00:00
|
|
|
|
openfiles[fd].fileoffset += nread;
|
2002-10-31 19:05:25 +00:00
|
|
|
|
LDEBUGF("fileoffset: %d\n", openfiles[fd].fileoffset);
|
|
|
|
|
|
|
|
|
|
/* adjust file size to length written */
|
|
|
|
|
if ( write && openfiles[fd].fileoffset > openfiles[fd].size )
|
|
|
|
|
openfiles[fd].size = openfiles[fd].fileoffset;
|
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
|
return nread;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
int write(int fd, void* buf, int count)
|
|
|
|
|
{
|
|
|
|
|
return readwrite(fd, buf, count, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int read(int fd, void* buf, int count)
|
|
|
|
|
{
|
|
|
|
|
return readwrite(fd, buf, count, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
|
int lseek(int fd, int offset, int whence)
|
|
|
|
|
{
|
|
|
|
|
int pos;
|
|
|
|
|
int newsector;
|
|
|
|
|
int oldsector;
|
|
|
|
|
int sectoroffset;
|
|
|
|
|
int rc;
|
|
|
|
|
|
2002-10-20 22:50:58 +00:00
|
|
|
|
LDEBUGF("lseek(%d,%d,%d)\n",fd,offset,whence);
|
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
|
if ( !openfiles[fd].busy ) {
|
|
|
|
|
errno = EBADF;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-22 15:06:08 +00:00
|
|
|
|
if ( openfiles[fd].write ) {
|
|
|
|
|
DEBUGF("lseek() is not supported when writing\n");
|
|
|
|
|
errno = EROFS;
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-05-08 15:16:02 +00:00
|
|
|
|
switch ( whence ) {
|
|
|
|
|
case SEEK_SET:
|
|
|
|
|
pos = offset;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEEK_CUR:
|
|
|
|
|
pos = openfiles[fd].fileoffset + offset;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case SEEK_END:
|
2002-05-27 12:38:41 +00:00
|
|
|
|
pos = openfiles[fd].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-05-27 12:38:41 +00:00
|
|
|
|
if ((pos < 0) || (pos > openfiles[fd].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;
|
|
|
|
|
oldsector = openfiles[fd].fileoffset / SECTOR_SIZE;
|
|
|
|
|
sectoroffset = pos % SECTOR_SIZE;
|
|
|
|
|
|
|
|
|
|
if ( (newsector != oldsector) ||
|
|
|
|
|
((openfiles[fd].cacheoffset==-1) && sectoroffset) ) {
|
|
|
|
|
if ( newsector != oldsector ) {
|
|
|
|
|
rc = fat_seek(&(openfiles[fd].fatfile), newsector);
|
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
|
errno = EIO;
|
2002-10-20 22:50:58 +00:00
|
|
|
|
return -4;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-10-31 20:41:36 +00:00
|
|
|
|
if ( sectoroffset ) {
|
|
|
|
|
rc = fat_readwrite(&(openfiles[fd].fatfile), 1,
|
|
|
|
|
&(openfiles[fd].cache),false);
|
|
|
|
|
if ( rc < 0 ) {
|
|
|
|
|
errno = EIO;
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
|
|
|
|
openfiles[fd].cacheoffset = sectoroffset;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-06-26 14:21:25 +00:00
|
|
|
|
else
|
|
|
|
|
if ( openfiles[fd].cacheoffset != -1 )
|
|
|
|
|
openfiles[fd].cacheoffset = sectoroffset;
|
2002-05-08 15:16:02 +00:00
|
|
|
|
|
|
|
|
|
openfiles[fd].fileoffset = pos;
|
|
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-07 16:01:53 +00:00
|
|
|
|
/*
|
|
|
|
|
* local variables:
|
|
|
|
|
* eval: (load-file "../rockbox-mode.el")
|
|
|
|
|
* end:
|
|
|
|
|
*/
|