-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpack_fat.cpp
546 lines (471 loc) · 16.5 KB
/
pack_fat.cpp
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//
// pack.cpp
//
// Created by zhao hui jiang on 07/03/2020.
// Copyright (c). All rights reserved.
//
#include "pack_fat.h"
static const char *BASE_PATH = "/fatfs";
static RAM_handle_t s_ram_handle;
static FATFS* s_fs = NULL;
struct dirent_1 {
int d_ino; /*!< file number */
uint8_t d_type; /*!< not defined in POSIX, but present in BSD and Linux */
uint8_t no_use[3];
// #define DT_UNKNOWN 0
// #define DT_REG 1
// #define DT_DIR 2
char d_name[256]; /*!< zero-terminated file name */
}dirent_t;
Pack_fat::Pack_fat()
{}
Pack_fat::~Pack_fat()
{}
/**
* @brief Mount RAM fat filesystem(esp32's filesystem) to "/fatfs". so we can use vfs read/write it.
* mount to "/spiflash",size:s_imageSize, return: s_ram_handle s_fs
* @param
* @return True or false.
*/
bool Pack_fat::fsMount(int s_imageSize) {
bool result;
esp_vfs_fat_mount_config_t mountConfig;
mountConfig.max_files = 4; //Max files.
mountConfig.format_if_mount_failed = true;
result = (ESP_OK == emulate_esp_vfs_fat_spiflash_mount(BASE_PATH, &mountConfig, &s_ram_handle, &s_fs, s_imageSize));
return result;
}
/**
* @brief Unount RAM fat filesystem(esp32's filesystem).
* @param
* @return True or false.
*/
bool Pack_fat::fsUnmount() {
bool result;
result = (ESP_OK == emulate_esp_vfs_fat_spiflash_unmount(BASE_PATH, s_ram_handle));
if (result) {
if (g_debugLevel > 0) {
std::cout << "Unmounted successfully" << std::endl;
}
} else {
std::cerr << "Unmount failed" << std::endl;
}
return result;
}
size_t Pack_fat::getFileSize(FILE* fp)
{
fseek(fp, 0L, SEEK_END);
size_t size = (size_t) ftell(fp);
fseek(fp, 0L, SEEK_SET);
return size;
}
/**
* @brief Check if directory exists.
* @param path Directory path.
* @return True if exists otherwise false.
*
*/
bool Pack_fat::dirExists(const char* path) {
DIR *d = opendir(path);
if (d) {
closedir(d);
return true;
}
return false;
}
/**
* @brief Create directory if it not exists.
* @param path Directory path.
* @return True or false.
*/
bool Pack_fat::dirCreate(const char* path) {
// Check if directory also exists.
if (dirExists(path)) {
return false;
}
// platform stuff...
#if defined(_WIN32)
if (mkdir(path) != 0) {
#else
if (mkdir(path, S_IRWXU | S_IXGRP | S_IRGRP | S_IROTH | S_IXOTH) != 0) {
#endif
std::cerr << "Can not create directory!!!" << std::endl;
return false;
}
return true;
}
/**
* @brief Create directory in RAM fatfs.
* @param path Directory name, full path.
* @return True or false.
*/
int Pack_fat::parkDirToRamFS(const char* name) {
std::string fileName = name;
fileName += "/.";
if (g_debugLevel > 0) {
std::cout << "creating dir: " << fileName << std::endl;
}
std::string nameInFat = BASE_PATH;
nameInFat += name;
int res = emulate_vfs_mkdir(nameInFat.c_str(), O_CREAT);
if (res < 0) {
std::cerr << "failed to create dir" << std::endl;
}
return 0;
}
/**
* @brief Create file in RAM fatfs.
* @param path_src source Directory path, full path.
* @param path_des des Directory path, full path.
* @return True or false.
*/
int Pack_fat::parkFileToRamFS(char* path_src, const char* path_des) {
FILE* f_src = fopen(path_src, "rb"); //open file in pc.
if (!f_src) {
std::cerr << "error: failed to open " << path_src << " for reading" << std::endl;
return 1;
}
std::string nameInFat = path_des;
const int flags = O_CREAT | O_TRUNC | O_RDWR;
int fd = emulate_esp_vfs_open(nameInFat.c_str(), flags, 0); //if not exist, create it.
if (fd < 0) {
std::cerr << "error: failed to open \"" << nameInFat << "\" for writing" << std::endl;
return 0; //0 does not stop copying files
}
// read src file size
fseek(f_src, 0, SEEK_END);
size_t size = ftell(f_src);
fseek(f_src, 0, SEEK_SET);
if (g_debugLevel > 0) {
std::cout << "file size: " << size << std::endl;
}
size_t left = size;
uint8_t data_byte;
while (left > 0){
if (1 != fread(&data_byte, 1, 1, f_src)) {
std::cerr << "fread error!" << std::endl;
fclose(f_src);
emulate_esp_vfs_close(fd);
return 1;
}
ssize_t res = emulate_esp_vfs_write(fd, &data_byte, 1);
if (res < 0) {
std::cerr << "esp_vfs_write() error" << std::endl;
if (g_debugLevel > 0) {
std::cout << "data left: " << left << std::endl;
}
fclose(f_src);
emulate_esp_vfs_close(fd);
return 1;
}
left -= 1;
}
emulate_esp_vfs_close(fd);
fclose(f_src);
return 0;
}
/**
* @brief copy directorys/files in pc to vfs fat filesystem(esp32)
* @param dirSrc Directory path in pc.
* @param dirDes root Directory path of vfs fat(/fatfs).
* @return True if exists otherwise false.
*
*/
bool Pack_fat::parkFilesToRamFS(const char* dirSrc, const char* dirDes)
{
DIR *dir;
bool error = false;
intptr_t handle;
std::string dir_root;
std::string dir_pc = dirSrc;
std::string dir_RAM_fs = dirDes;
std::string dir_full_path_s;
std::string dir_full_path_d;
// Open directory
if ((dir = opendir (dir_pc.c_str())) != NULL)
{
closedir (dir);
}else{
std::cerr << "warning: can't read source directory: \"" << dir_pc.c_str() << "\"" << std::endl;
return error;
}
#if defined(_WIN32)
_finddata_t findData;
dir_root = dir_pc + "\\*.*";
handle = _findfirst(dir_root.c_str(), &findData);
if (handle == -1)
return error;
do
{
dir_full_path_s = dir_pc + "\\" + findData.name;
dir_full_path_d = dir_RAM_fs + "/" + findData.name;
if (findData.attrib & _A_SUBDIR) //目录
{
if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
continue;
if (g_debugLevel > 0) {
std::cout << "\n" << "dir_RAM_fs: "<< dir_RAM_fs.c_str() << " dir_pc: "<< dir_pc.c_str() << std::endl;
std::cout << "Sub dir: "<< findData.name << " full dir_full_path_s: "<< dir_full_path_s.c_str() << " full dir_full_path_d: "<< dir_full_path_d.c_str() << "\n" << std::endl;
}
emulate_vfs_mkdir(dir_full_path_d.c_str(), 1);
parkFilesToRamFS(dir_full_path_s.c_str(), dir_full_path_d.c_str());
}
else
{
// Add File to image.
if (parkFileToRamFS((char*)dir_full_path_s.c_str(), dir_full_path_d.c_str()) != 0) {
std::cerr << "error adding file!" << "\n" << std::endl;
error = true;
if (g_debugLevel > 0) {
std::cout << std::endl;
}
break;
}
if (g_debugLevel > 0) {
std::cout << "Pack file, dir_full_path_s: " << dir_full_path_s.c_str() << "dir_full_path_d: "<< dir_full_path_d.c_str() << " file name: "<< findData.name << std::endl;
}
}
} while (_findnext(handle, &findData) == 0);
_findclose(handle);
#else
struct dirent *ent;
dir_root = dir_pc + "/";
// Open directory
if ((dir = opendir (dir_root.c_str())) != NULL) {
// Read files from directory.
while ((ent = readdir (dir)) != NULL)
{
dir_full_path_s = dir_pc + "/" + ent->d_name;
dir_full_path_d = dir_RAM_fs + "/" + ent->d_name;
if (ent->d_name[0] == '.') // Ignore dir itself.
continue;
if (g_debugLevel > 0) {
std::cout << "\n" << "dir_RAM_fs: "<< dir_RAM_fs.c_str() << " dir_pc: "<< dir_pc.c_str() << std::endl;
std::cout << "Sub dir: "<< ent->d_name << " full dir_full_path_s: "<< dir_full_path_s.c_str() << " full dir_full_path_d: "<< dir_full_path_d.c_str() << "\n" << std::endl;
}
struct stat path_stat;
stat (dir_full_path_s.c_str(), &path_stat);
if (!S_ISREG(path_stat.st_mode)) {
if (S_ISDIR(path_stat.st_mode)) { // Check if path is a directory.
emulate_vfs_mkdir(dir_full_path_d.c_str(), 1); //create directory in vfs fat filesystem.
parkFilesToRamFS(dir_full_path_s.c_str(), dir_full_path_d.c_str());
}
else
{
std::cerr << "skipping " << ent->d_name << std::endl;
continue;
}
}
// Add File to image.
if (parkFileToRamFS((char*)dir_full_path_s.c_str(), dir_full_path_d.c_str()) != 0) {
std::cerr << "error adding file!" << std::endl;
error = true;
if (g_debugLevel > 0) {
std::cout << std::endl;
}
break;
}
} // end while
closedir (dir);
}
#endif
return error;
}
/**
* @brief ccpy all directorys/files from RAM fat filesystem to pc.
* @param path_src: source file, in RAM file system
* @param path_des: des file, to pc
* @return True or false.
*/
int Pack_fat::unparkFileFromRamFS(const char* path_src, const char* path_des)
{
std::vector<uint8_t> temp_buf;
const int flags = O_RDONLY;
int f_src = emulate_esp_vfs_open(path_src, flags, 0 ); //源文件
if (f_src < 0) {
std::cerr << "error: failed to open" << path_src << "\" for writing" << std::endl;
return 0; //0 does not stop copying files
}
FILE* f_des = fopen(path_des, "wb");
if (!f_des) {
std::cerr << "error: failed to open " << path_des << " for reading" << std::endl;
return 1;
}
size_t size = emulate_esp_vfs_lseek(f_src, 0, SEEK_END);
emulate_esp_vfs_lseek(f_src, 0, SEEK_SET);
if (g_debugLevel > 0) {
std::cout << "file size: " << size << std::endl;
}
temp_buf.resize(size);
emulate_esp_vfs_read(f_src, (void *)&temp_buf[0], size);
fwrite ( (const void *)&temp_buf[0], 1, size, f_des );
emulate_esp_vfs_close(f_src);
fclose(f_des);
return 0;
}
/**
* @brief ccpy all directorys/files from RAM fat filesystem to pc.
* @param dirSrc directory/file in RAM fatfs.
* @param dirDes directory/file in pc.
* @return True or false.
*/
bool Pack_fat::unparkFilesFromRamFS(const char* dirSrc, const char* dirDes)
{
DIR *dir;
struct dirent_1 *ent;
bool error = false;
std::string dir_pc = dirDes;
std::string dir_RAM_fs = dirSrc;
std::string dir_full_path_s;
std::string dir_full_path_d;
std::cout << "dir_RAM_fs: "<< dir_RAM_fs.c_str() << " dir_pc: "<< dir_pc.c_str() << std::endl;
// Open directory
if ((dir = emulate_vfs_opendir(dir_RAM_fs.c_str())) != NULL) {
// Read files from directory.
while ((ent = (struct dirent_1 *)emulate_vfs_readdir (dir)) != NULL) { //处理目录中的每一项
// Ignore dir itself.
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{
continue;
}
if (g_debugLevel > 0) {
std::cout << "ent->d_name: "<< ent->d_name << std::endl;
}
dir_full_path_s = dir_RAM_fs + "/" + ent->d_name;
#if defined(_WIN32)
dir_full_path_d = dir_pc + "\\" + ent->d_name;
#else
dir_full_path_d = dir_pc + "/" + ent->d_name;
#endif
if (g_debugLevel > 0) {
std::cout << "RAM dir: "<< dir_full_path_s.c_str() << std::endl;
}
struct stat path_stat;
emulate_esp_vfs_stat (dir_full_path_s.c_str(), &path_stat); //get file info.
if (g_debugLevel > 0) {
std::cout << "file mode: "<< path_stat.st_mode << std::endl;
}
#if defined(_WIN32)
if (S_ISDIR(path_stat.st_mode))
#else
if (path_stat.st_mode & 0x4000)
#endif
{
if (g_debugLevel > 0) {
std::cout << "Sub dir: "<< ent->d_name << " full dir_full_path_s: "<< dir_full_path_s.c_str() << " full dir_full_path_d: "<< dir_full_path_d.c_str() << "\n" << std::endl;
}
dirCreate(dir_full_path_d.c_str());
if (unparkFilesFromRamFS(dir_full_path_s.c_str(), dir_full_path_d.c_str()) != 0)
{
std::cerr << "Error for unpark content from " << ent->d_name << "!" << std::endl;
}
}
#if defined(_WIN32)
else if (S_ISREG(path_stat.st_mode)) //normal file
#else
else if (path_stat.st_mode & 0x8000)
#endif
{
// Add File to image.
if (unparkFileFromRamFS(dir_full_path_s.c_str(), dir_full_path_d.c_str()) != 0) {
std::cerr << "error unpark file!" << std::endl;
error = true;
if (g_debugLevel > 0) {
std::cout << std::endl;
}
break;
}
}
} // end while
emulate_vfs_closedir (dir);
}
else {
std::cerr << "warning: can't read source directory: \"" << dir_RAM_fs.c_str() << "\"" << std::endl;
return 1;
}
return (error) ? 1 : 0;
}
/**
* @brief ccpy all directory/file to esp32's fat filesystem.
* @param
* @return True or false.
*/
int Pack_fat::actionPack(std::string s_dirName, std::string s_imageName, int s_imageSize) {
int ret = 0; //0 - ok
// 1. resize g_flashmem and fill 0xff, it will used for RAM fat filesystem.
g_flashmem.resize(s_imageSize, 0xff);
// 2. mount g_flashmem(in RAM) as a fat filesystem, mount point is BASE_PATH(root directory of the RAM filesystem).
if (fsMount(s_imageSize)) {
if (g_debugLevel > 0) {
std::cout << "Mounted successfully" << std::endl;
}
} else {
std::cerr << "Mount failed" << std::endl;
return 1;
}
// 3. copy all directorys/files to the RAM fat filesystem.
ret = parkFilesToRamFS(s_dirName.c_str(), BASE_PATH);
// 4. unmount the RAM fat filesystem.
fsUnmount();
// 5. open *.bin file witch read from esp32.
FILE* fdres = fopen(s_imageName.c_str(), "wb");
if (!fdres) {
std::cerr << "error: failed to open image file" << std::endl;
return 1;
}
// 6. copy all data in g_flashmem to *.bin file.
if (g_debugLevel > 0) {
std::cout << "g_flashmem[0]: " << g_flashmem[0] << "size: " << g_flashmem.size() << std::endl;
}
fwrite(&g_flashmem[0], 4, g_flashmem.size()/4, fdres); //write all data in RAM fatfs to *.bin.
fclose(fdres);
if (g_debugLevel > 0) {
std::cout << "Image file is written to \"" << s_imageName << "\"" << std::endl;
}
return ret;
}
/**
* @brief Unpack action.
* unpack all the directorys/files in *bin(read from esp32) to a pc directory
* @param s_imageName *.bin file name.
* @param s_imageSize *.bin file's size.
* @param s_dirName directory in pc, we will read all files to it.
* @return 0 success, 1 error
*/
int Pack_fat::actionUnpack(std::string s_imageName, std::string s_dirName,int s_imageSize)
{
int ret = 0;
// 1. open fatfs image file(bin)
FILE* f_src = fopen(s_imageName.c_str(), "rb");
if (!f_src) {
std::cerr << "error: failed to open image file" << std::endl;
return 1;
}
if (s_imageSize == 0) {
s_imageSize = getFileSize(f_src);
}
g_flashmem.resize(s_imageSize, 0xff); //set RAM fatfs size
// 2. read content in *.bin into g_flashmem, --> to RAM file system, like memcopy.
size_t sz = fread(&g_flashmem[0], 4, g_flashmem.size() / 4, f_src); //read *.bin to RAM fatfs.
// close file handle
fclose(f_src);
// 3. mount RAM(g_flashmem) file system, so we can use vfs read file in RAM file system.
if (fsMount(s_imageSize)) {
if (g_debugLevel > 0) {
std::cout << "Mounted successfully" << std::endl;
}
} else {
std::cerr << "Mount failed" << std::endl;
return 1;
}
// std::cout << "g_flashmem: "<< g_flashmem[3] << std::endl; //test
// 4. fine dir_pc dir, if no create it.
dirCreate(s_dirName.c_str());
// 5. unpack files,s_dirName:RAM fatfs --> pc
if (!unparkFilesFromRamFS(BASE_PATH, s_dirName.c_str())) {
ret = 1;
}
// unmount file system
fsUnmount();
return ret;
}