Nicolas Borla / Mbed OS BBR_1Ebene
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::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::dir_open(fs_dir_t *dir, const char *path)
00088 {
00089     return -ENOSYS;
00090 }
00091 
00092 int FileSystem::dir_close(fs_dir_t dir)
00093 {
00094     return -ENOSYS;
00095 }
00096 
00097 ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
00098 {
00099     return -ENOSYS;
00100 }
00101 
00102 void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
00103 {
00104 }
00105 
00106 off_t FileSystem::dir_tell(fs_dir_t dir)
00107 {
00108     return 0;
00109 }
00110 
00111 void FileSystem::dir_rewind(fs_dir_t dir)
00112 {
00113     // Note, the may not satisfy rewind on all filesystems
00114     dir_seek(dir, 0);
00115 }
00116 
00117 size_t FileSystem::dir_size(fs_dir_t dir)
00118 {
00119     off_t off = dir_tell(dir);
00120     size_t size = 0;
00121     struct dirent *ent = new struct dirent;
00122 
00123     dir_rewind(dir);
00124     while (true) {
00125         int res = dir_read(dir, ent);
00126         if (res <= 0) {
00127             break;
00128         }
00129 
00130         size += 1;
00131     }
00132     dir_seek(dir, off);
00133 
00134     delete ent;
00135     return size;
00136 }
00137 
00138 // Internally used file wrapper that manages memory on close
00139 template <typename F>
00140 class Managed : public F {
00141 public:
00142     virtual int close() {
00143         int err = F::close();
00144         delete this;
00145         return err;
00146     }
00147 };
00148 
00149 int FileSystem::open(FileHandle **file, const char *path, int flags)
00150 {
00151     File *f = new Managed<File>;
00152     int err = f->open(this, path, flags);
00153     if (err) {
00154         delete f;
00155         return err;
00156     }
00157 
00158     *file = f;
00159     return 0;
00160 }
00161 
00162 int FileSystem::open(DirHandle **dir, const char *path) {
00163     Dir *d = new Managed<Dir>;
00164     int err = d->open(this, path);
00165     if (err) {
00166         delete d;
00167         return err;
00168     }
00169 
00170     *dir = d;
00171     return 0;
00172 }