@@ -116,6 +116,69 @@ int delete() {
116116 return ret ;
117117}
118118
119+ static int extract_cb (const char * path , FILINFO * st )
120+ {
121+ char outpath [PATH_MAX ];
122+ unsigned char buffer [0x10000 ];
123+ FIL fp [1 ] = {};
124+ FILE * out = NULL ;
125+ int res ;
126+
127+ const char * ptr_path = strchr (path , ':' );
128+ if (!ptr_path ++ ) ptr_path = path ;
129+
130+ sprintf (outpath , "%s:%s%s" , GetActiveDeviceName (), EXTRACT_PATH , ptr_path );
131+ puts (outpath );
132+
133+ char * ptr = outpath ;
134+ while ((ptr = strchr (ptr , '/' )))
135+ {
136+ * ptr = 0 ;
137+ int ret = mkdir (outpath , 0644 );
138+
139+ if (ret < 0 && errno != EEXIST )
140+ goto error ;
141+
142+ * ptr ++ = '/' ;
143+ }
144+
145+ res = f_open (fp , path , FA_READ );
146+ if (res != FR_OK )
147+ {
148+ printf ("%s: f_open() failed (%i)\n" , path , res );
149+ return res ;
150+ }
151+
152+ out = fopen (outpath , "wb" );
153+ if (!out )
154+ {
155+ f_close (fp );
156+ perror (outpath );
157+ return - errno ;
158+ }
159+
160+ UINT read = 0 ;
161+ while (true)
162+ {
163+ res = f_read (fp , buffer , sizeof buffer , & read );
164+ if (res || !read )
165+ break ;
166+
167+ if (!fwrite (buffer , read , 1 , out )) {
168+ res = - errno ;
169+ break ;
170+ }
171+ }
172+
173+ f_close (fp );
174+ fclose (out );
175+ return res ;
176+
177+ error :
178+ perror (outpath );
179+ return - errno ;
180+ }
181+
119182static int export_cb (const char * path , FILINFO * st )
120183{
121184 int res ;
@@ -290,6 +353,32 @@ static int copytree(const char* src, int (*callback)(const char*, FILINFO*))
290353 return fres ;
291354}
292355
356+ int extract (void )
357+ {
358+ FATFS fs = {};
359+
360+ char filepath [PATH_MAX ];
361+ sprintf (filepath , "%s:%s" , GetActiveDeviceName (), FAT_TARGET );
362+ FILE * fp = fopen (filepath , "rb" );
363+ if (!fp ) {
364+ perror (filepath );
365+ return - errno ;
366+ }
367+
368+ f_mount (& fs , "vff:" , 0 );
369+
370+ int ret = VFFInit (fp , & fs );
371+ if (ret < 0 )
372+ return ret ;
373+
374+ ret = copytree ("vff:" , extract_cb );
375+ if (!ret ) puts ("\nOK!" );
376+
377+ f_unmount ("vff:" );
378+ fclose (fp );
379+ return ret ;
380+ }
381+
293382int export (void )
294383{
295384 FATFS fs = {};
@@ -309,15 +398,15 @@ int export(void)
309398 return ret ;
310399
311400 ret = copytree ("vff:" , export_cb );
312- puts ("\nOK!" );
401+ if (! ret ) puts ("\nOK!" );
313402
314403 f_unmount ("vff:" );
315404 fclose (fp );
316405 return ret ;
317406}
318407
319408
320- static MainMenuItem items [5 ] =
409+ static MainMenuItem items [] =
321410{
322411 {
323412 "Backup" ,
@@ -337,6 +426,12 @@ static MainMenuItem items[5] =
337426 delete
338427 },
339428
429+ {
430+ "Extract (raw)" ,
431+ "\x1b[33;0m" ,
432+ extract
433+ },
434+
340435 {
341436 "Export (text)" ,
342437 "\x1b[33;1m" , // light yellow
@@ -365,7 +460,7 @@ int main() {
365460 if (!FATMount ())
366461 goto waitexit ;
367462
368- MainMenu (items , 5 );
463+ MainMenu (items , 6 );
369464
370465exit :
371466 FATUnmount ();
0 commit comments