1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Björn Stenberg
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #include "dir_uncached.h"
29 #include "filefuncs.h"
33 These functions provide a roughly POSIX-compatible file IO API.
35 Since the fat32 driver only manages sectors, we maintain a one-sector
36 cache for each open file. This way we can provide byte access without
37 having to re-read the sector each time.
38 The penalty is the RAM used for the cache and slightly more complex code.
42 unsigned char cache
[SECTOR_SIZE
];
43 int cacheoffset
; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
47 struct fat_file fatfile
;
54 static struct filedesc openfiles
[MAX_OPEN_FILES
];
56 static int flush_cache(int fd
);
58 int creat(const char *pathname
)
60 return open(pathname
, O_WRONLY
|O_CREAT
|O_TRUNC
);
63 static int open_internal(const char* pathname
, int flags
, bool use_cache
)
66 struct dirent_uncached
* entry
;
68 char pathnamecopy
[MAX_PATH
];
70 struct filedesc
* file
= NULL
;
76 LDEBUGF("open(\"%s\",%d)\n",pathname
,flags
);
78 if ( pathname
[0] != '/' ) {
79 DEBUGF("'%s' is not an absolute path.\n",pathname
);
80 DEBUGF("Only absolute pathnames supported at the moment\n");
85 /* find a free file descriptor */
86 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++ )
87 if ( !openfiles
[fd
].busy
)
90 if ( fd
== MAX_OPEN_FILES
) {
91 DEBUGF("Too many files open\n");
96 file
= &openfiles
[fd
];
97 memset(file
, 0, sizeof(struct filedesc
));
99 if (flags
& (O_RDWR
| O_WRONLY
)) {
108 if (dircache_is_enabled() && !file
->write
&& use_cache
)
110 const struct dircache_entry
*ce
;
111 # ifdef HAVE_MULTIVOLUME
112 int volume
= strip_volume(pathname
, pathnamecopy
);
115 ce
= dircache_get_entry_ptr(pathname
);
123 fat_open(IF_MV2(volume
,)
127 file
->size
= ce
->size
;
128 file
->attr
= ce
->attribute
;
129 file
->cacheoffset
= -1;
130 file
->fileoffset
= 0;
136 strlcpy(pathnamecopy
, pathname
, sizeof(pathnamecopy
));
138 /* locate filename */
139 name
=strrchr(pathnamecopy
+1,'/');
142 dir
= opendir_uncached(pathnamecopy
);
147 dir
= opendir_uncached("/");
148 name
= pathnamecopy
+1;
151 DEBUGF("Failed opening dir\n");
158 DEBUGF("Empty file name\n");
161 closedir_uncached(dir
);
165 /* scan dir for name */
166 while ((entry
= readdir_uncached(dir
))) {
167 if ( !strcasecmp(name
, entry
->d_name
) ) {
168 fat_open(IF_MV2(dir
->fatdir
.file
.volume
,)
172 file
->size
= file
->trunc
? 0 : entry
->size
;
173 file
->attr
= entry
->attribute
;
179 LDEBUGF("Didn't find file %s\n",name
);
180 if ( file
->write
&& (flags
& O_CREAT
) ) {
181 rc
= fat_create_file(name
,
185 DEBUGF("Couldn't create %s in %s\n",name
,pathnamecopy
);
188 closedir_uncached(dir
);
192 dircache_add_file(pathname
, file
->fatfile
.firstcluster
);
198 DEBUGF("Couldn't find %s in %s\n",name
,pathnamecopy
);
201 closedir_uncached(dir
);
205 if(file
->write
&& (file
->attr
& FAT_ATTR_DIRECTORY
)) {
208 closedir_uncached(dir
);
212 closedir_uncached(dir
);
214 file
->cacheoffset
= -1;
215 file
->fileoffset
= 0;
217 if (file
->write
&& (flags
& O_APPEND
)) {
218 rc
= lseek(fd
,0,SEEK_END
);
225 dircache_bind(fd
, pathname
);
231 int open(const char* pathname
, int flags
)
233 /* By default, use the dircache if available. */
234 return open_internal(pathname
, flags
, true);
239 struct filedesc
* file
= &openfiles
[fd
];
242 LDEBUGF("close(%d)\n", fd
);
244 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
257 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
258 dircache_update_filetime(fd
);
268 struct filedesc
* file
= &openfiles
[fd
];
271 LDEBUGF("fsync(%d)\n", fd
);
273 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
282 /* flush sector cache */
284 rc
= flush_cache(fd
);
287 /* when failing, try to close the file anyway */
288 fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
295 rc
= ftruncate(fd
, file
->size
);
298 /* when failing, try to close the file anyway */
299 fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
304 /* tie up all loose ends */
305 rc
= fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
312 int remove(const char* name
)
315 struct filedesc
* file
;
316 /* Can't use dircache now, because we need to access the fat structures. */
317 int fd
= open_internal(name
, O_WRONLY
, false);
321 file
= &openfiles
[fd
];
323 dircache_remove(name
);
325 rc
= fat_remove(&(file
->fatfile
));
327 DEBUGF("Failed removing file: %d\n", rc
);
341 int rename(const char* path
, const char* newpath
)
347 struct filedesc
* file
;
348 char newpath2
[MAX_PATH
];
350 /* verify new path does not already exist */
351 /* If it is a directory, errno == EISDIR if the name exists */
352 fd
= open(newpath
, O_RDONLY
);
353 if ( fd
>= 0 || errno
== EISDIR
) {
360 fd
= open_internal(path
, O_RDONLY
, false);
366 /* extract new file name */
367 nameptr
= strrchr(newpath
,'/');
375 /* Extract new path */
376 strcpy(newpath2
, newpath
);
378 dirptr
= strrchr(newpath2
,'/');
388 if(strlen(dirptr
) == 0) {
392 dir
= opendir_uncached(dirptr
);
398 file
= &openfiles
[fd
];
400 rc
= fat_rename(&file
->fatfile
, &dir
->fatdir
, nameptr
,
401 file
->size
, file
->attr
);
402 #ifdef HAVE_MULTIVOLUME
405 closedir_uncached(dir
);
406 DEBUGF("Failed renaming file across volumnes: %d\n", rc
);
413 closedir_uncached(dir
);
414 DEBUGF("Failed renaming file: %d\n", rc
);
420 dircache_rename(path
, newpath
);
425 closedir_uncached(dir
);
430 rc
= closedir_uncached(dir
);
439 int ftruncate(int fd
, off_t size
)
442 struct filedesc
* file
= &openfiles
[fd
];
444 sector
= size
/ SECTOR_SIZE
;
445 if (size
% SECTOR_SIZE
)
448 rc
= fat_seek(&(file
->fatfile
), sector
);
454 rc
= fat_truncate(&(file
->fatfile
));
462 dircache_update_filesize(fd
, size
, file
->fatfile
.firstcluster
);
468 static int flush_cache(int fd
)
471 struct filedesc
* file
= &openfiles
[fd
];
472 long sector
= file
->fileoffset
/ SECTOR_SIZE
;
474 DEBUGF("Flushing dirty sector cache\n");
476 /* make sure we are on correct sector */
477 rc
= fat_seek(&(file
->fatfile
), sector
);
481 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, true );
484 if(file
->fatfile
.eof
)
495 static int readwrite(int fd
, void* buf
, long count
, bool write
)
499 struct filedesc
* file
;
502 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
507 file
= &openfiles
[fd
];
514 if(file
->attr
& FAT_ATTR_DIRECTORY
) {
519 LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n",
520 fd
,(long)buf
,count
,write
?"write":"read");
522 /* attempt to read past EOF? */
523 if (!write
&& count
> file
->size
- file
->fileoffset
)
524 count
= file
->size
- file
->fileoffset
;
526 /* any head bytes? */
527 if ( file
->cacheoffset
!= -1 ) {
528 int offs
= file
->cacheoffset
;
529 int headbytes
= MIN(count
, SECTOR_SIZE
- offs
);
532 memcpy( file
->cache
+ offs
, buf
, headbytes
);
536 memcpy( buf
, file
->cache
+ offs
, headbytes
);
539 if (offs
+ headbytes
== SECTOR_SIZE
) {
541 rc
= flush_cache(fd
);
547 file
->cacheoffset
= -1;
550 file
->cacheoffset
+= headbytes
;
557 /* If the buffer has been modified, either it has been flushed already
558 * (if (offs+headbytes == SECTOR_SIZE)...) or does not need to be (no
559 * more data to follow in this call). Do NOT flush here. */
561 /* read/write whole sectors right into/from the supplied buffer */
562 sectors
= count
/ SECTOR_SIZE
;
564 rc
= fat_readwrite(&(file
->fatfile
), sectors
,
565 (unsigned char*)buf
+nread
, write
);
567 DEBUGF("Failed read/writing %ld sectors\n",sectors
);
569 if(write
&& file
->fatfile
.eof
) {
570 DEBUGF("No space left on device\n");
573 file
->fileoffset
+= nread
;
575 file
->cacheoffset
= -1;
576 /* adjust file size to length written */
577 if ( write
&& file
->fileoffset
> file
->size
)
579 file
->size
= file
->fileoffset
;
581 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
584 return nread
? nread
: rc
* 10 - 4;
588 nread
+= rc
* SECTOR_SIZE
;
589 count
-= sectors
* SECTOR_SIZE
;
591 /* if eof, skip tail bytes */
600 file
->cacheoffset
= -1;
604 /* any tail bytes? */
607 if ( file
->fileoffset
+ nread
< file
->size
) {
608 /* sector is only partially filled. copy-back from disk */
609 LDEBUGF("Copy-back tail cache\n");
610 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, false );
612 DEBUGF("Failed writing\n");
614 file
->fileoffset
+= nread
;
615 file
->cacheoffset
= -1;
616 /* adjust file size to length written */
617 if ( file
->fileoffset
> file
->size
)
619 file
->size
= file
->fileoffset
;
621 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
624 return nread
? nread
: rc
* 10 - 5;
626 /* seek back one sector to put file position right */
627 rc
= fat_seek(&(file
->fatfile
),
628 (file
->fileoffset
+ nread
) /
631 DEBUGF("fat_seek() failed\n");
633 file
->fileoffset
+= nread
;
634 file
->cacheoffset
= -1;
635 /* adjust file size to length written */
636 if ( file
->fileoffset
> file
->size
)
638 file
->size
= file
->fileoffset
;
640 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
643 return nread
? nread
: rc
* 10 - 6;
646 memcpy( file
->cache
, (unsigned char*)buf
+ nread
, count
);
650 rc
= fat_readwrite(&(file
->fatfile
), 1, &(file
->cache
),false);
652 DEBUGF("Failed caching sector\n");
654 file
->fileoffset
+= nread
;
655 file
->cacheoffset
= -1;
656 return nread
? nread
: rc
* 10 - 7;
658 memcpy( (unsigned char*)buf
+ nread
, file
->cache
, count
);
662 file
->cacheoffset
= count
;
665 file
->fileoffset
+= nread
;
666 LDEBUGF("fileoffset: %ld\n", file
->fileoffset
);
668 /* adjust file size to length written */
669 if ( write
&& file
->fileoffset
> file
->size
)
671 file
->size
= file
->fileoffset
;
673 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
680 ssize_t
write(int fd
, const void* buf
, size_t count
)
682 if (!openfiles
[fd
].write
) {
686 return readwrite(fd
, (void *)buf
, count
, true);
689 ssize_t
read(int fd
, void* buf
, size_t count
)
691 return readwrite(fd
, buf
, count
, false);
695 off_t
lseek(int fd
, off_t offset
, int whence
)
702 struct filedesc
* file
= &openfiles
[fd
];
704 LDEBUGF("lseek(%d,%ld,%d)\n",fd
,offset
,whence
);
706 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
721 pos
= file
->fileoffset
+ offset
;
725 pos
= file
->size
+ offset
;
732 if ((pos
< 0) || (pos
> file
->size
)) {
738 newsector
= pos
/ SECTOR_SIZE
;
739 oldsector
= file
->fileoffset
/ SECTOR_SIZE
;
740 sectoroffset
= pos
% SECTOR_SIZE
;
742 if ( (newsector
!= oldsector
) ||
743 ((file
->cacheoffset
==-1) && sectoroffset
) ) {
745 if ( newsector
!= oldsector
) {
747 rc
= flush_cache(fd
);
752 rc
= fat_seek(&(file
->fatfile
), newsector
);
758 if ( sectoroffset
) {
759 rc
= fat_readwrite(&(file
->fatfile
), 1,
760 &(file
->cache
),false);
765 file
->cacheoffset
= sectoroffset
;
768 file
->cacheoffset
= -1;
771 if ( file
->cacheoffset
!= -1 )
772 file
->cacheoffset
= sectoroffset
;
774 file
->fileoffset
= pos
;
779 off_t
filesize(int fd
)
781 struct filedesc
* file
= &openfiles
[fd
];
783 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
797 /* release all file handles on a given volume "by force", to avoid leaks */
798 int release_files(int volume
)
800 struct filedesc
* pfile
= openfiles
;
803 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++, pfile
++)
805 #ifdef HAVE_MULTIVOLUME
806 if (pfile
->fatfile
.volume
== volume
)
811 pfile
->busy
= false; /* mark as available, no further action */
815 return closed
; /* return how many we did */
817 #endif /* #ifdef HAVE_HOTSWAP */