Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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