Skip to content

Commit fbd5ec9

Browse files
committed
Small NXDT-code refactoring
1 parent 050982f commit fbd5ec9

File tree

3 files changed

+114
-76
lines changed

3 files changed

+114
-76
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.Utilities.nxdumptool;
20+
21+
class NxdtHostIOException extends Exception {
22+
NxdtHostIOException(){
23+
super();
24+
}
25+
NxdtHostIOException(String message){
26+
super(message);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2019-2020 Dmitry Isaenko
3+
4+
This file is part of NS-USBloader.
5+
6+
NS-USBloader is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
NS-USBloader is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with NS-USBloader. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package nsusbloader.Utilities.nxdumptool;
20+
21+
class NxdtMalformedException extends Exception {
22+
NxdtMalformedException(){
23+
super();
24+
}
25+
NxdtMalformedException(String message){
26+
super(message);
27+
}
28+
}

src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java

Lines changed: 58 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -201,100 +201,85 @@ private void performHandshake(byte[] message) throws Exception{
201201
}
202202

203203
private void handleSendFileProperties(byte[] message) throws Exception{
204-
final long fullSize = getLElong(message, 0x10);
205-
final int fileNameLen = getLEint(message, 0x18);
206-
final int headerSize = getLEint(message, 0x1C);
207-
208-
if (checkFileNameLen(fileNameLen)) // In case of negative value we should better handle it before String constructor throws error
209-
return;
210-
211-
String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8);
212-
String absoluteFilePath = getAbsoluteFilePath(filename);
213-
File fileToDump = new File(absoluteFilePath);
214-
215-
if (checkSizes(fullSize, headerSize))
216-
return;
204+
try {
205+
final long fullSize = getLElong(message, 0x10);
206+
final int fileNameLen = getLEint(message, 0x18);
207+
final int headerSize = getLEint(message, 0x1C);
208+
checkFileNameLen(fileNameLen); // In case of negative value we should better handle it before String constructor throws error
209+
String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8);
210+
String absoluteFilePath = getAbsoluteFilePath(filename);
211+
File fileToDump = new File(absoluteFilePath);
212+
213+
checkSizes(fullSize, headerSize);
214+
createPath(absoluteFilePath);
215+
checkFileSystem(fileToDump, fullSize);
216+
217+
if (headerSize > 0){ // if NSP
218+
logPrinter.print("Receiving NSP file: '"+filename+"' ("+formatByteSize(fullSize)+")", EMsgType.PASS);
219+
createNewNsp(filename, headerSize, fullSize, fileToDump);
220+
return;
221+
}
222+
else {
223+
// TODO: Note, in case of a big amount of small files performance decreases dramatically. It's better to handle this only in case of 1-big-file-transfer
224+
logPrinter.print("Receiving: '"+filename+"' ("+fullSize+" b)", EMsgType.INFO);
225+
}
217226

218-
if (createPath(absoluteFilePath))
219-
return;
227+
writeUsb(USBSTATUS_SUCCESS);
220228

229+
if (fullSize == 0)
230+
return;
221231

222-
if (checkFileSystem(fileToDump, fullSize))
223-
return;
232+
if (isNspTransfer())
233+
dumpNspFile(fullSize);
234+
else
235+
dumpFile(fileToDump, fullSize);
224236

225-
if (headerSize > 0){ // if NSP
226-
logPrinter.print("Receiving NSP file: '"+filename+"' ("+formatByteSize(fullSize)+")", EMsgType.PASS);
227-
createNewNsp(filename, headerSize, fullSize, fileToDump);
228-
return;
237+
writeUsb(USBSTATUS_SUCCESS);
229238
}
230-
else {
231-
// TODO: Note, in case of a big amount of small files performance decreases dramatically. It's better to handle this only in case of 1-big-file-transfer
232-
logPrinter.print("Receiving: '"+filename+"' ("+fullSize+" b)", EMsgType.INFO);
239+
catch (NxdtMalformedException malformed){
240+
logPrinter.print(malformed.getMessage(), EMsgType.FAIL);
241+
writeUsb(USBSTATUS_MALFORMED_REQUEST);
242+
}
243+
catch (NxdtHostIOException ioException){
244+
logPrinter.print(ioException.getMessage(), EMsgType.FAIL);
245+
writeUsb(USBSTATUS_HOSTIOERROR);
233246
}
234-
235-
writeUsb(USBSTATUS_SUCCESS);
236-
237-
if (fullSize == 0)
238-
return;
239-
240-
if (isNspTransfer())
241-
dumpNspFile(nspFile, fullSize);
242-
else
243-
dumpFile(fileToDump, fullSize);
244-
245-
writeUsb(USBSTATUS_SUCCESS);
246-
247247
}
248-
private boolean checkFileNameLen(int fileNameLen) throws Exception{
248+
private void checkFileNameLen(int fileNameLen) throws NxdtMalformedException{
249249
if (fileNameLen <= 0 || fileNameLen > NXDT_FILE_PROPERTIES_MAX_NAME_LENGTH){
250-
logPrinter.print("Invalid filename length!", EMsgType.FAIL);
251-
writeUsb(USBSTATUS_MALFORMED_REQUEST);
252-
return true;
250+
throw new NxdtMalformedException("Invalid filename length!");
253251
}
254-
return false;
255252
}
256-
private boolean checkSizes(long fileSize, int headerSize) throws Exception{
253+
private void checkSizes(long fileSize, int headerSize) throws Exception{
257254
if (headerSize >= fileSize){
258-
logPrinter.print(String.format("File size (%d) should not be less or equal to header size (%d)!", fileSize, headerSize), EMsgType.FAIL);
259255
resetNsp();
260-
writeUsb(USBSTATUS_MALFORMED_REQUEST);
261-
return true;
256+
throw new NxdtMalformedException(String.format("File size (%d) should not be less or equal to header size (%d)!", fileSize, headerSize));
262257
}
263258
if (fileSize < 0){ // It's possible to have files of zero-length, so only less is the problem
264-
logPrinter.print("File size should not be less then zero!", EMsgType.FAIL);
265259
resetNsp();
266-
writeUsb(USBSTATUS_MALFORMED_REQUEST);
267-
return true;
260+
throw new NxdtMalformedException("File size should not be less then zero!");
268261
}
269-
return false;
270262
}
271-
private boolean checkFileSystem(File fileToDump, long fileSize) throws Exception{
263+
private void checkFileSystem(File fileToDump, long fileSize) throws Exception{
272264
// Check if enough space
273265
if (fileToDump.getParentFile().getFreeSpace() <= fileSize){
274-
writeUsb(USBSTATUS_HOSTIOERROR);
275-
logPrinter.print("Not enough space on selected volume. Need: "+fileSize+
276-
" while available: "+fileToDump.getParentFile().getFreeSpace(), EMsgType.FAIL);
277-
return true;
266+
throw new NxdtHostIOException("Not enough space on selected volume. Need: "+fileSize+
267+
" while available: "+fileToDump.getParentFile().getFreeSpace());
278268
}
279269
// Check if FS is NOT read-only
280270
if (! (fileToDump.canWrite() || fileToDump.createNewFile()) ){
281-
writeUsb(USBSTATUS_HOSTIOERROR);
282-
logPrinter.print("Unable to write into selected volume: "+fileToDump.getAbsolutePath(), EMsgType.FAIL);
283-
return true;
271+
throw new NxdtHostIOException("Unable to write into selected volume: "+fileToDump.getAbsolutePath());
284272
}
285-
return false;
286273
}
287-
private void createNewNsp(String filename, int headerSize, long fileSize, File fileToDump) throws Exception{
274+
private void createNewNsp(String filename, int headerSize, long fileSize, File fileToDump) throws NxdtHostIOException{
288275
try {
289276
nspFile = new NxdtNspFile(filename, headerSize, fileSize, fileToDump);
277+
writeUsb(USBSTATUS_SUCCESS);
290278
}
291279
catch (Exception e){
292-
logPrinter.print("Unable to create new file for: "+filename+" :"+e.getMessage(), EMsgType.FAIL);
293280
e.printStackTrace();
294-
writeUsb(USBSTATUS_HOSTIOERROR);
295-
return;
281+
throw new NxdtHostIOException("Unable to create new file for: "+filename+" :"+e.getMessage());
296282
}
297-
writeUsb(USBSTATUS_SUCCESS);
298283
}
299284
private int getLEint(byte[] bytes, int fromOffset){
300285
return ByteBuffer.wrap(bytes, fromOffset, 0x4).order(ByteOrder.LITTLE_ENDIAN).getInt();
@@ -316,24 +301,21 @@ private boolean isRomFs(String filename){
316301
return filename.startsWith("/");
317302
}
318303

319-
private boolean createPath(String path) throws Exception{
304+
private void createPath(String path) throws Exception{
320305
try {
321306
Path folderForTheFile = Paths.get(path).getParent();
322307
Files.createDirectories(folderForTheFile);
323-
return false;
324308
}
325309
catch (Exception e){
326-
logPrinter.print("Unable to create dir(s) for file "+path, EMsgType.FAIL);
327-
writeUsb(USBSTATUS_HOSTIOERROR);
328-
return true;
310+
throw new NxdtHostIOException("Unable to create dir(s) for file '"+path+"':"+e.getMessage());
329311
}
330312
}
331313

332314
// @see https://bugs.openjdk.java.net/browse/JDK-8146538
333315
private void dumpFile(File file, long size) throws Exception{
334316
FileOutputStream fos = new FileOutputStream(file, true);
335317

336-
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
318+
try (BufferedOutputStream bos = new BufferedOutputStream(fos)){
337319
FileDescriptor fd = fos.getFD();
338320
byte[] readBuffer;
339321
long received = 0;
@@ -360,12 +342,12 @@ private void dumpFile(File file, long size) throws Exception{
360342
}
361343
}
362344

363-
private void dumpNspFile(NxdtNspFile nsp, long size) throws Exception{
364-
FileOutputStream fos = new FileOutputStream(nsp.getFile(), true);
365-
long nspSize = nsp.getFullSize();
345+
private void dumpNspFile(long size) throws Exception{
346+
FileOutputStream fos = new FileOutputStream(nspFile.getFile(), true);
347+
long nspSize = nspFile.getFullSize();
366348

367349
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
368-
long nspRemainingSize = nsp.getNspRemainingSize();
350+
long nspRemainingSize = nspFile.getNspRemainingSize();
369351

370352
FileDescriptor fd = fos.getFD();
371353
byte[] readBuffer;
@@ -390,14 +372,15 @@ private void dumpNspFile(NxdtNspFile nsp, long size) throws Exception{
390372
if (isWindows10)
391373
fd.sync();
392374
nspRemainingSize -= (lastChunkSize - 1);
393-
nsp.setNspRemainingSize(nspRemainingSize);
375+
nspFile.setNspRemainingSize(nspRemainingSize);
394376
}
395377
}
396378

397379
private void handleSendNspHeader(byte[] message) throws Exception{
398380
final int headerSize = getLEint(message, 0x8);
399381
NxdtNspFile nsp = nspFile;
400382
resetNsp();
383+
logPrinter.updateProgress(1.0);
401384

402385
if (nsp == null) {
403386
writeUsb(USBSTATUS_MALFORMED_REQUEST);
@@ -423,7 +406,6 @@ private void handleSendNspHeader(byte[] message) throws Exception{
423406
raf.write(headerData);
424407
}
425408
logPrinter.print("NSP file: '"+nsp.getName()+"' successfully received!", EMsgType.PASS);
426-
logPrinter.updateProgress(1.0);
427409
writeUsb(USBSTATUS_SUCCESS);
428410
}
429411
private void resetNsp(){

0 commit comments

Comments
 (0)