001
platform/DirHandle.h@0:13413ea9a877, 2022-06-12 (annotated)
- Committer:
- ganlikun
- Date:
- Sun Jun 12 14:02:44 2022 +0000
- Revision:
- 0:13413ea9a877
00
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganlikun | 0:13413ea9a877 | 1 | /* mbed Microcontroller Library |
ganlikun | 0:13413ea9a877 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ganlikun | 0:13413ea9a877 | 3 | * |
ganlikun | 0:13413ea9a877 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ganlikun | 0:13413ea9a877 | 5 | * you may not use this file except in compliance with the License. |
ganlikun | 0:13413ea9a877 | 6 | * You may obtain a copy of the License at |
ganlikun | 0:13413ea9a877 | 7 | * |
ganlikun | 0:13413ea9a877 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ganlikun | 0:13413ea9a877 | 9 | * |
ganlikun | 0:13413ea9a877 | 10 | * Unless required by applicable law or agreed to in writing, software |
ganlikun | 0:13413ea9a877 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ganlikun | 0:13413ea9a877 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ganlikun | 0:13413ea9a877 | 13 | * See the License for the specific language governing permissions and |
ganlikun | 0:13413ea9a877 | 14 | * limitations under the License. |
ganlikun | 0:13413ea9a877 | 15 | */ |
ganlikun | 0:13413ea9a877 | 16 | #ifndef MBED_DIRHANDLE_H |
ganlikun | 0:13413ea9a877 | 17 | #define MBED_DIRHANDLE_H |
ganlikun | 0:13413ea9a877 | 18 | |
ganlikun | 0:13413ea9a877 | 19 | #include <stdint.h> |
ganlikun | 0:13413ea9a877 | 20 | #include "platform/platform.h" |
ganlikun | 0:13413ea9a877 | 21 | #include "platform/FileHandle.h" |
ganlikun | 0:13413ea9a877 | 22 | #include "platform/NonCopyable.h" |
ganlikun | 0:13413ea9a877 | 23 | |
ganlikun | 0:13413ea9a877 | 24 | namespace mbed { |
ganlikun | 0:13413ea9a877 | 25 | /** \addtogroup platform */ |
ganlikun | 0:13413ea9a877 | 26 | |
ganlikun | 0:13413ea9a877 | 27 | |
ganlikun | 0:13413ea9a877 | 28 | /** Represents a directory stream. Objects of this type are returned |
ganlikun | 0:13413ea9a877 | 29 | * by an opendir function. The core functions are read and seek, |
ganlikun | 0:13413ea9a877 | 30 | * but only a subset needs to be provided. |
ganlikun | 0:13413ea9a877 | 31 | * |
ganlikun | 0:13413ea9a877 | 32 | * If a FileSystemLike class defines the opendir method, then the |
ganlikun | 0:13413ea9a877 | 33 | * directories of an object of that type can be accessed by |
ganlikun | 0:13413ea9a877 | 34 | * DIR *d = opendir("/example/directory") (or opendir("/example") |
ganlikun | 0:13413ea9a877 | 35 | * to open the root of the filesystem), and then using readdir(d) etc. |
ganlikun | 0:13413ea9a877 | 36 | * |
ganlikun | 0:13413ea9a877 | 37 | * The root directory is considered to contain all FileHandle and |
ganlikun | 0:13413ea9a877 | 38 | * FileSystem objects, so the DIR* returned by opendir("/") will |
ganlikun | 0:13413ea9a877 | 39 | * reflect this. |
ganlikun | 0:13413ea9a877 | 40 | * |
ganlikun | 0:13413ea9a877 | 41 | * @note to create a directory, @see Dir |
ganlikun | 0:13413ea9a877 | 42 | * @note Synchronization level: Set by subclass |
ganlikun | 0:13413ea9a877 | 43 | * @ingroup platform |
ganlikun | 0:13413ea9a877 | 44 | */ |
ganlikun | 0:13413ea9a877 | 45 | class DirHandle : private NonCopyable<DirHandle> { |
ganlikun | 0:13413ea9a877 | 46 | public: |
ganlikun | 0:13413ea9a877 | 47 | virtual ~DirHandle() {} |
ganlikun | 0:13413ea9a877 | 48 | |
ganlikun | 0:13413ea9a877 | 49 | /** Read the next directory entry |
ganlikun | 0:13413ea9a877 | 50 | * |
ganlikun | 0:13413ea9a877 | 51 | * @param ent The directory entry to fill out |
ganlikun | 0:13413ea9a877 | 52 | * @return 1 on reading a filename, 0 at end of directory, negative error on failure |
ganlikun | 0:13413ea9a877 | 53 | */ |
ganlikun | 0:13413ea9a877 | 54 | virtual ssize_t read(struct dirent *ent) = 0; |
ganlikun | 0:13413ea9a877 | 55 | |
ganlikun | 0:13413ea9a877 | 56 | /** Close a directory |
ganlikun | 0:13413ea9a877 | 57 | * |
ganlikun | 0:13413ea9a877 | 58 | * @return 0 on success, negative error code on failure |
ganlikun | 0:13413ea9a877 | 59 | */ |
ganlikun | 0:13413ea9a877 | 60 | virtual int close() = 0; |
ganlikun | 0:13413ea9a877 | 61 | |
ganlikun | 0:13413ea9a877 | 62 | /** Set the current position of the directory |
ganlikun | 0:13413ea9a877 | 63 | * |
ganlikun | 0:13413ea9a877 | 64 | * @param offset Offset of the location to seek to, |
ganlikun | 0:13413ea9a877 | 65 | * must be a value returned from tell |
ganlikun | 0:13413ea9a877 | 66 | */ |
ganlikun | 0:13413ea9a877 | 67 | virtual void seek(off_t offset) = 0; |
ganlikun | 0:13413ea9a877 | 68 | |
ganlikun | 0:13413ea9a877 | 69 | /** Get the current position of the directory |
ganlikun | 0:13413ea9a877 | 70 | * |
ganlikun | 0:13413ea9a877 | 71 | * @return Position of the directory that can be passed to rewind |
ganlikun | 0:13413ea9a877 | 72 | */ |
ganlikun | 0:13413ea9a877 | 73 | virtual off_t tell() = 0; |
ganlikun | 0:13413ea9a877 | 74 | |
ganlikun | 0:13413ea9a877 | 75 | /** Rewind the current position to the beginning of the directory |
ganlikun | 0:13413ea9a877 | 76 | */ |
ganlikun | 0:13413ea9a877 | 77 | virtual void rewind() = 0; |
ganlikun | 0:13413ea9a877 | 78 | |
ganlikun | 0:13413ea9a877 | 79 | /** Get the sizeof the directory |
ganlikun | 0:13413ea9a877 | 80 | * |
ganlikun | 0:13413ea9a877 | 81 | * @return Number of files in the directory |
ganlikun | 0:13413ea9a877 | 82 | */ |
ganlikun | 0:13413ea9a877 | 83 | virtual size_t size() |
ganlikun | 0:13413ea9a877 | 84 | { |
ganlikun | 0:13413ea9a877 | 85 | off_t off = tell(); |
ganlikun | 0:13413ea9a877 | 86 | size_t size = 0; |
ganlikun | 0:13413ea9a877 | 87 | struct dirent *ent = new struct dirent; |
ganlikun | 0:13413ea9a877 | 88 | |
ganlikun | 0:13413ea9a877 | 89 | rewind(); |
ganlikun | 0:13413ea9a877 | 90 | while (read(ent) > 0) { |
ganlikun | 0:13413ea9a877 | 91 | size += 1; |
ganlikun | 0:13413ea9a877 | 92 | } |
ganlikun | 0:13413ea9a877 | 93 | seek(off); |
ganlikun | 0:13413ea9a877 | 94 | |
ganlikun | 0:13413ea9a877 | 95 | delete ent; |
ganlikun | 0:13413ea9a877 | 96 | return size; |
ganlikun | 0:13413ea9a877 | 97 | } |
ganlikun | 0:13413ea9a877 | 98 | |
ganlikun | 0:13413ea9a877 | 99 | /** Closes the directory. |
ganlikun | 0:13413ea9a877 | 100 | * |
ganlikun | 0:13413ea9a877 | 101 | * @returns |
ganlikun | 0:13413ea9a877 | 102 | * 0 on success, |
ganlikun | 0:13413ea9a877 | 103 | * -1 on error. |
ganlikun | 0:13413ea9a877 | 104 | */ |
ganlikun | 0:13413ea9a877 | 105 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close") |
ganlikun | 0:13413ea9a877 | 106 | virtual int closedir() { return close(); }; |
ganlikun | 0:13413ea9a877 | 107 | |
ganlikun | 0:13413ea9a877 | 108 | /** Return the directory entry at the current position, and |
ganlikun | 0:13413ea9a877 | 109 | * advances the position to the next entry. |
ganlikun | 0:13413ea9a877 | 110 | * |
ganlikun | 0:13413ea9a877 | 111 | * @returns |
ganlikun | 0:13413ea9a877 | 112 | * A pointer to a dirent structure representing the |
ganlikun | 0:13413ea9a877 | 113 | * directory entry at the current position, or NULL on reaching |
ganlikun | 0:13413ea9a877 | 114 | * end of directory or error. |
ganlikun | 0:13413ea9a877 | 115 | */ |
ganlikun | 0:13413ea9a877 | 116 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read") |
ganlikun | 0:13413ea9a877 | 117 | virtual struct dirent *readdir() |
ganlikun | 0:13413ea9a877 | 118 | { |
ganlikun | 0:13413ea9a877 | 119 | static struct dirent ent; |
ganlikun | 0:13413ea9a877 | 120 | return (read(&ent) > 0) ? &ent : NULL; |
ganlikun | 0:13413ea9a877 | 121 | } |
ganlikun | 0:13413ea9a877 | 122 | |
ganlikun | 0:13413ea9a877 | 123 | /** Resets the position to the beginning of the directory. |
ganlikun | 0:13413ea9a877 | 124 | */ |
ganlikun | 0:13413ea9a877 | 125 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind") |
ganlikun | 0:13413ea9a877 | 126 | virtual void rewinddir() { rewind(); } |
ganlikun | 0:13413ea9a877 | 127 | |
ganlikun | 0:13413ea9a877 | 128 | /** Returns the current position of the DirHandle. |
ganlikun | 0:13413ea9a877 | 129 | * |
ganlikun | 0:13413ea9a877 | 130 | * @returns |
ganlikun | 0:13413ea9a877 | 131 | * the current position, |
ganlikun | 0:13413ea9a877 | 132 | * -1 on error. |
ganlikun | 0:13413ea9a877 | 133 | */ |
ganlikun | 0:13413ea9a877 | 134 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell") |
ganlikun | 0:13413ea9a877 | 135 | virtual off_t telldir() { return tell(); } |
ganlikun | 0:13413ea9a877 | 136 | |
ganlikun | 0:13413ea9a877 | 137 | /** Sets the position of the DirHandle. |
ganlikun | 0:13413ea9a877 | 138 | * |
ganlikun | 0:13413ea9a877 | 139 | * @param location The location to seek to. Must be a value returned by telldir. |
ganlikun | 0:13413ea9a877 | 140 | */ |
ganlikun | 0:13413ea9a877 | 141 | MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek") |
ganlikun | 0:13413ea9a877 | 142 | virtual void seekdir(off_t location) { seek(location); } |
ganlikun | 0:13413ea9a877 | 143 | }; |
ganlikun | 0:13413ea9a877 | 144 | |
ganlikun | 0:13413ea9a877 | 145 | |
ganlikun | 0:13413ea9a877 | 146 | } // namespace mbed |
ganlikun | 0:13413ea9a877 | 147 | |
ganlikun | 0:13413ea9a877 | 148 | #endif /* MBED_DIRHANDLE_H */ |
ganlikun | 0:13413ea9a877 | 149 |