VitaShell/makezip.c

337 lines
8.2 KiB
C
Raw Normal View History

2016-10-15 13:03:26 +00:00
/*
2017-10-13 10:42:36 +00:00
VitaShell
Copyright (C) 2015-2017, TheFloW
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2016-10-15 13:03:26 +00:00
*/
#include "main.h"
#include "io_process.h"
#include "makezip.h"
#include "file.h"
#include "utils.h"
#include "minizip/zip.h"
2017-02-12 15:27:43 +00:00
static void convertToZipTime(SceDateTime *time, tm_zip *tmzip) {
2017-10-13 10:42:36 +00:00
SceDateTime time_local;
convertUtcToLocalTime(&time_local, time);
tmzip->tm_sec = time_local.second;
tmzip->tm_min = time_local.minute;
tmzip->tm_hour = time_local.hour;
tmzip->tm_mday = time_local.day;
tmzip->tm_mon = time_local.month;
tmzip->tm_year = time_local.year;
2016-10-15 13:03:26 +00:00
}
2017-02-12 15:27:43 +00:00
static int zipAddFile(zipFile zf, const char *path, int filename_start, int level, FileProcessParam *param) {
2017-10-13 10:42:36 +00:00
int res;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Get file stat
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
res = sceIoGetstat(path, &stat);
if (res < 0)
return res;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Get file local time
zip_fileinfo zi;
memset(&zi, 0, sizeof(zip_fileinfo));
convertToZipTime(&stat.st_mtime, &zi.tmz_date);
2016-10-27 16:38:21 +00:00
2017-10-13 10:42:36 +00:00
// Large file?
int use_zip64 = (stat.st_size >= 0xFFFFFFFF);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Open new file in zip
char filename[MAX_PATH_LENGTH];
strcpy(filename, path+filename_start);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
res = zipOpenNewFileInZip3_64(zf, filename, &zi,
NULL, 0, NULL, 0, NULL,
(level != 0) ? Z_DEFLATED : 0,
level, 0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, use_zip64);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (res < 0)
return res;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Open file to add
SceUID fd = sceIoOpen(path, SCE_O_RDONLY, 0);
if (fd < 0) {
zipCloseFileInZip(zf);
return fd;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Add file to zip
void *buf = malloc(TRANSFER_SIZE);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
uint64_t seek = 0;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
while (1) {
int read = sceIoRead(fd, buf, TRANSFER_SIZE);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (read < 0) {
free(buf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
sceIoClose(fd);
zipCloseFileInZip(zf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
return read;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (read == 0)
break;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
int written = zipWriteInFileInZip(zf, buf, read);
if (written < 0) {
free(buf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
sceIoClose(fd);
zipCloseFileInZip(zf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
return written;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
seek += written;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (param) {
if (param->value)
(*param->value) += read;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (param->cancelHandler && param->cancelHandler()) {
free(buf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
sceIoClose(fd);
zipCloseFileInZip(zf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
return 0;
}
}
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
free(buf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
sceIoClose(fd);
zipCloseFileInZip(zf);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
return 1;
2016-10-15 13:03:26 +00:00
}
2017-02-12 15:27:43 +00:00
static int zipAddFolder(zipFile zf, const char *path, int filename_start, int level, FileProcessParam *param) {
2017-10-13 10:42:36 +00:00
int res;
// Get file stat
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
res = sceIoGetstat(path, &stat);
if (res < 0)
return res;
// Get file local time
zip_fileinfo zi;
memset(&zi, 0, sizeof(zip_fileinfo));
convertToZipTime(&stat.st_mtime, &zi.tmz_date);
// Open new file in zip
char filename[MAX_PATH_LENGTH];
strcpy(filename, path+filename_start);
addEndSlash(filename);
res = zipOpenNewFileInZip3_64(zf, filename, &zi,
NULL, 0, NULL, 0, NULL,
(level != 0) ? Z_DEFLATED : 0,
level, 0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, 0);
if (res < 0)
return res;
if (param) {
if (param->value)
(*param->value)++;
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (param->cancelHandler && param->cancelHandler()) {
zipCloseFileInZip(zf);
return 0;
}
}
zipCloseFileInZip(zf);
return 1;
2016-10-15 14:49:06 +00:00
}
2017-02-12 15:27:43 +00:00
static int zipAddPath(zipFile zf, const char *path, int filename_start, int level, FileProcessParam *param) {
2017-10-13 10:42:36 +00:00
SceUID dfd = sceIoDopen(path);
if (dfd >= 0) {
int ret = zipAddFolder(zf, path, filename_start, level, param);
if (ret <= 0)
return ret;
int res = 0;
do {
SceIoDirent dir;
memset(&dir, 0, sizeof(SceIoDirent));
res = sceIoDread(dfd, &dir);
if (res > 0) {
char *new_path = malloc(strlen(path) + strlen(dir.d_name) + 2);
snprintf(new_path, MAX_PATH_LENGTH, "%s%s%s", path, hasEndSlash(path) ? "" : "/", dir.d_name);
int ret = 0;
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
ret = zipAddPath(zf, new_path, filename_start, level, param);
} else {
ret = zipAddFile(zf, new_path, filename_start, level, param);
}
free(new_path);
// Some folders are protected and return 0x80010001. Bypass them
if (ret <= 0 && ret != 0x80010001) {
sceIoDclose(dfd);
return ret;
}
}
} while (res > 0);
sceIoDclose(dfd);
} else {
return zipAddFile(zf, path, filename_start, level, param);
}
return 1;
2016-10-15 13:03:26 +00:00
}
2017-02-12 15:27:43 +00:00
int makeZip(const char *zip_file, const char *src_path, int filename_start, int level, int append, FileProcessParam *param) {
2017-10-13 10:42:36 +00:00
zipFile zf = zipOpen64(zip_file, append);
if (zf == NULL)
return -1;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
int res = zipAddPath(zf, src_path, filename_start, level, param);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
zipClose(zf, NULL);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
return res;
2016-10-15 13:03:26 +00:00
}
int compress_thread(SceSize args_size, CompressArguments *args) {
2017-10-13 10:42:36 +00:00
SceUID thid = -1;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Lock power timers
powerLock();
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Set progress to 0%
sceMsgDialogProgressBarSetValue(SCE_MSG_DIALOG_PROGRESSBAR_TARGET_BAR_DEFAULT, 0);
sceKernelDelayThread(DIALOG_WAIT); // Needed to see the percentage
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
FileListEntry *file_entry = fileListGetNthEntry(args->file_list, args->index);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
int count = 0;
FileListEntry *head = NULL;
FileListEntry *mark_entry_one = NULL;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (fileListFindEntry(args->mark_list, file_entry->name)) { // On marked entry
count = args->mark_list->length;
head = args->mark_list->head;
} else {
count = 1;
mark_entry_one = malloc(sizeof(FileListEntry));
strcpy(mark_entry_one->name, file_entry->name);
mark_entry_one->type = file_entry->type;
head = mark_entry_one;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
char path[MAX_PATH_LENGTH];
FileListEntry *mark_entry = NULL;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Get paths info
uint64_t size = 0;
uint32_t folders = 0, files = 0;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
mark_entry = head;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
int i;
for (i = 0; i < count; i++) {
snprintf(path, MAX_PATH_LENGTH, "%s%s", args->file_list->path, mark_entry->name);
getPathInfo(path, &size, &folders, &files, NULL);
mark_entry = mark_entry->next;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Check memory card free space
double guessed_size = (double)size * 0.7;
if (checkMemoryCardFreeSpace(args->path, (uint64_t)guessed_size))
goto EXIT;
2016-10-15 14:49:06 +00:00
2017-10-13 10:42:36 +00:00
// Update thread
thid = createStartUpdateThread(size+folders, 1);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Remove process
uint64_t value = 0;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
mark_entry = head;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
for (i = 0; i < count; i++) {
snprintf(path, MAX_PATH_LENGTH, "%s%s", args->file_list->path, mark_entry->name);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
FileProcessParam param;
param.value = &value;
param.max = size;
param.SetProgress = SetProgress;
param.cancelHandler = cancelHandler;
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
int res = makeZip(args->path, path, strlen(args->file_list->path), args->level, i == 0 ? APPEND_STATUS_CREATE : APPEND_STATUS_ADDINZIP, &param);
if (res <= 0) {
closeWaitDialog();
setDialogStep(DIALOG_STEP_CANCELLED);
errorDialog(res);
goto EXIT;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
mark_entry = mark_entry->next;
}
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Set progress to 100%
sceMsgDialogProgressBarSetValue(SCE_MSG_DIALOG_PROGRESSBAR_TARGET_BAR_DEFAULT, 100);
sceKernelDelayThread(COUNTUP_WAIT);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Close
sceMsgDialogClose();
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
setDialogStep(DIALOG_STEP_COMPRESSED);
2016-10-15 13:03:26 +00:00
EXIT:
2017-10-13 10:42:36 +00:00
if (mark_entry_one)
free(mark_entry_one);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
if (thid >= 0)
sceKernelWaitThreadEnd(thid, NULL, NULL);
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
// Unlock power timers
powerUnlock();
2016-10-15 13:03:26 +00:00
2017-10-13 10:42:36 +00:00
return sceKernelExitDeleteThread(0);
2016-10-15 13:03:26 +00:00
}