2016-10-23 15:34:57 +02:00
|
|
|
package de.mas.wupclient.client.operations;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2016-10-23 16:28:05 +02:00
|
|
|
import java.util.Arrays;
|
2016-10-23 15:34:57 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import de.mas.wupclient.client.WUPClient;
|
|
|
|
import de.mas.wupclient.client.utils.FEntry;
|
2016-10-23 23:09:51 +02:00
|
|
|
import de.mas.wupclient.client.utils.Logger;
|
2016-10-23 15:34:57 +02:00
|
|
|
import de.mas.wupclient.client.utils.Result;
|
2016-10-23 16:28:05 +02:00
|
|
|
import de.mas.wupclient.client.utils.Utils;
|
2016-10-23 15:34:57 +02:00
|
|
|
|
|
|
|
public class FSAOperations extends Operations {
|
|
|
|
private static Map<WUPClient,FSAOperations> instances = new HashMap<>();
|
|
|
|
public static FSAOperations FSAOperationsFactory(WUPClient client){
|
|
|
|
if(!instances.containsKey(client)){
|
|
|
|
instances.put(client, new FSAOperations(client));
|
|
|
|
}
|
|
|
|
return instances.get(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IoctlOperations ioctl = null;
|
|
|
|
|
2016-10-23 23:09:51 +02:00
|
|
|
private FSAOperations(WUPClient client) {
|
2016-10-23 15:34:57 +02:00
|
|
|
super(client);
|
|
|
|
setIoctlOperations(IoctlOperations.IoctlOperationsFactory(client));
|
|
|
|
}
|
|
|
|
|
|
|
|
public IoctlOperations getIoctlOperations() {
|
|
|
|
return ioctl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setIoctlOperations(IoctlOperations ioctl) {
|
|
|
|
this.ioctl = ioctl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<Integer> FSA_OpenDir(int handle, String path) throws IOException{
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
2016-10-23 16:28:05 +02:00
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, path, 0x04);
|
2016-10-23 15:34:57 +02:00
|
|
|
|
|
|
|
Result<byte[]> res = ioctl.ioctl(handle, 0x0A, inbuffer, 0x293);
|
2016-10-23 16:28:05 +02:00
|
|
|
|
2016-10-23 23:09:51 +02:00
|
|
|
return new Result<Integer>(res.getResultValue(),Utils.bigEndianByteArrayToInt(Arrays.copyOfRange(res.getData(), 0x04, 0x08)));
|
2016-10-23 15:34:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public int FSA_CloseDir(int handle, int dirhandle) throws IOException{
|
2016-10-23 16:28:05 +02:00
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeIntToByteArray(inbuffer, dirhandle, 0x04);
|
2016-10-23 15:34:57 +02:00
|
|
|
Result<byte[]> res = ioctl.ioctl(handle, 0x0D, inbuffer, 0x293);
|
|
|
|
return res.getResultValue();
|
|
|
|
}
|
|
|
|
|
2016-10-23 16:28:05 +02:00
|
|
|
public Result<FEntry> FSA_ReadDir(int fsa_handle, int dirhandle) throws IOException {
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeIntToByteArray(inbuffer,dirhandle,4);
|
|
|
|
|
|
|
|
Result<byte[]> res = ioctl.ioctl(fsa_handle, 0x0B, inbuffer, 0x293);
|
2016-10-23 23:09:51 +02:00
|
|
|
|
2016-10-23 16:28:05 +02:00
|
|
|
byte[] unknowndata = Arrays.copyOfRange(res.getData(), 0x04, 0x68);
|
|
|
|
String filename = Utils.getStringFromByteArray(Arrays.copyOfRange(res.getData(), 0x68, res.getData().length));
|
|
|
|
if(res.getResultValue() == 0){
|
2016-10-23 23:09:51 +02:00
|
|
|
return new Result<FEntry>(res.getResultValue(),new FEntry(filename,((char)unknowndata[0] & 128) != 128,unknowndata));
|
2016-10-23 16:28:05 +02:00
|
|
|
}else{
|
|
|
|
return new Result<FEntry>(res.getResultValue(),null);
|
|
|
|
}
|
|
|
|
}
|
2016-10-23 23:09:51 +02:00
|
|
|
|
|
|
|
public int FSA_Mount(int handle, String device_path, String volume_path, int flags) throws IOException{
|
|
|
|
Logger.logCmd("Mounting " + device_path + " to " + volume_path);
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, device_path, 0x0004);
|
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, volume_path, 0x0284);
|
|
|
|
Utils.writeIntToByteArray(inbuffer,flags,0x0504);
|
|
|
|
|
|
|
|
Result<byte[][]> result = ioctl.ioctlv(handle, 0x01, new byte[][] {inbuffer,new byte[0]}, new int[]{0x293});
|
|
|
|
return result.getResultValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int FSA_Unmount(int handle, String volume_path, int flags) throws IOException{
|
|
|
|
Logger.logCmd("Unmounting " + volume_path);
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, volume_path, 0x04);
|
|
|
|
Utils.writeIntToByteArray(inbuffer,flags,0x284);
|
|
|
|
Result<byte[]> result = ioctl.ioctl(handle,0x02,inbuffer,0x293);
|
|
|
|
return result.getResultValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int FSA_MakeDir(int handle, String path, int flags) throws IOException{
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, path, 0x04);
|
|
|
|
Utils.writeIntToByteArray(inbuffer,flags,0x284);
|
|
|
|
Result<byte[]> result = ioctl.ioctl(handle, 0x07, inbuffer, 0x293);
|
|
|
|
return result.getResultValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<Integer> FSA_OpenFile(int handle, String path, String mode) throws IOException{
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, path, 0x04);
|
|
|
|
Utils.writeNullTerminatedStringToByteArray(inbuffer, mode, 0x284);
|
|
|
|
Result<byte[]> result = ioctl.ioctl(handle, 0x0E, inbuffer, 0x293);
|
|
|
|
|
|
|
|
return new Result<Integer>(result.getResultValue(),Utils.bigEndianByteArrayToInt(Arrays.copyOfRange(result.getData(), 0x04, 0x08)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int FSA_CloseFile(int handle, int file_handle) throws IOException{
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeIntToByteArray(inbuffer, file_handle, 0x04);
|
|
|
|
Result<byte[]> res = ioctl.ioctl(handle, 0x15, inbuffer, 0x293);
|
|
|
|
return res.getResultValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<byte[]> FSA_ReadFile(int handle, int file_handle, int size, int cnt) throws IOException{
|
|
|
|
byte[] inbuffer = new byte[0x520];
|
|
|
|
Utils.writeIntToByteArray(inbuffer, size, 0x08);
|
|
|
|
Utils.writeIntToByteArray(inbuffer, cnt, 0x0C);
|
|
|
|
Utils.writeIntToByteArray(inbuffer, file_handle, 0x14);
|
|
|
|
Result<byte[][]> result = ioctl.ioctlv(handle, 0x0F, new byte[][] {inbuffer}, new int[]{size * cnt,0x293});
|
|
|
|
|
|
|
|
return new Result<byte[]>(result.getResultValue(),result.getData()[0]);
|
|
|
|
}
|
2016-10-23 15:34:57 +02:00
|
|
|
}
|