VitaShell/archive.c

965 lines
24 KiB
C
Raw Normal View History

2016-08-06 06:59:41 +00:00
/*
2017-10-13 10:42:36 +00:00
VitaShell
2017-12-30 10:43:37 +00:00
Copyright (C) 2015-2018, TheFloW
2016-08-06 06:59:41 +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-08-06 06:59:41 +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-08-06 06:59:41 +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-08-06 06:59:41 +00:00
*/
#include "main.h"
2018-09-13 13:12:28 +00:00
#include "browser.h"
2016-08-06 06:59:41 +00:00
#include "archive.h"
2018-01-25 15:48:51 +00:00
#include "psarc.h"
2016-08-06 06:59:41 +00:00
#include "file.h"
#include "utils.h"
#include "elf.h"
2016-08-06 06:59:41 +00:00
2018-01-25 15:48:51 +00:00
static int is_psarc = 0;
2017-12-30 19:35:25 +00:00
static char archive_file[MAX_PATH_LENGTH];
static int archive_path_start = 0;
2017-12-30 19:35:25 +00:00
struct archive *archive_fd = NULL;
static int need_password = 0;
static char password[128];
2016-08-06 06:59:41 +00:00
int checkForUnsafeImports(void *buffer);
char *uncompressBuffer(const Elf32_Ehdr *ehdr, const Elf32_Phdr *phdr, const segment_info *segment,
2017-10-13 10:42:36 +00:00
const char *buffer);
2018-01-01 01:25:51 +00:00
void waitpid() {}
void __archive_create_child() {}
void __archive_check_child() {}
struct archive_data {
2018-01-02 20:56:03 +00:00
char *filename;
SceUID fd;
void *buffer;
int block_size;
};
2018-01-02 19:50:41 +00:00
static const char *file_passphrase(struct archive *a, void *client_data) {
return password;
2018-01-02 19:50:41 +00:00
}
static int file_open(struct archive *a, void *client_data) {
struct archive_data *archive_data = client_data;
archive_data->fd = sceIoOpen(archive_data->filename, SCE_O_RDONLY, 0);
if (archive_data->fd < 0)
return ARCHIVE_FATAL;
2018-01-25 21:22:07 +00:00
archive_data->buffer = memalign(4096, TRANSFER_SIZE);
archive_data->block_size = TRANSFER_SIZE;
return ARCHIVE_OK;
}
static ssize_t file_read(struct archive *a, void *client_data, const void **buff) {
struct archive_data *archive_data = client_data;
*buff = archive_data->buffer;
return sceIoRead(archive_data->fd, archive_data->buffer, archive_data->block_size);
}
static int64_t file_skip(struct archive *a, void *client_data, int64_t request) {
struct archive_data *archive_data = client_data;
int64_t old_offset, new_offset;
if ((old_offset = sceIoLseek(archive_data->fd, 0, SCE_SEEK_CUR)) >= 0 &&
(new_offset = sceIoLseek(archive_data->fd, request, SCE_SEEK_CUR)) >= 0)
return new_offset - old_offset;
return -1;
}
static int64_t file_seek(struct archive *a, void *client_data, int64_t request, int whence) {
struct archive_data *archive_data = client_data;
int64_t r;
r = sceIoLseek(archive_data->fd, request, whence);
if (r >= 0)
return r;
return ARCHIVE_FATAL;
}
static int file_close2(struct archive *a, void *client_data) {
struct archive_data *archive_data = client_data;
if (archive_data->fd >= 0) {
sceIoClose(archive_data->fd);
archive_data->fd = -1;
}
free(archive_data->buffer);
archive_data->buffer = NULL;
return ARCHIVE_OK;
}
static int file_close(struct archive *a, void *client_data) {
struct archive_data *archive_data = client_data;
file_close2(a, client_data);
2018-01-02 20:56:03 +00:00
free(archive_data->filename);
free(archive_data);
return ARCHIVE_OK;
}
static int file_switch(struct archive *a, void *client_data1, void *client_data2) {
file_close2(a, client_data1);
return file_open(a, client_data2);
}
int append_archive(struct archive *a, const char *filename) {
struct archive_data *archive_data = malloc(sizeof(struct archive_data));
if (archive_data) {
archive_data->filename = malloc(strlen(filename) + 1);
strcpy(archive_data->filename, filename);
if (archive_read_append_callback_data(a, archive_data) != ARCHIVE_OK) {
free(archive_data->filename);
free(archive_data);
return ARCHIVE_FATAL;
}
}
return ARCHIVE_OK;
}
2018-01-02 19:50:41 +00:00
struct archive *open_archive(const char *filename) {
struct archive *a = archive_read_new();
if (!a)
return NULL;
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
archive_read_set_passphrase_callback(a, NULL, file_passphrase);
archive_read_set_open_callback(a, file_open);
archive_read_set_read_callback(a, file_read);
archive_read_set_skip_callback(a, file_skip);
archive_read_set_close_callback(a, file_close);
archive_read_set_switch_callback(a, file_switch);
archive_read_set_seek_callback(a, file_seek);
2018-01-02 19:50:41 +00:00
2018-01-02 20:56:03 +00:00
// Get path and name of filename
int type = 0;
2018-01-02 19:50:41 +00:00
2018-01-02 20:56:03 +00:00
char *p = strrchr(filename, '/');
if (!p)
p = strrchr(filename, ':');
if (p) {
char *q = strrchr(p + 1, '.');
if (q) {
char path[MAX_PATH_LENGTH];
char name[MAX_NAME_LENGTH];
char new_path[MAX_PATH_LENGTH];
char format[24];
int part_format = 0;
strncpy(path, filename, p - filename + 1);
path[p - filename + 1] = '\0';
strncpy(name, p + 1, q - (p + 1));
name[q - (p + 1)] = '\0';
// Check for multi volume rar
if (strcasecmp(q, ".rar") == 0) {
// Check for .partXXXX.rar archives
q = strrchr(name, '.');
if (q) {
if (strncasecmp(q + 1, "part", 4) == 0) {
name[q - name] = '\0';
for (part_format = 0; part_format < 4; part_format++) {
strcpy(format, "%s%s.part%0Xd.rar");
format[11] = '1' + part_format;
2018-08-27 09:56:01 +00:00
snprintf(new_path, MAX_PATH_LENGTH, format, path, name, 1);
if (checkFileExist(new_path)) {
type = 1;
break;
}
}
}
}
// Check for .rXX archives
if (type == 0) {
2018-08-27 09:56:01 +00:00
snprintf(new_path, MAX_PATH_LENGTH, "%s%s.r00", path, name);
if (checkFileExist(new_path)) {
strcpy(format, "%s%s.r%02d");
type = 2;
}
}
if (type != 0) {
int num = 1;
if (type == 2) {
num = 0; // this type begins with .r00
// Append .rar as first archive
if (append_archive(a, filename) != ARCHIVE_OK) {
archive_read_free(a);
return NULL;
}
}
// Append other parts
while (1) {
2018-08-27 09:56:01 +00:00
snprintf(new_path, MAX_PATH_LENGTH, format, path, name, num);
if (!checkFileExist(new_path))
break;
if (append_archive(a, new_path) != ARCHIVE_OK) {
archive_read_free(a);
return NULL;
}
num++;
}
}
}
2018-01-02 20:56:03 +00:00
}
}
// Single volume
2018-01-02 20:56:03 +00:00
if (type == 0) {
if (append_archive(a, filename) != ARCHIVE_OK) {
archive_read_free(a);
return NULL;
}
2018-01-02 20:56:03 +00:00
}
// Open archive
2018-01-02 20:56:03 +00:00
if (archive_read_open1(a)) {
archive_read_free(a);
2018-01-02 19:50:41 +00:00
return NULL;
2018-01-02 20:56:03 +00:00
}
2018-01-02 19:50:41 +00:00
return a;
}
2018-01-01 01:25:51 +00:00
typedef struct ArchiveFileNode {
struct ArchiveFileNode *child;
struct ArchiveFileNode *next;
2018-01-02 19:50:41 +00:00
char *name;
SceIoStat stat;
2018-01-01 01:25:51 +00:00
} ArchiveFileNode;
static ArchiveFileNode *archive_root = NULL;
char *serializePathName(char *name, char **p) {
if (!p)
return name;
2018-01-02 19:50:41 +00:00
if (*p) {
**p = '/'; // restore
2018-01-01 01:25:51 +00:00
name = *p + 1;
2018-01-02 19:50:41 +00:00
}
2018-01-01 01:25:51 +00:00
*p = strchr(name, '/');
if (*p)
**p = '\0';
return name;
}
ArchiveFileNode *createArchiveNode(const char *name, SceIoStat *stat) {
2018-01-01 01:25:51 +00:00
ArchiveFileNode *node = malloc(sizeof(ArchiveFileNode));
if (!node)
return NULL;
memset(node, 0, sizeof(ArchiveFileNode));
2018-01-02 19:50:41 +00:00
node->name = malloc(strlen(name) + 1);
if (!node->name) {
free(node);
return NULL;
}
strcpy(node->name, name);
2018-01-01 01:25:51 +00:00
node->child = NULL;
node->next = NULL;
2018-01-02 19:50:41 +00:00
memcpy(&node->stat, stat, sizeof(SceIoStat));
2018-01-01 01:25:51 +00:00
return node;
}
ArchiveFileNode *_findArchiveNode(ArchiveFileNode *parent, char *name, ArchiveFileNode **parent_out,
ArchiveFileNode **prev_out, char **name_out, char **p_out) {
char *p = NULL;
ArchiveFileNode *prev = NULL;
// Remove trailing slash
removeEndSlash(name);
// Serialize path name
name = serializePathName(name, &p);
// Root
if (name[0] == '\0')
return parent;
// Traverse
ArchiveFileNode *curr = parent->child;
while (curr) {
// Found name
if (strcasecmp(curr->name, name) == 0) {
2018-01-01 01:25:51 +00:00
// Found node
if (!p)
return curr;
// Is folder
if (SCE_S_ISDIR(curr->stat.st_mode)) {
2018-01-01 01:25:51 +00:00
// Serialize path name
name = serializePathName(name, &p);
// Get child entry of this directory
parent = curr;
curr = curr->child;
continue;
}
}
// Get next entry in this directory
prev = curr;
curr = curr->next;
}
// Out
if (parent_out)
*parent_out = parent;
if (prev_out)
*prev_out = prev;
if (name_out)
*name_out = name;
if (p_out)
*p_out = p;
return NULL;
}
ArchiveFileNode *findArchiveNode(const char *path) {
char name[MAX_PATH_LENGTH];
strcpy(name, path);
return _findArchiveNode(archive_root, name, NULL, NULL, NULL, NULL);
}
int addArchiveNodeRecursive(ArchiveFileNode *parent, char *name, SceIoStat *stat) {
2018-01-01 01:25:51 +00:00
char *p = NULL;
ArchiveFileNode *prev = NULL;
if (!parent)
2018-08-27 07:54:46 +00:00
return VITASHELL_ERROR_ILLEGAL_ADDR;
2018-01-01 01:25:51 +00:00
ArchiveFileNode *res = _findArchiveNode(parent, name, &parent, &prev, &name, &p);
// Already exist
if (res) {
memcpy(&res->stat, stat, sizeof(SceIoStat));
2018-01-01 01:25:51 +00:00
return 0;
}
2018-01-01 01:25:51 +00:00
// Create new node
SceIoStat node_stat;
memcpy(&node_stat, stat, sizeof(SceIoStat));
node_stat.st_mode = SCE_S_IFDIR;
node_stat.st_size = 0;
ArchiveFileNode *node = createArchiveNode(name, p ? &node_stat : stat);
2018-01-01 01:25:51 +00:00
if (!parent->child) { // First child
parent->child = node;
} else { // Neighbour
prev->next = node;
}
// Recursion
if (p)
addArchiveNodeRecursive(node, p + 1, stat);
return 0;
}
void addArchiveNode(const char *path, SceIoStat *stat) {
2018-01-01 01:25:51 +00:00
char name[MAX_PATH_LENGTH];
strcpy(name, path);
addArchiveNodeRecursive(archive_root, name, stat);
}
void freeArchiveNodes(ArchiveFileNode *curr) {
// Traverse
while (curr) {
// Enter directory
if (SCE_S_ISDIR(curr->stat.st_mode)) {
2018-01-01 01:25:51 +00:00
// Traverse child entry of this directory
freeArchiveNodes(curr->child);
}
// Get next entry in this directory
ArchiveFileNode *next = curr->next;
2018-01-02 19:50:41 +00:00
free(curr->name);
2018-01-01 01:25:51 +00:00
free(curr);
curr = next;
}
}
2018-01-02 19:50:41 +00:00
int archiveCheckFilesForUnsafeFself() {
2018-01-01 01:25:51 +00:00
// Open archive file
2018-01-02 19:50:41 +00:00
struct archive *archive = open_archive(archive_file);
if (!archive)
2018-01-01 01:25:51 +00:00
return 0;
// Traverse
while (1) {
struct archive_entry *archive_entry;
2018-01-02 19:50:41 +00:00
int res = archive_read_next_header(archive, &archive_entry);
2018-01-01 01:25:51 +00:00
if (res == ARCHIVE_EOF)
break;
if (res != ARCHIVE_OK) {
archive_read_free(archive);
return 0;
}
// Get entry information
const char *name = archive_entry_pathname(archive_entry);
const struct stat *stat = archive_entry_stat(archive_entry);
// Read magic
uint32_t magic = 0;
archive_read_data(archive, &magic, sizeof(uint32_t));
// SCE magic
if (magic == 0x00454353) {
char sce_header[0x84];
archive_read_data(archive, sce_header, sizeof(sce_header));
uint64_t elf1_offset = *(uint64_t *)(sce_header + 0x3C);
uint64_t phdr_offset = *(uint64_t *)(sce_header + 0x44);
uint64_t section_info_offset = *(uint64_t *)(sce_header + 0x54);
// jump to elf1
// Until here we have read 0x88 bytes
int i;
for (i = 0; i < elf1_offset - 0x88; i += sizeof(uint32_t)) {
uint32_t dummy = 0;
archive_read_data(archive, &dummy, sizeof(uint32_t));
}
2017-12-30 22:08:04 +00:00
2018-01-01 01:25:51 +00:00
// Check imports
char *buffer = malloc(stat->st_size);
if (buffer) {
archive_read_data(archive, buffer, stat->st_size);
2017-12-30 22:08:04 +00:00
2018-01-01 01:25:51 +00:00
Elf32_Ehdr *elf1 = (Elf32_Ehdr*)buffer;
Elf32_Phdr *phdr = (Elf32_Phdr*)(buffer + phdr_offset - elf1_offset);
segment_info *info = (segment_info*)(buffer + section_info_offset - elf1_offset);
// segment is elf2 section
char *segment = buffer + info->offset - elf1_offset;
// zlib compress magic
char *uncompressed_buffer = NULL;
if (segment[0] == 0x78) {
// uncompressedBuffer will return elf2 section
uncompressed_buffer = uncompressBuffer(elf1, phdr, info, segment);
if (uncompressed_buffer) {
segment = uncompressed_buffer;
2017-12-30 22:08:04 +00:00
}
}
2018-01-01 01:25:51 +00:00
int unsafe = checkForUnsafeImports(segment);
2017-12-30 22:08:04 +00:00
2018-01-01 01:25:51 +00:00
if (uncompressed_buffer)
free(uncompressed_buffer);
free(buffer);
if (unsafe) {
archive_read_free(archive);
return unsafe;
2017-12-30 22:08:04 +00:00
}
}
2018-01-01 01:25:51 +00:00
// Check authid flag
uint64_t authid = *(uint64_t *)(sce_header + 0x7C);
if (authid != 0x2F00000000000002) {
archive_read_free(archive);
return 1; // Unsafe
}
2017-12-30 22:08:04 +00:00
}
}
2018-01-01 01:25:51 +00:00
archive_read_free(archive);
return 0;
}
2017-02-12 15:27:43 +00:00
int fileListGetArchiveEntries(FileList *list, const char *path, int sort) {
2018-01-25 15:48:51 +00:00
if (is_psarc)
return fileListGetPsarcEntries(list, path, sort);
2017-10-13 10:42:36 +00:00
int res;
2017-12-30 19:35:25 +00:00
if (!list)
2018-08-27 07:54:46 +00:00
return VITASHELL_ERROR_ILLEGAL_ADDR;
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
FileListEntry *entry = malloc(sizeof(FileListEntry));
2018-01-02 19:50:41 +00:00
if (entry) {
2018-01-03 21:37:11 +00:00
entry->name_length = strlen(DIR_UP);
entry->name = malloc(entry->name_length + 1);
2018-01-02 19:50:41 +00:00
strcpy(entry->name, DIR_UP);
entry->is_folder = 1;
entry->is_symlink = 0;
2018-01-02 19:50:41 +00:00
entry->type = FILE_TYPE_UNKNOWN;
fileListAddEntry(list, entry, sort);
}
2018-01-01 01:25:51 +00:00
// Traverse
ArchiveFileNode *curr = findArchiveNode(path + archive_path_start);
if (curr)
curr = curr->child;
while (curr) {
FileListEntry *entry = malloc(sizeof(FileListEntry));
2018-01-02 19:50:41 +00:00
if (entry) {
entry->is_symlink = 0;
entry->is_folder = SCE_S_ISDIR(curr->stat.st_mode);
2018-01-02 19:50:41 +00:00
if (entry->is_folder) {
2018-01-03 21:37:11 +00:00
entry->name_length = strlen(curr->name) + 1;
entry->name = malloc(entry->name_length + 1);
strcpy(entry->name, curr->name);
2018-01-02 19:50:41 +00:00
addEndSlash(entry->name);
entry->type = FILE_TYPE_UNKNOWN;
list->folders++;
} else {
2018-01-03 21:37:11 +00:00
entry->name_length = strlen(curr->name);
entry->name = malloc(entry->name_length + 1);
strcpy(entry->name, curr->name);
2018-01-02 19:50:41 +00:00
entry->type = getFileType(entry->name);
list->files++;
}
2016-08-06 06:59:41 +00:00
entry->size = curr->stat.st_size;
2018-01-02 19:50:41 +00:00
memcpy(&entry->ctime, (SceDateTime *)&curr->stat.st_ctime, sizeof(SceDateTime));
memcpy(&entry->mtime, (SceDateTime *)&curr->stat.st_mtime, sizeof(SceDateTime));
memcpy(&entry->atime, (SceDateTime *)&curr->stat.st_atime, sizeof(SceDateTime));
2018-01-02 19:50:41 +00:00
fileListAddEntry(list, entry, sort);
2017-10-13 10:42:36 +00:00
}
2018-01-01 01:25:51 +00:00
// Get next entry in this directory
curr = curr->next;
2017-10-13 10:42:36 +00:00
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
return 0;
2016-08-06 06:59:41 +00:00
}
int getArchivePathInfo(const char *path, uint64_t *size, uint32_t *folders,
uint32_t *files, int (* handler)(const char *path)) {
2018-01-25 15:48:51 +00:00
if (is_psarc)
return getPsarcPathInfo(path, size, folders, files, handler);
2017-10-13 10:42:36 +00:00
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
2018-01-01 11:11:02 +00:00
int res = archiveFileGetstat(path, &stat);
if (res < 0)
return res;
if (SCE_S_ISDIR(stat.st_mode)) {
2017-10-13 10:42:36 +00:00
FileList list;
memset(&list, 0, sizeof(FileList));
fileListGetArchiveEntries(&list, path, SORT_NONE);
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
FileListEntry *entry = list.head->next; // Ignore ..
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
int i;
2018-01-25 10:11:08 +00:00
for (i = 0; i < list.length - 1; i++, entry = entry->next) {
2017-10-13 10:42:36 +00:00
char *new_path = malloc(strlen(path) + strlen(entry->name) + 2);
2018-08-27 09:56:01 +00:00
snprintf(new_path, MAX_PATH_LENGTH, "%s%s", path, entry->name);
2018-01-25 10:11:08 +00:00
if (handler && handler(new_path)) {
free(new_path);
continue;
}
2018-01-25 15:48:51 +00:00
if (entry->is_folder) {
int ret = getArchivePathInfo(new_path, size, folders, files, handler);
if (ret <= 0) {
free(new_path);
fileListEmpty(&list);
return ret;
}
} else {
if (size)
(*size) += entry->size;
if (files)
(*files)++;
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
free(new_path);
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
if (folders)
(*folders)++;
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
fileListEmpty(&list);
} else {
2018-01-25 10:11:08 +00:00
if (handler && handler(path))
return 1;
2017-10-13 10:42:36 +00:00
if (size)
(*size) += stat.st_size;
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
if (files)
(*files)++;
}
2016-08-06 06:59:41 +00:00
2018-01-27 22:06:38 +00:00
return 1;
2016-08-06 06:59:41 +00:00
}
2018-01-25 15:48:51 +00:00
int extractArchiveFile(const char *src_path, const char *dst_path, FileProcessParam *param) {
SceUID fdsrc = archiveFileOpen(src_path, SCE_O_RDONLY, 0);
if (fdsrc < 0)
return fdsrc;
SceUID fddst = sceIoOpen(dst_path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
if (fddst < 0) {
psarcFileClose(fdsrc);
return fddst;
}
2018-01-25 21:22:07 +00:00
void *buf = memalign(4096, TRANSFER_SIZE);
2018-01-25 15:48:51 +00:00
while (1) {
int read = archiveFileRead(fdsrc, buf, TRANSFER_SIZE);
if (read < 0) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
sceIoRemove(dst_path);
return read;
}
if (read == 0)
break;
int written = sceIoWrite(fddst, buf, read);
if (written < 0) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
sceIoRemove(dst_path);
return written;
}
if (param) {
if (param->value)
(*param->value) += read;
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (param->cancelHandler && param->cancelHandler()) {
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
sceIoRemove(dst_path);
return 0;
}
}
}
free(buf);
sceIoClose(fddst);
archiveFileClose(fdsrc);
return 1;
}
int extractArchivePath(const char *src_path, const char *dst_path, FileProcessParam *param) {
if (is_psarc)
return extractPsarcPath(src_path, dst_path, param);
2017-10-13 10:42:36 +00:00
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
2018-01-01 11:11:02 +00:00
2018-01-25 15:48:51 +00:00
int res = archiveFileGetstat(src_path, &stat);
2018-01-01 11:11:02 +00:00
if (res < 0)
return res;
if (SCE_S_ISDIR(stat.st_mode)) {
2017-10-13 10:42:36 +00:00
FileList list;
memset(&list, 0, sizeof(FileList));
2018-01-25 15:48:51 +00:00
fileListGetArchiveEntries(&list, src_path, SORT_NONE);
2016-08-06 06:59:41 +00:00
2018-01-25 15:48:51 +00:00
int ret = sceIoMkdir(dst_path, 0777);
2017-10-13 10:42:36 +00:00
if (ret < 0 && ret != SCE_ERROR_ERRNO_EEXIST) {
fileListEmpty(&list);
return ret;
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
if (param) {
if (param->value)
(*param->value) += DIRECTORY_SIZE;
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
if (param->SetProgress)
param->SetProgress(param->value ? *param->value : 0, param->max);
if (param->cancelHandler && param->cancelHandler()) {
fileListEmpty(&list);
return 0;
}
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
FileListEntry *entry = list.head->next; // Ignore ..
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
int i;
2018-01-25 10:11:08 +00:00
for (i = 0; i < list.length - 1; i++, entry = entry->next) {
2018-01-25 15:48:51 +00:00
char *new_src_path = malloc(strlen(src_path) + strlen(entry->name) + 2);
2018-08-27 09:56:01 +00:00
snprintf(new_src_path, MAX_PATH_LENGTH, "%s%s", src_path, entry->name);
2016-08-06 06:59:41 +00:00
2018-01-25 15:48:51 +00:00
char *new_dst_path = malloc(strlen(dst_path) + strlen(entry->name) + 2);
2018-08-27 09:56:01 +00:00
snprintf(new_dst_path, MAX_PATH_LENGTH, "%s%s", dst_path, entry->name);
2016-08-06 06:59:41 +00:00
2018-01-25 15:48:51 +00:00
int ret = 0;
if (entry->is_folder) {
ret = extractArchivePath(new_src_path, new_dst_path, param);
} else {
ret = extractArchiveFile(new_src_path, new_dst_path, param);
}
free(new_dst_path);
free(new_src_path);
2017-10-13 10:42:36 +00:00
if (ret <= 0) {
fileListEmpty(&list);
return ret;
}
}
2016-08-24 18:13:06 +00:00
2017-10-13 10:42:36 +00:00
fileListEmpty(&list);
} else {
2018-01-25 15:48:51 +00:00
return extractArchiveFile(src_path, dst_path, param);
2017-10-13 10:42:36 +00:00
}
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
return 1;
2016-08-06 06:59:41 +00:00
}
int archiveFileGetstat(const char *file, SceIoStat *stat) {
2018-01-25 15:48:51 +00:00
if (is_psarc)
return psarcFileGetstat(file, stat);
2018-01-01 01:25:51 +00:00
ArchiveFileNode *node = findArchiveNode(file + archive_path_start);
if (!node)
2018-08-27 07:54:46 +00:00
return VITASHELL_ERROR_ILLEGAL_ADDR;
2018-01-01 01:25:51 +00:00
if (stat)
memcpy(stat, &node->stat, sizeof(SceIoStat));
2018-01-01 01:25:51 +00:00
return 0;
2016-08-06 06:59:41 +00:00
}
2018-01-25 15:48:51 +00:00
int archiveFileOpen(const char *file, int flags, SceMode mode) {
if (is_psarc)
return psarcFileOpen(file, flags, mode);
2017-12-30 19:35:25 +00:00
// A file is already open
if (archive_fd)
return -1;
2016-08-06 06:59:41 +00:00
2017-12-30 19:35:25 +00:00
// Open archive file
2018-01-02 19:50:41 +00:00
archive_fd = open_archive(archive_file);
if (!archive_fd)
2017-12-30 19:35:25 +00:00
return -1;
2018-01-01 01:25:51 +00:00
// Traverse
2017-12-30 19:35:25 +00:00
while (1) {
struct archive_entry *archive_entry;
2018-01-02 19:50:41 +00:00
int res = archive_read_next_header(archive_fd, &archive_entry);
2017-12-30 19:35:25 +00:00
if (res == ARCHIVE_EOF)
break;
if (res != ARCHIVE_OK) {
archive_read_free(archive_fd);
return -1;
}
2018-01-01 01:25:51 +00:00
// Compare pathname
2017-12-30 19:35:25 +00:00
const char *name = archive_entry_pathname(archive_entry);
if (strcasecmp(name, file + archive_path_start) == 0) {
2017-10-13 10:42:36 +00:00
return ARCHIVE_FD;
}
}
2016-08-06 06:59:41 +00:00
2017-12-30 19:35:25 +00:00
archive_read_free(archive_fd);
archive_fd = NULL;
2017-10-13 10:42:36 +00:00
return -1;
2016-08-06 06:59:41 +00:00
}
int archiveFileRead(SceUID fd, void *data, SceSize size) {
2018-01-25 15:48:51 +00:00
if (is_psarc)
return psarcFileRead(fd, data, size);
2017-12-30 19:35:25 +00:00
if (!archive_fd || fd != ARCHIVE_FD)
2017-10-13 10:42:36 +00:00
return -1;
2016-08-06 06:59:41 +00:00
2017-12-30 19:35:25 +00:00
return archive_read_data(archive_fd, data, size);
2016-08-06 06:59:41 +00:00
}
int archiveFileClose(SceUID fd) {
2018-01-25 15:48:51 +00:00
if (is_psarc)
return psarcFileClose(fd);
2017-12-30 19:35:25 +00:00
if (!archive_fd || fd != ARCHIVE_FD)
2017-10-13 10:42:36 +00:00
return -1;
2016-08-06 06:59:41 +00:00
2017-12-30 19:35:25 +00:00
archive_read_free(archive_fd);
archive_fd = NULL;
return 0;
2016-08-06 06:59:41 +00:00
}
2017-02-12 15:27:43 +00:00
int ReadArchiveFile(const char *file, void *buf, int size) {
2017-10-13 10:42:36 +00:00
SceUID fd = archiveFileOpen(file, SCE_O_RDONLY, 0);
if (fd < 0)
return fd;
2016-08-06 06:59:41 +00:00
2017-10-13 10:42:36 +00:00
int read = archiveFileRead(fd, buf, size);
archiveFileClose(fd);
return read;
2016-08-06 06:59:41 +00:00
}
int archiveNeedPassword() {
return need_password;
}
void archiveClearPassword() {
memset(password, 0, sizeof(password));
}
void archiveSetPassword(char *string) {
strncpy(password, string, sizeof(password) - 1);
password[sizeof(password) - 1] = '\0';
}
int archiveClose() {
2018-01-25 15:48:51 +00:00
if (is_psarc)
return psarcClose();
2018-01-01 11:11:02 +00:00
freeArchiveNodes(archive_root);
2017-10-13 10:42:36 +00:00
return 0;
2016-08-06 06:59:41 +00:00
}
SceMode convert_stat_mode(mode_t mode) {
SceMode sce_mode = 0;
if (mode & S_IFDIR)
sce_mode |= SCE_S_IFDIR;
if (mode & S_IFREG)
sce_mode |= SCE_S_IFREG;
return sce_mode;
}
2017-02-12 15:27:43 +00:00
int archiveOpen(const char *file) {
2018-01-25 15:48:51 +00:00
// Read magic
uint32_t magic;
int read = ReadFile(file, &magic, sizeof(uint32_t));
if (read < 0)
return read;
// PSARC file
is_psarc = 0;
if (magic == 0x52415350) {
is_psarc = 1;
return psarcOpen(file);
}
2017-10-13 10:42:36 +00:00
// Start position of the archive path
archive_path_start = strlen(file) + 1;
2017-12-30 19:35:25 +00:00
strcpy(archive_file, file);
// Open archive file
2018-01-02 19:50:41 +00:00
struct archive *archive = open_archive(file);
if (!archive)
2017-12-30 19:35:25 +00:00
return -1;
// Need password?
need_password = 0;
if (archive_read_has_encrypted_entries(archive) == 1)
need_password = 1;
2018-01-01 01:25:51 +00:00
// Create archive root
SceIoStat root_stat;
memset(&root_stat, 0, sizeof(SceIoStat));
root_stat.st_mode = SCE_S_IFDIR;
archive_root = createArchiveNode("/", &root_stat);
2018-01-01 01:25:51 +00:00
// Traverse
2017-12-30 19:35:25 +00:00
while (1) {
struct archive_entry *archive_entry;
2018-01-02 19:50:41 +00:00
int res = archive_read_next_header(archive, &archive_entry);
2017-12-30 19:35:25 +00:00
if (res == ARCHIVE_EOF)
break;
if (res != ARCHIVE_OK) {
archive_read_free(archive);
return -1;
}
2017-12-30 22:08:04 +00:00
// // Need password?
if (archive_entry_is_encrypted(archive_entry))
need_password = 1;
2018-01-01 01:25:51 +00:00
// Get entry information
2017-12-30 19:35:25 +00:00
const char *name = archive_entry_pathname(archive_entry);
// File stat
SceIoStat stat;
memset(&stat, 0, sizeof(SceIoStat));
stat.st_mode = convert_stat_mode(archive_entry_mode(archive_entry));
stat.st_size = archive_entry_size(archive_entry);
SceDateTime time;
sceRtcSetTime_t(&time, archive_entry_ctime(archive_entry));
convertLocalTimeToUtc(&stat.st_ctime, &time);
sceRtcSetTime_t(&time, archive_entry_mtime(archive_entry));
convertLocalTimeToUtc(&stat.st_mtime, &time);
sceRtcSetTime_t(&time, archive_entry_atime(archive_entry));
convertLocalTimeToUtc(&stat.st_atime, &time);
2017-12-30 19:35:25 +00:00
2018-01-01 01:25:51 +00:00
// Add node
addArchiveNode(name, &stat);
2017-10-13 10:42:36 +00:00
}
2016-08-06 06:59:41 +00:00
2017-12-30 19:35:25 +00:00
archive_read_free(archive);
2017-10-13 10:42:36 +00:00
return 0;
}