Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* mbed Microcontroller Library
borlanic 0:380207fcb5c1 2 * Copyright (c) 2006-2013 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:380207fcb5c1 5 * you may not use this file except in compliance with the License.
borlanic 0:380207fcb5c1 6 * You may obtain a copy of the License at
borlanic 0:380207fcb5c1 7 *
borlanic 0:380207fcb5c1 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:380207fcb5c1 9 *
borlanic 0:380207fcb5c1 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:380207fcb5c1 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:380207fcb5c1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:380207fcb5c1 13 * See the License for the specific language governing permissions and
borlanic 0:380207fcb5c1 14 * limitations under the License.
borlanic 0:380207fcb5c1 15 */
borlanic 0:380207fcb5c1 16
borlanic 0:380207fcb5c1 17 #include "mbed.h"
borlanic 0:380207fcb5c1 18 #include "filesystem/Dir.h"
borlanic 0:380207fcb5c1 19 #include "filesystem/File.h"
borlanic 0:380207fcb5c1 20 #include "filesystem/FileSystem.h"
borlanic 0:380207fcb5c1 21 #include <errno.h>
borlanic 0:380207fcb5c1 22
borlanic 0:380207fcb5c1 23
borlanic 0:380207fcb5c1 24 FileSystem::FileSystem(const char *name)
borlanic 0:380207fcb5c1 25 : FileSystemLike(name)
borlanic 0:380207fcb5c1 26 {
borlanic 0:380207fcb5c1 27 }
borlanic 0:380207fcb5c1 28
borlanic 0:380207fcb5c1 29 int FileSystem::reformat(BlockDevice *bd)
borlanic 0:380207fcb5c1 30 {
borlanic 0:380207fcb5c1 31 return -ENOSYS;
borlanic 0:380207fcb5c1 32 }
borlanic 0:380207fcb5c1 33
borlanic 0:380207fcb5c1 34 int FileSystem::remove(const char *path)
borlanic 0:380207fcb5c1 35 {
borlanic 0:380207fcb5c1 36 return -ENOSYS;
borlanic 0:380207fcb5c1 37 }
borlanic 0:380207fcb5c1 38
borlanic 0:380207fcb5c1 39 int FileSystem::rename(const char *path, const char *newpath)
borlanic 0:380207fcb5c1 40 {
borlanic 0:380207fcb5c1 41 return -ENOSYS;
borlanic 0:380207fcb5c1 42 }
borlanic 0:380207fcb5c1 43
borlanic 0:380207fcb5c1 44 int FileSystem::stat(const char *path, struct stat *st)
borlanic 0:380207fcb5c1 45 {
borlanic 0:380207fcb5c1 46 return -ENOSYS;
borlanic 0:380207fcb5c1 47 }
borlanic 0:380207fcb5c1 48
borlanic 0:380207fcb5c1 49 int FileSystem::mkdir(const char *path, mode_t mode)
borlanic 0:380207fcb5c1 50 {
borlanic 0:380207fcb5c1 51 return -ENOSYS;
borlanic 0:380207fcb5c1 52 }
borlanic 0:380207fcb5c1 53
borlanic 0:380207fcb5c1 54 int FileSystem::statvfs(const char *path, struct statvfs *buf)
borlanic 0:380207fcb5c1 55 {
borlanic 0:380207fcb5c1 56 return -ENOSYS;
borlanic 0:380207fcb5c1 57 }
borlanic 0:380207fcb5c1 58
borlanic 0:380207fcb5c1 59 int FileSystem::file_sync(fs_file_t file)
borlanic 0:380207fcb5c1 60 {
borlanic 0:380207fcb5c1 61 return 0;
borlanic 0:380207fcb5c1 62 }
borlanic 0:380207fcb5c1 63
borlanic 0:380207fcb5c1 64 int FileSystem::file_isatty(fs_file_t file)
borlanic 0:380207fcb5c1 65 {
borlanic 0:380207fcb5c1 66 return false;
borlanic 0:380207fcb5c1 67 }
borlanic 0:380207fcb5c1 68
borlanic 0:380207fcb5c1 69 off_t FileSystem::file_tell(fs_file_t file)
borlanic 0:380207fcb5c1 70 {
borlanic 0:380207fcb5c1 71 return file_seek(file, 0, SEEK_CUR);
borlanic 0:380207fcb5c1 72 }
borlanic 0:380207fcb5c1 73
borlanic 0:380207fcb5c1 74 void FileSystem::file_rewind(fs_file_t file)
borlanic 0:380207fcb5c1 75 {
borlanic 0:380207fcb5c1 76 file_seek(file, 0, SEEK_SET);
borlanic 0:380207fcb5c1 77 }
borlanic 0:380207fcb5c1 78
borlanic 0:380207fcb5c1 79 off_t FileSystem::file_size(fs_file_t file)
borlanic 0:380207fcb5c1 80 {
borlanic 0:380207fcb5c1 81 off_t off = file_tell(file);
borlanic 0:380207fcb5c1 82 off_t size = file_seek(file, 0, SEEK_END);
borlanic 0:380207fcb5c1 83 file_seek(file, off, SEEK_SET);
borlanic 0:380207fcb5c1 84 return size;
borlanic 0:380207fcb5c1 85 }
borlanic 0:380207fcb5c1 86
borlanic 0:380207fcb5c1 87 int FileSystem::dir_open(fs_dir_t *dir, const char *path)
borlanic 0:380207fcb5c1 88 {
borlanic 0:380207fcb5c1 89 return -ENOSYS;
borlanic 0:380207fcb5c1 90 }
borlanic 0:380207fcb5c1 91
borlanic 0:380207fcb5c1 92 int FileSystem::dir_close(fs_dir_t dir)
borlanic 0:380207fcb5c1 93 {
borlanic 0:380207fcb5c1 94 return -ENOSYS;
borlanic 0:380207fcb5c1 95 }
borlanic 0:380207fcb5c1 96
borlanic 0:380207fcb5c1 97 ssize_t FileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
borlanic 0:380207fcb5c1 98 {
borlanic 0:380207fcb5c1 99 return -ENOSYS;
borlanic 0:380207fcb5c1 100 }
borlanic 0:380207fcb5c1 101
borlanic 0:380207fcb5c1 102 void FileSystem::dir_seek(fs_dir_t dir, off_t offset)
borlanic 0:380207fcb5c1 103 {
borlanic 0:380207fcb5c1 104 }
borlanic 0:380207fcb5c1 105
borlanic 0:380207fcb5c1 106 off_t FileSystem::dir_tell(fs_dir_t dir)
borlanic 0:380207fcb5c1 107 {
borlanic 0:380207fcb5c1 108 return 0;
borlanic 0:380207fcb5c1 109 }
borlanic 0:380207fcb5c1 110
borlanic 0:380207fcb5c1 111 void FileSystem::dir_rewind(fs_dir_t dir)
borlanic 0:380207fcb5c1 112 {
borlanic 0:380207fcb5c1 113 // Note, the may not satisfy rewind on all filesystems
borlanic 0:380207fcb5c1 114 dir_seek(dir, 0);
borlanic 0:380207fcb5c1 115 }
borlanic 0:380207fcb5c1 116
borlanic 0:380207fcb5c1 117 size_t FileSystem::dir_size(fs_dir_t dir)
borlanic 0:380207fcb5c1 118 {
borlanic 0:380207fcb5c1 119 off_t off = dir_tell(dir);
borlanic 0:380207fcb5c1 120 size_t size = 0;
borlanic 0:380207fcb5c1 121 struct dirent *ent = new struct dirent;
borlanic 0:380207fcb5c1 122
borlanic 0:380207fcb5c1 123 dir_rewind(dir);
borlanic 0:380207fcb5c1 124 while (true) {
borlanic 0:380207fcb5c1 125 int res = dir_read(dir, ent);
borlanic 0:380207fcb5c1 126 if (res <= 0) {
borlanic 0:380207fcb5c1 127 break;
borlanic 0:380207fcb5c1 128 }
borlanic 0:380207fcb5c1 129
borlanic 0:380207fcb5c1 130 size += 1;
borlanic 0:380207fcb5c1 131 }
borlanic 0:380207fcb5c1 132 dir_seek(dir, off);
borlanic 0:380207fcb5c1 133
borlanic 0:380207fcb5c1 134 delete ent;
borlanic 0:380207fcb5c1 135 return size;
borlanic 0:380207fcb5c1 136 }
borlanic 0:380207fcb5c1 137
borlanic 0:380207fcb5c1 138 // Internally used file wrapper that manages memory on close
borlanic 0:380207fcb5c1 139 template <typename F>
borlanic 0:380207fcb5c1 140 class Managed : public F {
borlanic 0:380207fcb5c1 141 public:
borlanic 0:380207fcb5c1 142 virtual int close() {
borlanic 0:380207fcb5c1 143 int err = F::close();
borlanic 0:380207fcb5c1 144 delete this;
borlanic 0:380207fcb5c1 145 return err;
borlanic 0:380207fcb5c1 146 }
borlanic 0:380207fcb5c1 147 };
borlanic 0:380207fcb5c1 148
borlanic 0:380207fcb5c1 149 int FileSystem::open(FileHandle **file, const char *path, int flags)
borlanic 0:380207fcb5c1 150 {
borlanic 0:380207fcb5c1 151 File *f = new Managed<File>;
borlanic 0:380207fcb5c1 152 int err = f->open(this, path, flags);
borlanic 0:380207fcb5c1 153 if (err) {
borlanic 0:380207fcb5c1 154 delete f;
borlanic 0:380207fcb5c1 155 return err;
borlanic 0:380207fcb5c1 156 }
borlanic 0:380207fcb5c1 157
borlanic 0:380207fcb5c1 158 *file = f;
borlanic 0:380207fcb5c1 159 return 0;
borlanic 0:380207fcb5c1 160 }
borlanic 0:380207fcb5c1 161
borlanic 0:380207fcb5c1 162 int FileSystem::open(DirHandle **dir, const char *path) {
borlanic 0:380207fcb5c1 163 Dir *d = new Managed<Dir>;
borlanic 0:380207fcb5c1 164 int err = d->open(this, path);
borlanic 0:380207fcb5c1 165 if (err) {
borlanic 0:380207fcb5c1 166 delete d;
borlanic 0:380207fcb5c1 167 return err;
borlanic 0:380207fcb5c1 168 }
borlanic 0:380207fcb5c1 169
borlanic 0:380207fcb5c1 170 *dir = d;
borlanic 0:380207fcb5c1 171 return 0;
borlanic 0:380207fcb5c1 172 }