Successfully opens any file name given to it and pretends that all files have a certain number of bytes (set by application at time of mounting the file system.) Can be used to simulate large files sourced from an ultra fast file system to find and correct bugs in other areas of an application.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FakeFileSystem.cpp Source File

FakeFileSystem.cpp

00001 /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)
00002 
00003    Licensed under the Apache License, Version 2.0 (the "License");
00004    you may not use this file except in compliance with the License.
00005    You may obtain a copy of the License at
00006 
00007        http://www.apache.org/licenses/LICENSE-2.0
00008 
00009    Unless required by applicable law or agreed to in writing, software
00010    distributed under the License is distributed on an "AS IS" BASIS,
00011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012    See the License for the specific language governing permissions and
00013    limitations under the License.
00014 */
00015 /* Specifies the classes used to implement the FakeFileSystem which is a
00016    read-only file system that exists in the internal Fake of the mbed
00017    device.
00018 */
00019 #include <mbed.h>
00020 #include <assert.h>
00021 #include "FakeFileSystem.h"
00022 
00023 
00024 FakeFileSystemFileHandle::FakeFileSystemFileHandle(size_t FileSize)
00025 {
00026     SetEntry(FileSize);
00027 }
00028 
00029 FakeFileSystemFileHandle::FakeFileSystemFileHandle()
00030 {
00031     SetEntry(0);
00032 }
00033     
00034 ssize_t FakeFileSystemFileHandle::write(const void* pBuffer, size_t Length)
00035 {
00036     // This file system doesn't support writing.
00037     return -1;
00038 }
00039 
00040 
00041 int FakeFileSystemFileHandle::close()
00042 {
00043     SetEntry(0);
00044     
00045     return 0;
00046 }
00047 
00048 ssize_t FakeFileSystemFileHandle::read(void* pBuffer, size_t Length)
00049 {
00050     unsigned int    BytesLeft;
00051 
00052     // Don't read more bytes than what are left in the file.
00053     BytesLeft = m_Size - m_Offset;
00054     if (Length > BytesLeft)
00055     {
00056         Length = BytesLeft;
00057     }
00058     
00059     // Just leave buffer as is since we are just faking file reads.
00060     
00061     // Update the file pointer.
00062     m_Offset += Length;
00063     
00064     return Length;
00065 }
00066 
00067 int FakeFileSystemFileHandle::isatty()
00068 {
00069     return 0;
00070 }
00071 
00072 off_t FakeFileSystemFileHandle::lseek(off_t offset, int whence)
00073 {
00074     switch(whence)
00075     {
00076     case SEEK_SET:
00077         m_Offset = offset;
00078         break;
00079     case SEEK_CUR:
00080         m_Offset += offset;
00081         break;
00082     case SEEK_END:
00083         m_Offset = (m_Size - 1) + offset;
00084         break;
00085     default:
00086         return -1;
00087     }
00088     
00089     return m_Offset;
00090 }
00091 
00092 int FakeFileSystemFileHandle::fsync()
00093 {
00094     return 0;
00095 }
00096 
00097 off_t FakeFileSystemFileHandle::flen()
00098 {
00099     return m_Size;
00100 }
00101 
00102 
00103 
00104 
00105 FakeFileSystem::FakeFileSystem(const char* pName, size_t FakeFileSize) : FileSystemLike(pName)
00106 {
00107     m_FakeFileSize = FakeFileSize;
00108 }
00109 
00110 FileHandle* FakeFileSystem::open(const char* pFilename, int Flags)
00111 {
00112     // Can't find the file if file system hasn't been mounted.
00113     if (!IsMounted())
00114     {
00115         return NULL;
00116     }
00117 
00118     // Can only open fake files for read.
00119     if (O_RDONLY != Flags)
00120     {
00121         return NULL;
00122     }
00123     
00124     // Attempt to find a free file handle.
00125     FakeFileSystemFileHandle* pFileHandle = FindFreeFileHandle();
00126     if (!pFileHandle)
00127     {
00128         return NULL;
00129     }
00130     
00131     // Initialize the file handle and return it to caller.
00132     pFileHandle->SetEntry(m_FakeFileSize);
00133     return pFileHandle;
00134 }
00135 
00136 DirHandle*  FakeFileSystem::opendir(const char *pDirectoryName)
00137 {
00138     return NULL;
00139 }
00140 
00141 FakeFileSystemFileHandle* FakeFileSystem::FindFreeFileHandle()
00142 {
00143     size_t  i;
00144     
00145     // Iterate through the file handle array, looking for a close file handle.
00146     for (i = 0 ; i < sizeof(m_FileHandles)/sizeof(m_FileHandles[0]) ; i++)
00147     {
00148         if (m_FileHandles[i].IsClosed())
00149         {
00150             return &(m_FileHandles[i]);
00151         }
00152     }
00153     
00154     // If we get here, then no free entries were found.
00155     return NULL;
00156 }