Skip to content

Commit 51311cf

Browse files
committed
Retry on verification failed.
Also, fix memory leak, avoid unneeded reallocation.
1 parent 301b04c commit 51311cf

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

source/dumper.h

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void md5HashFromFile(std::string filename, unsigned char* out)
2020
FILE *inFile = fopen (filename.c_str(), "rb");
2121
mbedtls_md5_context md5Context;
2222
int bytes;
23-
u64 bufSize = 512000;
23+
const u64 bufSize = 512000;
2424
unsigned char data[bufSize];
2525

2626
if (inFile == NULL)
@@ -56,7 +56,7 @@ void copy(const char* from, const char* to, bool exfat = false)
5656
//const u64 fat32Max = 0xFFFFFFFF;
5757
//const u64 splitSize = 0xFFFF0000;
5858
const u64 smashTID = 0x01006A800016E000;
59-
u64 bufSize = 0x0F116C00;
59+
const u64 bufSize = 0x0F116C00;
6060

6161
if(runningTID() != smashTID)
6262
{
@@ -146,19 +146,19 @@ void copy(const char* from, const char* to, bool exfat = false)
146146
int percent = 0;
147147
u32 srcCRC;
148148
u32 destCRC;
149+
u32 failCount = 0;
150+
u64 dataSize = bufSize;
149151
if(size == 0)
150152
printf(CONSOLE_RED "\nThere was a problem opening the data.arc" CONSOLE_RESET);
151153
while(sizeWritten < size)
152154
{
153-
if(sizeWritten + bufSize > size)
155+
if(sizeWritten + dataSize > size)
154156
{
155-
delete[] buf;
156-
bufSize = size-sizeWritten;
157-
buf = new char[bufSize];
157+
dataSize = size-sizeWritten;
158158
}
159-
fread(buf, sizeof(char), bufSize, source);
160-
ret = fwrite(buf, sizeof(char), bufSize, dest);
161-
if(ret != bufSize)
159+
fread(buf, sizeof(char), dataSize, source);
160+
ret = fwrite(buf, sizeof(char), dataSize, dest);
161+
if(ret != dataSize)
162162
{
163163
printf(CONSOLE_RED "\nSomething went wrong!" CONSOLE_RESET);
164164
if(sizeWritten > 0 && exfat)
@@ -170,21 +170,40 @@ void copy(const char* from, const char* to, bool exfat = false)
170170
}
171171
if(verifyDump)
172172
{
173-
srcCRC = crc32Calculate(buf, bufSize);
174-
fseek(dest, -bufSize, SEEK_CUR);
175-
fread(buf, sizeof(char), bufSize, dest);
176-
destCRC = crc32Calculate(buf, bufSize);
173+
srcCRC = crc32Calculate(buf, dataSize);
174+
fseek(dest, -dataSize, SEEK_CUR);
175+
fread(buf, sizeof(char), dataSize, dest);
176+
destCRC = crc32Calculate(buf, dataSize);
177177
if(srcCRC != destCRC)
178178
{
179-
printf(CONSOLE_RED "\nVerification failed. An error has occured in writing the file. Halting dump." CONSOLE_RESET);
180-
fclose(dest);
181-
fclose(source);
182-
romfsUnmount("romfs");
183-
fsFsDeleteFile(fsdevGetDeviceFileSystem("sdmc"), outPath.c_str());
184-
return;
179+
if(failCount < 3)
180+
{
181+
++failCount;
182+
printf(CONSOLE_RED "\nVerification failed %u time(s). Retrying." CONSOLE_RESET, failCount);
183+
consoleUpdate(NULL);
184+
fseek(dest, -dataSize, SEEK_CUR);
185+
fseek(source, -dataSize, SEEK_CUR);
186+
continue;
187+
}
188+
else
189+
{
190+
printf(CONSOLE_RED "\nVerification failed. An error has occured in writing the file. Halting dump." CONSOLE_RESET);
191+
fclose(dest);
192+
fclose(source);
193+
delete[] buf;
194+
romfsUnmount("romfs");
195+
fsFsDeleteFile(fsdevGetDeviceFileSystem("sdmc"), outPath.c_str());
196+
return;
197+
}
198+
}
199+
else if (failCount != 0)
200+
{
201+
// clearing "failed" lines
202+
printf("\x1b[%uA\x1b[0J",failCount);
203+
failCount = 0;
185204
}
186205
}
187-
sizeWritten += bufSize;
206+
sizeWritten += dataSize;
188207
percent = sizeWritten * 100 / size;
189208
print_progress(percent, 100);
190209
//printf("\x1b[20;2Hdest pos: %lld, source pos: %lld", (long long int)dest.tellp(), (long long int)source.tellg()); // Debug log

0 commit comments

Comments
 (0)