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
] CACHEALIGN_ATTR
;
43 int cacheoffset
; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
47 struct fat_file fatfile
;
54 static struct filedesc openfiles
[MAX_OPEN_FILES
] CACHEALIGN_ATTR
;
56 static int flush_cache(int fd
);
58 int file_creat(const char *pathname
)
60 return open(pathname
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
63 static int open_internal(const char* pathname
, int flags
, bool use_cache
)
66 struct dirent_uncached
* entry
;
68 int pathnamesize
= strlen(pathname
) + 1;
69 char pathnamecopy
[pathnamesize
];
71 struct filedesc
* file
= NULL
;
77 LDEBUGF("open(\"%s\",%d)\n",pathname
,flags
);
79 if ( pathname
[0] != '/' ) {
80 DEBUGF("'%s' is not an absolute path.\n",pathname
);
81 DEBUGF("Only absolute pathnames supported at the moment\n");
86 /* find a free file descriptor */
87 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++ )
88 if ( !openfiles
[fd
].busy
)
91 if ( fd
== MAX_OPEN_FILES
) {
92 DEBUGF("Too many files open\n");
97 file
= &openfiles
[fd
];
98 memset(file
, 0, sizeof(struct filedesc
));
100 if (flags
& (O_RDWR
| O_WRONLY
)) {
109 if (dircache_is_enabled() && !file
->write
&& use_cache
)
111 const struct dircache_entry
*ce
;
112 # ifdef HAVE_MULTIVOLUME
113 int volume
= strip_volume(pathname
, pathnamecopy
);
116 ce
= dircache_get_entry_ptr(pathname
);
124 fat_open(IF_MV2(volume
,)
128 file
->size
= ce
->info
.size
;
129 file
->attr
= ce
->info
.attribute
;
130 file
->cacheoffset
= -1;
131 file
->fileoffset
= 0;
137 strlcpy(pathnamecopy
, pathname
, pathnamesize
);
139 /* locate filename */
140 name
=strrchr(pathnamecopy
+1,'/');
143 dir
= opendir_uncached(pathnamecopy
);
148 dir
= opendir_uncached("/");
149 name
= pathnamecopy
+1;
152 DEBUGF("Failed opening dir\n");
159 DEBUGF("Empty file name\n");
162 closedir_uncached(dir
);
166 /* scan dir for name */
167 while ((entry
= readdir_uncached(dir
))) {
168 if ( !strcasecmp(name
, entry
->d_name
) ) {
169 fat_open(IF_MV2(dir
->fatdir
.file
.volume
,)
173 file
->size
= file
->trunc
? 0 : entry
->info
.size
;
174 file
->attr
= entry
->info
.attribute
;
180 LDEBUGF("Didn't find file %s\n",name
);
181 if ( file
->write
&& (flags
& O_CREAT
) ) {
182 rc
= fat_create_file(name
,
186 DEBUGF("Couldn't create %s in %s\n",name
,pathnamecopy
);
189 closedir_uncached(dir
);
193 dircache_add_file(pathname
, file
->fatfile
.firstcluster
);
199 DEBUGF("Couldn't find %s in %s\n",name
,pathnamecopy
);
202 closedir_uncached(dir
);
206 if(file
->write
&& (file
->attr
& FAT_ATTR_DIRECTORY
)) {
209 closedir_uncached(dir
);
213 closedir_uncached(dir
);
215 file
->cacheoffset
= -1;
216 file
->fileoffset
= 0;
218 if (file
->write
&& (flags
& O_APPEND
)) {
219 rc
= lseek(fd
,0,SEEK_END
);
226 dircache_bind(fd
, pathname
);
232 int file_open(const char* pathname
, int flags
)
234 /* By default, use the dircache if available. */
235 return open_internal(pathname
, flags
, true);
240 struct filedesc
* file
= &openfiles
[fd
];
243 LDEBUGF("close(%d)\n", fd
);
245 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
258 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
259 dircache_update_filetime(fd
);
269 struct filedesc
* file
= &openfiles
[fd
];
272 LDEBUGF("fsync(%d)\n", fd
);
274 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
283 /* flush sector cache */
285 rc
= flush_cache(fd
);
288 /* when failing, try to close the file anyway */
289 fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
296 rc
= ftruncate(fd
, file
->size
);
299 /* when failing, try to close the file anyway */
300 fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
305 /* tie up all loose ends */
306 rc
= fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
313 int remove(const char* name
)
316 struct filedesc
* file
;
317 /* Can't use dircache now, because we need to access the fat structures. */
318 int fd
= open_internal(name
, O_WRONLY
, false);
322 file
= &openfiles
[fd
];
324 dircache_remove(name
);
326 rc
= fat_remove(&(file
->fatfile
));
328 DEBUGF("Failed removing file: %d\n", rc
);
342 int rename(const char* path
, const char* newpath
)
348 struct filedesc
* file
;
349 char newpath2
[MAX_PATH
];
351 /* verify new path does not already exist */
352 /* If it is a directory, errno == EISDIR if the name exists */
353 fd
= open(newpath
, O_RDONLY
);
354 if ( fd
>= 0 || errno
== EISDIR
) {
361 fd
= open_internal(path
, O_RDONLY
, false);
367 /* extract new file name */
368 nameptr
= strrchr(newpath
,'/');
376 /* Extract new path */
377 strcpy(newpath2
, newpath
);
379 dirptr
= strrchr(newpath2
,'/');
389 if(strlen(dirptr
) == 0) {
393 dir
= opendir_uncached(dirptr
);
399 file
= &openfiles
[fd
];
401 rc
= fat_rename(&file
->fatfile
, &dir
->fatdir
, nameptr
,
402 file
->size
, file
->attr
);
403 #ifdef HAVE_MULTIVOLUME
406 closedir_uncached(dir
);
407 DEBUGF("Failed renaming file across volumnes: %d\n", rc
);
414 closedir_uncached(dir
);
415 DEBUGF("Failed renaming file: %d\n", rc
);
421 dircache_rename(path
, newpath
);
426 closedir_uncached(dir
);
431 rc
= closedir_uncached(dir
);
440 int ftruncate(int fd
, off_t size
)
443 struct filedesc
* file
= &openfiles
[fd
];
445 sector
= size
/ SECTOR_SIZE
;
446 if (size
% SECTOR_SIZE
)
449 rc
= fat_seek(&(file
->fatfile
), sector
);
455 rc
= fat_truncate(&(file
->fatfile
));
463 dircache_update_filesize(fd
, size
, file
->fatfile
.firstcluster
);
469 static int flush_cache(int fd
)
472 struct filedesc
* file
= &openfiles
[fd
];
473 long sector
= file
->fileoffset
/ SECTOR_SIZE
;
475 DEBUGF("Flushing dirty sector cache\n");
477 /* make sure we are on correct sector */
478 rc
= fat_seek(&(file
->fatfile
), sector
);
482 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, true );
485 if(file
->fatfile
.eof
)
496 static int readwrite(int fd
, void* buf
, long count
, bool write
)
500 struct filedesc
* file
;
502 #ifdef STORAGE_NEEDS_ALIGN
507 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
512 file
= &openfiles
[fd
];
519 if(file
->attr
& FAT_ATTR_DIRECTORY
) {
524 LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n",
525 fd
,(long)buf
,count
,write
?"write":"read");
527 /* attempt to read past EOF? */
528 if (!write
&& count
> file
->size
- file
->fileoffset
)
529 count
= file
->size
- file
->fileoffset
;
531 /* any head bytes? */
532 if ( file
->cacheoffset
!= -1 ) {
533 int offs
= file
->cacheoffset
;
534 int headbytes
= MIN(count
, SECTOR_SIZE
- offs
);
537 memcpy( file
->cache
+ offs
, buf
, headbytes
);
541 memcpy( buf
, file
->cache
+ offs
, headbytes
);
544 if (offs
+ headbytes
== SECTOR_SIZE
) {
546 rc
= flush_cache(fd
);
552 file
->cacheoffset
= -1;
555 file
->cacheoffset
+= headbytes
;
562 /* If the buffer has been modified, either it has been flushed already
563 * (if (offs+headbytes == SECTOR_SIZE)...) or does not need to be (no
564 * more data to follow in this call). Do NOT flush here. */
566 /* read/write whole sectors right into/from the supplied buffer */
567 sectors
= count
/ SECTOR_SIZE
;
570 #ifdef STORAGE_NEEDS_ALIGN
571 if (((uint32_t)buf
+ nread
) & (CACHEALIGN_SIZE
- 1))
572 for (i
= 0; i
< sectors
; i
++)
574 if (write
) memcpy(file
->cache
, buf
+nread
+i
*SECTOR_SIZE
, SECTOR_SIZE
);
575 rc2
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, write
);
582 if (!write
) memcpy(buf
+nread
+i
*SECTOR_SIZE
, file
->cache
, SECTOR_SIZE
);
586 rc
= fat_readwrite(&(file
->fatfile
), sectors
, (unsigned char*)buf
+nread
, write
);
588 DEBUGF("Failed read/writing %ld sectors\n",sectors
);
590 if(write
&& file
->fatfile
.eof
) {
591 DEBUGF("No space left on device\n");
594 file
->fileoffset
+= nread
;
596 file
->cacheoffset
= -1;
597 /* adjust file size to length written */
598 if ( write
&& file
->fileoffset
> file
->size
)
600 file
->size
= file
->fileoffset
;
602 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
605 return nread
? nread
: rc
* 10 - 4;
609 nread
+= rc
* SECTOR_SIZE
;
610 count
-= sectors
* SECTOR_SIZE
;
612 /* if eof, skip tail bytes */
621 file
->cacheoffset
= -1;
625 /* any tail bytes? */
628 if ( file
->fileoffset
+ nread
< file
->size
) {
629 /* sector is only partially filled. copy-back from disk */
630 LDEBUGF("Copy-back tail cache\n");
631 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, false );
633 DEBUGF("Failed writing\n");
635 file
->fileoffset
+= nread
;
636 file
->cacheoffset
= -1;
637 /* adjust file size to length written */
638 if ( file
->fileoffset
> file
->size
)
640 file
->size
= file
->fileoffset
;
642 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
645 return nread
? nread
: rc
* 10 - 5;
647 /* seek back one sector to put file position right */
648 rc
= fat_seek(&(file
->fatfile
),
649 (file
->fileoffset
+ nread
) /
652 DEBUGF("fat_seek() failed\n");
654 file
->fileoffset
+= nread
;
655 file
->cacheoffset
= -1;
656 /* adjust file size to length written */
657 if ( file
->fileoffset
> file
->size
)
659 file
->size
= file
->fileoffset
;
661 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
664 return nread
? nread
: rc
* 10 - 6;
667 memcpy( file
->cache
, (unsigned char*)buf
+ nread
, count
);
671 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
,false);
673 DEBUGF("Failed caching sector\n");
675 file
->fileoffset
+= nread
;
676 file
->cacheoffset
= -1;
677 return nread
? nread
: rc
* 10 - 7;
679 memcpy( (unsigned char*)buf
+ nread
, file
->cache
, count
);
683 file
->cacheoffset
= count
;
686 file
->fileoffset
+= nread
;
687 LDEBUGF("fileoffset: %ld\n", file
->fileoffset
);
689 /* adjust file size to length written */
690 if ( write
&& file
->fileoffset
> file
->size
)
692 file
->size
= file
->fileoffset
;
694 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
701 ssize_t
write(int fd
, const void* buf
, size_t count
)
703 if (!openfiles
[fd
].write
) {
707 return readwrite(fd
, (void *)buf
, count
, true);
710 ssize_t
read(int fd
, void* buf
, size_t count
)
712 return readwrite(fd
, buf
, count
, false);
716 off_t
lseek(int fd
, off_t offset
, int whence
)
723 struct filedesc
* file
= &openfiles
[fd
];
725 LDEBUGF("lseek(%d,%ld,%d)\n",fd
,offset
,whence
);
727 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
742 pos
= file
->fileoffset
+ offset
;
746 pos
= file
->size
+ offset
;
753 if ((pos
< 0) || (pos
> file
->size
)) {
759 newsector
= pos
/ SECTOR_SIZE
;
760 oldsector
= file
->fileoffset
/ SECTOR_SIZE
;
761 sectoroffset
= pos
% SECTOR_SIZE
;
763 if ( (newsector
!= oldsector
) ||
764 ((file
->cacheoffset
==-1) && sectoroffset
) ) {
766 if ( newsector
!= oldsector
) {
768 rc
= flush_cache(fd
);
773 rc
= fat_seek(&(file
->fatfile
), newsector
);
779 if ( sectoroffset
) {
780 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
,false);
785 file
->cacheoffset
= sectoroffset
;
788 file
->cacheoffset
= -1;
791 if ( file
->cacheoffset
!= -1 )
792 file
->cacheoffset
= sectoroffset
;
794 file
->fileoffset
= pos
;
799 off_t
filesize(int fd
)
801 struct filedesc
* file
= &openfiles
[fd
];
803 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
816 /* release all file handles on a given volume "by force", to avoid leaks */
817 int release_files(int volume
)
819 struct filedesc
* pfile
= openfiles
;
822 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++, pfile
++)
824 #ifdef HAVE_MULTIVOLUME
825 if (pfile
->fatfile
.volume
== volume
)
830 pfile
->busy
= false; /* mark as available, no further action */
834 return closed
; /* return how many we did */