rbutiQt: fix a bug with font installation.
[Rockbox.git] / firmware / common / file.c
blobd526f288598d6d11a4233b72ee9e7c18131d9bee
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include <string.h>
20 #include <errno.h>
21 #include <stdbool.h>
22 #include "file.h"
23 #include "fat.h"
24 #include "dir_uncached.h"
25 #include "debug.h"
26 #include "dircache.h"
27 #include "system.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.
38 struct filedesc {
39 unsigned char cache[SECTOR_SIZE];
40 int cacheoffset; /* invariant: 0 <= cacheoffset <= SECTOR_SIZE */
41 long fileoffset;
42 long size;
43 int attr;
44 struct fat_file fatfile;
45 bool busy;
46 bool write;
47 bool dirty;
48 bool trunc;
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)
62 DIR_UNCACHED* dir;
63 struct dirent_uncached* entry;
64 int fd;
65 char pathnamecopy[MAX_PATH];
66 char* name;
67 struct filedesc* file = NULL;
68 int rc;
69 #ifndef HAVE_DIRCACHE
70 (void)use_cache;
71 #endif
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");
78 errno = EINVAL;
79 return -1;
82 /* find a free file descriptor */
83 for ( fd=0; fd<MAX_OPEN_FILES; fd++ )
84 if ( !openfiles[fd].busy )
85 break;
87 if ( fd == MAX_OPEN_FILES ) {
88 DEBUGF("Too many files open\n");
89 errno = EMFILE;
90 return -2;
93 file = &openfiles[fd];
94 memset(file, 0, sizeof(struct filedesc));
96 if (flags & (O_RDWR | O_WRONLY)) {
97 file->write = true;
99 if (flags & O_TRUNC)
100 file->trunc = true;
102 file->busy = true;
104 #ifdef HAVE_DIRCACHE
105 if (dircache_is_enabled() && !file->write && use_cache)
107 const struct dircache_entry *ce;
109 ce = dircache_get_entry_ptr(pathname);
110 if (!ce)
112 errno = ENOENT;
113 file->busy = false;
114 return -7;
117 fat_open(IF_MV2(unsupported at the moment,)
118 ce->startcluster,
119 &(file->fatfile),
120 NULL);
121 file->size = ce->size;
122 file->attr = ce->attribute;
123 file->cacheoffset = -1;
124 file->fileoffset = 0;
126 return fd;
128 #endif
130 strncpy(pathnamecopy,pathname,sizeof(pathnamecopy));
131 pathnamecopy[sizeof(pathnamecopy)-1] = 0;
133 /* locate filename */
134 name=strrchr(pathnamecopy+1,'/');
135 if ( name ) {
136 *name = 0;
137 dir = opendir_uncached(pathnamecopy);
138 *name = '/';
139 name++;
141 else {
142 dir = opendir_uncached("/");
143 name = pathnamecopy+1;
145 if (!dir) {
146 DEBUGF("Failed opening dir\n");
147 errno = EIO;
148 file->busy = false;
149 return -4;
152 if(name[0] == 0) {
153 DEBUGF("Empty file name\n");
154 errno = EINVAL;
155 file->busy = false;
156 closedir_uncached(dir);
157 return -5;
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,)
164 entry->startcluster,
165 &(file->fatfile),
166 &(dir->fatdir));
167 file->size = file->trunc ? 0 : entry->size;
168 file->attr = entry->attribute;
169 break;
173 if ( !entry ) {
174 LDEBUGF("Didn't find file %s\n",name);
175 if ( file->write && (flags & O_CREAT) ) {
176 rc = fat_create_file(name,
177 &(file->fatfile),
178 &(dir->fatdir));
179 if (rc < 0) {
180 DEBUGF("Couldn't create %s in %s\n",name,pathnamecopy);
181 errno = EIO;
182 file->busy = false;
183 closedir_uncached(dir);
184 return rc * 10 - 6;
186 #ifdef HAVE_DIRCACHE
187 dircache_add_file(pathname, file->fatfile.firstcluster);
188 #endif
189 file->size = 0;
190 file->attr = 0;
192 else {
193 DEBUGF("Couldn't find %s in %s\n",name,pathnamecopy);
194 errno = ENOENT;
195 file->busy = false;
196 closedir_uncached(dir);
197 return -7;
199 } else {
200 if(file->write && (file->attr & FAT_ATTR_DIRECTORY)) {
201 errno = EISDIR;
202 file->busy = false;
203 closedir_uncached(dir);
204 return -8;
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);
214 if (rc < 0 )
215 return rc * 10 - 9;
218 #ifdef HAVE_DIRCACHE
219 if (file->write)
220 dircache_bind(fd, pathname);
221 #endif
223 return fd;
226 int open(const char* pathname, int flags)
228 /* By default, use the dircache if available. */
229 return open_internal(pathname, flags, true);
232 int close(int fd)
234 struct filedesc* file = &openfiles[fd];
235 int rc = 0;
237 LDEBUGF("close(%d)\n", fd);
239 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
240 errno = EINVAL;
241 return -1;
243 if (!file->busy) {
244 errno = EBADF;
245 return -2;
247 if (file->write) {
248 rc = fsync(fd);
249 if (rc < 0)
250 return rc * 10 - 3;
251 #ifdef HAVE_DIRCACHE
252 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
253 #endif
256 file->busy = false;
257 return 0;
260 int fsync(int fd)
262 struct filedesc* file = &openfiles[fd];
263 int rc = 0;
265 LDEBUGF("fsync(%d)\n", fd);
267 if (fd < 0 || fd > MAX_OPEN_FILES-1) {
268 errno = EINVAL;
269 return -1;
271 if (!file->busy) {
272 errno = EBADF;
273 return -2;
275 if (file->write) {
276 /* flush sector cache */
277 if ( file->dirty ) {
278 rc = flush_cache(fd);
279 if (rc < 0)
280 return rc * 10 - 3;
283 /* truncate? */
284 if (file->trunc) {
285 rc = ftruncate(fd, file->size);
286 if (rc < 0)
287 return rc * 10 - 4;
290 /* tie up all loose ends */
291 rc = fat_closewrite(&(file->fatfile), file->size, file->attr);
292 if (rc < 0)
293 return rc * 10 - 5;
295 return 0;
298 int remove(const char* name)
300 int rc;
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);
304 if ( fd < 0 )
305 return fd * 10 - 1;
307 file = &openfiles[fd];
308 #ifdef HAVE_DIRCACHE
309 dircache_remove(name);
310 #endif
311 rc = fat_remove(&(file->fatfile));
312 if ( rc < 0 ) {
313 DEBUGF("Failed removing file: %d\n", rc);
314 errno = EIO;
315 return rc * 10 - 3;
318 file->size = 0;
320 rc = close(fd);
321 if (rc<0)
322 return rc * 10 - 4;
324 return 0;
327 int rename(const char* path, const char* newpath)
329 int rc, fd;
330 DIR_UNCACHED* dir;
331 char* nameptr;
332 char* dirptr;
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) {
340 close(fd);
341 errno = EBUSY;
342 return -1;
344 close(fd);
346 fd = open_internal(path, O_RDONLY, false);
347 if ( fd < 0 ) {
348 errno = EIO;
349 return fd * 10 - 2;
352 /* extract new file name */
353 nameptr = strrchr(newpath,'/');
354 if (nameptr)
355 nameptr++;
356 else
357 return - 3;
359 /* Extract new path */
360 strcpy(newpath2, newpath);
362 dirptr = strrchr(newpath2,'/');
363 if(dirptr)
364 *dirptr = 0;
365 else
366 return - 4;
368 dirptr = newpath2;
370 if(strlen(dirptr) == 0) {
371 dirptr = "/";
374 dir = opendir_uncached(dirptr);
375 if(!dir)
376 return - 5;
378 file = &openfiles[fd];
379 #ifdef HAVE_DIRCACHE
380 dircache_rename(path, newpath);
381 #endif
383 rc = fat_rename(&file->fatfile, &dir->fatdir, nameptr,
384 file->size, file->attr);
385 #ifdef HAVE_MULTIVOLUME
386 if ( rc == -1) {
387 DEBUGF("Failed renaming file across volumnes: %d\n", rc);
388 errno = EXDEV;
389 return -6;
391 #endif
392 if ( rc < 0 ) {
393 DEBUGF("Failed renaming file: %d\n", rc);
394 errno = EIO;
395 return rc * 10 - 7;
398 rc = close(fd);
399 if (rc<0) {
400 errno = EIO;
401 return rc * 10 - 8;
404 rc = closedir_uncached(dir);
405 if (rc<0) {
406 errno = EIO;
407 return rc * 10 - 9;
410 return 0;
413 int ftruncate(int fd, off_t size)
415 int rc, sector;
416 struct filedesc* file = &openfiles[fd];
418 sector = size / SECTOR_SIZE;
419 if (size % SECTOR_SIZE)
420 sector++;
422 rc = fat_seek(&(file->fatfile), sector);
423 if (rc < 0) {
424 errno = EIO;
425 return rc * 10 - 1;
428 rc = fat_truncate(&(file->fatfile));
429 if (rc < 0) {
430 errno = EIO;
431 return rc * 10 - 2;
434 file->size = size;
435 #ifdef HAVE_DIRCACHE
436 dircache_update_filesize(fd, size, file->fatfile.firstcluster);
437 #endif
439 return 0;
442 static int flush_cache(int fd)
444 int rc;
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);
452 if ( rc < 0 )
453 return rc * 10 - 3;
455 rc = fat_readwrite(&(file->fatfile), 1,
456 file->cache, true );
458 if ( rc < 0 ) {
459 if(file->fatfile.eof)
460 errno = ENOSPC;
462 return rc * 10 - 2;
465 file->dirty = false;
467 return 0;
470 static int readwrite(int fd, void* buf, long count, bool write)
472 long sectors;
473 long nread=0;
474 struct filedesc* file = &openfiles[fd];
475 int rc;
477 if ( !file->busy ) {
478 errno = EBADF;
479 return -1;
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);
494 if (write) {
495 memcpy( file->cache + offs, buf, headbytes );
496 file->dirty = true;
498 else {
499 memcpy( buf, file->cache + offs, headbytes );
502 if (offs + headbytes == SECTOR_SIZE) {
503 if (file->dirty) {
504 rc = flush_cache(fd);
505 if ( rc < 0 ) {
506 errno = EIO;
507 return rc * 10 - 2;
510 file->cacheoffset = -1;
512 else {
513 file->cacheoffset += headbytes;
516 nread = headbytes;
517 count -= 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;
526 if ( sectors ) {
527 rc = fat_readwrite(&(file->fatfile), sectors,
528 (unsigned char*)buf+nread, write );
529 if ( rc < 0 ) {
530 DEBUGF("Failed read/writing %ld sectors\n",sectors);
531 errno = EIO;
532 if(write && file->fatfile.eof) {
533 DEBUGF("No space left on device\n");
534 errno = ENOSPC;
535 } else {
536 file->fileoffset += nread;
538 file->cacheoffset = -1;
539 return nread ? nread : rc * 10 - 4;
541 else {
542 if ( rc > 0 ) {
543 nread += rc * SECTOR_SIZE;
544 count -= sectors * SECTOR_SIZE;
546 /* if eof, skip tail bytes */
547 if ( rc < sectors )
548 count = 0;
550 else {
551 /* eof */
552 count=0;
555 file->cacheoffset = -1;
559 /* any tail bytes? */
560 if ( count ) {
561 if (write) {
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 );
566 if ( rc < 0 ) {
567 DEBUGF("Failed writing\n");
568 errno = EIO;
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) /
576 SECTOR_SIZE);
577 if ( rc < 0 ) {
578 DEBUGF("fat_seek() failed\n");
579 errno = EIO;
580 file->fileoffset += nread;
581 file->cacheoffset = -1;
582 return nread ? nread : rc * 10 - 6;
585 memcpy( file->cache, (unsigned char*)buf + nread, count );
586 file->dirty = true;
588 else {
589 rc = fat_readwrite(&(file->fatfile), 1, &(file->cache),false);
590 if (rc < 1 ) {
591 DEBUGF("Failed caching sector\n");
592 errno = EIO;
593 file->fileoffset += nread;
594 file->cacheoffset = -1;
595 return nread ? nread : rc * 10 - 7;
597 memcpy( (unsigned char*)buf + nread, file->cache, count );
600 nread += 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;
611 #ifdef HAVE_DIRCACHE
612 dircache_update_filesize(fd, file->size, file->fatfile.firstcluster);
613 #endif
616 return nread;
619 ssize_t write(int fd, const void* buf, size_t count)
621 if (!openfiles[fd].write) {
622 errno = EACCES;
623 return -1;
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)
636 off_t pos;
637 long newsector;
638 long oldsector;
639 int sectoroffset;
640 int rc;
641 struct filedesc* file = &openfiles[fd];
643 LDEBUGF("lseek(%d,%ld,%d)\n",fd,offset,whence);
645 if ( !file->busy ) {
646 errno = EBADF;
647 return -1;
650 switch ( whence ) {
651 case SEEK_SET:
652 pos = offset;
653 break;
655 case SEEK_CUR:
656 pos = file->fileoffset + offset;
657 break;
659 case SEEK_END:
660 pos = file->size + offset;
661 break;
663 default:
664 errno = EINVAL;
665 return -2;
667 if ((pos < 0) || (pos > file->size)) {
668 errno = EINVAL;
669 return -3;
672 /* new sector? */
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 ) {
681 if (file->dirty) {
682 rc = flush_cache(fd);
683 if (rc < 0)
684 return rc * 10 - 5;
687 rc = fat_seek(&(file->fatfile), newsector);
688 if ( rc < 0 ) {
689 errno = EIO;
690 return rc * 10 - 4;
693 if ( sectoroffset ) {
694 rc = fat_readwrite(&(file->fatfile), 1,
695 &(file->cache),false);
696 if ( rc < 0 ) {
697 errno = EIO;
698 return rc * 10 - 6;
700 file->cacheoffset = sectoroffset;
702 else
703 file->cacheoffset = -1;
705 else
706 if ( file->cacheoffset != -1 )
707 file->cacheoffset = sectoroffset;
709 file->fileoffset = pos;
711 return pos;
714 off_t filesize(int fd)
716 struct filedesc* file = &openfiles[fd];
718 if ( !file->busy ) {
719 errno = EBADF;
720 return -1;
723 return file->size;
727 #ifdef HAVE_HOTSWAP
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;
732 int fd;
733 int closed = 0;
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 */
739 closed++;
742 return closed; /* return how many we did */
744 #endif /* #ifdef HAVE_HOTSWAP */