@@ -64,23 +64,17 @@ namespace fs
6464
6565 debug (" System ID: \t %.8s\n " , bpb->system_id );
6666
67- // initialize FAT
68- if (bpb->small_sectors ) // FAT16
69- {
70- this ->fat_type = FAT::T_FAT16;
71- this ->sectors = bpb->small_sectors ;
72- this ->sectors_per_fat = bpb->sectors_per_fat ;
73- this ->root_dir_sectors = ((bpb->root_entries * 32 ) + (sector_size - 1 )) / sector_size;
74- // printf("Root dir sectors: %u\n", this->root_dir_sectors);
75- // this->root_dir_sectors = 0;
76- }
67+ // sector count
68+ if (bpb->small_sectors )
69+ this ->sectors = bpb->small_sectors ;
7770 else
78- {
79- this ->fat_type = FAT::T_FAT32;
80- this ->sectors = bpb->large_sectors ;
71+ this ->sectors = bpb->large_sectors ;
72+ // sectors per FAT (not sure about the rule here)
73+ this ->sectors_per_fat = bpb->sectors_per_fat ;
74+ if (this ->sectors_per_fat == 0 )
8175 this ->sectors_per_fat = *(uint32_t *) &mbr->boot [25 ];
82- this -> root_dir_sectors = 0 ;
83- }
76+ // root dir sectors from root entries
77+ this -> root_dir_sectors = ((bpb-> root_entries * 32 ) + (sector_size - 1 )) / sector_size;
8478 // calculate index of first data sector
8579 this ->data_index = bpb->reserved_sectors + (bpb->fa_tables * this ->sectors_per_fat ) + this ->root_dir_sectors ;
8680 debug (" First data sector: %u\n " , this ->data_index );
@@ -115,7 +109,12 @@ namespace fs
115109 {
116110 this ->fat_type = FAT::T_FAT32;
117111 this ->root_cluster = *(uint32_t *) &mbr->boot [33 ];
112+ this ->root_cluster = 2 ;
118113 debug (" The image is type FAT32, with %u clusters\n " , this ->clusters );
114+ // printf("Root dir entries: %u clusters\n", bpb->root_entries);
115+ // assert(bpb->root_entries == 0);
116+ // this->root_dir_sectors = 0;
117+ // this->data_index = bpb->reserved_sectors + bpb->fa_tables * this->sectors_per_fat;
119118 }
120119 debug (" Root cluster index: %u (sector %u)\n " , this ->root_cluster , cl_to_sector (root_cluster));
121120 debug (" System ID: %.8s\n " , bpb->system_id );
@@ -234,7 +233,7 @@ namespace fs
234233 }
235234
236235 final_name[final_count] = 0 ;
237- // printf ("Long name: %s\n", final_name);
236+ debug (" Long name: %s\n " , final_name);
238237
239238 i++; // skip over the long version
240239 // to the short version for the stats and cluster
@@ -254,7 +253,8 @@ namespace fs
254253 else
255254 {
256255 auto * D = &root[i];
257- // printf("Short name: %.11s\n", D->shortname);
256+ debug (" Short name: %.11s\n " , D->shortname );
257+
258258 std::string dirname ((char *) D->shortname , 11 );
259259 dirname = trim_right_copy (dirname);
260260
@@ -277,13 +277,16 @@ namespace fs
277277 dirvec_t dirents,
278278 on_internal_ls_func callback)
279279 {
280- std::function<void (uint32_t )> next;
280+ // list contents of meme sector by sector
281+ typedef std::function<void (uint32_t )> next_func_t ;
281282
282- next = [this , sector, callback, &dirents, next] (uint32_t sector)
283+ auto next = std::make_shared<next_func_t > ();
284+ *next =
285+ [this , sector, callback, dirents, next] (uint32_t sector)
283286 {
284- // printf ("int_ls: sec=%u\n", sector);
287+ debug (" int_ls: sec=%u\n " , sector);
285288 device.read (sector,
286- [this , sector, callback, & dirents, next] (buffer_t data)
289+ [this , sector, callback, dirents, next] (buffer_t data)
287290 {
288291 if (!data)
289292 {
@@ -302,14 +305,14 @@ namespace fs
302305 else
303306 {
304307 // go to next sector
305- next (sector+1 );
308+ (* next) (sector+1 );
306309 }
307310
308311 }); // read root dir
309312 };
310313
311314 // start reading sectors asynchronously
312- next (sector);
315+ (* next) (sector);
313316 }
314317
315318 void FAT::traverse (std::shared_ptr<Path> path, cluster_func callback)
@@ -318,9 +321,9 @@ namespace fs
318321 typedef std::function<void (uint32_t )> next_func_t ;
319322
320323 // asynch stack traversal
321- next_func_t next;
322- next =
323- [this , path, & next, callback] (uint32_t cluster)
324+ auto next = std::make_shared< next_func_t > () ;
325+ * next =
326+ [this , path, next, callback] (uint32_t cluster)
324327 {
325328 if (path->empty ())
326329 {
@@ -350,7 +353,7 @@ namespace fs
350353
351354 // list directory contents
352355 int_ls (S, dirents,
353- [name, dirents, & next, callback] (error_t error, dirvec_t ents)
356+ [name, dirents, next, callback] (error_t error, dirvec_t ents)
354357 {
355358 if (unlikely (error))
356359 {
@@ -370,7 +373,7 @@ namespace fs
370373 debug (" \t\t cluster: %llu\n " , e.block );
371374 // only follow directories
372375 if (e.type () == DIR)
373- next (e.block );
376+ (* next) (e.block );
374377 else
375378 callback (true , dirents);
376379 return ;
@@ -382,9 +385,8 @@ namespace fs
382385 });
383386
384387 };
385-
386388 // start by reading root directory
387- next (0 );
389+ (* next) (0 );
388390 }
389391
390392 void FAT::ls (const std::string& path, on_ls_func on_ls)
@@ -412,13 +414,13 @@ namespace fs
412414 // number of sectors to read ahead
413415 size_t chunks = ent.size / sector_size + 1 ;
414416 // allocate buffer
415- uint8_t * buffer = new uint8_t [chunks * sector_size];
417+ auto * buffer = new uint8_t [chunks * sector_size];
416418 // at which sector we will stop
417419 size_t total = chunks;
418420 size_t current = 0 ;
419421
420422 typedef std::function<void (uint32_t , size_t , size_t )> next_func_t ;
421- auto * next = new next_func_t ;
423+ auto next = std::make_shared< next_func_t > () ;
422424
423425 *next =
424426 [this , buffer, ent, callback, next] (uint32_t sector, size_t current, size_t total)
@@ -432,8 +434,6 @@ namespace fs
432434 auto buffer_ptr = buffer_t (buffer, std::default_delete<uint8_t []>());
433435 // notify caller
434436 callback (no_error, buffer_ptr, ent.size );
435- // cleanup (after callback)
436- delete next;
437437 return ;
438438 }
439439 device.read (sector,
@@ -444,7 +444,6 @@ namespace fs
444444 // general I/O error occurred
445445 debug (" Failed to read sector %u for read()" , sector);
446446 // cleanup
447- delete next;
448447 delete[] buffer;
449448 callback (true , buffer_t (), 0 );
450449 return ;
@@ -498,28 +497,31 @@ namespace fs
498497 });
499498 } // readFile()
500499
501- void FAT::stat (const std::string& strpath, on_stat_func callback )
500+ void FAT::stat (const std::string& strpath, on_stat_func func )
502501 {
503502 auto path = std::make_shared<Path> (strpath);
504503 if (unlikely (path->empty ()))
505504 {
506- // root doesn't have any stat anyways (except ATTR_VOLUME_ID in FAT)
507- callback (true , Dirent (INVALID_ENTITY, strpath));
505+ // root doesn't have any stat anyways
506+ // Note: could use ATTR_VOLUME_ID in FAT
507+ func (true , Dirent (INVALID_ENTITY, strpath));
508508 return ;
509509 }
510510
511511 debug (" stat: %s\n " , path->back ().c_str ());
512512 // extract file we are looking for
513513 std::string filename = path->back ();
514514 path->pop_back ();
515+ // we need to remember this later
516+ auto callback = std::make_shared<on_stat_func> (func);
515517
516518 traverse (path,
517- [this , filename, & callback] (error_t error, dirvec_t dirents)
519+ [this , filename, callback] (error_t error, dirvec_t dirents)
518520 {
519521 if (unlikely (error))
520522 {
521523 // no path, no file!
522- callback (error, Dirent (INVALID_ENTITY, filename));
524+ (* callback) (error, Dirent (INVALID_ENTITY, filename));
523525 return ;
524526 }
525527
@@ -529,13 +531,13 @@ namespace fs
529531 if (unlikely (e.name () == filename))
530532 {
531533 // read this file
532- callback (no_error, e);
534+ (* callback) (no_error, e);
533535 return ;
534536 }
535537 }
536538
537539 // not found
538- callback (true , Dirent (INVALID_ENTITY, filename));
540+ (* callback) (true , Dirent (INVALID_ENTITY, filename));
539541 });
540542 }
541543}
0 commit comments