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 * @ingroup platform
41 */
42class LocalFileHandle : public FileHandle, private NonCopyable<LocalFileHandle> {
43
44public:
45 LocalFileHandle(FILEHANDLE fh);
46
47 virtual int close();
48
49 virtual ssize_t write(const void *buffer, size_t length);
50
51 virtual ssize_t read(void *buffer, size_t length);
52
53 virtual int isatty();
54
55 virtual off_t seek(off_t position, int whence);
56
57 virtual int sync();
58
59 virtual off_t size();
60
61protected:
62 virtual void lock();
63 virtual void unlock();
64 FILEHANDLE _fh;
65 int pos;
66 rtos::Mutex _mutex;
67};
68
69/** A filesystem for accessing the local mbed Microcontroller USB disk drive
70 *
71 * This allows programs to read and write files on the same disk drive that is used to program the
72 * mbed Microcontroller. Once created, the standard C file access functions are used to open,
73 * read and write files.
74 *
75 * @note Synchronization level: Thread safe
76 *
77 * Example:
78 * @code
79 * #include "mbed.h"
80 *
81 * LocalFileSystem local("local"); // Create the local filesystem under the name "local"
82 *
83 * int main() {
84 * FILE *fp = fopen("/local/out.txt", "w"); // Open "out.txt" on the local file system for writing
85 * fprintf(fp, "Hello World!");
86 * fclose(fp);
87 * remove("/local/out.txt"); // Removes the file "out.txt" from the local file system
88 *
89 * DIR *d = opendir("/local"); // Opens the root directory of the local file system
90 * struct dirent *p;
91 * while((p = readdir(d)) != NULL) { // Print the names of the files in the local file system
92 * printf("%s\n", p->d_name); // to stdout.
93 * }
94 * closedir(d);
95 * }
96 * @endcode
97 *
98 * @note
99 * If the microcontroller program makes an access to the local drive, it will be marked as "removed"
100 * on the Host computer. This means it is no longer accessible from the Host Computer.
101 *
102 * The drive will only re-appear when the microcontroller program exists. Note that if the program does
103 * not exit, you will need to hold down reset on the mbed Microcontroller to be able to see the drive again!
104 * @ingroup platform
105 */
106class LocalFileSystem : public FileSystemLike, private NonCopyable<LocalFileSystem> {
107 // No modifiable state
108
109public:
110 LocalFileSystem(const char *n) : FileSystemLike(n)
111 {
112
113 }
114
115 virtual int open(FileHandle **file, const char *path, int flags);
116 virtual int open(DirHandle **dir, const char *name);
117 virtual int remove(const char *filename);
118};
119
120/**@}*/
121
122/**@}*/
123
124} // namespace mbed
125
126#endif
127
128#endif
129
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.
Definition: NonCopyable.h:162
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70