Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /* mbed Microcontroller Library
Simon Cooksey 0:fb7af294d5d9 2 * Copyright (c) 2006-2013 ARM Limited
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Licensed under the Apache License, Version 2.0 (the "License");
Simon Cooksey 0:fb7af294d5d9 5 * you may not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 6 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 7 *
Simon Cooksey 0:fb7af294d5d9 8 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 9 *
Simon Cooksey 0:fb7af294d5d9 10 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 11 * distributed under the License is distributed on an "AS IS" BASIS,
Simon Cooksey 0:fb7af294d5d9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 13 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 14 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 15 */
Simon Cooksey 0:fb7af294d5d9 16 #include "drivers/FileSystemLike.h"
Simon Cooksey 0:fb7af294d5d9 17
Simon Cooksey 0:fb7af294d5d9 18 namespace mbed {
Simon Cooksey 0:fb7af294d5d9 19
Simon Cooksey 0:fb7af294d5d9 20 class BaseDirHandle : public DirHandle {
Simon Cooksey 0:fb7af294d5d9 21 public:
Simon Cooksey 0:fb7af294d5d9 22 /*
Simon Cooksey 0:fb7af294d5d9 23 We keep track of our current location as the n'th object in the
Simon Cooksey 0:fb7af294d5d9 24 FileSystemLike list. Using a Base* instead would cause problems if that
Simon Cooksey 0:fb7af294d5d9 25 object were to be destroyed between readdirs.
Simon Cooksey 0:fb7af294d5d9 26 Using this method does mean though that destroying/creating objects can
Simon Cooksey 0:fb7af294d5d9 27 give unusual results from readdir.
Simon Cooksey 0:fb7af294d5d9 28 */
Simon Cooksey 0:fb7af294d5d9 29 off_t n;
Simon Cooksey 0:fb7af294d5d9 30 struct dirent cur_entry;
Simon Cooksey 0:fb7af294d5d9 31
Simon Cooksey 0:fb7af294d5d9 32 BaseDirHandle() : n(0), cur_entry() {
Simon Cooksey 0:fb7af294d5d9 33 }
Simon Cooksey 0:fb7af294d5d9 34
Simon Cooksey 0:fb7af294d5d9 35 virtual int closedir() {
Simon Cooksey 0:fb7af294d5d9 36 // No lock can be used in destructor
Simon Cooksey 0:fb7af294d5d9 37 delete this;
Simon Cooksey 0:fb7af294d5d9 38 return 0;
Simon Cooksey 0:fb7af294d5d9 39 }
Simon Cooksey 0:fb7af294d5d9 40
Simon Cooksey 0:fb7af294d5d9 41 virtual struct dirent *readdir() {
Simon Cooksey 0:fb7af294d5d9 42 lock();
Simon Cooksey 0:fb7af294d5d9 43 FileBase *ptr = FileBase::get(n);
Simon Cooksey 0:fb7af294d5d9 44 if (ptr == NULL) {
Simon Cooksey 0:fb7af294d5d9 45 unlock();
Simon Cooksey 0:fb7af294d5d9 46 return NULL;
Simon Cooksey 0:fb7af294d5d9 47 }
Simon Cooksey 0:fb7af294d5d9 48
Simon Cooksey 0:fb7af294d5d9 49 /* Increment n, so next readdir gets the next item */
Simon Cooksey 0:fb7af294d5d9 50 n++;
Simon Cooksey 0:fb7af294d5d9 51
Simon Cooksey 0:fb7af294d5d9 52 /* Setup cur entry and return a pointer to it */
Simon Cooksey 0:fb7af294d5d9 53 std::strncpy(cur_entry.d_name, ptr->getName(), NAME_MAX);
Simon Cooksey 0:fb7af294d5d9 54 unlock();
Simon Cooksey 0:fb7af294d5d9 55 return &cur_entry;
Simon Cooksey 0:fb7af294d5d9 56 }
Simon Cooksey 0:fb7af294d5d9 57
Simon Cooksey 0:fb7af294d5d9 58 virtual off_t telldir() {
Simon Cooksey 0:fb7af294d5d9 59 lock();
Simon Cooksey 0:fb7af294d5d9 60 off_t offset = n;
Simon Cooksey 0:fb7af294d5d9 61 unlock();
Simon Cooksey 0:fb7af294d5d9 62 return offset;
Simon Cooksey 0:fb7af294d5d9 63 }
Simon Cooksey 0:fb7af294d5d9 64
Simon Cooksey 0:fb7af294d5d9 65 virtual void seekdir(off_t offset) {
Simon Cooksey 0:fb7af294d5d9 66 lock();
Simon Cooksey 0:fb7af294d5d9 67 n = offset;
Simon Cooksey 0:fb7af294d5d9 68 unlock();
Simon Cooksey 0:fb7af294d5d9 69 }
Simon Cooksey 0:fb7af294d5d9 70
Simon Cooksey 0:fb7af294d5d9 71 virtual void rewinddir() {
Simon Cooksey 0:fb7af294d5d9 72 lock();
Simon Cooksey 0:fb7af294d5d9 73 n = 0;
Simon Cooksey 0:fb7af294d5d9 74 unlock();
Simon Cooksey 0:fb7af294d5d9 75 }
Simon Cooksey 0:fb7af294d5d9 76
Simon Cooksey 0:fb7af294d5d9 77 protected:
Simon Cooksey 0:fb7af294d5d9 78 PlatformMutex _mutex;
Simon Cooksey 0:fb7af294d5d9 79
Simon Cooksey 0:fb7af294d5d9 80 virtual void lock() {
Simon Cooksey 0:fb7af294d5d9 81 _mutex.lock();
Simon Cooksey 0:fb7af294d5d9 82 }
Simon Cooksey 0:fb7af294d5d9 83
Simon Cooksey 0:fb7af294d5d9 84 virtual void unlock() {
Simon Cooksey 0:fb7af294d5d9 85 _mutex.unlock();
Simon Cooksey 0:fb7af294d5d9 86 }
Simon Cooksey 0:fb7af294d5d9 87 };
Simon Cooksey 0:fb7af294d5d9 88
Simon Cooksey 0:fb7af294d5d9 89 FileSystemLike::FileSystemLike(const char *name) : FileBase(name, FileSystemPathType) {
Simon Cooksey 0:fb7af294d5d9 90
Simon Cooksey 0:fb7af294d5d9 91 }
Simon Cooksey 0:fb7af294d5d9 92
Simon Cooksey 0:fb7af294d5d9 93 FileSystemLike::~FileSystemLike() {
Simon Cooksey 0:fb7af294d5d9 94
Simon Cooksey 0:fb7af294d5d9 95 }
Simon Cooksey 0:fb7af294d5d9 96
Simon Cooksey 0:fb7af294d5d9 97 DirHandle *FileSystemLike::opendir() {
Simon Cooksey 0:fb7af294d5d9 98 return new BaseDirHandle();
Simon Cooksey 0:fb7af294d5d9 99 }
Simon Cooksey 0:fb7af294d5d9 100
Simon Cooksey 0:fb7af294d5d9 101 } // namespace mbed