Result backupByConfig(u8 *filebuffer, size_t bufsize) { memset(filebuffer, 0, bufsize); FILE *configfile = fopen("config.txt", "r"); if (configfile==NULL) return errno; char source_path[1000], sd_destination[1000]; u32 source_archive; mediatypes_enum mtype; FS_archiveIds atype; int buf2size = 0x10000; u8 *buf2 = malloc(buf2size); if (buf2==NULL) { printf("malloc failed\n"); gfxFlushBuffers(); gfxSwapBuffers(); return -1; } memset(buf2, 0, buf2size); while (fgets((char*) buf2, buf2size, configfile) != NULL) { if (sscanf((const char*) buf2, "DUMP \"%x:%999[^\"]\" \"%999[^\"]\"", (unsigned int*) &source_archive, source_path, sd_destination) == 3) { if (source_archive >= 0x80000000) { mtype = mediatype_NAND; atype = ARCH_SHARED_EXTDATA; } else { mtype = mediatype_SDMC; atype = ARCH_EXTDATA; } u32 extdata_archive_lowpathdata[3] = {mtype, source_archive, 0}; extdata_archive = (FS_archive){atype, (FS_path){PATH_BINARY, 0xC, (u8*)extdata_archive_lowpathdata}}; Result ret = FSUSER_OpenArchive(NULL, &extdata_archive); if(ret!=0) { printf("could not open archive\n"); gfxFlushBuffers(); gfxSwapBuffers(); continue; } printf("Dumping from %s\n", source_path); gfxFlushBuffers(); gfxSwapBuffers(); ret = archive_copyfile(Extdata_Archive, SDArchive, source_path, sd_destination, filebuffer, 0, bufsize, source_path); if (ret) { printf("Copying failed!\n"); } else { printf("Success.\n"); } gfxFlushBuffers(); gfxSwapBuffers(); FSUSER_CloseArchive(NULL, &extdata_archive); } } free(buf2); fclose(configfile); return 0; }
Result restoreFromSd(u8 *buf, size_t bufsize) { memset(buf, 0, bufsize); FILE *configfile = fopen("config.txt", "r"); if (configfile==NULL) return errno; char sd_source[1000], destination_path[1000]; u32 destination_archive; mediatypes_enum mtype; FS_archiveIds atype; u8 *filebuffer2 = malloc(bufsize); memset(filebuffer2, 0, bufsize); while (fgets((char*) buf, bufsize, configfile) != NULL) { if (sscanf((const char*) buf, "RESTORE \"%999[^\"]\" \"%x:%999[^\"]\"", sd_source, (unsigned int*) &destination_archive, destination_path) == 3) { if (destination_archive >= 0x80000000) { mtype = mediatype_NAND; atype = ARCH_SHARED_EXTDATA; } else { mtype = mediatype_SDMC; atype = ARCH_EXTDATA; } u32 extdata_archive_lowpathdata[3] = {mtype, destination_archive, 0}; extdata_archive = (FS_archive) { atype, (FS_path) { PATH_BINARY, 0xC, (u8*)extdata_archive_lowpathdata } }; Result ret = FSUSER_OpenArchive(NULL, &extdata_archive); if(ret!=0) { printf("could not open archive\n"); gfxFlushBuffers(); gfxSwapBuffers(); continue; } printf("Restoring to %s\n", destination_path); gfxFlushBuffers(); gfxSwapBuffers(); ret = archive_copyfile(SDArchive, Extdata_Archive, sd_source, destination_path, filebuffer2, 0, bufsize, destination_path); if (ret) { printf("Copying failed!\n"); } else { printf("Success.\n"); } gfxFlushBuffers(); gfxSwapBuffers(); FSUSER_CloseArchive(NULL, &extdata_archive); } } free(filebuffer2); fclose(configfile); return 0; }
void dumpFolder(char *path, u32 lowpath_id, char *dumpfolder, u8 *filebuffer, size_t bufsize) { Handle extdata_dir; Result ret = FSUSER_OpenDirectory(NULL, &extdata_dir, extdata_archive, FS_makePath(PATH_CHAR, path)); if (ret!=0) { printf("could not open dir\n"); gfxFlushBuffers(); gfxSwapBuffers(); return; } char dirname[0x120]; sprintf(dirname, "%s/%08x%s", dumpfolder, (unsigned int) lowpath_id, path); mkdir(dirname, 0777); FS_dirent dirStruct; char fileName[0x106] = ""; int cont = 0; while(1) { u32 dataRead = 0; FSDIR_Read(extdata_dir, &dataRead, 1, &dirStruct); if(dataRead == 0) break; unicodeToChar(fileName, dirStruct.name); printf("name: %s%s%s\n", path, fileName, dirStruct.isDirectory ? " (DIRECTORY)" : ""); gfxFlushBuffers(); gfxSwapBuffers(); cont++; if (dirStruct.isDirectory) { char newpath[0x120]; sprintf(newpath, "%s%s/", path, fileName); dumpFolder(newpath, lowpath_id, dumpfolder, filebuffer, bufsize); } else { char file_inpath[0x120]; char file_outpath[0x120]; char file_display_path[0x120]; sprintf(file_inpath, "%s%s", path, fileName); sprintf(file_outpath, "%s/%08x%s%s", dumpfolder, (unsigned int) lowpath_id, path, fileName); sprintf(file_display_path, "%08x%s%s", (unsigned int) lowpath_id, path, fileName); archive_copyfile(Extdata_Archive, SDArchive, file_inpath, file_outpath, filebuffer, 0, bufsize, file_display_path); } } printf("total files in 0x%08x%s: %d\n", (unsigned int) lowpath_id, path, (unsigned int) cont); gfxFlushBuffers(); gfxSwapBuffers(); FSDIR_Close(extdata_dir); }