Marco Mayer / Mbed OS Queue
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 "mbed.h"
00018 #include "filesystem/Dir.h"
00019 #include "filesystem/File.h"
00020 #include "filesystem/FileSystem.h"
00021 #include <errno.h>
00022 
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::file_sync(fs_file_t file)
00055 {
00056     return 0;
00057 }
00058 
00059 int FileSystem::file_isatty(fs_file_t file)
00060 {
00061     return false;
00062 }
00063 
00064 off_t FileSystem::file_tell(fs_file_t file)
00065 {
00066     return file_seek(file, 0, SEEK_CUR);
00067 }
00068 
00069 void FileSystem::file_rewind(fs_file_t file)
00070 {
00071     file_seek(file, 0, SEEK_SET);
00072 }
00073 
00074 off_t FileSystem::file_size(fs_file_t file)
00075 {
00076     off_t off = file_tell(file);
00077     off_t size = file_seek(file, 0, SEEK_END);
00078     file_seek(file, off, SEEK_SET);
00079     return size;
00080 }
00081 
00082 int FileSystem::dir_open(fs_dir_t *dir, const char *path)
00083 {
00084     return -ENOSYS;
00085 }
00086 
00087 int FileSystem::dir_close(fs_dir_t dir)
00088 {
00089     return -ENOSYS;
00090 }
00091 
00092 ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
00093 {
00094     return -ENOSYS;
00095 }
00096 
00097 void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
00098 {
00099 }
00100 
00101 off_t FileSystem::dir_tell(fs_dir_t dir)
00102 {
00103     return 0;
00104 }
00105 
00106 void FileSystem::dir_rewind(fs_dir_t dir)
00107 {
00108     // Note, the may not satisfy rewind on all filesystems
00109     dir_seek(dir, 0);
00110 }
00111 
00112 size_t FileSystem::dir_size(fs_dir_t dir)
00113 {
00114     off_t off = dir_tell(dir);
00115     size_t size = 0;
00116     struct dirent *ent = new struct dirent;
00117 
00118     dir_rewind(dir);
00119     while (true) {
00120         int res = dir_read(dir, ent);
00121         if (res <= 0) {
00122             break;
00123         }
00124 
00125         size += 1;
00126     }
00127     dir_seek(dir, off);
00128 
00129     delete ent;
00130     return size;
00131 }
00132 
00133 // Internally used file wrapper that manages memory on close
00134 template <typename F>
00135 class Managed : public F {
00136 public:
00137     virtual int close() {
00138         int err = F::close();
00139         delete this;
00140         return err;
00141     }
00142 };
00143 
00144 int FileSystem::open(FileHandle **file, const char *path, int flags)
00145 {
00146     File *f = new Managed<File>;
00147     int err = f->open(this, path, flags);
00148     if (err) {
00149         delete f;
00150         return err;
00151     }
00152 
00153     *file = f;
00154     return 0;
00155 }
00156 
00157 int FileSystem::open(DirHandle **dir, const char *path) {
00158     Dir *d = new Managed<Dir>;
00159     int err = d->open(this, path);
00160     if (err) {
00161         delete d;
00162         return err;
00163     }
00164 
00165     *dir = d;
00166     return 0;
00167 }