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