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