1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Björn Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
24 #include "dir_uncached.h"
30 These functions provide a roughly POSIX-compatible file IO API.
32 Since the fat32 driver only manages sectors, we maintain a one-sector
33 cache for each open file. This way we can provide byte access without
34 having to re-read the sector each time.
35 The penalty is the RAM used for the cache and slightly more complex code.
39 unsigned char cache
[SECTOR_SIZE
];
40 int cacheoffset
; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
44 struct fat_file fatfile
;
51 static struct filedesc openfiles
[MAX_OPEN_FILES
];
53 static int flush_cache(int fd
);
55 int creat(const char *pathname
)
57 return open(pathname
, O_WRONLY
|O_CREAT
|O_TRUNC
);
60 static int open_internal(const char* pathname
, int flags
, bool use_cache
)
63 struct dirent_uncached
* entry
;
65 char pathnamecopy
[MAX_PATH
];
67 struct filedesc
* file
= NULL
;
73 LDEBUGF("open(\"%s\",%d)\n",pathname
,flags
);
75 if ( pathname
[0] != '/' ) {
76 DEBUGF("'%s' is not an absolute path.\n",pathname
);
77 DEBUGF("Only absolute pathnames supported at the moment\n");
82 /* find a free file descriptor */
83 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++ )
84 if ( !openfiles
[fd
].busy
)
87 if ( fd
== MAX_OPEN_FILES
) {
88 DEBUGF("Too many files open\n");
93 file
= &openfiles
[fd
];
94 memset(file
, 0, sizeof(struct filedesc
));
96 if (flags
& (O_RDWR
| O_WRONLY
)) {
105 if (dircache_is_enabled() && !file
->write
&& use_cache
)
107 const struct dircache_entry
*ce
;
109 ce
= dircache_get_entry_ptr(pathname
);
117 fat_open(IF_MV2(unsupported at the moment
,)
121 file
->size
= ce
->size
;
122 file
->attr
= ce
->attribute
;
123 file
->cacheoffset
= -1;
124 file
->fileoffset
= 0;
130 strncpy(pathnamecopy
,pathname
,sizeof(pathnamecopy
));
131 pathnamecopy
[sizeof(pathnamecopy
)-1] = 0;
133 /* locate filename */
134 name
=strrchr(pathnamecopy
+1,'/');
137 dir
= opendir_uncached(pathnamecopy
);
142 dir
= opendir_uncached("/");
143 name
= pathnamecopy
+1;
146 DEBUGF("Failed opening dir\n");
153 DEBUGF("Empty file name\n");
156 closedir_uncached(dir
);
160 /* scan dir for name */
161 while ((entry
= readdir_uncached(dir
))) {
162 if ( !strcasecmp(name
, entry
->d_name
) ) {
163 fat_open(IF_MV2(dir
->fatdir
.file
.volume
,)
167 file
->size
= file
->trunc
? 0 : entry
->size
;
168 file
->attr
= entry
->attribute
;
174 LDEBUGF("Didn't find file %s\n",name
);
175 if ( file
->write
&& (flags
& O_CREAT
) ) {
176 rc
= fat_create_file(name
,
180 DEBUGF("Couldn't create %s in %s\n",name
,pathnamecopy
);
183 closedir_uncached(dir
);
187 dircache_add_file(pathname
, file
->fatfile
.firstcluster
);
193 DEBUGF("Couldn't find %s in %s\n",name
,pathnamecopy
);
196 closedir_uncached(dir
);
200 if(file
->write
&& (file
->attr
& FAT_ATTR_DIRECTORY
)) {
203 closedir_uncached(dir
);
207 closedir_uncached(dir
);
209 file
->cacheoffset
= -1;
210 file
->fileoffset
= 0;
212 if (file
->write
&& (flags
& O_APPEND
)) {
213 rc
= lseek(fd
,0,SEEK_END
);
220 dircache_bind(fd
, pathname
);
226 int open(const char* pathname
, int flags
)
228 /* By default, use the dircache if available. */
229 return open_internal(pathname
, flags
, true);
234 struct filedesc
* file
= &openfiles
[fd
];
237 LDEBUGF("close(%d)\n", fd
);
239 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
252 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
262 struct filedesc
* file
= &openfiles
[fd
];
265 LDEBUGF("fsync(%d)\n", fd
);
267 if (fd
< 0 || fd
> MAX_OPEN_FILES
-1) {
276 /* flush sector cache */
278 rc
= flush_cache(fd
);
285 rc
= ftruncate(fd
, file
->size
);
290 /* tie up all loose ends */
291 rc
= fat_closewrite(&(file
->fatfile
), file
->size
, file
->attr
);
298 int remove(const char* name
)
301 struct filedesc
* file
;
302 /* Can't use dircache now, because we need to access the fat structures. */
303 int fd
= open_internal(name
, O_WRONLY
, false);
307 file
= &openfiles
[fd
];
309 dircache_remove(name
);
311 rc
= fat_remove(&(file
->fatfile
));
313 DEBUGF("Failed removing file: %d\n", rc
);
327 int rename(const char* path
, const char* newpath
)
333 struct filedesc
* file
;
334 char newpath2
[MAX_PATH
];
336 /* verify new path does not already exist */
337 /* If it is a directory, errno == EISDIR if the name exists */
338 fd
= open(newpath
, O_RDONLY
);
339 if ( fd
>= 0 || errno
== EISDIR
) {
346 fd
= open_internal(path
, O_RDONLY
, false);
352 /* extract new file name */
353 nameptr
= strrchr(newpath
,'/');
359 /* Extract new path */
360 strcpy(newpath2
, newpath
);
362 dirptr
= strrchr(newpath2
,'/');
370 if(strlen(dirptr
) == 0) {
374 dir
= opendir_uncached(dirptr
);
378 file
= &openfiles
[fd
];
380 dircache_rename(path
, newpath
);
383 rc
= fat_rename(&file
->fatfile
, &dir
->fatdir
, nameptr
,
384 file
->size
, file
->attr
);
385 #ifdef HAVE_MULTIVOLUME
387 DEBUGF("Failed renaming file across volumnes: %d\n", rc
);
393 DEBUGF("Failed renaming file: %d\n", rc
);
404 rc
= closedir_uncached(dir
);
413 int ftruncate(int fd
, off_t size
)
416 struct filedesc
* file
= &openfiles
[fd
];
418 sector
= size
/ SECTOR_SIZE
;
419 if (size
% SECTOR_SIZE
)
422 rc
= fat_seek(&(file
->fatfile
), sector
);
428 rc
= fat_truncate(&(file
->fatfile
));
436 dircache_update_filesize(fd
, size
, file
->fatfile
.firstcluster
);
442 static int flush_cache(int fd
)
445 struct filedesc
* file
= &openfiles
[fd
];
446 long sector
= file
->fileoffset
/ SECTOR_SIZE
;
448 DEBUGF("Flushing dirty sector cache\n");
450 /* make sure we are on correct sector */
451 rc
= fat_seek(&(file
->fatfile
), sector
);
455 rc
= fat_readwrite(&(file
->fatfile
), 1,
459 if(file
->fatfile
.eof
)
470 static int readwrite(int fd
, void* buf
, long count
, bool write
)
474 struct filedesc
* file
= &openfiles
[fd
];
482 LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n",
483 fd
,(long)buf
,count
,write
?"write":"read");
485 /* attempt to read past EOF? */
486 if (!write
&& count
> file
->size
- file
->fileoffset
)
487 count
= file
->size
- file
->fileoffset
;
489 /* any head bytes? */
490 if ( file
->cacheoffset
!= -1 ) {
491 int offs
= file
->cacheoffset
;
492 int headbytes
= MIN(count
, SECTOR_SIZE
- offs
);
495 memcpy( file
->cache
+ offs
, buf
, headbytes
);
499 memcpy( buf
, file
->cache
+ offs
, headbytes
);
502 if (offs
+ headbytes
== SECTOR_SIZE
) {
504 rc
= flush_cache(fd
);
510 file
->cacheoffset
= -1;
513 file
->cacheoffset
+= headbytes
;
520 /* If the buffer has been modified, either it has been flushed already
521 * (if (offs+headbytes == SECTOR_SIZE)...) or does not need to be (no
522 * more data to follow in this call). Do NOT flush here. */
524 /* read/write whole sectors right into/from the supplied buffer */
525 sectors
= count
/ SECTOR_SIZE
;
527 rc
= fat_readwrite(&(file
->fatfile
), sectors
,
528 (unsigned char*)buf
+nread
, write
);
530 DEBUGF("Failed read/writing %ld sectors\n",sectors
);
532 if(write
&& file
->fatfile
.eof
) {
533 DEBUGF("No space left on device\n");
536 file
->fileoffset
+= nread
;
538 file
->cacheoffset
= -1;
539 return nread
? nread
: rc
* 10 - 4;
543 nread
+= rc
* SECTOR_SIZE
;
544 count
-= sectors
* SECTOR_SIZE
;
546 /* if eof, skip tail bytes */
555 file
->cacheoffset
= -1;
559 /* any tail bytes? */
562 if ( file
->fileoffset
+ nread
< file
->size
) {
563 /* sector is only partially filled. copy-back from disk */
564 LDEBUGF("Copy-back tail cache\n");
565 rc
= fat_readwrite(&(file
->fatfile
), 1, file
->cache
, false );
567 DEBUGF("Failed writing\n");
569 file
->fileoffset
+= nread
;
570 file
->cacheoffset
= -1;
571 return nread
? nread
: rc
* 10 - 5;
573 /* seek back one sector to put file position right */
574 rc
= fat_seek(&(file
->fatfile
),
575 (file
->fileoffset
+ nread
) /
578 DEBUGF("fat_seek() failed\n");
580 file
->fileoffset
+= nread
;
581 file
->cacheoffset
= -1;
582 return nread
? nread
: rc
* 10 - 6;
585 memcpy( file
->cache
, (unsigned char*)buf
+ nread
, count
);
589 rc
= fat_readwrite(&(file
->fatfile
), 1, &(file
->cache
),false);
591 DEBUGF("Failed caching sector\n");
593 file
->fileoffset
+= nread
;
594 file
->cacheoffset
= -1;
595 return nread
? nread
: rc
* 10 - 7;
597 memcpy( (unsigned char*)buf
+ nread
, file
->cache
, count
);
601 file
->cacheoffset
= count
;
604 file
->fileoffset
+= nread
;
605 LDEBUGF("fileoffset: %ld\n", file
->fileoffset
);
607 /* adjust file size to length written */
608 if ( write
&& file
->fileoffset
> file
->size
)
610 file
->size
= file
->fileoffset
;
612 dircache_update_filesize(fd
, file
->size
, file
->fatfile
.firstcluster
);
619 ssize_t
write(int fd
, const void* buf
, size_t count
)
621 if (!openfiles
[fd
].write
) {
625 return readwrite(fd
, (void *)buf
, count
, true);
628 ssize_t
read(int fd
, void* buf
, size_t count
)
630 return readwrite(fd
, buf
, count
, false);
634 off_t
lseek(int fd
, off_t offset
, int whence
)
641 struct filedesc
* file
= &openfiles
[fd
];
643 LDEBUGF("lseek(%d,%ld,%d)\n",fd
,offset
,whence
);
656 pos
= file
->fileoffset
+ offset
;
660 pos
= file
->size
+ offset
;
667 if ((pos
< 0) || (pos
> file
->size
)) {
673 newsector
= pos
/ SECTOR_SIZE
;
674 oldsector
= file
->fileoffset
/ SECTOR_SIZE
;
675 sectoroffset
= pos
% SECTOR_SIZE
;
677 if ( (newsector
!= oldsector
) ||
678 ((file
->cacheoffset
==-1) && sectoroffset
) ) {
680 if ( newsector
!= oldsector
) {
682 rc
= flush_cache(fd
);
687 rc
= fat_seek(&(file
->fatfile
), newsector
);
693 if ( sectoroffset
) {
694 rc
= fat_readwrite(&(file
->fatfile
), 1,
695 &(file
->cache
),false);
700 file
->cacheoffset
= sectoroffset
;
703 file
->cacheoffset
= -1;
706 if ( file
->cacheoffset
!= -1 )
707 file
->cacheoffset
= sectoroffset
;
709 file
->fileoffset
= pos
;
714 off_t
filesize(int fd
)
716 struct filedesc
* file
= &openfiles
[fd
];
728 // release all file handles on a given volume "by force", to avoid leaks
729 int release_files(int volume
)
731 struct filedesc
* pfile
= openfiles
;
734 for ( fd
=0; fd
<MAX_OPEN_FILES
; fd
++, pfile
++)
736 if (pfile
->fatfile
.volume
== volume
)
738 pfile
->busy
= false; /* mark as available, no further action */
742 return closed
; /* return how many we did */
744 #endif /* #ifdef HAVE_HOTSWAP */