mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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