/
FatVolume.h
347 lines (345 loc) · 11.3 KB
/
FatVolume.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* Copyright (c) 2011-2022 Bill Greiman
* This file is part of the SdFat library for SD memory cards.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef FatVolume_h
#define FatVolume_h
#include "FatFile.h"
/**
* \file
* \brief FatVolume class
*/
//------------------------------------------------------------------------------
/**
* \class FatVolume
* \brief Integration class for the FatLib library.
*/
class FatVolume : public FatPartition {
public:
/** Get file's user settable attributes.
* \param[in] path path to file.
* \return user settable file attributes for success else -1.
*/
int attrib(const char* path) {
File32 tmpFile;
return tmpFile.open(this, path, O_RDONLY) ? tmpFile.attrib() : -1;
}
/** Set file's user settable attributes.
* \param[in] path path to file.
* \param[in] bits bit-wise or of selected attributes: FS_ATTRIB_READ_ONLY,
* FS_ATTRIB_HIDDEN, FS_ATTRIB_SYSTEM, FS_ATTRIB_ARCHIVE.
*
* \return true for success or false for failure.
*/
bool attrib(const char* path, uint8_t bits) {
File32 tmpFile;
return tmpFile.open(this, path, O_RDONLY) ? tmpFile.attrib(bits) : false;
}
/**
* Initialize an FatVolume object.
* \param[in] dev Device block driver.
* \param[in] setCwv Set current working volume if true.
* \param[in] part partition to initialize.
* \param[in] volStart Start sector of volume if part is zero.
* \return true for success or false for failure.
*/
bool begin(FsBlockDevice* dev, bool setCwv = true, uint8_t part = 1,
uint32_t volStart = 0) {
if (!init(dev, part, volStart)) {
return false;
}
if (!chdir()) {
return false;
}
if (setCwv || !m_cwv) {
m_cwv = this;
}
return true;
}
/** Change global current working volume to this volume. */
void chvol() { m_cwv = this; }
/**
* Set volume working directory to root.
* \return true for success or false for failure.
*/
bool chdir() {
m_vwd.close();
return m_vwd.openRoot(this);
}
/**
* Set volume working directory.
* \param[in] path Path for volume working directory.
* \return true for success or false for failure.
*/
bool chdir(const char* path);
//----------------------------------------------------------------------------
/**
* Test for the existence of a file.
*
* \param[in] path Path of the file to be tested for.
*
* \return true if the file exists else false.
*/
bool exists(const char* path) {
FatFile tmp;
return tmp.open(this, path, O_RDONLY);
}
//----------------------------------------------------------------------------
/** List the directory contents of the volume root directory.
*
* \param[in] pr Print stream for list.
*
* \param[in] flags The inclusive OR of
*
* LS_DATE - %Print file modification date
*
* LS_SIZE - %Print file size.
*
* LS_R - Recursive list of subdirectories.
*
* \return true for success or false for failure.
*/
bool ls(print_t* pr, uint8_t flags = 0) { return m_vwd.ls(pr, flags); }
//----------------------------------------------------------------------------
/** List the contents of a directory.
*
* \param[in] pr Print stream for list.
*
* \param[in] path directory to list.
*
* \param[in] flags The inclusive OR of
*
* LS_DATE - %Print file modification date
*
* LS_SIZE - %Print file size.
*
* LS_R - Recursive list of subdirectories.
*
* \return true for success or false for failure.
*/
bool ls(print_t* pr, const char* path, uint8_t flags) {
FatFile dir;
return dir.open(this, path, O_RDONLY) && dir.ls(pr, flags);
}
//----------------------------------------------------------------------------
/** Make a subdirectory in the volume root directory.
*
* \param[in] path A path with a valid name for the subdirectory.
*
* \param[in] pFlag Create missing parent directories if true.
*
* \return true for success or false for failure.
*/
bool mkdir(const char* path, bool pFlag = true) {
FatFile sub;
return sub.mkdir(vwd(), path, pFlag);
}
//----------------------------------------------------------------------------
/** open a file
*
* \param[in] path location of file to be opened.
* \param[in] oflag open flags.
* \return a File32 object.
*/
File32 open(const char* path, oflag_t oflag = O_RDONLY) {
File32 tmpFile;
tmpFile.open(this, path, oflag);
return tmpFile;
}
//----------------------------------------------------------------------------
/** Remove a file from the volume root directory.
*
* \param[in] path A path with a valid name for the file.
*
* \return true for success or false for failure.
*/
bool remove(const char* path) {
FatFile tmp;
return tmp.open(this, path, O_WRONLY) && tmp.remove();
}
//----------------------------------------------------------------------------
/** Rename a file or subdirectory.
*
* \param[in] oldPath Path name to the file or subdirectory to be renamed.
*
* \param[in] newPath New path name of the file or subdirectory.
*
* The \a newPath object must not exist before the rename call.
*
* The file to be renamed must not be open. The directory entry may be
* moved and file system corruption could occur if the file is accessed by
* a file object that was opened before the rename() call.
*
* \return true for success or false for failure.
*/
bool rename(const char* oldPath, const char* newPath) {
FatFile file;
return file.open(vwd(), oldPath, O_RDONLY) && file.rename(vwd(), newPath);
}
//----------------------------------------------------------------------------
/** Remove a subdirectory from the volume's working directory.
*
* \param[in] path A path with a valid name for the subdirectory.
*
* The subdirectory file will be removed only if it is empty.
*
* \return true for success or false for failure.
*/
bool rmdir(const char* path) {
FatFile sub;
return sub.open(this, path, O_RDONLY) && sub.rmdir();
}
//----------------------------------------------------------------------------
/** Truncate a file to a specified length. The current file position
* will be at the new EOF.
*
* \param[in] path A path with a valid name for the file.
* \param[in] length The desired length for the file.
*
* \return true for success or false for failure.
*/
bool truncate(const char* path, uint32_t length) {
FatFile file;
return file.open(this, path, O_WRONLY) && file.truncate(length);
}
#if ENABLE_ARDUINO_SERIAL
/** List the directory contents of the root directory to Serial.
*
* \param[in] flags The inclusive OR of
*
* LS_DATE - %Print file modification date
*
* LS_SIZE - %Print file size.
*
* LS_R - Recursive list of subdirectories.
*
* \return true for success or false for failure.
*/
bool ls(uint8_t flags = 0) { return ls(&Serial, flags); }
/** List the directory contents of a directory to Serial.
*
* \param[in] path directory to list.
*
* \param[in] flags The inclusive OR of
*
* LS_DATE - %Print file modification date
*
* LS_SIZE - %Print file size.
*
* LS_R - Recursive list of subdirectories.
*
* \return true for success or false for failure.
*/
bool ls(const char* path, uint8_t flags = 0) {
return ls(&Serial, path, flags);
}
#endif // ENABLE_ARDUINO_SERIAL
#if ENABLE_ARDUINO_STRING
//----------------------------------------------------------------------------
/**
* Set volume working directory.
* \param[in] path Path for volume working directory.
* \return true for success or false for failure.
*/
bool chdir(const String& path) { return chdir(path.c_str()); }
/**
* Test for the existence of a file.
*
* \param[in] path Path of the file to be tested for.
*
* \return true if the file exists else false.
*/
bool exists(const String& path) { return exists(path.c_str()); }
/** Make a subdirectory in the volume root directory.
*
* \param[in] path A path with a valid name for the subdirectory.
*
* \param[in] pFlag Create missing parent directories if true.
*
* \return true for success or false for failure.
*/
bool mkdir(const String& path, bool pFlag = true) {
return mkdir(path.c_str(), pFlag);
}
/** open a file
*
* \param[in] path location of file to be opened.
* \param[in] oflag open flags.
* \return a File32 object.
*/
File32 open(const String& path, oflag_t oflag = O_RDONLY) {
return open(path.c_str(), oflag);
}
/** Remove a file from the volume root directory.
*
* \param[in] path A path with a valid name for the file.
*
* \return true for success or false for failure.
*/
bool remove(const String& path) { return remove(path.c_str()); }
/** Rename a file or subdirectory.
*
* \param[in] oldPath Path name to the file or subdirectory to be renamed.
*
* \param[in] newPath New path name of the file or subdirectory.
*
* The \a newPath object must not exist before the rename call.
*
* The file to be renamed must not be open. The directory entry may be
* moved and file system corruption could occur if the file is accessed by
* a file object that was opened before the rename() call.
*
* \return true for success or false for failure.
*/
bool rename(const String& oldPath, const String& newPath) {
return rename(oldPath.c_str(), newPath.c_str());
}
/** Remove a subdirectory from the volume's working directory.
*
* \param[in] path A path with a valid name for the subdirectory.
*
* The subdirectory file will be removed only if it is empty.
*
* \return true for success or false for failure.
*/
bool rmdir(const String& path) { return rmdir(path.c_str()); }
/** Truncate a file to a specified length. The current file position
* will be at the new EOF.
*
* \param[in] path A path with a valid name for the file.
* \param[in] length The desired length for the file.
*
* \return true for success or false for failure.
*/
bool truncate(const String& path, uint32_t length) {
return truncate(path.c_str(), length);
}
#endif // ENABLE_ARDUINO_STRING
private:
friend FatFile;
static FatVolume* cwv() { return m_cwv; }
FatFile* vwd() { return &m_vwd; }
static FatVolume* m_cwv;
FatFile m_vwd;
};
#endif // FatVolume_h