Add the Lua scripting language plugin to the manual.
[kugel-rb/myfork.git] / firmware / common / file.c
blob7c01f038171b4941d168ab6b69f974deed82f403
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include <string.h>
22 #include <errno.h>
23 #include <stdbool.h>
24 #include "file.h"
25 #include "fat.h"
26 #include "dir_uncached.h"
27 #include "debug.h"
28 #include "dircache.h"
29 #include "system.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.
40 struct filedesc {
41 unsigned char cache[SECTOR_SIZE];
42 int cacheoffset; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
43 long fileoffset;
44 long size;
45 int attr;
46 struct fat_file fatfile;
47 bool busy;
48 bool write;
49 bool dirty;
50 bool trunc;
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)
64 DIR_UNCACHED* dir;
65 struct dirent_uncached* entry;
66 int fd;
67 char pathnamecopy[MAX_PATH];
68 char* name;
69 struct filedesc* file = NULL;
70 int rc;
71 #ifndef HAVE_DIRCACHE
72 (void)use_cache;
73 #endif
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");
80 errno = EINVAL;
81 return -1;
84 /* find a free file descriptor */
85 for ( fd=0; fd<MAX_OPEN_FILES; fd++ )
86 if ( !openfiles[fd].busy )
87 break;
89 if ( fd == MAX_OPEN_FILES ) {
90 DEBUGF("Too many files open\n");
91 errno = EMFILE;
92 return -2;
95 file = &openfiles[fd];
96 memset(file, 0, sizeof(struct filedesc));
98 if (flags & (O_RDWR | O_WRONLY)) {
99 file->write = true;
101 if (flags & O_TRUNC)
102 file->trunc = true;
104 file->busy = true;
106 #ifdef HAVE_DIRCACHE
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);
112 # endif
114 ce = dircache_get_entry_ptr(pathname);
115 if (!ce)
117 errno = ENOENT;
118 file->busy = false;
119 return -7;
122 fat_open(IF_MV2(volume,)
123 ce->startcluster,
124 &(file->fatfile),
125 NULL);
126 file->size = ce->size;
127 file->attr = ce->attribute;
128 file->cacheoffset = -1;
129 file->fileoffset = 0;
131 return fd;
133 #endif
135 strncpy(pathnamecopy,pathname,sizeof(pathnamecopy));
136 pathnamecopy[sizeof(pathnamecopy)-1] = 0;
138 /* locate filename */
139 name=strrchr(pathnamecopy+1,'/');
140 if ( name ) {
141 *name = 0;
142 dir = opendir_uncached(pathnamecopy);
143 *name = '/';
144 name++;
146 else {
147 dir = opendir_uncached("/");
148 name = pathnamecopy+1;
150 if (!dir) {
151 DEBUGF("Failed opening dir\n");
152 errno = EIO;
153 file->busy = false;
154 return -4;
157 if(name[0] == 0) {
158 DEBUGF("Empty file name\n");
159 errno = EINVAL;
160 file->busy = false;
161 closedir_uncached(dir);
162 return -5;
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,)
169 entry->startcluster,
170 &(file->fatfile),
171 &(dir->fatdir));
172 file->size = file->trunc ? 0 : entry->size;
173 file->attr = entry->attribute;
174 break;
178 if ( !entry ) {
179 LDEBUGF("Didn't find file %s\n",name);
180 if ( file->write && (flags & O_CREAT) ) {
181 rc = fat_create_file(name,
182 &(file->fatfile),
183 &(dir->fatdir));
184 if (rc < 0) {
185 DEBUGF("Couldn't create %s in %s\n",name,pathnamecopy);
186 errno = EIO;
187 file->busy = false;
188 closedir_uncached(dir);
189 return rc * 10 - 6;
191 #ifdef HAVE_DIRCACHE
192 dircache_add_file(pathname, file->fatfile.firstcluster);
193 #endif
194 file->size = 0;
195 file->attr = 0;
197 else {
198 DEBUGF("Couldn't find %s in %s\n",name,pathnamecopy);
199 errno = ENOENT;
200 file->busy = false;
201 closedir_uncached(dir);
202 return -7;
204 } else {
205 if(file->write && (file->attr & FAT_ATTR_DIRECTORY)) {
206 errno = EISDIR;
207 file->busy = false;
208 closedir_uncached(dir);
209 return -8;
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);
219 if (rc < 0 )
220 return rc * 10 - 9;
223 #ifdef HAVE_DIRCACHE
224 if (file->write)
225 dircache_bind(fd, pathname);
226 #endif
228 return fd;
231 int open(const char* pathname, int flags)
233 /* By default, use the dircache if available. */
234 return open_internal(pathname, flags, true);
237 int close(int fd)
239 struct filedesc* file = &openfiles[fd];
240 int rc = 0;
242 LDEBUGF("close(%d)\n", fd);
244 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
245 errno = EINVAL;
246 return -1;
248 if (!file->busy) {
249 errno = EBADF;
250 return -2;
252 if (file->write) {
253 rc = fsync(fd);
254 if (rc < 0)
255 return rc * 10 - 3;
256 #ifdef HAVE_DIRCACHE
257 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
258 dircache_update_filetime(fd);
259 #endif
262 file->busy = false;
263 return 0;
266 int fsync(int fd)
268 struct filedesc* file = &openfiles[fd];
269 int rc = 0;
271 LDEBUGF("fsync(%d)\n", fd);
273 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
274 errno = EINVAL;
275 return -1;
277 if (!file->busy) {
278 errno = EBADF;
279 return -2;
281 if (file->write) {
282 /* flush sector cache */
283 if ( file->dirty ) {
284 rc = flush_cache(fd);
285 if (rc < 0)
287 /* when failing, try to close the file anyway */
288 fat_closewrite(&(file->fatfile), file->size, file->attr);
289 return rc * 10 - 3;
293 /* truncate? */
294 if (file->trunc) {
295 rc = ftruncate(fd, file->size);
296 if (rc < 0)
298 /* when failing, try to close the file anyway */
299 fat_closewrite(&(file->fatfile), file->size, file->attr);
300 return rc * 10 - 4;
304 /* tie up all loose ends */
305 rc = fat_closewrite(&(file->fatfile), file->size, file->attr);
306 if (rc < 0)
307 return rc * 10 - 5;
309 return 0;
312 int remove(const char* name)
314 int rc;
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);
318 if ( fd < 0 )
319 return fd * 10 - 1;
321 file = &openfiles[fd];
322 #ifdef HAVE_DIRCACHE
323 dircache_remove(name);
324 #endif
325 rc = fat_remove(&(file->fatfile));
326 if ( rc < 0 ) {
327 DEBUGF("Failed removing file: %d\n", rc);
328 errno = EIO;
329 return rc * 10 - 3;
332 file->size = 0;
334 rc = close(fd);
335 if (rc<0)
336 return rc * 10 - 4;
338 return 0;
341 int rename(const char* path, const char* newpath)
343 int rc, fd;
344 DIR_UNCACHED* dir;
345 char* nameptr;
346 char* dirptr;
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) {
354 close(fd);
355 errno = EBUSY;
356 return -1;
358 close(fd);
360 fd = open_internal(path, O_RDONLY, false);
361 if ( fd < 0 ) {
362 errno = EIO;
363 return fd * 10 - 2;
366 /* extract new file name */
367 nameptr = strrchr(newpath,'/');
368 if (nameptr)
369 nameptr++;
370 else
371 return - 3;
373 /* Extract new path */
374 strcpy(newpath2, newpath);
376 dirptr = strrchr(newpath2,'/');
377 if(dirptr)
378 *dirptr = 0;
379 else
380 return - 4;
382 dirptr = newpath2;
384 if(strlen(dirptr) == 0) {
385 dirptr = "/";
388 dir = opendir_uncached(dirptr);
389 if(!dir)
390 return - 5;
392 file = &openfiles[fd];
394 rc = fat_rename(&file->fatfile, &dir->fatdir, nameptr,
395 file->size, file->attr);
396 #ifdef HAVE_MULTIVOLUME
397 if ( rc == -1) {
398 DEBUGF("Failed renaming file across volumnes: %d\n", rc);
399 errno = EXDEV;
400 return -6;
402 #endif
403 if ( rc < 0 ) {
404 DEBUGF("Failed renaming file: %d\n", rc);
405 errno = EIO;
406 return rc * 10 - 7;
409 #ifdef HAVE_DIRCACHE
410 dircache_rename(path, newpath);
411 #endif
413 rc = close(fd);
414 if (rc<0) {
415 errno = EIO;
416 return rc * 10 - 8;
419 rc = closedir_uncached(dir);
420 if (rc<0) {
421 errno = EIO;
422 return rc * 10 - 9;
425 return 0;
428 int ftruncate(int fd, off_t size)
430 int rc, sector;
431 struct filedesc* file = &openfiles[fd];
433 sector = size / SECTOR_SIZE;
434 if (size % SECTOR_SIZE)
435 sector++;
437 rc = fat_seek(&(file->fatfile), sector);
438 if (rc < 0) {
439 errno = EIO;
440 return rc * 10 - 1;
443 rc = fat_truncate(&(file->fatfile));
444 if (rc < 0) {
445 errno = EIO;
446 return rc * 10 - 2;
449 file->size = size;
450 #ifdef HAVE_DIRCACHE
451 dircache_update_filesize(fd, size, file->fatfile.firstcluster);
452 #endif
454 return 0;
457 static int flush_cache(int fd)
459 int rc;
460 struct filedesc* file = &openfiles[fd];
461 long sector = file->fileoffset / SECTOR_SIZE;
463 DEBUGF("Flushing dirty sector cache\n");
465 /* make sure we are on correct sector */
466 rc = fat_seek(&(file->fatfile), sector);
467 if ( rc < 0 )
468 return rc * 10 - 3;
470 rc = fat_readwrite(&(file->fatfile), 1, file->cache, true );
472 if ( rc < 0 ) {
473 if(file->fatfile.eof)
474 errno = ENOSPC;
476 return rc * 10 - 2;
479 file->dirty = false;
481 return 0;
484 static int readwrite(int fd, void* buf, long count, bool write)
486 long sectors;
487 long nread=0;
488 struct filedesc* file = &openfiles[fd];
489 int rc;
491 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
492 errno = EINVAL;
493 return -1;
495 if ( !file->busy ) {
496 errno = EBADF;
497 return -1;
500 LDEBUGF( "readwrite(%d,%lx,%ld,%s)\n",
501 fd,(long)buf,count,write?"write":"read");
503 /* attempt to read past EOF? */
504 if (!write && count > file->size - file->fileoffset)
505 count = file->size - file->fileoffset;
507 /* any head bytes? */
508 if ( file->cacheoffset != -1 ) {
509 int offs = file->cacheoffset;
510 int headbytes = MIN(count, SECTOR_SIZE - offs);
512 if (write) {
513 memcpy( file->cache + offs, buf, headbytes );
514 file->dirty = true;
516 else {
517 memcpy( buf, file->cache + offs, headbytes );
520 if (offs + headbytes == SECTOR_SIZE) {
521 if (file->dirty) {
522 rc = flush_cache(fd);
523 if ( rc < 0 ) {
524 errno = EIO;
525 return rc * 10 - 2;
528 file->cacheoffset = -1;
530 else {
531 file->cacheoffset += headbytes;
534 nread = headbytes;
535 count -= headbytes;
538 /* If the buffer has been modified, either it has been flushed already
539 * (if (offs+headbytes == SECTOR_SIZE)...) or does not need to be (no
540 * more data to follow in this call). Do NOT flush here. */
542 /* read/write whole sectors right into/from the supplied buffer */
543 sectors = count / SECTOR_SIZE;
544 if ( sectors ) {
545 rc = fat_readwrite(&(file->fatfile), sectors,
546 (unsigned char*)buf+nread, write );
547 if ( rc < 0 ) {
548 DEBUGF("Failed read/writing %ld sectors\n",sectors);
549 errno = EIO;
550 if(write && file->fatfile.eof) {
551 DEBUGF("No space left on device\n");
552 errno = ENOSPC;
553 } else {
554 file->fileoffset += nread;
556 file->cacheoffset = -1;
557 /* adjust file size to length written */
558 if ( write && file->fileoffset > file->size )
560 file->size = file->fileoffset;
561 #ifdef HAVE_DIRCACHE
562 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
563 #endif
565 return nread ? nread : rc * 10 - 4;
567 else {
568 if ( rc > 0 ) {
569 nread += rc * SECTOR_SIZE;
570 count -= sectors * SECTOR_SIZE;
572 /* if eof, skip tail bytes */
573 if ( rc < sectors )
574 count = 0;
576 else {
577 /* eof */
578 count=0;
581 file->cacheoffset = -1;
585 /* any tail bytes? */
586 if ( count ) {
587 if (write) {
588 if ( file->fileoffset + nread < file->size ) {
589 /* sector is only partially filled. copy-back from disk */
590 LDEBUGF("Copy-back tail cache\n");
591 rc = fat_readwrite(&(file->fatfile), 1, file->cache, false );
592 if ( rc < 0 ) {
593 DEBUGF("Failed writing\n");
594 errno = EIO;
595 file->fileoffset += nread;
596 file->cacheoffset = -1;
597 /* adjust file size to length written */
598 if ( file->fileoffset > file->size )
600 file->size = file->fileoffset;
601 #ifdef HAVE_DIRCACHE
602 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
603 #endif
605 return nread ? nread : rc * 10 - 5;
607 /* seek back one sector to put file position right */
608 rc = fat_seek(&(file->fatfile),
609 (file->fileoffset + nread) /
610 SECTOR_SIZE);
611 if ( rc < 0 ) {
612 DEBUGF("fat_seek() failed\n");
613 errno = EIO;
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;
620 #ifdef HAVE_DIRCACHE
621 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
622 #endif
624 return nread ? nread : rc * 10 - 6;
627 memcpy( file->cache, (unsigned char*)buf + nread, count );
628 file->dirty = true;
630 else {
631 rc = fat_readwrite(&(file->fatfile), 1, &(file->cache),false);
632 if (rc < 1 ) {
633 DEBUGF("Failed caching sector\n");
634 errno = EIO;
635 file->fileoffset += nread;
636 file->cacheoffset = -1;
637 return nread ? nread : rc * 10 - 7;
639 memcpy( (unsigned char*)buf + nread, file->cache, count );
642 nread += count;
643 file->cacheoffset = count;
646 file->fileoffset += nread;
647 LDEBUGF("fileoffset: %ld\n", file->fileoffset);
649 /* adjust file size to length written */
650 if ( write && file->fileoffset > file->size )
652 file->size = file->fileoffset;
653 #ifdef HAVE_DIRCACHE
654 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
655 #endif
658 return nread;
661 ssize_t write(int fd, const void* buf, size_t count)
663 if (!openfiles[fd].write) {
664 errno = EACCES;
665 return -1;
667 return readwrite(fd, (void *)buf, count, true);
670 ssize_t read(int fd, void* buf, size_t count)
672 return readwrite(fd, buf, count, false);
676 off_t lseek(int fd, off_t offset, int whence)
678 off_t pos;
679 long newsector;
680 long oldsector;
681 int sectoroffset;
682 int rc;
683 struct filedesc* file = &openfiles[fd];
685 LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence);
687 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
688 errno = EINVAL;
689 return -1;
691 if ( !file->busy ) {
692 errno = EBADF;
693 return -1;
696 switch ( whence ) {
697 case SEEK_SET:
698 pos = offset;
699 break;
701 case SEEK_CUR:
702 pos = file->fileoffset + offset;
703 break;
705 case SEEK_END:
706 pos = file->size + offset;
707 break;
709 default:
710 errno = EINVAL;
711 return -2;
713 if ((pos < 0) || (pos > file->size)) {
714 errno = EINVAL;
715 return -3;
718 /* new sector? */
719 newsector = pos / SECTOR_SIZE;
720 oldsector = file->fileoffset / SECTOR_SIZE;
721 sectoroffset = pos % SECTOR_SIZE;
723 if ( (newsector != oldsector) ||
724 ((file->cacheoffset==-1) && sectoroffset) ) {
726 if ( newsector != oldsector ) {
727 if (file->dirty) {
728 rc = flush_cache(fd);
729 if (rc < 0)
730 return rc * 10 - 5;
733 rc = fat_seek(&(file->fatfile), newsector);
734 if ( rc < 0 ) {
735 errno = EIO;
736 return rc * 10 - 4;
739 if ( sectoroffset ) {
740 rc = fat_readwrite(&(file->fatfile), 1,
741 &(file->cache),false);
742 if ( rc < 0 ) {
743 errno = EIO;
744 return rc * 10 - 6;
746 file->cacheoffset = sectoroffset;
748 else
749 file->cacheoffset = -1;
751 else
752 if ( file->cacheoffset != -1 )
753 file->cacheoffset = sectoroffset;
755 file->fileoffset = pos;
757 return pos;
760 off_t filesize(int fd)
762 struct filedesc* file = &openfiles[fd];
764 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
765 errno = EINVAL;
766 return -1;
768 if ( !file->busy ) {
769 errno = EBADF;
770 return -1;
773 return file->size;
777 #ifdef HAVE_HOTSWAP
778 /* release all file handles on a given volume "by force", to avoid leaks */
779 int release_files(int volume)
781 struct filedesc* pfile = openfiles;
782 int fd;
783 int closed = 0;
784 for ( fd=0; fd<MAX_OPEN_FILES; fd++, pfile++)
786 if (pfile->fatfile.volume == volume)
788 pfile->busy = false; /* mark as available, no further action */
789 closed++;
792 return closed; /* return how many we did */
794 #endif /* #ifdef HAVE_HOTSWAP */