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