Mbed OS Reference
Loading...
Searching...
No Matches
LocalFileSystem.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2019 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef MBED_LOCALFILESYSTEM_H
18#define MBED_LOCALFILESYSTEM_H
19
20#include "platform/platform.h"
21
22#if DOXYGEN_ONLY || (COMPONENT_LOCALFILESYSTEM && MBED_CONF_TARGET_SEMIHOSTING_ENABLED)
23
24#include "platform/FileSystemLike.h"
25#include "rtos/Mutex.h"
26#include "platform/NonCopyable.h"
27
28namespace mbed {
29/** \addtogroup platform-public-api */
30/** @{*/
31/**
32 * \defgroup platform_LocalFileSystem LocalFileSystem functions
33 * @{
34 */
35
36FILEHANDLE local_file_open(const char *name, int flags);
37
38/**
39 * @class LocalFileHandle
40 */
41class LocalFileHandle : public FileHandle, private NonCopyable<LocalFileHandle> {
42
43public:
44 LocalFileHandle(FILEHANDLE fh);
45
46 virtual int close();
47
48 virtual ssize_t write(const void *buffer, size_t length);
49
50 virtual ssize_t read(void *buffer, size_t length);
51
52 virtual int isatty();
53
54 virtual off_t seek(off_t position, int whence);
55
56 virtual int sync();
57
58 virtual off_t size();
59
60protected:
61 virtual void lock();
62 virtual void unlock();
63 FILEHANDLE _fh;
64 int pos;
65 rtos::Mutex _mutex;
66};
67
68/** A filesystem for accessing the local mbed Microcontroller USB disk drive
69 *
70 * This allows programs to read and write files on the same disk drive that is used to program the
71 * mbed Microcontroller. Once created, the standard C file access functions are used to open,
72 * read and write files.
73 *
74 * @note Synchronization level: Thread safe
75 *
76 * Example:
77 * @code
78 * #include "mbed.h"
79 *
80 * LocalFileSystem local("local"); // Create the local filesystem under the name "local"
81 *
82 * int main() {
83 * FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
84 * fprintf(fp, "Hello World!");
85 * fclose(fp);
86 * remove("/local/out.txt"); // Removes the file "out.txt" from the local file system
87 *
88 * DIR *d = opendir("/local"); // Opens the root directory of the local file system
89 * struct dirent *p;
90 * while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
91 * printf("%s\n", p->d_name); // to stdout.
92 * }
93 * closedir(d);
94 * }
95 * @endcode
96 *
97 * @note
98 * If the microcontroller program makes an access to the local drive, it will be marked as "removed"
99 * on the Host computer. This means it is no longer accessible from the Host Computer.
100 *
101 * The drive will only re-appear when the microcontroller program exists. Note that if the program does
102 * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again!
103 */
104class LocalFileSystem : public FileSystemLike, private NonCopyable<LocalFileSystem> {
105 // No modifiable state
106
107public:
108 LocalFileSystem(const char *n) : FileSystemLike(n)
109 {
110
111 }
112
113 virtual int open(FileHandle **file, const char *path, int flags);
114 virtual int open(DirHandle **dir, const char *name);
115 virtual int remove(const char *filename);
116};
117
118/**@}*/
119
120/**@}*/
121
122} // namespace mbed
123
124#endif
125
126#endif
127
Represents a directory stream.
Definition DirHandle.h:55
Class FileHandle.
Definition FileHandle.h:46
A filesystem-like object is one that can be used to open file-like objects though it by fopen("/name/...
virtual ssize_t write(const void *buffer, size_t length)
Write the contents of a buffer to a file.
virtual int close()
Close a file.
virtual off_t seek(off_t position, int whence)
Move the file position to a given offset from from a given location.
virtual ssize_t read(void *buffer, size_t length)
Read the contents of a file into a buffer.
virtual int isatty()
Check if the file in an interactive terminal device.
virtual int sync()
Flush any buffers associated with the file.
virtual off_t size()
Get the size of the file.
A filesystem for accessing the local mbed Microcontroller USB disk drive.
virtual int open(FileHandle **file, const char *path, int flags)
Open a file on the filesystem.
virtual int open(DirHandle **dir, const char *name)
Open a directory on the filesystem.
virtual int remove(const char *filename)
Remove a file from the filesystem.
Prevents generation of copy constructor and copy assignment operator in derived classes.
The Mutex class is used to synchronize the execution of threads.
Definition Mutex.h:70