mbed-dev-f303
platform/DirHandle.h@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 | #ifndef MBED_DIRHANDLE_H |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 17 | #define MBED_DIRHANDLE_H |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 18 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 19 | #include <stdint.h> |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 20 | #include "platform/platform.h" |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 21 | #include "platform/FileHandle.h" |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 22 | #include "platform/NonCopyable.h" |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 23 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 24 | namespace mbed { |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 25 | /** \addtogroup platform */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 26 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 27 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 28 | /** Represents a directory stream. Objects of this type are returned |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 29 | * by an opendir function. The core functions are read and seek, |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 30 | * but only a subset needs to be provided. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 31 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 32 | * If a FileSystemLike class defines the opendir method, then the |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 33 | * directories of an object of that type can be accessed by |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 34 | * DIR *d = opendir("/example/directory") (or opendir("/example") |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 35 | * to open the root of the filesystem), and then using readdir(d) etc. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 36 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 37 | * The root directory is considered to contain all FileHandle and |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 38 | * FileSystem objects, so the DIR* returned by opendir("/") will |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 39 | * reflect this. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 40 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 41 | * @note to create a directory, @see Dir |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 42 | * @note Synchronization level: Set by subclass |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 43 | * @ingroup platform |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 44 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 45 | class DirHandle : private NonCopyable<DirHandle> { |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 46 | public: |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 47 | virtual ~DirHandle() {} |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 48 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 49 | /** Read the next directory entry |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 50 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 51 | * @param ent The directory entry to fill out |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 52 | * @return 1 on reading a filename, 0 at end of directory, negative error on failure |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 53 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 54 | virtual ssize_t read(struct dirent *ent) = 0; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 55 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 56 | /** Close a directory |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 57 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 58 | * @return 0 on success, negative error code on failure |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 59 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 60 | virtual int close() = 0; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 61 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 62 | /** Set the current position of the directory |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 63 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 64 | * @param offset Offset of the location to seek to, |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 65 | * must be a value returned from tell |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 66 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 67 | virtual void seek(off_t offset) = 0; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 68 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 69 | /** Get the current position of the directory |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 70 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 71 | * @return Position of the directory that can be passed to rewind |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 72 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 73 | virtual off_t tell() = 0; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 74 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 75 | /** Rewind the current position to the beginning of the directory |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 76 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 77 | virtual void rewind() = 0; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 78 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 79 | /** Get the sizeof the directory |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 80 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 81 | * @return Number of files in the directory |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 82 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 83 | virtual size_t size() |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 84 | { |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 85 | off_t off = tell(); |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 86 | size_t size = 0; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 87 | struct dirent *ent = new struct dirent; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 88 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 89 | rewind(); |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 90 | while (read(ent) > 0) { |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 91 | size += 1; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 92 | } |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 93 | seek(off); |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 94 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 95 | delete ent; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 96 | return size; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 97 | } |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 98 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 99 | /** Closes the directory. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 100 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 101 | * @returns |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 102 | * 0 on success, |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 103 | * -1 on error. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 104 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 105 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close") |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 106 | virtual int closedir() { return close(); }; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 107 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 108 | /** Return the directory entry at the current position, and |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 109 | * advances the position to the next entry. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 110 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 111 | * @returns |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 112 | * A pointer to a dirent structure representing the |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 113 | * directory entry at the current position, or NULL on reaching |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 114 | * end of directory or error. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 115 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 116 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read") |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 117 | virtual struct dirent *readdir() |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 118 | { |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 119 | static struct dirent ent; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 120 | return (read(&ent) > 0) ? &ent : NULL; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 121 | } |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 122 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 123 | /** Resets the position to the beginning of the directory. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 124 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 125 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind") |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 126 | virtual void rewinddir() { rewind(); } |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 127 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 128 | /** Returns the current position of the DirHandle. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 129 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 130 | * @returns |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 131 | * the current position, |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 132 | * -1 on error. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 133 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 134 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell") |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 135 | virtual off_t telldir() { return tell(); } |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 136 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 137 | /** Sets the position of the DirHandle. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 138 | * |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 139 | * @param location The location to seek to. Must be a value returned by telldir. |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 140 | */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 141 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek") |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 142 | virtual void seekdir(off_t location) { seek(location); } |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 143 | }; |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 144 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 145 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 146 | } // namespace mbed |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 147 | |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 148 | #endif /* MBED_DIRHANDLE_H */ |
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4 | 0:bdf663c61a82 | 149 |