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"
32 These functions provide a roughly POSIX-compatible file IO API.
34 Since the fat32 driver only manages sectors, we maintain a one-sector
35 cache for each open file. This way we can provide byte access without
36 having to re-read the sector each time.
37 The penalty is the RAM used for the cache and slightly more complex code.
41 unsigned char cache
[SECTOR_SIZE
];
42 int cacheoffset
; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
46 struct fat_file fatfile
;
53 static struct filedesc openfiles
[MAX_OPEN_FILES
];
55 static int flush_cache(int fd
);
57 int creat(const char *pathname
)
59 return open(pathname
, O_WRONLY
|O_CREAT
|O_TRUNC
);
62 static int open_internal(const char* pathname
, int flags
, bool use_cache
)
65 struct dirent_uncached
* entry
;
67 char pathnamecopy
[MAX_PATH
];
69 struct filedesc
* file
= NULL
;
75 LDEBUGF("open(\"%s\",%d)\n",pathname
,flags
);
77 if ( pathname
[0] != '/' ) {
78 DEBUGF("'%s' is not an absolute path.\n",pathname
);
79 DEBUGF("Only absolute pathnames supported at the moment\n");
84 /* find a free file descriptor */
85 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++ )
86 if ( !openfiles
[fd
].busy
)
89 if ( fd
== MAX_OPEN_FILES
) {
90 DEBUGF("Too many files open\n");
95 file
= &openfiles
[fd
];
96 memset(file
, 0, sizeof(struct filedesc
));
98 if (flags
& (O_RDWR
| O_WRONLY
)) {
107 if (dircache_is_enabled() && !file
->write
&& use_cache
)
109 const struct dircache_entry
*ce
;
110 # ifdef HAVE_MULTIVOLUME
111 int volume
= strip_volume(pathname
, pathnamecopy
);
114 ce
= dircache_get_entry_ptr(pathname
);
122 fat_open(IF_MV2(volume
,)
126 file
->size
= ce
->size
;
127 file
->attr
= ce
->attribute
;
128 file
->cacheoffset
= -1;
129 file
->fileoffset
= 0;
135 strlcpy(pathnamecopy
, pathname
, sizeof(pathnamecopy
));
137 /* locate filename */
138 name
=strrchr(pathnamecopy
+1,'/');
141 dir
= opendir_uncached(pathnamecopy
);
146 dir
= opendir_uncached("/");
147 name
= pathnamecopy
+1;
150 DEBUGF("Failed opening dir\n");
157 DEBUGF("Empty file name\n");
160 closedir_uncached(dir
);
164 /* scan dir for name */
165 while ((entry
= readdir_uncached(dir
))) {
166 if ( !strcasecmp(name
, entry
->d_name
) ) {
167 fat_open(IF_MV2(dir
->fatdir
.file
.volume
,)
171 file
->size
= file
->trunc
? 0 : entry
->size
;
172 file
->attr
= entry
->attribute
;
178 LDEBUGF("Didn't find file %s\n",name
);
179 if ( file
->write
&& (flags
& O_CREAT
) ) {
180 rc
= fat_create_file(name
,
184 DEBUGF("Couldn't create %s in %s\n",name
,pathnamecopy
);
187 closedir_uncached(dir
);
191 dircache_add_file(pathname
, file
->fatfile
.firstcluster
);
197 DEBUGF("Couldn't find %s in %s\n",name
,pathnamecopy
);
200 closedir_uncached(dir
);
204 if(file
->write
&& (file
->attr
& FAT_ATTR_DIRECTORY
)) {
207 closedir_uncached(dir
);
211 closedir_uncached(dir
);
213 file
->cacheoffset
= -1;
214 file
->fileoffset
= 0;
216 if (file
->write
&& (flags
& O_APPEND
)) {
217 rc
= lseek(fd
,0,SEEK_END
);
224 dircache_bind(fd
, pathname
);
230 int open(const char* pathname
, int flags
)
232 /* By default, use the dircache if available. */
233 return open_internal(pathname
, flags
, true);
238 struct filedesc
* file
= &openfiles
[fd
];
241 LDEBUGF("close(%d)\n", fd
);
243 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
256 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
257 dircache_update_filetime(fd
);
267 struct filedesc
* file
= &openfiles
[fd
];
270 LDEBUGF("fsync(%d)\n", fd
);
272 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
281 /* flush sector cache */
283 rc
= flush_cache(fd
);
286 /* when failing, try to close the file anyway */
287 fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
294 rc
= ftruncate(fd
, file
->size
);
297 /* when failing, try to close the file anyway */
298 fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
303 /* tie up all loose ends */
304 rc
= fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
311 int remove(const char* name
)
314 struct filedesc
* file
;
315 /* Can't use dircache now, because we need to access the fat structures. */
316 int fd
= open_internal(name
, O_WRONLY
, false);
320 file
= &openfiles
[fd
];
322 dircache_remove(name
);
324 rc
= fat_remove(&(file
->fatfile
));
326 DEBUGF("Failed removing file: %d\n", rc
);
340 int rename(const char* path
, const char* newpath
)
346 struct filedesc
* file
;
347 char newpath2
[MAX_PATH
];
349 /* verify new path does not already exist */
350 /* If it is a directory, errno == EISDIR if the name exists */
351 fd
= open(newpath
, O_RDONLY
);
352 if ( fd
>= 0 || errno
== EISDIR
) {
359 fd
= open_internal(path
, O_RDONLY
, false);
365 /* extract new file name */
366 nameptr
= strrchr(newpath
,'/');
372 /* Extract new path */
373 strcpy(newpath2
, newpath
);
375 dirptr
= strrchr(newpath2
,'/');
383 if(strlen(dirptr
) == 0) {
387 dir
= opendir_uncached(dirptr
);
391 file
= &openfiles
[fd
];
393 rc
= fat_rename(&file
->fatfile
, &dir
->fatdir
, nameptr
,
394 file
->size
, file
->attr
);
395 #ifdef HAVE_MULTIVOLUME
397 DEBUGF("Failed renaming file across volumnes: %d\n", rc
);
403 DEBUGF("Failed renaming file: %d\n", rc
);
409 dircache_rename(path
, newpath
);
418 rc
= closedir_uncached(dir
);
427 int ftruncate(int fd
, off_t size
)
430 struct filedesc
* file
= &openfiles
[fd
];
432 sector
= size
/ SECTOR_SIZE
;
433 if (size
% SECTOR_SIZE
)
436 rc
= fat_seek(&(file
->fatfile
), sector
);
442 rc
= fat_truncate(&(file
->fatfile
));
450 dircache_update_filesize(fd
, size
, file
->fatfile
.firstcluster
);
456 static int flush_cache(int fd
)
459 struct filedesc
* file
= &openfiles
[fd
];
460 long sector
= file
->fileoffset
/ SECTOR_SIZE
;
462 DEBUGF("Flushing dirty sector cache\n");
464 /* make sure we are on correct sector */
465 rc
= fat_seek(&(file
->fatfile
), sector
);
469 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, true );
472 if(file
->fatfile
.eof
)
483 static int readwrite(int fd
, void* buf
, long count
, bool write
)
487 struct filedesc
* file
= &openfiles
[fd
];
490 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
499 LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n",
500 fd
,(long)buf
,count
,write
?"write":"read");
502 /* attempt to read past EOF? */
503 if (!write
&& count
> file
->size
- file
->fileoffset
)
504 count
= file
->size
- file
->fileoffset
;
506 /* any head bytes? */
507 if ( file
->cacheoffset
!= -1 ) {
508 int offs
= file
->cacheoffset
;
509 int headbytes
= MIN(count
, SECTOR_SIZE
- offs
);
512 memcpy( file
->cache
+ offs
, buf
, headbytes
);
516 memcpy( buf
, file
->cache
+ offs
, headbytes
);
519 if (offs
+ headbytes
== SECTOR_SIZE
) {
521 rc
= flush_cache(fd
);
527 file
->cacheoffset
= -1;
530 file
->cacheoffset
+= headbytes
;
537 /* If the buffer has been modified, either it has been flushed already
538 * (if (offs+headbytes == SECTOR_SIZE)...) or does not need to be (no
539 * more data to follow in this call). Do NOT flush here. */
541 /* read/write whole sectors right into/from the supplied buffer */
542 sectors
= count
/ SECTOR_SIZE
;
544 rc
= fat_readwrite(&(file
->fatfile
), sectors
,
545 (unsigned char*)buf
+nread
, write
);
547 DEBUGF("Failed read/writing %ld sectors\n",sectors
);
549 if(write
&& file
->fatfile
.eof
) {
550 DEBUGF("No space left on device\n");
553 file
->fileoffset
+= nread
;
555 file
->cacheoffset
= -1;
556 /* adjust file size to length written */
557 if ( write
&& file
->fileoffset
> file
->size
)
559 file
->size
= file
->fileoffset
;
561 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
564 return nread
? nread
: rc
* 10 - 4;
568 nread
+= rc
* SECTOR_SIZE
;
569 count
-= sectors
* SECTOR_SIZE
;
571 /* if eof, skip tail bytes */
580 file
->cacheoffset
= -1;
584 /* any tail bytes? */
587 if ( file
->fileoffset
+ nread
< file
->size
) {
588 /* sector is only partially filled. copy-back from disk */
589 LDEBUGF("Copy-back tail cache\n");
590 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, false );
592 DEBUGF("Failed writing\n");
594 file
->fileoffset
+= nread
;
595 file
->cacheoffset
= -1;
596 /* adjust file size to length written */
597 if ( file
->fileoffset
> file
->size
)
599 file
->size
= file
->fileoffset
;
601 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
604 return nread
? nread
: rc
* 10 - 5;
606 /* seek back one sector to put file position right */
607 rc
= fat_seek(&(file
->fatfile
),
608 (file
->fileoffset
+ nread
) /
611 DEBUGF("fat_seek() failed\n");
613 file
->fileoffset
+= nread
;
614 file
->cacheoffset
= -1;
615 /* adjust file size to length written */
616 if ( file
->fileoffset
> file
->size
)
618 file
->size
= file
->fileoffset
;
620 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
623 return nread
? nread
: rc
* 10 - 6;
626 memcpy( file
->cache
, (unsigned char*)buf
+ nread
, count
);
630 rc
= fat_readwrite(&(file
->fatfile
), 1, &(file
->cache
),false);
632 DEBUGF("Failed caching sector\n");
634 file
->fileoffset
+= nread
;
635 file
->cacheoffset
= -1;
636 return nread
? nread
: rc
* 10 - 7;
638 memcpy( (unsigned char*)buf
+ nread
, file
->cache
, count
);
642 file
->cacheoffset
= count
;
645 file
->fileoffset
+= nread
;
646 LDEBUGF("fileoffset: %ld\n", file
->fileoffset
);
648 /* adjust file size to length written */
649 if ( write
&& file
->fileoffset
> file
->size
)
651 file
->size
= file
->fileoffset
;
653 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
660 ssize_t
write(int fd
, const void* buf
, size_t count
)
662 if (!openfiles
[fd
].write
) {
666 return readwrite(fd
, (void *)buf
, count
, true);
669 ssize_t
read(int fd
, void* buf
, size_t count
)
671 return readwrite(fd
, buf
, count
, false);
675 off_t
lseek(int fd
, off_t offset
, int whence
)
682 struct filedesc
* file
= &openfiles
[fd
];
684 LDEBUGF("lseek(%d,%ld,%d)\n",fd
,offset
,whence
);
686 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
701 pos
= file
->fileoffset
+ offset
;
705 pos
= file
->size
+ offset
;
712 if ((pos
< 0) || (pos
> file
->size
)) {
718 newsector
= pos
/ SECTOR_SIZE
;
719 oldsector
= file
->fileoffset
/ SECTOR_SIZE
;
720 sectoroffset
= pos
% SECTOR_SIZE
;
722 if ( (newsector
!= oldsector
) ||
723 ((file
->cacheoffset
==-1) && sectoroffset
) ) {
725 if ( newsector
!= oldsector
) {
727 rc
= flush_cache(fd
);
732 rc
= fat_seek(&(file
->fatfile
), newsector
);
738 if ( sectoroffset
) {
739 rc
= fat_readwrite(&(file
->fatfile
), 1,
740 &(file
->cache
),false);
745 file
->cacheoffset
= sectoroffset
;
748 file
->cacheoffset
= -1;
751 if ( file
->cacheoffset
!= -1 )
752 file
->cacheoffset
= sectoroffset
;
754 file
->fileoffset
= pos
;
759 off_t
filesize(int fd
)
761 struct filedesc
* file
= &openfiles
[fd
];
763 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
777 /* release all file handles on a given volume "by force", to avoid leaks */
778 int release_files(int volume
)
780 struct filedesc
* pfile
= openfiles
;
783 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++, pfile
++)
785 if (pfile
->fatfile
.volume
== volume
)
787 pfile
->busy
= false; /* mark as available, no further action */
791 return closed
; /* return how many we did */
793 #endif /* #ifdef HAVE_HOTSWAP */