4545
Fork of mbed-dev by
drivers/LocalFileSystem.cpp@166:33361e55dd8c, 2017-08-23 (annotated)
- Committer:
- iftaziz
- Date:
- Wed Aug 23 10:32:38 2017 +0000
- Revision:
- 166:33361e55dd8c
- Parent:
- 160:d5399cc887bb
r1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #include "drivers/LocalFileSystem.h" |
<> | 149:156823d33999 | 17 | |
<> | 149:156823d33999 | 18 | #if DEVICE_LOCALFILESYSTEM |
<> | 149:156823d33999 | 19 | |
<> | 160:d5399cc887bb | 20 | #include "platform/mbed_semihost_api.h" |
<> | 149:156823d33999 | 21 | #include <string.h> |
<> | 149:156823d33999 | 22 | #include <stdio.h> |
<> | 149:156823d33999 | 23 | |
<> | 149:156823d33999 | 24 | namespace mbed { |
<> | 149:156823d33999 | 25 | |
<> | 149:156823d33999 | 26 | /* Extension to FINFO type defined in RTL.h (in Keil RL) - adds 'create time'. */ |
<> | 149:156823d33999 | 27 | typedef struct { |
<> | 149:156823d33999 | 28 | unsigned char hr; /* Hours [0..23] */ |
<> | 149:156823d33999 | 29 | unsigned char min; /* Minutes [0..59] */ |
<> | 149:156823d33999 | 30 | unsigned char sec; /* Seconds [0..59] */ |
<> | 149:156823d33999 | 31 | unsigned char day; /* Day [1..31] */ |
<> | 149:156823d33999 | 32 | unsigned char mon; /* Month [1..12] */ |
<> | 149:156823d33999 | 33 | unsigned short year; /* Year [1980..2107] */ |
<> | 149:156823d33999 | 34 | } FTIME; |
<> | 149:156823d33999 | 35 | |
<> | 149:156823d33999 | 36 | typedef struct { /* File Search info record */ |
<> | 149:156823d33999 | 37 | char name[32]; /* File name */ |
<> | 149:156823d33999 | 38 | long size; /* File size in bytes */ |
<> | 149:156823d33999 | 39 | int fileID; /* System File Identification */ |
<> | 149:156823d33999 | 40 | FTIME create_time; /* Date & time file was created */ |
<> | 149:156823d33999 | 41 | FTIME write_time; /* Date & time of last write */ |
<> | 149:156823d33999 | 42 | } XFINFO; |
<> | 149:156823d33999 | 43 | |
<> | 149:156823d33999 | 44 | #define RESERVED_FOR_USER_APPLICATIONS (0x100) /* 0x100 - 0x1ff */ |
<> | 149:156823d33999 | 45 | #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0) |
<> | 149:156823d33999 | 46 | |
<> | 149:156823d33999 | 47 | static int xffind (const char *pattern, XFINFO *info) { |
<> | 149:156823d33999 | 48 | unsigned param[4]; |
<> | 149:156823d33999 | 49 | |
<> | 149:156823d33999 | 50 | param[0] = (unsigned long)pattern; |
<> | 149:156823d33999 | 51 | param[1] = (unsigned long)strlen(pattern); |
<> | 149:156823d33999 | 52 | param[2] = (unsigned long)info; |
<> | 149:156823d33999 | 53 | param[3] = (unsigned long)sizeof(XFINFO); |
<> | 149:156823d33999 | 54 | |
<> | 149:156823d33999 | 55 | return __semihost(USR_XFFIND, param); |
<> | 149:156823d33999 | 56 | } |
<> | 149:156823d33999 | 57 | |
<> | 149:156823d33999 | 58 | #define OPEN_R 0 |
<> | 149:156823d33999 | 59 | #define OPEN_B 1 |
<> | 149:156823d33999 | 60 | #define OPEN_PLUS 2 |
<> | 149:156823d33999 | 61 | #define OPEN_W 4 |
<> | 149:156823d33999 | 62 | #define OPEN_A 8 |
<> | 149:156823d33999 | 63 | #define OPEN_INVALID -1 |
<> | 149:156823d33999 | 64 | |
<> | 149:156823d33999 | 65 | int posix_to_semihost_open_flags(int flags) { |
<> | 149:156823d33999 | 66 | /* POSIX flags -> semihosting open mode */ |
<> | 149:156823d33999 | 67 | int openmode; |
<> | 149:156823d33999 | 68 | if (flags & O_RDWR) { |
<> | 149:156823d33999 | 69 | /* a plus mode */ |
<> | 149:156823d33999 | 70 | openmode = OPEN_PLUS; |
<> | 149:156823d33999 | 71 | if (flags & O_APPEND) { |
<> | 149:156823d33999 | 72 | openmode |= OPEN_A; |
<> | 149:156823d33999 | 73 | } else if (flags & O_TRUNC) { |
<> | 149:156823d33999 | 74 | openmode |= OPEN_W; |
<> | 149:156823d33999 | 75 | } else { |
<> | 149:156823d33999 | 76 | openmode |= OPEN_R; |
<> | 149:156823d33999 | 77 | } |
<> | 149:156823d33999 | 78 | } else if (flags & O_WRONLY) { |
<> | 149:156823d33999 | 79 | /* write or append */ |
<> | 149:156823d33999 | 80 | if (flags & O_APPEND) { |
<> | 149:156823d33999 | 81 | openmode = OPEN_A; |
<> | 149:156823d33999 | 82 | } else { |
<> | 149:156823d33999 | 83 | openmode = OPEN_W; |
<> | 149:156823d33999 | 84 | } |
<> | 149:156823d33999 | 85 | } else if (flags == O_RDONLY) { |
<> | 149:156823d33999 | 86 | /* read mode */ |
<> | 149:156823d33999 | 87 | openmode = OPEN_R; |
<> | 149:156823d33999 | 88 | } else { |
<> | 149:156823d33999 | 89 | /* invalid flags */ |
<> | 149:156823d33999 | 90 | openmode = OPEN_INVALID; |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | return openmode; |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | |
<> | 149:156823d33999 | 96 | FILEHANDLE local_file_open(const char* name, int flags) { |
<> | 149:156823d33999 | 97 | int openmode = posix_to_semihost_open_flags(flags); |
<> | 149:156823d33999 | 98 | if (openmode == OPEN_INVALID) { |
<> | 149:156823d33999 | 99 | return (FILEHANDLE)NULL; |
<> | 149:156823d33999 | 100 | } |
<> | 149:156823d33999 | 101 | |
<> | 149:156823d33999 | 102 | FILEHANDLE fh = semihost_open(name, openmode); |
<> | 149:156823d33999 | 103 | if (fh == -1) { |
<> | 149:156823d33999 | 104 | return (FILEHANDLE)NULL; |
<> | 149:156823d33999 | 105 | } |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | return fh; |
<> | 149:156823d33999 | 108 | } |
<> | 149:156823d33999 | 109 | |
<> | 149:156823d33999 | 110 | LocalFileHandle::LocalFileHandle(FILEHANDLE fh) : _fh(fh), pos(0) { |
<> | 149:156823d33999 | 111 | // No lock needed in constructor |
<> | 149:156823d33999 | 112 | } |
<> | 149:156823d33999 | 113 | |
<> | 149:156823d33999 | 114 | int LocalFileHandle::close() { |
<> | 149:156823d33999 | 115 | int retval = semihost_close(_fh); |
<> | 149:156823d33999 | 116 | delete this; |
<> | 149:156823d33999 | 117 | return retval; |
<> | 149:156823d33999 | 118 | } |
<> | 149:156823d33999 | 119 | |
<> | 149:156823d33999 | 120 | ssize_t LocalFileHandle::write(const void *buffer, size_t length) { |
<> | 149:156823d33999 | 121 | lock(); |
<> | 149:156823d33999 | 122 | ssize_t n = semihost_write(_fh, (const unsigned char*)buffer, length, 0); // number of characters not written |
<> | 149:156823d33999 | 123 | n = length - n; // number of characters written |
<> | 149:156823d33999 | 124 | pos += n; |
<> | 149:156823d33999 | 125 | unlock(); |
<> | 149:156823d33999 | 126 | return n; |
<> | 149:156823d33999 | 127 | } |
<> | 149:156823d33999 | 128 | |
<> | 149:156823d33999 | 129 | ssize_t LocalFileHandle::read(void *buffer, size_t length) { |
<> | 149:156823d33999 | 130 | lock(); |
<> | 149:156823d33999 | 131 | ssize_t n = semihost_read(_fh, (unsigned char*)buffer, length, 0); // number of characters not read |
<> | 149:156823d33999 | 132 | n = length - n; // number of characters read |
<> | 149:156823d33999 | 133 | pos += n; |
<> | 149:156823d33999 | 134 | unlock(); |
<> | 149:156823d33999 | 135 | return n; |
<> | 149:156823d33999 | 136 | } |
<> | 149:156823d33999 | 137 | |
<> | 149:156823d33999 | 138 | int LocalFileHandle::isatty() { |
<> | 149:156823d33999 | 139 | lock(); |
<> | 149:156823d33999 | 140 | int ret = semihost_istty(_fh); |
<> | 149:156823d33999 | 141 | unlock(); |
<> | 149:156823d33999 | 142 | return ret; |
<> | 149:156823d33999 | 143 | } |
<> | 149:156823d33999 | 144 | |
<> | 149:156823d33999 | 145 | off_t LocalFileHandle::lseek(off_t position, int whence) { |
<> | 149:156823d33999 | 146 | lock(); |
<> | 149:156823d33999 | 147 | if (whence == SEEK_CUR) { |
<> | 149:156823d33999 | 148 | position += pos; |
<> | 149:156823d33999 | 149 | } else if (whence == SEEK_END) { |
<> | 149:156823d33999 | 150 | position += semihost_flen(_fh); |
<> | 149:156823d33999 | 151 | } /* otherwise SEEK_SET, so position is fine */ |
<> | 149:156823d33999 | 152 | |
<> | 149:156823d33999 | 153 | /* Always seems to return -1, so just ignore for now. */ |
<> | 149:156823d33999 | 154 | semihost_seek(_fh, position); |
<> | 149:156823d33999 | 155 | pos = position; |
<> | 149:156823d33999 | 156 | unlock(); |
<> | 149:156823d33999 | 157 | return position; |
<> | 149:156823d33999 | 158 | } |
<> | 149:156823d33999 | 159 | |
<> | 149:156823d33999 | 160 | int LocalFileHandle::fsync() { |
<> | 149:156823d33999 | 161 | lock(); |
<> | 149:156823d33999 | 162 | int ret = semihost_ensure(_fh); |
<> | 149:156823d33999 | 163 | unlock(); |
<> | 149:156823d33999 | 164 | return ret; |
<> | 149:156823d33999 | 165 | } |
<> | 149:156823d33999 | 166 | |
<> | 149:156823d33999 | 167 | off_t LocalFileHandle::flen() { |
<> | 149:156823d33999 | 168 | lock(); |
<> | 149:156823d33999 | 169 | off_t off = semihost_flen(_fh); |
<> | 149:156823d33999 | 170 | unlock(); |
<> | 149:156823d33999 | 171 | return off; |
<> | 149:156823d33999 | 172 | } |
<> | 149:156823d33999 | 173 | |
<> | 149:156823d33999 | 174 | void LocalFileHandle::lock() { |
<> | 149:156823d33999 | 175 | _mutex.lock(); |
<> | 149:156823d33999 | 176 | } |
<> | 149:156823d33999 | 177 | |
<> | 149:156823d33999 | 178 | void LocalFileHandle::unlock() { |
<> | 149:156823d33999 | 179 | _mutex.unlock(); |
<> | 149:156823d33999 | 180 | } |
<> | 149:156823d33999 | 181 | |
<> | 149:156823d33999 | 182 | class LocalDirHandle : public DirHandle { |
<> | 149:156823d33999 | 183 | |
<> | 149:156823d33999 | 184 | public: |
<> | 149:156823d33999 | 185 | struct dirent cur_entry; |
<> | 149:156823d33999 | 186 | XFINFO info; |
<> | 149:156823d33999 | 187 | |
<> | 149:156823d33999 | 188 | LocalDirHandle() : cur_entry(), info() { |
<> | 149:156823d33999 | 189 | } |
<> | 149:156823d33999 | 190 | |
<> | 149:156823d33999 | 191 | virtual int closedir() { |
<> | 149:156823d33999 | 192 | // No lock can be used in destructor |
<> | 149:156823d33999 | 193 | delete this; |
<> | 149:156823d33999 | 194 | return 0; |
<> | 149:156823d33999 | 195 | } |
<> | 149:156823d33999 | 196 | |
<> | 149:156823d33999 | 197 | virtual struct dirent *readdir() { |
<> | 149:156823d33999 | 198 | lock(); |
<> | 149:156823d33999 | 199 | if (xffind("*", &info)!=0) { |
<> | 149:156823d33999 | 200 | unlock(); |
<> | 149:156823d33999 | 201 | return NULL; |
<> | 149:156823d33999 | 202 | } |
<> | 149:156823d33999 | 203 | memcpy(cur_entry.d_name, info.name, sizeof(info.name)); |
<> | 149:156823d33999 | 204 | unlock(); |
<> | 149:156823d33999 | 205 | return &cur_entry; |
<> | 149:156823d33999 | 206 | } |
<> | 149:156823d33999 | 207 | |
<> | 149:156823d33999 | 208 | virtual void rewinddir() { |
<> | 149:156823d33999 | 209 | lock(); |
<> | 149:156823d33999 | 210 | info.fileID = 0; |
<> | 149:156823d33999 | 211 | unlock(); |
<> | 149:156823d33999 | 212 | } |
<> | 149:156823d33999 | 213 | |
<> | 149:156823d33999 | 214 | virtual off_t telldir() { |
<> | 149:156823d33999 | 215 | lock(); |
<> | 149:156823d33999 | 216 | int fileId = info.fileID; |
<> | 149:156823d33999 | 217 | unlock(); |
<> | 149:156823d33999 | 218 | return fileId; |
<> | 149:156823d33999 | 219 | } |
<> | 149:156823d33999 | 220 | |
<> | 149:156823d33999 | 221 | virtual void seekdir(off_t offset) { |
<> | 149:156823d33999 | 222 | lock(); |
<> | 149:156823d33999 | 223 | info.fileID = offset; |
<> | 149:156823d33999 | 224 | unlock(); |
<> | 149:156823d33999 | 225 | } |
<> | 149:156823d33999 | 226 | |
<> | 149:156823d33999 | 227 | protected: |
<> | 149:156823d33999 | 228 | PlatformMutex _mutex; |
<> | 149:156823d33999 | 229 | |
<> | 149:156823d33999 | 230 | virtual void lock() { |
<> | 149:156823d33999 | 231 | _mutex.lock(); |
<> | 149:156823d33999 | 232 | } |
<> | 149:156823d33999 | 233 | |
<> | 149:156823d33999 | 234 | virtual void unlock() { |
<> | 149:156823d33999 | 235 | _mutex.unlock(); |
<> | 149:156823d33999 | 236 | } |
<> | 149:156823d33999 | 237 | }; |
<> | 149:156823d33999 | 238 | |
<> | 149:156823d33999 | 239 | FileHandle *LocalFileSystem::open(const char* name, int flags) { |
<> | 149:156823d33999 | 240 | // No global state modified so function is thread safe |
<> | 149:156823d33999 | 241 | |
<> | 149:156823d33999 | 242 | /* reject filenames with / in them */ |
<> | 149:156823d33999 | 243 | for (const char *tmp = name; *tmp; tmp++) { |
<> | 149:156823d33999 | 244 | if (*tmp == '/') { |
<> | 149:156823d33999 | 245 | return NULL; |
<> | 149:156823d33999 | 246 | } |
<> | 149:156823d33999 | 247 | } |
<> | 149:156823d33999 | 248 | |
<> | 149:156823d33999 | 249 | int openmode = posix_to_semihost_open_flags(flags); |
<> | 149:156823d33999 | 250 | if (openmode == OPEN_INVALID) { |
<> | 149:156823d33999 | 251 | return NULL; |
<> | 149:156823d33999 | 252 | } |
<> | 149:156823d33999 | 253 | |
<> | 149:156823d33999 | 254 | FILEHANDLE fh = semihost_open(name, openmode); |
<> | 149:156823d33999 | 255 | if (fh == -1) { |
<> | 149:156823d33999 | 256 | return NULL; |
<> | 149:156823d33999 | 257 | } |
<> | 149:156823d33999 | 258 | return new LocalFileHandle(fh); |
<> | 149:156823d33999 | 259 | } |
<> | 149:156823d33999 | 260 | |
<> | 149:156823d33999 | 261 | int LocalFileSystem::remove(const char *filename) { |
<> | 149:156823d33999 | 262 | // No global state modified so function is thread safe |
<> | 149:156823d33999 | 263 | |
<> | 149:156823d33999 | 264 | return semihost_remove(filename); |
<> | 149:156823d33999 | 265 | } |
<> | 149:156823d33999 | 266 | |
<> | 149:156823d33999 | 267 | DirHandle *LocalFileSystem::opendir(const char *name) { |
<> | 149:156823d33999 | 268 | // No global state modified so function is thread safe |
<> | 149:156823d33999 | 269 | |
<> | 149:156823d33999 | 270 | return new LocalDirHandle(); |
<> | 149:156823d33999 | 271 | } |
<> | 149:156823d33999 | 272 | |
<> | 149:156823d33999 | 273 | } // namespace mbed |
<> | 149:156823d33999 | 274 | |
<> | 149:156823d33999 | 275 | #endif |