Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FileSystem.cpp Source File

FileSystem.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "features/storage/filesystem/Dir.h"
00018 #include "features/storage/filesystem/File.h"
00019 #include "features/storage/filesystem/FileSystem.h"
00020 #include <errno.h>
00021 
00022 namespace mbed {
00023 
00024 FileSystem::FileSystem(const char *name)
00025     : FileSystemLike(name)
00026 {
00027 }
00028 
00029 int FileSystem::reformat(BlockDevice *bd)
00030 {
00031     return -ENOSYS;
00032 }
00033 
00034 int FileSystem::remove(const char *path)
00035 {
00036     return -ENOSYS;
00037 }
00038 
00039 int FileSystem::rename(const char *path, const char *newpath)
00040 {
00041     return -ENOSYS;
00042 }
00043 
00044 int FileSystem::stat(const char *path, struct stat *st)
00045 {
00046     return -ENOSYS;
00047 }
00048 
00049 int FileSystem::mkdir(const char *path, mode_t mode)
00050 {
00051     return -ENOSYS;
00052 }
00053 
00054 int FileSystem::statvfs(const char *path, struct statvfs *buf)
00055 {
00056     return -ENOSYS;
00057 }
00058 
00059 int FileSystem::file_sync(fs_file_t file)
00060 {
00061     return 0;
00062 }
00063 
00064 int FileSystem::file_isatty(fs_file_t file)
00065 {
00066     return false;
00067 }
00068 
00069 off_t FileSystem::file_tell(fs_file_t file)
00070 {
00071     return file_seek(file, 0, SEEK_CUR);
00072 }
00073 
00074 void FileSystem::file_rewind(fs_file_t file)
00075 {
00076     file_seek(file, 0, SEEK_SET);
00077 }
00078 
00079 off_t FileSystem::file_size(fs_file_t file)
00080 {
00081     off_t off = file_tell(file);
00082     off_t size = file_seek(file, 0, SEEK_END);
00083     file_seek(file, off, SEEK_SET);
00084     return size;
00085 }
00086 
00087 int FileSystem::file_truncate(fs_file_t file, off_t length)
00088 {
00089     return -ENOSYS;
00090 }
00091 
00092 int FileSystem::dir_open(fs_dir_t *dir, const char *path)
00093 {
00094     return -ENOSYS;
00095 }
00096 
00097 int FileSystem::dir_close(fs_dir_t dir)
00098 {
00099     return -ENOSYS;
00100 }
00101 
00102 ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
00103 {
00104     return -ENOSYS;
00105 }
00106 
00107 void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
00108 {
00109 }
00110 
00111 off_t FileSystem::dir_tell(fs_dir_t dir)
00112 {
00113     return 0;
00114 }
00115 
00116 void FileSystem::dir_rewind(fs_dir_t dir)
00117 {
00118     // Note, the may not satisfy rewind on all filesystems
00119     dir_seek(dir, 0);
00120 }
00121 
00122 size_t FileSystem::dir_size(fs_dir_t dir)
00123 {
00124     off_t off = dir_tell(dir);
00125     size_t size = 0;
00126     struct dirent *ent = new struct dirent;
00127 
00128     dir_rewind(dir);
00129     while (true) {
00130         int res = dir_read(dir, ent);
00131         if (res <= 0) {
00132             break;
00133         }
00134 
00135         size += 1;
00136     }
00137     dir_seek(dir, off);
00138 
00139     delete ent;
00140     return size;
00141 }
00142 
00143 // Internally used file wrapper that manages memory on close
00144 template <typename F>
00145 class Managed : public F {
00146 public:
00147     virtual int close()
00148     {
00149         int err = F::close();
00150         delete this;
00151         return err;
00152     }
00153 };
00154 
00155 int FileSystem::open(FileHandle **file, const char *path, int flags)
00156 {
00157     File *f = new Managed<File>;
00158     int err = f->open(this, path, flags);
00159     if (err) {
00160         delete f;
00161         return err;
00162     }
00163 
00164     *file = f;
00165     return 0;
00166 }
00167 
00168 int FileSystem::open(DirHandle **dir, const char *path)
00169 {
00170     Dir *d = new Managed<Dir>;
00171     int err = d->open(this, path);
00172     if (err) {
00173         delete d;
00174         return err;
00175     }
00176 
00177     *dir = d;
00178     return 0;
00179 }
00180 
00181 } // namespace mbed