some improvements, untested

- returns the number of injected files
- only flush the data on success
- fix false error messages of FSClose stuff

untested
This commit is contained in:
Maschell 2015-10-23 23:05:04 +02:00
parent 6cdc9276aa
commit 6318737325
2 changed files with 152 additions and 116 deletions

View File

@ -199,7 +199,7 @@ static int remove_files_in_dir(void * pClient,void * pCmd, char * path, int hand
return -1; return -1;
} }
} }
if((FSCloseDir(pClient, pCmd, handle, FS_RET_NO_ERROR)) <=0 ){ if((FSCloseDir(pClient, pCmd, handle, FS_RET_NO_ERROR)) < 0 ){
log_string(bss.logsock, "error while closing dir", BYTE_LOG_STR); log_string(bss.logsock, "error while closing dir", BYTE_LOG_STR);
} }
} }
@ -306,20 +306,20 @@ static int dump_dir(void *pClient,int client, void *pCmd, char *path, int error,
} }
return 0; return 0;
} }
#define BUFFER_SIZE (1024)*100
static void handle_saves(void *pClient, void *pCmd,int error, int client){ static void handle_saves(void *pClient, void *pCmd,int error, int client){
log_string(bss.logsock, "handle_saves", BYTE_LOG_STR); log_string(bss.logsock, "handle_saves", BYTE_LOG_STR);
init_Save(); init_Save();
long id = getPesistentID(); long id = getPesistentID();
int mode;
log_string(bss.logsock, "user savedata", BYTE_LOG_STR); log_string(bss.logsock, "user savedata", BYTE_LOG_STR);
if(getMode(bss.socket_fsa[client],&mode)){
if(id >= 0x80000000 && id <= 0x90000000){ if(id >= 0x80000000 && id <= 0x90000000){
char savepath[20]; char savepath[20];
int mode;
__os_snprintf(savepath, sizeof(savepath), "/vol/save/%08x",id); __os_snprintf(savepath, sizeof(savepath), "/vol/save/%08x",id);
log_string(bss.logsock, "Getting mode!", BYTE_LOG_STR); log_string(bss.logsock, "Getting mode!", BYTE_LOG_STR);
if(getMode(bss.socket_fsa[client],&mode)){ {
if(mode == BYTE_MODE_D){ if(mode == BYTE_MODE_D){
log_string(bss.logsock, "dump mode!", BYTE_LOG_STR); log_string(bss.logsock, "dump mode!", BYTE_LOG_STR);
dump_dir(pClient,client,pCmd,savepath,-1,50); dump_dir(pClient,client,pCmd,savepath,-1,50);
@ -327,47 +327,68 @@ static void handle_saves(void *pClient, void *pCmd,int error, int client){
log_string(bss.logsock, "inject mode", BYTE_LOG_STR); log_string(bss.logsock, "inject mode", BYTE_LOG_STR);
log_string(bss.logsock, "deleting current save", BYTE_LOG_STR); log_string(bss.logsock, "deleting current save", BYTE_LOG_STR);
remove_files_in_dir(pClient,pCmd,savepath,0); remove_files_in_dir(pClient,pCmd,savepath,0);
injectFiles(pClient,pCmd,savepath,"/",-1); int buf_size = BUFFER_SIZE;
log_string(bss.logsock, "flushing quota", BYTE_LOG_STR); char * pBuffer;
FSFlushQuota(pClient,pCmd,savepath,-1); int failed = 0;
do{
buf_size -= 0x200;
if(buf_size < 0){
log_string(bss.logsock, "error on buffer allocation", BYTE_LOG_STR);
failed = 1;
break;
}
pBuffer = (char *)MEMAllocFromDefaultHeapEx(buf_size, 0x40);
}while(!pBuffer);
if(!failed){
int result = injectFiles(pClient,pCmd,savepath,"/",pBuffer,buf_size,-1);
if(result == -1){
log_string(bss.logsock, "injection failed, trying to restore the data", BYTE_LOG_STR);
//TODO FSRollbackQuota
}else{
char logbugger[50];
__os_snprintf(logbugger, sizeof(logbugger), "injected %d files, flushing the data now",result);
log_string(bss.logsock, logbugger, BYTE_LOG_STR);
if(FSFlushQuota(pClient,pCmd,savepath,-1) == 0){
log_string(bss.logsock, "success", BYTE_LOG_STR);
}else{
log_string(bss.logsock, "failed", BYTE_LOG_STR);
}
} }
} }
} }
/* }
}
if(mode == BYTE_MODE_D){
log_string(bss.logsock, "dumping common savedata", BYTE_LOG_STR); log_string(bss.logsock, "dumping common savedata", BYTE_LOG_STR);
dump_dir(pClient,client,pCmd,"/vol/save/common/",error,60); dump_dir(pClient,client,pCmd,"/vol/save/common/",error,60);
log_string(bss.logsock, "done!", BYTE_LOG_STR);*/ log_string(bss.logsock, "done!", BYTE_LOG_STR);
}
}
} }
#define BUFFER_SIZE (1024)*100 int injectFiles(void *pClient, void *pCmd, char * path,char * relativepath,char * pBuffer, int buffer_size, int error){
void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int error){
//FSStatus (*FSWriteFileWithPos) (FSClient *pClient, FSCmdBlock *pCmd, const void *source,int size,int count,int fpos,int fileHandle,int flag,FSRetFlag errHandling); //FSStatus (*FSWriteFileWithPos) (FSClient *pClient, FSCmdBlock *pCmd, const void *source,int size,int count,int fpos,int fileHandle,int flag,FSRetFlag errHandling);
int client = client_num(pClient); int client = client_num(pClient);
int failed = 0;
int filesinjected = 0;
int type = 0; int type = 0;
log_string(bss.logsock, "injecting files", BYTE_LOG_STR); log_string(bss.logsock, "injecting files", BYTE_LOG_STR);
char namebuffer[255]; char namebuffer[255];
char logbugger[255]; char logbugger[255];
int filesize = 0; int filesize = 0;
int buf_size = BUFFER_SIZE;
char * pBuffer;
do{
buf_size -= 0x200;
if(buf_size < 0){
log_string(bss.logsock, "error on buffer allocation", BYTE_LOG_STR);
return;
}
pBuffer = (char *)MEMAllocFromDefaultHeapEx(buf_size, 0x40);
}while(!pBuffer);
__os_snprintf(logbugger, sizeof(logbugger), "buffer size: %d bytes",buf_size);
if(!failed){
__os_snprintf(logbugger, sizeof(logbugger), "buffer size: %d bytes",buffer_size);
log_string(bss.logsock, logbugger, BYTE_LOG_STR); log_string(bss.logsock, logbugger, BYTE_LOG_STR);
while(getFiles(bss.socket_fsa[client],path,namebuffer, &type,&filesize)){ while(getFiles(bss.socket_fsa[client],path,namebuffer, &type,&filesize) && !failed){
if(DEBUG_LOG)log_string(bss.logsock, "got a file", BYTE_LOG_STR); if(DEBUG_LOG)log_string(bss.logsock, "got a file", BYTE_LOG_STR);
char newpath[strlen(path) + 1 + strlen(namebuffer)]; char newpath[strlen(path) + 1 + strlen(namebuffer)];
__os_snprintf(newpath, sizeof(newpath), "%s/%s",path,namebuffer); __os_snprintf(newpath, sizeof(newpath), "%s/%s",path,namebuffer);
@ -386,7 +407,7 @@ void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int
if(DEBUG_LOG)__os_snprintf(logbugger, sizeof(logbugger), "cafiine_fopen with handle %d",myhandle); if(DEBUG_LOG)__os_snprintf(logbugger, sizeof(logbugger), "cafiine_fopen with handle %d",myhandle);
if(DEBUG_LOG) log_string(bss.logsock, logbugger, BYTE_LOG_STR); if(DEBUG_LOG) log_string(bss.logsock, logbugger, BYTE_LOG_STR);
int size = sizeof(char); int size = sizeof(char);
int count = buf_size; int count = buffer_size;
int retsize = 0; int retsize = 0;
int pos = 0; int pos = 0;
while(pos < filesize){ while(pos < filesize){
@ -404,12 +425,14 @@ void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int
__os_snprintf(logbugger, sizeof(logbugger), "my_FSWriteFile failed with error: %d",fwrite); __os_snprintf(logbugger, sizeof(logbugger), "my_FSWriteFile failed with error: %d",fwrite);
if(DEBUG_LOG) log_string(bss.logsock, logbugger, BYTE_LOG_STR); if(DEBUG_LOG) log_string(bss.logsock, logbugger, BYTE_LOG_STR);
log_string(bss.logsock, "error while FSWriteFile", BYTE_LOG_STR); log_string(bss.logsock, "error while FSWriteFile", BYTE_LOG_STR);
failed = 1;
} }
__os_snprintf(logbugger, sizeof(logbugger), "old p %d new p %d",pos,pos+retsize); __os_snprintf(logbugger, sizeof(logbugger), "old p %d new p %d",pos,pos+retsize);
if(DEBUG_LOG) log_string(bss.logsock, logbugger, BYTE_LOG_STR); if(DEBUG_LOG) log_string(bss.logsock, logbugger, BYTE_LOG_STR);
pos += retsize; pos += retsize;
}else{ }else{
log_string(bss.logsock, "error while recieving file", BYTE_LOG_STR); log_string(bss.logsock, "error while recieving file", BYTE_LOG_STR);
failed = 1;
break; break;
} }
} }
@ -419,6 +442,7 @@ void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int
if(DEBUG_LOG) log_string(bss.logsock, "cafiine_fclose success", BYTE_LOG_STR); if(DEBUG_LOG) log_string(bss.logsock, "cafiine_fclose success", BYTE_LOG_STR);
}else{ }else{
log_string(bss.logsock, "cafiine_fclose failed", BYTE_LOG_STR); log_string(bss.logsock, "cafiine_fclose failed", BYTE_LOG_STR);
failed = 1;
} }
@ -427,10 +451,11 @@ void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int
} }
} }
if((FSCloseFile (pClient, pCmd, handle, -1)) <= 0) if((FSCloseFile (pClient, pCmd, handle, -1)) < 0)
log_string(bss.logsock, "FSCloseFile failed", BYTE_LOG_STR); log_string(bss.logsock, "FSCloseFile failed", BYTE_LOG_STR);
}
}
if(!failed) filesinjected++;
}else if( type == BYTE_FOLDER){ }else if( type == BYTE_FOLDER){
__os_snprintf(logbugger, sizeof(logbugger), "dir: %s",namebuffer); __os_snprintf(logbugger, sizeof(logbugger), "dir: %s",namebuffer);
log_string(bss.logsock, logbugger, BYTE_LOG_STR); log_string(bss.logsock, logbugger, BYTE_LOG_STR);
@ -438,14 +463,25 @@ void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int
if(FSMakeDir(pClient, pCmd, newpath, -1) == 0){ if(FSMakeDir(pClient, pCmd, newpath, -1) == 0){
char op_offset[strlen(relativepath) + strlen(namebuffer)+ 1 + 1]; char op_offset[strlen(relativepath) + strlen(namebuffer)+ 1 + 1];
__os_snprintf(op_offset, sizeof(op_offset), "%s%s/",relativepath,namebuffer); __os_snprintf(op_offset, sizeof(op_offset), "%s%s/",relativepath,namebuffer);
injectFiles(pClient, pCmd, newpath,op_offset,error); int injectedsub = injectFiles(pClient, pCmd, newpath,op_offset,pBuffer,buffer_size,error);
if(injectedsub == -1){
failed = 1;
}else{
filesinjected += injectedsub;
}
}else{ }else{
log_string(bss.logsock, "folder creation failed", BYTE_LOG_STR); log_string(bss.logsock, "folder creation failed", BYTE_LOG_STR);
failed = 1;
} }
} }
} }
free(pBuffer); free(pBuffer);
log_string(bss.logsock, "getting files done", BYTE_LOG_STR); if(failed) return -1;
else return filesinjected;
}else{
return -1;
}
} }
#define MAKE_MAGIC(x) { x, my_ ## x, &real_ ## x } #define MAKE_MAGIC(x) { x, my_ ## x, &real_ ## x }

View File

@ -56,7 +56,7 @@ extern int send(int socket, const void *buffer, int size, int flags);
extern int recv(int socket, void *buffer, int size, int flags); extern int recv(int socket, void *buffer, int size, int flags);
extern int __os_snprintf(char* s, int n, const char * format, ...); extern int __os_snprintf(char* s, int n, const char * format, ...);
int getFiles(int sock, char * path,char * resultname, int * resulttype,int *filesize); int getFiles(int sock, char * path,char * resultname, int * resulttype,int *filesize);
void injectFiles(void *pClient, void *pCmd, char * path,char * relativepath, int error); int injectFiles(void *pClient, void *pCmd, char * path,char * relativepath,char * pBuffer, int buffer_size, int error);
struct in_addr { struct in_addr {
unsigned int s_addr; unsigned int s_addr;