First working version of a FATFileSystem compatible Chan FAT v0.8 implementation. This is intended to use with long file names and RTOS support. For now long file names work but RTOS support is still untested.

Dependents:   USB_MSC USB_CDC_MSD_Hello TFTPServerTest DMXStation ... more

Committer:
NeoBelerophon
Date:
Fri Feb 04 21:14:33 2011 +0000
Revision:
2:629e4be333f3
Parent:
0:8ea634413549
getdir() did not work -> remove

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NeoBelerophon 0:8ea634413549 1 /* mbed Microcontroller Library - FATFileHandle
NeoBelerophon 0:8ea634413549 2 * Copyright (c) 2008, sford
NeoBelerophon 0:8ea634413549 3 */
NeoBelerophon 0:8ea634413549 4
NeoBelerophon 0:8ea634413549 5 #include "FATFileHandle.h"
NeoBelerophon 0:8ea634413549 6
NeoBelerophon 0:8ea634413549 7 #include <stdio.h>
NeoBelerophon 0:8ea634413549 8 #include <stdlib.h>
NeoBelerophon 0:8ea634413549 9 #include "ff.h"
NeoBelerophon 0:8ea634413549 10 #include "FATFileSystem.h"
NeoBelerophon 0:8ea634413549 11
NeoBelerophon 0:8ea634413549 12 namespace mbed {
NeoBelerophon 0:8ea634413549 13
NeoBelerophon 0:8ea634413549 14 #if FFSDEBUG_ENABLED
NeoBelerophon 0:8ea634413549 15 static const char *FR_ERRORS[] = {
NeoBelerophon 0:8ea634413549 16 "FR_OK = 0",
NeoBelerophon 0:8ea634413549 17 "FR_NOT_READY",
NeoBelerophon 0:8ea634413549 18 "FR_NO_FILE",
NeoBelerophon 0:8ea634413549 19 "FR_NO_PATH",
NeoBelerophon 0:8ea634413549 20 "FR_INVALID_NAME",
NeoBelerophon 0:8ea634413549 21 "FR_INVALID_DRIVE",
NeoBelerophon 0:8ea634413549 22 "FR_DENIED",
NeoBelerophon 0:8ea634413549 23 "FR_EXIST",
NeoBelerophon 0:8ea634413549 24 "FR_RW_ERROR",
NeoBelerophon 0:8ea634413549 25 "FR_WRITE_PROTECTED",
NeoBelerophon 0:8ea634413549 26 "FR_NOT_ENABLED",
NeoBelerophon 0:8ea634413549 27 "FR_NO_FILESYSTEM",
NeoBelerophon 0:8ea634413549 28 "FR_INVALID_OBJECT",
NeoBelerophon 0:8ea634413549 29 "FR_MKFS_ABORTED"
NeoBelerophon 0:8ea634413549 30 };
NeoBelerophon 0:8ea634413549 31 #endif
NeoBelerophon 0:8ea634413549 32
NeoBelerophon 0:8ea634413549 33 FATFileHandle::FATFileHandle(FIL_t fh) {
NeoBelerophon 0:8ea634413549 34 _fh = fh;
NeoBelerophon 0:8ea634413549 35 }
NeoBelerophon 0:8ea634413549 36
NeoBelerophon 0:8ea634413549 37 int FATFileHandle::close() {
NeoBelerophon 0:8ea634413549 38 FFSDEBUG("close\n");
NeoBelerophon 0:8ea634413549 39 int retval = f_close(&_fh);
NeoBelerophon 0:8ea634413549 40 delete this;
NeoBelerophon 0:8ea634413549 41 return retval;
NeoBelerophon 0:8ea634413549 42 }
NeoBelerophon 0:8ea634413549 43
NeoBelerophon 0:8ea634413549 44 ssize_t FATFileHandle::write(const void* buffer, size_t length) {
NeoBelerophon 0:8ea634413549 45 FFSDEBUG("write(%d)\n", length);
NeoBelerophon 0:8ea634413549 46 UINT n;
NeoBelerophon 0:8ea634413549 47 FRESULT res = f_write(&_fh, buffer, length, &n);
NeoBelerophon 0:8ea634413549 48 if(res) {
NeoBelerophon 0:8ea634413549 49 FFSDEBUG("f_write() failed (%d, %s)", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 50 return -1;
NeoBelerophon 0:8ea634413549 51 }
NeoBelerophon 0:8ea634413549 52 return n;
NeoBelerophon 0:8ea634413549 53 }
NeoBelerophon 0:8ea634413549 54
NeoBelerophon 0:8ea634413549 55 ssize_t FATFileHandle::read(void* buffer, size_t length) {
NeoBelerophon 0:8ea634413549 56 FFSDEBUG("read(%d)\n", length);
NeoBelerophon 0:8ea634413549 57 UINT n;
NeoBelerophon 0:8ea634413549 58 FRESULT res = f_read(&_fh, buffer, length, &n);
NeoBelerophon 0:8ea634413549 59 if(res) {
NeoBelerophon 0:8ea634413549 60 FFSDEBUG("f_read() failed (%d, %s)\n", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 61 return -1;
NeoBelerophon 0:8ea634413549 62 }
NeoBelerophon 0:8ea634413549 63 return n;
NeoBelerophon 0:8ea634413549 64 }
NeoBelerophon 0:8ea634413549 65
NeoBelerophon 0:8ea634413549 66 int FATFileHandle::isatty() {
NeoBelerophon 0:8ea634413549 67 return 0;
NeoBelerophon 0:8ea634413549 68 }
NeoBelerophon 0:8ea634413549 69
NeoBelerophon 0:8ea634413549 70 off_t FATFileHandle::lseek(off_t position, int whence) {
NeoBelerophon 0:8ea634413549 71 FFSDEBUG("lseek(%i,%i)\n",position,whence);
NeoBelerophon 0:8ea634413549 72 if(whence == SEEK_END) {
NeoBelerophon 0:8ea634413549 73 position += _fh.fsize;
NeoBelerophon 0:8ea634413549 74 } else if(whence==SEEK_CUR) {
NeoBelerophon 0:8ea634413549 75 position += _fh.fptr;
NeoBelerophon 0:8ea634413549 76 }
NeoBelerophon 0:8ea634413549 77 FRESULT res = f_lseek(&_fh, position);
NeoBelerophon 0:8ea634413549 78 if(res) {
NeoBelerophon 0:8ea634413549 79 FFSDEBUG("lseek failed (%d, %s)\n", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 80 return -1;
NeoBelerophon 0:8ea634413549 81 } else {
NeoBelerophon 0:8ea634413549 82 FFSDEBUG("lseek OK, returning %i\n", _fh.fptr);
NeoBelerophon 0:8ea634413549 83 return _fh.fptr;
NeoBelerophon 0:8ea634413549 84 }
NeoBelerophon 0:8ea634413549 85 }
NeoBelerophon 0:8ea634413549 86
NeoBelerophon 0:8ea634413549 87 int FATFileHandle::fsync() {
NeoBelerophon 0:8ea634413549 88 FFSDEBUG("fsync()\n");
NeoBelerophon 0:8ea634413549 89 FRESULT res = f_sync(&_fh);
NeoBelerophon 0:8ea634413549 90 if (res) {
NeoBelerophon 0:8ea634413549 91 FFSDEBUG("f_sync() failed (%d, %s)\n", res, FR_ERRORS[res]);
NeoBelerophon 0:8ea634413549 92 return -1;
NeoBelerophon 0:8ea634413549 93 }
NeoBelerophon 0:8ea634413549 94 return 0;
NeoBelerophon 0:8ea634413549 95 }
NeoBelerophon 0:8ea634413549 96
NeoBelerophon 0:8ea634413549 97 off_t FATFileHandle::flen() {
NeoBelerophon 0:8ea634413549 98 FFSDEBUG("flen\n");
NeoBelerophon 0:8ea634413549 99 return _fh.fsize;
NeoBelerophon 0:8ea634413549 100 }
NeoBelerophon 0:8ea634413549 101
NeoBelerophon 0:8ea634413549 102 } // namespace mbed