Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FATFileHandle.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #include "ff.h" 00023 #include "ffconf.h" 00024 #include "mbed_debug.h" 00025 00026 #include "FATFileHandle.h" 00027 00028 FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex) { 00029 _fh = fh; 00030 } 00031 00032 int FATFileHandle::close() { 00033 lock(); 00034 int retval = f_close(&_fh); 00035 unlock(); 00036 delete this; 00037 return retval; 00038 } 00039 00040 ssize_t FATFileHandle::write(const void* buffer, size_t length) { 00041 lock(); 00042 UINT n; 00043 FRESULT res = f_write(&_fh, buffer, length, &n); 00044 if (res) { 00045 debug_if(FFS_DBG, "f_write() failed: %d", res); 00046 unlock(); 00047 return -1; 00048 } 00049 unlock(); 00050 return n; 00051 } 00052 00053 ssize_t FATFileHandle::read(void* buffer, size_t length) { 00054 lock(); 00055 debug_if(FFS_DBG, "read(%d)\n", length); 00056 UINT n; 00057 FRESULT res = f_read(&_fh, buffer, length, &n); 00058 if (res) { 00059 debug_if(FFS_DBG, "f_read() failed: %d\n", res); 00060 unlock(); 00061 return -1; 00062 } 00063 unlock(); 00064 return n; 00065 } 00066 00067 int FATFileHandle::isatty() { 00068 return 0; 00069 } 00070 00071 off_t FATFileHandle::lseek(off_t position, int whence) { 00072 lock(); 00073 if (whence == SEEK_END) { 00074 position += _fh.fsize; 00075 } else if(whence==SEEK_CUR) { 00076 position += _fh.fptr; 00077 } 00078 FRESULT res = f_lseek(&_fh, position); 00079 if (res) { 00080 debug_if(FFS_DBG, "lseek failed: %d\n", res); 00081 unlock(); 00082 return -1; 00083 } else { 00084 debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); 00085 unlock(); 00086 return _fh.fptr; 00087 } 00088 } 00089 00090 int FATFileHandle::fsync() { 00091 lock(); 00092 FRESULT res = f_sync(&_fh); 00093 if (res) { 00094 debug_if(FFS_DBG, "f_sync() failed: %d\n", res); 00095 unlock(); 00096 return -1; 00097 } 00098 unlock(); 00099 return 0; 00100 } 00101 00102 off_t FATFileHandle::flen() { 00103 lock(); 00104 off_t size = _fh.fsize; 00105 unlock(); 00106 return size; 00107 } 00108 00109 void FATFileHandle::lock() { 00110 _mutex->lock(); 00111 } 00112 00113 void FATFileHandle::unlock() { 00114 _mutex->unlock(); 00115 }
Generated on Tue Jul 12 2022 17:34:42 by
1.7.2