Use FileProcessParam.

This commit is contained in:
TheFloW 2016-09-10 14:35:15 +02:00
parent 8379eac6b7
commit 31913b1556
7 changed files with 159 additions and 107 deletions

View File

@ -134,7 +134,7 @@ int getArchivePathInfo(char *path, uint64_t *size, uint32_t *folders, uint32_t *
return 0;
}
int extractArchivePath(char *src, char *dst, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)()) {
int extractArchivePath(char *src, char *dst, FileProcessParam *param) {
if (!uf)
return -1;
@ -151,15 +151,17 @@ int extractArchivePath(char *src, char *dst, uint64_t *value, uint64_t max, void
return ret;
}
if (value)
(*value)++;
if (param) {
if (param->value)
(*param->value)++;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (cancelHandler && cancelHandler()) {
fileListEmpty(&list);
return 0;
if (param->SetProgress)
SetProgress(param->value ? *param->value : 0, param->max);
if (param->cancelHandler && param->cancelHandler()) {
fileListEmpty(&list);
return 0;
}
}
FileListEntry *entry = list.head->next; // Ignore ..
@ -172,7 +174,7 @@ int extractArchivePath(char *src, char *dst, uint64_t *value, uint64_t max, void
char *dst_path = malloc(strlen(dst) + strlen(entry->name) + 2);
snprintf(dst_path, MAX_PATH_LENGTH, "%s%s", dst, entry->name);
int ret = extractArchivePath(src_path, dst_path, value, max, SetProgress, cancelHandler);
int ret = extractArchivePath(src_path, dst_path, param);
free(dst_path);
free(src_path);
@ -211,19 +213,21 @@ int extractArchivePath(char *src, char *dst, uint64_t *value, uint64_t max, void
return res;
}
if (value)
(*value) += read;
if (param) {
if (param->value)
(*param->value) += read;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (cancelHandler && cancelHandler()) {
free(buf);
if (param->cancelHandler && param->cancelHandler()) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
sceIoClose(fddst);
archiveFileClose(fdsrc);
return 0;
return 0;
}
}
}

View File

@ -26,7 +26,7 @@
int fileListGetArchiveEntries(FileList *list, char *path);
int getArchivePathInfo(char *path, uint64_t *size, uint32_t *folders, uint32_t *files);
int extractArchivePath(char *src, char *dst, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)());
int extractArchivePath(char *src, char *dst, FileProcessParam *param);
int archiveFileGetstat(const char *file, SceIoStat *stat);
int archiveFileOpen(const char *file, int flags, SceMode mode);

142
file.c
View File

@ -95,9 +95,8 @@ int getFileSize(char *pInputFileName)
return fileSize;
}
int getFileSha1(char *pInputFileName, uint8_t *pSha1Out, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)()) {
// Set up SHA1 context
int getFileSha1(char *pInputFileName, uint8_t *pSha1Out, FileProcessParam *param) {
// Set up SHA1 context
SHA1_CTX ctx;
sha1_init(&ctx);
@ -112,29 +111,30 @@ int getFileSha1(char *pInputFileName, uint8_t *pSha1Out, uint64_t *value, uint64
int read;
// Actually take the SHA1 sum
while ((read = sceIoRead(fd, buf, TRANSFER_SIZE)) > 0)
{
while ((read = sceIoRead(fd, buf, TRANSFER_SIZE)) > 0) {
sha1_update(&ctx, buf, read);
// Defined in io_process.c, check to make sure pointer isn't null before incrementing
if(value)
(*value)++; // Note: Max value is filesize/TRANSFER_SIZE
if (param) {
// Defined in io_process.c, check to make sure pointer isn't null before incrementing
if (param->value)
(*param->value)++; // Note: Max value is filesize/TRANSFER_SIZE
if(SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
// Check to see if cancelHandler exists, if so call it and free memory if cancelled
if(cancelHandler && cancelHandler()) {
free(buf);
sceIoClose(fd);
return 0;
// Check to see if param->cancelHandler exists, if so call it and free memory if cancelled
if (param->cancelHandler && param->cancelHandler()) {
free(buf);
sceIoClose(fd);
return 0;
}
// This is CPU intensive so the progress bar won't refresh unless we sleep
// DIALOG_WAIT seemed too long for this application
// so I set it to 1/2 of a second every 8192 TRANSFER_SIZE blocks
if ((*param->value) % 8192 == 0)
sceKernelDelayThread(500000);
}
// This is CPU intensive so the progress bar won't refresh unless we sleep
// DIALOG_WAIT seemed too long for this application
// so I set it to 1/2 of a second every 8192 TRANSFER_SIZE blocks
if((*value)%8192 == 0)
sceKernelDelayThread(500000);
}
// Final iteration of SHA1 sum, dump final value into pSha1Out buffer
@ -207,7 +207,7 @@ int getPathInfo(char *path, uint64_t *size, uint32_t *folders, uint32_t *files)
return 1;
}
int removePath(char *path, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)()) {
int removePath(char *path, FileProcessParam *param) {
SceUID dfd = sceIoDopen(path);
if (dfd >= 0) {
int res = 0;
@ -225,7 +225,7 @@ int removePath(char *path, uint64_t *value, uint64_t max, void (* SetProgress)(u
snprintf(new_path, MAX_PATH_LENGTH, "%s%s%s", path, hasEndSlash(path) ? "" : "/", dir.d_name);
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
int ret = removePath(new_path, value, max, SetProgress, cancelHandler);
int ret = removePath(new_path, param);
if (ret <= 0) {
free(new_path);
sceIoDclose(dfd);
@ -239,16 +239,18 @@ int removePath(char *path, uint64_t *value, uint64_t max, void (* SetProgress)(u
return ret;
}
if (value)
(*value)++;
if (param) {
if (param->value)
(*param->value)++;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (cancelHandler && cancelHandler()) {
free(new_path);
sceIoDclose(dfd);
return 0;
if (param->cancelHandler && param->cancelHandler()) {
free(new_path);
sceIoDclose(dfd);
return 0;
}
}
}
@ -262,35 +264,39 @@ int removePath(char *path, uint64_t *value, uint64_t max, void (* SetProgress)(u
if (ret < 0)
return ret;
if (value)
(*value)++;
if (param) {
if (param->value)
(*param->value)++;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (cancelHandler && cancelHandler()) {
return 0;
if (param->cancelHandler && param->cancelHandler()) {
return 0;
}
}
} else {
int ret = sceIoRemove(path);
if (ret < 0)
return ret;
if (value)
(*value)++;
if (param) {
if (param->value)
(*param->value)++;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (cancelHandler && cancelHandler()) {
return 0;
if (param->cancelHandler && param->cancelHandler()) {
return 0;
}
}
}
return 1;
}
int copyFile(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)()) {
int copyFile(char *src_path, char *dst_path, FileProcessParam *param) {
// The source and destination paths are identical
if (strcasecmp(src_path, dst_path) == 0) {
return -1;
@ -326,19 +332,21 @@ int copyFile(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void
return res;
}
if (value)
(*value) += read;
if (param) {
if (param->value)
(*param->value) += read;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (cancelHandler && cancelHandler()) {
free(buf);
if (param->cancelHandler && param->cancelHandler()) {
free(buf);
sceIoClose(fddst);
sceIoClose(fdsrc);
sceIoClose(fddst);
sceIoClose(fdsrc);
return 0;
return 0;
}
}
}
@ -350,7 +358,7 @@ int copyFile(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void
return 1;
}
int copyPath(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)()) {
int copyPath(char *src_path, char *dst_path, FileProcessParam *param) {
// The source and destination paths are identical
if (strcasecmp(src_path, dst_path) == 0) {
return -1;
@ -370,15 +378,17 @@ int copyPath(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void
return ret;
}
if (value)
(*value)++;
if (param) {
if (param->value)
(*param->value)++;
if (SetProgress)
SetProgress(value ? *value : 0, max);
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (cancelHandler && cancelHandler()) {
sceIoDclose(dfd);
return 0;
if (param->cancelHandler && param->cancelHandler()) {
sceIoDclose(dfd);
return 0;
}
}
int res = 0;
@ -401,9 +411,9 @@ int copyPath(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void
int ret = 0;
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
ret = copyPath(new_src_path, new_dst_path, value, max, SetProgress, cancelHandler);
ret = copyPath(new_src_path, new_dst_path, param);
} else {
ret = copyFile(new_src_path, new_dst_path, value, max, SetProgress, cancelHandler);
ret = copyFile(new_src_path, new_dst_path, param);
}
free(new_dst_path);
@ -418,13 +428,13 @@ int copyPath(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void
sceIoDclose(dfd);
} else {
return copyFile(src_path, dst_path, value, max, SetProgress, cancelHandler);
return copyFile(src_path, dst_path, param);
}
return 1;
}
int movePath(char *src_path, char *dst_path, int flags, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)()) {
int movePath(char *src_path, char *dst_path, int flags, FileProcessParam *param) {
// The source and destination paths are identical
if (strcasecmp(src_path, dst_path) == 0) {
return -1;
@ -495,7 +505,7 @@ int movePath(char *src_path, char *dst_path, int flags, uint64_t *value, uint64_
snprintf(new_dst_path, MAX_PATH_LENGTH, "%s%s%s", dst_path, hasEndSlash(dst_path) ? "" : "/", dir.d_name);
// Recursive move
int ret = movePath(new_src_path, new_dst_path, flags, value, max, SetProgress, cancelHandler);
int ret = movePath(new_src_path, new_dst_path, flags, param);
free(new_dst_path);
free(new_src_path);

21
file.h
View File

@ -43,16 +43,23 @@ enum FileTypes {
FILE_TYPE_ZIP,
};
enum SortFlags {
enum FileSortFlags {
SORT_NONE,
SORT_BY_NAME_AND_FOLDER,
};
enum MoveFlags {
enum FileMoveFlags {
MOVE_INTEGRATE = 0x1, // Integrate directories
MOVE_REPLACE = 0x2, // Replace files
};
typedef struct {
uint64_t *value;
uint64_t max;
void (* SetProgress)(uint64_t value, uint64_t max);
int (* cancelHandler)();
} FileProcessParam;
typedef struct FileListEntry {
struct FileListEntry *next;
struct FileListEntry *previous;
@ -79,12 +86,12 @@ int ReadFile(char *file, void *buf, int size);
int WriteFile(char *file, void *buf, int size);
int getFileSize(char *pInputFileName);
int getFileSha1(char *pInputFileName, uint8_t *pSha1Out, uint64_t *value, uint64_t max, void (*SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)());
int getFileSha1(char *pInputFileName, uint8_t *pSha1Out, FileProcessParam *param);
int getPathInfo(char *path, uint64_t *size, uint32_t *folders, uint32_t *files);
int removePath(char *path, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)());
int copyFile(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)());
int copyPath(char *src_path, char *dst_path, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)());
int movePath(char *src_path, char *dst_path, int flags, uint64_t *value, uint64_t max, void (* SetProgress)(uint64_t value, uint64_t max), int (* cancelHandler)());
int removePath(char *path, FileProcessParam *param);
int copyFile(char *src_path, char *dst_path, FileProcessParam *param);
int copyPath(char *src_path, char *dst_path, FileProcessParam *param);
int movePath(char *src_path, char *dst_path, int flags, FileProcessParam *param);
int getFileType(char *file);

View File

@ -140,7 +140,12 @@ int delete_thread(SceSize args_size, DeleteArguments *args) {
snprintf(path, MAX_PATH_LENGTH, "%s%s", args->file_list->path, mark_entry->name);
removeEndSlash(path);
int res = removePath(path, &value, folders + files, SetProgress, cancelHandler);
FileProcessParam param;
param.value = &value;
param.max = folders + files;
param.SetProgress = SetProgress;
param.cancelHandler = cancelHandler;
int res = removePath(path, &param);
if (res <= 0) {
closeWaitDialog();
dialog_step = DIALOG_STEP_CANCELLED;
@ -199,7 +204,7 @@ int copy_thread(SceSize args_size, CopyArguments *args) {
removeEndSlash(src_path);
removeEndSlash(dst_path);
int res = movePath(src_path, dst_path, MOVE_INTEGRATE | MOVE_REPLACE, NULL, 0, NULL, NULL);
int res = movePath(src_path, dst_path, MOVE_INTEGRATE | MOVE_REPLACE, NULL);
if (res < 0) {
closeWaitDialog();
errorDialog(res);
@ -270,8 +275,14 @@ int copy_thread(SceSize args_size, CopyArguments *args) {
removeEndSlash(src_path);
removeEndSlash(dst_path);
FileProcessParam param;
param.value = &value;
param.max = size + folders;
param.SetProgress = SetProgress;
param.cancelHandler = cancelHandler;
if (args->copy_mode == COPY_MODE_EXTRACT) {
int res = extractArchivePath(src_path, dst_path, &value, size + folders, SetProgress, cancelHandler);
int res = extractArchivePath(src_path, dst_path, &param);
if (res <= 0) {
closeWaitDialog();
dialog_step = DIALOG_STEP_CANCELLED;
@ -279,7 +290,7 @@ int copy_thread(SceSize args_size, CopyArguments *args) {
goto EXIT;
}
} else {
int res = copyPath(src_path, dst_path, &value, size + folders, SetProgress, cancelHandler);
int res = copyPath(src_path, dst_path, &param);
if (res <= 0) {
closeWaitDialog();
dialog_step = DIALOG_STEP_CANCELLED;
@ -339,8 +350,14 @@ int hash_thread(SceSize args_size, HashArguments *args) {
// Spin off a thread to update the progress dialog
thid = createStartUpdateThread(max);
FileProcessParam param;
param.value = &value;
param.max = max;
param.SetProgress = SetProgress;
param.cancelHandler = cancelHandler;
uint8_t sha1out[20];
int res = getFileSha1(args->file_path, sha1out, &value, max, SetProgress, cancelHandler);
int res = getFileSha1(args->file_path, sha1out, &param);
if (res <= 0) {
// SHA1 Didn't complete successfully, or was cancelled
closeWaitDialog();

View File

@ -219,7 +219,7 @@ EXIT:
void installUpdater() {
// Recursively clean up package_temp directory
removePath(PACKAGE_PARENT, NULL, 0, NULL, NULL);
removePath(PACKAGE_PARENT, NULL);
sceIoMkdir(PACKAGE_PARENT, 0777);
// Make dirs
@ -252,7 +252,7 @@ int update_extract_thread(SceSize args, void *argp) {
installUpdater();
// Recursively clean up package_temp directory
removePath(PACKAGE_PARENT, NULL, 0, NULL, NULL);
removePath(PACKAGE_PARENT, NULL);
sceIoMkdir(PACKAGE_PARENT, 0777);
// Open archive
@ -276,7 +276,14 @@ int update_extract_thread(SceSize args, void *argp) {
// Extract process
uint64_t value = 0;
res = extractArchivePath(src_path, PACKAGE_DIR "/", &value, size + folders, SetProgress, cancelHandler);
FileProcessParam param;
param.value = &value;
param.max = size + folders;
param.SetProgress = SetProgress;
param.cancelHandler = cancelHandler;
res = extractArchivePath(src_path, PACKAGE_DIR "/", &param);
if (res <= 0) {
closeWaitDialog();
dialog_step = DIALOG_STEP_CANCELLED;

View File

@ -96,12 +96,12 @@ int promoteUpdate(char *path, char *titleid, char *category, void *sfo_buffer, i
*/
// Integrate patch to app
res = movePath(path, app_path, MOVE_INTEGRATE | MOVE_REPLACE, NULL, 0, NULL, NULL);
res = movePath(path, app_path, MOVE_INTEGRATE | MOVE_REPLACE, NULL);
if (res < 0)
return res;
// Move app to promotion directory
res = movePath(app_path, path, 0, NULL, 0, NULL, NULL);
res = movePath(app_path, path, 0, NULL);
if (res < 0)
return res;
}
@ -280,7 +280,7 @@ int installPackage(char *file) {
int res;
// Recursively clean up package_temp directory
removePath(PACKAGE_PARENT, NULL, 0, NULL, NULL);
removePath(PACKAGE_PARENT, NULL);
sceIoMkdir(PACKAGE_PARENT, 0777);
// Open archive
@ -294,7 +294,7 @@ int installPackage(char *file) {
addEndSlash(src_path);
// Extract process
res = extractArchivePath(src_path, PACKAGE_DIR "/", NULL, 0, NULL, NULL);
res = extractArchivePath(src_path, PACKAGE_DIR "/", NULL);
if (res < 0)
return res;
@ -329,7 +329,7 @@ int install_thread(SceSize args_size, InstallArguments *args) {
sceKernelDelayThread(DIALOG_WAIT); // Needed to see the percentage
// Recursively clean up package_temp directory
removePath(PACKAGE_PARENT, NULL, 0, NULL, NULL);
removePath(PACKAGE_PARENT, NULL);
sceIoMkdir(PACKAGE_PARENT, 0777);
// Open archive
@ -396,7 +396,14 @@ int install_thread(SceSize args_size, InstallArguments *args) {
// Extract process
uint64_t value = 0;
res = extractArchivePath(src_path, PACKAGE_DIR "/", &value, size + folders, SetProgress, cancelHandler);
FileProcessParam param;
param.value = &value;
param.max = size + folders;
param.SetProgress = SetProgress;
param.cancelHandler = cancelHandler;
res = extractArchivePath(src_path, PACKAGE_DIR "/", &param);
if (res <= 0) {
closeWaitDialog();
dialog_step = DIALOG_STEP_CANCELLED;