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"
|
2017-02-13 20:07:09 +00:00
|
|
|
#include "init.h"
|
2016-08-06 06:59:41 +00:00
|
|
|
#include "archive.h"
|
|
|
|
#include "file.h"
|
|
|
|
#include "utils.h"
|
2016-09-06 23:52:33 +00:00
|
|
|
#include "sha1.h"
|
2018-01-25 10:11:08 +00:00
|
|
|
#include "strnatcmp.h"
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2016-11-01 15:34:15 +00:00
|
|
|
static char *devices[] = {
|
2018-09-13 10:00:30 +00:00
|
|
|
"gro0:",
|
|
|
|
"grw0:",
|
|
|
|
"imc0:",
|
|
|
|
"os0:",
|
|
|
|
"pd0:",
|
|
|
|
"sa0:",
|
|
|
|
"sd0:",
|
|
|
|
"tm0:",
|
|
|
|
"ud0:",
|
|
|
|
"uma0:",
|
|
|
|
"ur0:",
|
|
|
|
"ux0:",
|
|
|
|
"vd0:",
|
|
|
|
"vs0:",
|
|
|
|
"xmc0:",
|
|
|
|
"host0:",
|
2016-08-06 06:59:41 +00:00
|
|
|
};
|
|
|
|
|
2017-02-26 21:02:29 +00:00
|
|
|
#define N_DEVICES (sizeof(devices) / sizeof(char **))
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2018-09-13 10:00:30 +00:00
|
|
|
const char symlink_header_bytes[SYMLINK_HEADER_SIZE] = {0xF1, 0x1E, 0x00, 0x00};
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int allocateReadFile(const char *file, void **buffer) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fd = sceIoOpen(file, SCE_O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2016-09-05 20:05:05 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int size = sceIoLseek32(fd, 0, SCE_SEEK_END);
|
|
|
|
sceIoLseek32(fd, 0, SCE_SEEK_SET);
|
2016-09-05 20:05:05 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
*buffer = malloc(size);
|
|
|
|
if (!*buffer) {
|
|
|
|
sceIoClose(fd);
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_NO_MEMORY;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
2016-09-05 20:05:05 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int read = sceIoRead(fd, *buffer, size);
|
|
|
|
sceIoClose(fd);
|
2016-09-05 20:05:05 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return read;
|
2016-09-05 20:05:05 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int ReadFile(const char *file, void *buf, int size) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fd = sceIoOpen(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 = sceIoRead(fd, buf, size);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fd);
|
|
|
|
return read;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int WriteFile(const char *file, const void *buf, int size) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fd = sceIoOpen(file, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int written = sceIoWrite(fd, buf, size);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fd);
|
|
|
|
return written;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-13 10:06:23 +00:00
|
|
|
int getFileSize(const char *file) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fd = sceIoOpen(file, SCE_O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
2016-09-03 22:08:22 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int fileSize = sceIoLseek(fd, 0, SCE_SEEK_END);
|
2017-09-16 20:34:06 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fd);
|
|
|
|
return fileSize;
|
2016-08-08 21:31:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 14:27:16 +00:00
|
|
|
int checkFileExist(const char *file) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fd = sceIoOpen(file, SCE_O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
2017-09-16 11:40:19 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fd);
|
|
|
|
return 1;
|
2017-09-16 11:40:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int checkFolderExist(const char *folder) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID dfd = sceIoDopen(folder);
|
|
|
|
if (dfd < 0)
|
|
|
|
return 0;
|
2017-03-05 14:27:16 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoDclose(dfd);
|
|
|
|
return 1;
|
2017-03-05 14:27:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int getFileSha1(const char *file, uint8_t *pSha1Out, FileProcessParam *param) {
|
2017-10-13 10:42:36 +00:00
|
|
|
// Set up SHA1 context
|
|
|
|
SHA1_CTX ctx;
|
|
|
|
sha1_init(&ctx);
|
|
|
|
|
|
|
|
// Open the file to read, else return the error
|
|
|
|
SceUID fd = sceIoOpen(file, SCE_O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
// Open up the buffer for copying data into
|
2018-01-25 21:22:07 +00:00
|
|
|
void *buf = memalign(4096, TRANSFER_SIZE);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
// Actually take the SHA1 sum
|
|
|
|
while (1) {
|
|
|
|
int read = sceIoRead(fd, buf, TRANSFER_SIZE);
|
|
|
|
|
|
|
|
if (read < 0) {
|
|
|
|
free(buf);
|
|
|
|
sceIoClose(fd);
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sha1_update(&ctx, buf, read);
|
|
|
|
|
|
|
|
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 (param->SetProgress)
|
|
|
|
param->SetProgress(param->value ? *param->value : 0, param->max);
|
|
|
|
|
2018-01-28 10:28:02 +00:00
|
|
|
// Check to see if param->cancelHandler exists, if so call it and free memory if canceled
|
2017-10-13 10:42:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Final iteration of SHA1 sum, dump final value into pSha1Out buffer
|
|
|
|
sha1_final(&ctx, pSha1Out);
|
|
|
|
|
|
|
|
// Free up file buffer
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
// Close file proper
|
|
|
|
sceIoClose(fd);
|
|
|
|
return 1;
|
2016-09-06 23:52:33 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 15:34:44 +00:00
|
|
|
int getPathInfo(const char *path, uint64_t *size, uint32_t *folders,
|
|
|
|
uint32_t *files, int (* handler)(const char *path)) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID dfd = sceIoDopen(path);
|
|
|
|
if (dfd >= 0) {
|
|
|
|
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);
|
2018-08-27 09:56:01 +00:00
|
|
|
snprintf(new_path, MAX_PATH_LENGTH, "%s%s%s", path, hasEndSlash(path) ? "" : "/", dir.d_name);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
if (handler && handler(new_path)) {
|
|
|
|
free(new_path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
|
|
|
|
int ret = getPathInfo(new_path, size, folders, files, handler);
|
|
|
|
if (ret <= 0) {
|
|
|
|
free(new_path);
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (size)
|
|
|
|
(*size) += dir.d_stat.st_size;
|
|
|
|
|
|
|
|
if (files)
|
|
|
|
(*files)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(new_path);
|
|
|
|
}
|
|
|
|
} while (res > 0);
|
|
|
|
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
|
|
|
|
if (folders)
|
|
|
|
(*folders)++;
|
|
|
|
} else {
|
|
|
|
if (handler && handler(path))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
SceIoStat stat;
|
|
|
|
memset(&stat, 0, sizeof(SceIoStat));
|
|
|
|
|
|
|
|
int res = sceIoGetstat(path, &stat);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
(*size) += stat.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files)
|
|
|
|
(*files)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int removePath(const char *path, FileProcessParam *param) {
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID dfd = sceIoDopen(path);
|
|
|
|
if (dfd >= 0) {
|
|
|
|
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);
|
2018-08-27 09:56:01 +00:00
|
|
|
snprintf(new_path, MAX_PATH_LENGTH, "%s%s%s", path, hasEndSlash(path) ? "" : "/", dir.d_name);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
|
|
|
|
int ret = removePath(new_path, param);
|
|
|
|
if (ret <= 0) {
|
|
|
|
free(new_path);
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int ret = sceIoRemove(new_path);
|
|
|
|
if (ret < 0) {
|
|
|
|
free(new_path);
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param) {
|
|
|
|
if (param->value)
|
|
|
|
(*param->value)++;
|
|
|
|
|
|
|
|
if (param->SetProgress)
|
|
|
|
param->SetProgress(param->value ? *param->value : 0, param->max);
|
|
|
|
|
|
|
|
if (param->cancelHandler && param->cancelHandler()) {
|
|
|
|
free(new_path);
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(new_path);
|
|
|
|
}
|
|
|
|
} while (res > 0);
|
|
|
|
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
|
|
|
|
int ret = sceIoRmdir(path);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (param) {
|
|
|
|
if (param->value)
|
|
|
|
(*param->value)++;
|
|
|
|
|
|
|
|
if (param->SetProgress)
|
|
|
|
param->SetProgress(param->value ? *param->value : 0, param->max);
|
|
|
|
|
|
|
|
if (param->cancelHandler && param->cancelHandler()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int ret = sceIoRemove(path);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (param) {
|
|
|
|
if (param->value)
|
|
|
|
(*param->value)++;
|
|
|
|
|
|
|
|
if (param->SetProgress)
|
|
|
|
param->SetProgress(param->value ? *param->value : 0, param->max);
|
|
|
|
|
|
|
|
if (param->cancelHandler && param->cancelHandler()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int copyFile(const char *src_path, const char *dst_path, FileProcessParam *param) {
|
2017-10-13 10:42:36 +00:00
|
|
|
// The source and destination paths are identical
|
|
|
|
if (strcasecmp(src_path, dst_path) == 0) {
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_SRC_AND_DST_IDENTICAL;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
// The destination is a subfolder of the source folder
|
|
|
|
int len = strlen(src_path);
|
2018-01-03 21:37:11 +00:00
|
|
|
if (strncasecmp(src_path, dst_path, len) == 0 && (dst_path[len] == '/' || dst_path[len - 1] == '/')) {
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_DST_IS_SUBFOLDER_OF_SRC;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fdsrc = sceIoOpen(src_path, SCE_O_RDONLY, 0);
|
|
|
|
if (fdsrc < 0)
|
|
|
|
return fdsrc;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID fddst = sceIoOpen(dst_path, SCE_O_WRONLY | SCE_O_CREAT | SCE_O_TRUNC, 0777);
|
|
|
|
if (fddst < 0) {
|
|
|
|
sceIoClose(fdsrc);
|
|
|
|
return fddst;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2018-01-25 21:22:07 +00:00
|
|
|
void *buf = memalign(4096, TRANSFER_SIZE);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
while (1) {
|
|
|
|
int read = sceIoRead(fdsrc, buf, TRANSFER_SIZE);
|
2016-09-12 18:36:07 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (read < 0) {
|
|
|
|
free(buf);
|
2016-08-21 12:30:40 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fddst);
|
|
|
|
sceIoClose(fdsrc);
|
2016-08-21 12:30:40 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoRemove(dst_path);
|
2017-03-05 17:31:21 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return read;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (read == 0)
|
|
|
|
break;
|
2016-09-12 18:36:07 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int written = sceIoWrite(fddst, buf, read);
|
2016-09-12 18:36:07 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (written < 0) {
|
|
|
|
free(buf);
|
2016-09-12 18:36:07 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fddst);
|
|
|
|
sceIoClose(fdsrc);
|
2016-09-12 18:36:07 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoRemove(dst_path);
|
2017-03-05 17:31:21 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return written;
|
|
|
|
}
|
2016-09-12 18:36:07 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (param) {
|
|
|
|
if (param->value)
|
|
|
|
(*param->value) += read;
|
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);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (param->cancelHandler && param->cancelHandler()) {
|
|
|
|
free(buf);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fddst);
|
|
|
|
sceIoClose(fdsrc);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoRemove(dst_path);
|
2017-03-05 17:31:21 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
free(buf);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
// Inherit file stat
|
|
|
|
SceIoStat stat;
|
|
|
|
memset(&stat, 0, sizeof(SceIoStat));
|
|
|
|
sceIoGetstatByFd(fdsrc, &stat);
|
|
|
|
sceIoChstatByFd(fddst, &stat, 0x3B);
|
2017-03-05 17:31:21 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoClose(fddst);
|
|
|
|
sceIoClose(fdsrc);
|
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
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int copyPath(const char *src_path, const char *dst_path, FileProcessParam *param) {
|
2017-10-13 10:42:36 +00:00
|
|
|
// The source and destination paths are identical
|
|
|
|
if (strcasecmp(src_path, dst_path) == 0) {
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_SRC_AND_DST_IDENTICAL;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The destination is a subfolder of the source folder
|
|
|
|
int len = strlen(src_path);
|
2018-01-03 21:37:11 +00:00
|
|
|
if (strncasecmp(src_path, dst_path, len) == 0 && (dst_path[len] == '/' || dst_path[len - 1] == '/')) {
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_DST_IS_SUBFOLDER_OF_SRC;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SceUID dfd = sceIoDopen(src_path);
|
|
|
|
if (dfd >= 0) {
|
|
|
|
SceIoStat stat;
|
|
|
|
memset(&stat, 0, sizeof(SceIoStat));
|
|
|
|
sceIoGetstatByFd(dfd, &stat);
|
|
|
|
|
2017-12-30 00:26:52 +00:00
|
|
|
stat.st_mode |= SCE_S_IWUSR;
|
2018-09-13 10:00:30 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int ret = sceIoMkdir(dst_path, stat.st_mode & 0xFFF);
|
|
|
|
if (ret < 0 && ret != SCE_ERROR_ERRNO_EEXIST) {
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-09-13 10:00:30 +00:00
|
|
|
|
2017-12-30 00:26:52 +00:00
|
|
|
if (ret == SCE_ERROR_ERRNO_EEXIST) {
|
|
|
|
sceIoChstat(dst_path, &stat, 0x3B);
|
|
|
|
}
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
if (param) {
|
|
|
|
if (param->value)
|
|
|
|
(*param->value) += DIRECTORY_SIZE;
|
|
|
|
|
|
|
|
if (param->SetProgress)
|
|
|
|
param->SetProgress(param->value ? *param->value : 0, param->max);
|
|
|
|
|
|
|
|
if (param->cancelHandler && param->cancelHandler()) {
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
SceIoDirent dir;
|
|
|
|
memset(&dir, 0, sizeof(SceIoDirent));
|
|
|
|
|
|
|
|
res = sceIoDread(dfd, &dir);
|
|
|
|
if (res > 0) {
|
|
|
|
char *new_src_path = malloc(strlen(src_path) + strlen(dir.d_name) + 2);
|
2018-08-27 09:56:01 +00:00
|
|
|
snprintf(new_src_path, MAX_PATH_LENGTH, "%s%s%s", src_path, hasEndSlash(src_path) ? "" : "/", dir.d_name);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
char *new_dst_path = malloc(strlen(dst_path) + strlen(dir.d_name) + 2);
|
2018-08-27 09:56:01 +00:00
|
|
|
snprintf(new_dst_path, MAX_PATH_LENGTH, "%s%s%s", dst_path, hasEndSlash(dst_path) ? "" : "/", dir.d_name);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (SCE_S_ISDIR(dir.d_stat.st_mode)) {
|
|
|
|
ret = copyPath(new_src_path, new_dst_path, param);
|
|
|
|
} else {
|
|
|
|
ret = copyFile(new_src_path, new_dst_path, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(new_dst_path);
|
|
|
|
free(new_src_path);
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (res > 0);
|
|
|
|
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
} else {
|
|
|
|
return copyFile(src_path, dst_path, param);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int movePath(const char *src_path, const char *dst_path, int flags, FileProcessParam *param) {
|
2017-10-13 10:42:36 +00:00
|
|
|
// The source and destination paths are identical
|
|
|
|
if (strcasecmp(src_path, dst_path) == 0) {
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_SRC_AND_DST_IDENTICAL;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The destination is a subfolder of the source folder
|
|
|
|
int len = strlen(src_path);
|
2018-01-03 21:37:11 +00:00
|
|
|
if (strncasecmp(src_path, dst_path, len) == 0 && (dst_path[len] == '/' || dst_path[len - 1] == '/')) {
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_DST_IS_SUBFOLDER_OF_SRC;
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int res = sceIoRename(src_path, dst_path);
|
|
|
|
|
|
|
|
if (res == SCE_ERROR_ERRNO_EEXIST && flags & (MOVE_INTEGRATE | MOVE_REPLACE)) {
|
|
|
|
// Src stat
|
|
|
|
SceIoStat src_stat;
|
|
|
|
memset(&src_stat, 0, sizeof(SceIoStat));
|
|
|
|
res = sceIoGetstat(src_path, &src_stat);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
// Dst stat
|
|
|
|
SceIoStat dst_stat;
|
|
|
|
memset(&dst_stat, 0, sizeof(SceIoStat));
|
|
|
|
res = sceIoGetstat(dst_path, &dst_stat);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
// Is dir
|
|
|
|
int src_is_dir = SCE_S_ISDIR(src_stat.st_mode);
|
|
|
|
int dst_is_dir = SCE_S_ISDIR(dst_stat.st_mode);
|
|
|
|
|
|
|
|
// One of them is a file and the other a directory, no replacement or integration possible
|
|
|
|
if (src_is_dir != dst_is_dir)
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_INVALID_TYPE;
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
// Replace file
|
|
|
|
if (!src_is_dir && !dst_is_dir && flags & MOVE_REPLACE) {
|
|
|
|
sceIoRemove(dst_path);
|
|
|
|
|
|
|
|
res = sceIoRename(src_path, dst_path);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integrate directory
|
|
|
|
if (src_is_dir && dst_is_dir && flags & MOVE_INTEGRATE) {
|
|
|
|
SceUID dfd = sceIoDopen(src_path);
|
|
|
|
if (dfd < 0)
|
|
|
|
return dfd;
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
SceIoDirent dir;
|
|
|
|
memset(&dir, 0, sizeof(SceIoDirent));
|
|
|
|
|
|
|
|
res = sceIoDread(dfd, &dir);
|
|
|
|
if (res > 0) {
|
|
|
|
char *new_src_path = malloc(strlen(src_path) + strlen(dir.d_name) + 2);
|
2018-08-27 09:56:01 +00:00
|
|
|
snprintf(new_src_path, MAX_PATH_LENGTH, "%s%s%s", src_path, hasEndSlash(src_path) ? "" : "/", dir.d_name);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
char *new_dst_path = malloc(strlen(dst_path) + strlen(dir.d_name) + 2);
|
2018-08-27 09:56:01 +00:00
|
|
|
snprintf(new_dst_path, MAX_PATH_LENGTH, "%s%s%s", dst_path, hasEndSlash(dst_path) ? "" : "/", dir.d_name);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
// Recursive move
|
|
|
|
int ret = movePath(new_src_path, new_dst_path, flags, param);
|
|
|
|
|
|
|
|
free(new_dst_path);
|
|
|
|
free(new_src_path);
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (res > 0);
|
|
|
|
|
|
|
|
sceIoDclose(dfd);
|
|
|
|
|
|
|
|
// Integrated, now remove this directory
|
|
|
|
sceIoRmdir(src_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-09-06 19:19:21 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 06:59:41 +00:00
|
|
|
typedef struct {
|
2017-10-13 10:42:36 +00:00
|
|
|
char *extension;
|
|
|
|
int type;
|
2016-08-06 06:59:41 +00:00
|
|
|
} ExtensionType;
|
|
|
|
|
|
|
|
static ExtensionType extension_types[] = {
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".7Z", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".AR", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".BMP", FILE_TYPE_BMP },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".BZ2", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".CPIO", FILE_TYPE_ARCHIVE },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".GRZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".GZ", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".INI", FILE_TYPE_INI },
|
|
|
|
{ ".ISO", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".JPEG", FILE_TYPE_JPEG },
|
|
|
|
{ ".JPG", FILE_TYPE_JPEG },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".LRZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".LZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".LZ4", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".LZMA", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".LZO", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".MP3", FILE_TYPE_MP3 },
|
|
|
|
{ ".MP4", FILE_TYPE_MP4 },
|
|
|
|
{ ".MTREE", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".OGG", FILE_TYPE_OGG },
|
|
|
|
{ ".PNG", FILE_TYPE_PNG },
|
2018-01-25 15:48:51 +00:00
|
|
|
{ ".PSARC", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".PSP2ARC", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".PSP2DMP", FILE_TYPE_PSP2DMP },
|
|
|
|
{ ".RAR", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".SFO", FILE_TYPE_SFO },
|
|
|
|
{ ".SHAR", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TAR", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TAZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TBZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TBZ2", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TGZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TLZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TMP", FILE_TYPE_PSP2DMP },
|
|
|
|
{ ".TXT", FILE_TYPE_TXT },
|
|
|
|
{ ".TXZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TZ2", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TZMA", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".TZO", FILE_TYPE_ARCHIVE },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".TZST", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".UU", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".VPK", FILE_TYPE_VPK },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".WAR", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".XAR", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".XML", FILE_TYPE_XML },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".XZ", FILE_TYPE_ARCHIVE },
|
|
|
|
{ ".Z", FILE_TYPE_ARCHIVE },
|
2017-12-31 00:47:19 +00:00
|
|
|
{ ".ZIP", FILE_TYPE_ARCHIVE },
|
2018-01-03 09:38:53 +00:00
|
|
|
{ ".ZST", FILE_TYPE_ARCHIVE },
|
2016-08-06 06:59:41 +00:00
|
|
|
};
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int getFileType(const char *file) {
|
2017-10-13 10:42:36 +00:00
|
|
|
char *p = strrchr(file, '.');
|
|
|
|
if (p) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < (sizeof(extension_types) / sizeof(ExtensionType)); i++) {
|
|
|
|
if (strcasecmp(p, extension_types[i].extension) == 0) {
|
|
|
|
return extension_types[i].type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FILE_TYPE_UNKNOWN;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:34:15 +00:00
|
|
|
int getNumberOfDevices() {
|
2017-10-13 10:42:36 +00:00
|
|
|
return N_DEVICES;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:34:15 +00:00
|
|
|
char **getDevices() {
|
2017-10-13 10:42:36 +00:00
|
|
|
return devices;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-02 19:50:41 +00:00
|
|
|
FileListEntry *fileListCopyEntry(FileListEntry *src) {
|
|
|
|
FileListEntry *dst = malloc(sizeof(FileListEntry));
|
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memcpy(dst, src, sizeof(FileListEntry));
|
2018-01-03 21:37:11 +00:00
|
|
|
dst->name = malloc(src->name_length + 1);
|
|
|
|
strcpy(dst->name, src->name);
|
2018-01-02 19:50:41 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
FileListEntry *fileListFindEntry(FileList *list, const char *name) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
|
|
|
return NULL;
|
2017-09-16 21:49:32 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
FileListEntry *entry = list->head;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int name_length = strlen(name);
|
2016-08-23 12:50:39 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
while (entry) {
|
|
|
|
if (entry->name_length == name_length && strcasecmp(entry->name, name) == 0)
|
|
|
|
return entry;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
entry = entry->next;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return NULL;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileListEntry *fileListGetNthEntry(FileList *list, int n) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
|
|
|
return NULL;
|
2017-09-16 21:49:32 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
FileListEntry *entry = list->head;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
while (n > 0 && entry) {
|
|
|
|
n--;
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (n != 0)
|
|
|
|
return NULL;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return entry;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int fileListGetNumberByName(FileList *list, const char *name) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_ILLEGAL_ADDR;
|
2017-09-16 21:49:32 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
FileListEntry *entry = list->head;
|
2016-09-04 13:30:39 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int name_length = strlen(name);
|
2016-09-04 13:30:39 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int n = 0;
|
2016-09-04 13:30:39 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
while (entry) {
|
|
|
|
if (entry->name_length == name_length && strcasecmp(entry->name, name) == 0)
|
2017-12-30 10:43:37 +00:00
|
|
|
return n;
|
2016-09-04 13:30:39 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
n++;
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
2016-09-04 13:30:39 +00:00
|
|
|
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_NOT_FOUND;
|
2016-09-04 13:30:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 06:59:41 +00:00
|
|
|
void fileListAddEntry(FileList *list, FileListEntry *entry, int sort) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list || !entry)
|
|
|
|
return;
|
|
|
|
|
|
|
|
entry->next = NULL;
|
|
|
|
entry->previous = NULL;
|
|
|
|
|
|
|
|
if (list->head == NULL) {
|
|
|
|
list->head = entry;
|
|
|
|
list->tail = entry;
|
|
|
|
} else {
|
|
|
|
if (sort != SORT_NONE) {
|
|
|
|
FileListEntry *p = list->head;
|
|
|
|
FileListEntry *previous = NULL;
|
|
|
|
|
2018-01-25 10:11:08 +00:00
|
|
|
char entry_name[MAX_NAME_LENGTH];
|
|
|
|
strcpy(entry_name, entry->name);
|
|
|
|
removeEndSlash(entry_name);
|
2018-09-13 10:00:30 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
while (p) {
|
2018-01-25 10:11:08 +00:00
|
|
|
char p_name[MAX_NAME_LENGTH];
|
|
|
|
strcpy(p_name, p->name);
|
|
|
|
removeEndSlash(p_name);
|
2018-09-13 10:00:30 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
// '..' is always at first
|
2018-01-25 10:11:08 +00:00
|
|
|
if (strcmp(entry_name, "..") == 0)
|
2017-10-13 10:42:36 +00:00
|
|
|
break;
|
|
|
|
|
2018-01-25 10:11:08 +00:00
|
|
|
if (strcmp(p_name, "..") == 0) {
|
2017-10-13 10:42:36 +00:00
|
|
|
previous = p;
|
|
|
|
p = p->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by type
|
|
|
|
if (sort == SORT_BY_NAME) {
|
|
|
|
// First folders then files
|
|
|
|
if (entry->is_folder > p->is_folder)
|
|
|
|
break;
|
|
|
|
} else if (sort == SORT_BY_SIZE || sort == SORT_BY_DATE) {
|
|
|
|
// First files then folders
|
|
|
|
if (entry->is_folder < p->is_folder)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort == SORT_BY_NAME) {
|
|
|
|
// Sort by name within the same type
|
|
|
|
if (entry->is_folder == p->is_folder) {
|
2018-01-25 10:11:08 +00:00
|
|
|
if (strnatcasecmp(entry_name, p_name) < 0) {
|
2017-10-13 10:42:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (sort == SORT_BY_SIZE) {
|
|
|
|
// Sort by name for folders
|
|
|
|
if (entry->is_folder && p->is_folder) {
|
2018-01-25 10:11:08 +00:00
|
|
|
if (strnatcasecmp(entry_name, p_name) < 0) {
|
2017-10-13 10:42:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (!entry->is_folder && !p->is_folder) {
|
|
|
|
// Sort by size for files
|
|
|
|
if (entry->size > p->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Sort by name for files with the same size
|
|
|
|
if (entry->size == p->size) {
|
2018-01-25 10:11:08 +00:00
|
|
|
if (strnatcasecmp(entry_name, p_name) < 0) {
|
2017-10-13 10:42:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (sort == SORT_BY_DATE) {
|
|
|
|
if (entry->is_folder == p->is_folder) {
|
|
|
|
SceRtcTick entry_tick, p_tick;
|
|
|
|
sceRtcGetTick(&entry->mtime, &entry_tick);
|
|
|
|
sceRtcGetTick(&p->mtime, &p_tick);
|
|
|
|
|
|
|
|
// Sort by date within the same type
|
|
|
|
if (entry_tick.tick > p_tick.tick)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Sort by name for files and folders with the same date
|
|
|
|
if (entry_tick.tick == p_tick.tick) {
|
2018-01-25 10:11:08 +00:00
|
|
|
if (strnatcasecmp(entry_name, p_name) < 0) {
|
2017-10-13 10:42:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
previous = p;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previous == NULL) { // Order: entry (new head) -> p (old head)
|
|
|
|
entry->next = p;
|
|
|
|
p->previous = entry;
|
|
|
|
list->head = entry;
|
|
|
|
} else if (previous->next == NULL) { // Order: p (old tail) -> entry (new tail)
|
|
|
|
FileListEntry *tail = list->tail;
|
|
|
|
tail->next = entry;
|
|
|
|
entry->previous = tail;
|
|
|
|
list->tail = entry;
|
|
|
|
} else { // Order: previous -> entry -> p
|
|
|
|
previous->next = entry;
|
2018-09-13 10:00:30 +00:00
|
|
|
entry->previous = previous;
|
2017-10-13 10:42:36 +00:00
|
|
|
entry->next = p;
|
|
|
|
p->previous = entry;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FileListEntry *tail = list->tail;
|
|
|
|
tail->next = entry;
|
|
|
|
entry->previous = tail;
|
|
|
|
list->tail = entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list->length++;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int fileListRemoveEntry(FileList *list, FileListEntry *entry) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list || !entry)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (entry->previous) {
|
|
|
|
entry->previous->next = entry->next;
|
|
|
|
} else {
|
|
|
|
list->head = entry->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry->next) {
|
|
|
|
entry->next->previous = entry->previous;
|
|
|
|
} else {
|
|
|
|
list->tail = entry->previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->length--;
|
2018-01-03 21:53:18 +00:00
|
|
|
free(entry->name);
|
2017-10-13 10:42:36 +00:00
|
|
|
free(entry);
|
|
|
|
|
|
|
|
if (list->length == 0) {
|
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int fileListRemoveEntryByName(FileList *list, const char *name) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
|
|
|
return 0;
|
2017-09-16 21:49:32 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
FileListEntry *entry = list->head;
|
|
|
|
FileListEntry *previous = NULL;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int name_length = strlen(name);
|
2016-08-23 12:50:39 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
while (entry) {
|
|
|
|
if (entry->name_length == name_length && strcasecmp(entry->name, name) == 0) {
|
|
|
|
if (previous) {
|
|
|
|
previous->next = entry->next;
|
|
|
|
} else {
|
|
|
|
list->head = entry->next;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (list->tail == entry) {
|
|
|
|
list->tail = previous;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
list->length--;
|
2018-01-03 21:53:18 +00:00
|
|
|
free(entry->name);
|
2017-10-13 10:42:36 +00:00
|
|
|
free(entry);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
if (list->length == 0) {
|
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
}
|
2016-09-14 12:30:30 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
previous = entry;
|
|
|
|
entry = entry->next;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void fileListEmpty(FileList *list) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FileListEntry *entry = list->head;
|
|
|
|
|
|
|
|
while (entry) {
|
|
|
|
FileListEntry *next = entry->next;
|
2018-01-03 21:53:18 +00:00
|
|
|
free(entry->name);
|
2017-10-13 10:42:36 +00:00
|
|
|
free(entry);
|
|
|
|
entry = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->head = NULL;
|
|
|
|
list->tail = NULL;
|
|
|
|
list->length = 0;
|
|
|
|
list->files = 0;
|
|
|
|
list->folders = 0;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 15:34:15 +00:00
|
|
|
int fileListGetDeviceEntries(FileList *list) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_ILLEGAL_ADDR;
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < N_DEVICES; i++) {
|
|
|
|
if (devices[i]) {
|
|
|
|
if (is_safe_mode && strcmp(devices[i], "ux0:") != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SceIoStat stat;
|
|
|
|
memset(&stat, 0, sizeof(SceIoStat));
|
|
|
|
if (sceIoGetstat(devices[i], &stat) >= 0) {
|
|
|
|
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(devices[i]);
|
|
|
|
entry->name = malloc(entry->name_length + 1);
|
2018-01-02 19:50:41 +00:00
|
|
|
strcpy(entry->name, devices[i]);
|
|
|
|
entry->is_folder = 1;
|
|
|
|
entry->type = FILE_TYPE_UNKNOWN;
|
2018-09-13 10:00:30 +00:00
|
|
|
entry->is_symlink = 0;
|
2018-01-02 19:50:41 +00:00
|
|
|
|
|
|
|
SceIoDevInfo info;
|
|
|
|
memset(&info, 0, sizeof(SceIoDevInfo));
|
|
|
|
int res = sceIoDevctl(entry->name, 0x3001, NULL, 0, &info, sizeof(SceIoDevInfo));
|
|
|
|
if (res >= 0) {
|
|
|
|
entry->size = info.free_size;
|
|
|
|
entry->size2 = info.max_size;
|
2017-10-13 10:42:36 +00:00
|
|
|
} else {
|
2018-01-02 19:50:41 +00:00
|
|
|
if (strcmp(devices[i], "ux0:") == 0) {
|
|
|
|
sceAppMgrGetDevInfo("ux0:", (uint64_t *)&entry->size2, (uint64_t *)&entry->size);
|
|
|
|
} else {
|
|
|
|
entry->size = 0;
|
|
|
|
entry->size2 = 0;
|
|
|
|
}
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 10:00:30 +00:00
|
|
|
memcpy(&entry->ctime, (SceDateTime *) &stat.st_ctime, sizeof(SceDateTime));
|
|
|
|
memcpy(&entry->mtime, (SceDateTime *) &stat.st_mtime, sizeof(SceDateTime));
|
|
|
|
memcpy(&entry->atime, (SceDateTime *) &stat.st_atime, sizeof(SceDateTime));
|
2017-10-13 10:42:36 +00:00
|
|
|
|
2018-01-02 19:50:41 +00:00
|
|
|
fileListAddEntry(list, entry, SORT_BY_NAME);
|
2017-10-13 10:42:36 +00:00
|
|
|
|
2018-01-02 19:50:41 +00:00
|
|
|
list->folders++;
|
|
|
|
}
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-08-06 06:59:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int fileListGetDirectoryEntries(FileList *list, const char *path, int sort) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_ILLEGAL_ADDR;
|
2017-09-16 21:49:32 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
SceUID dfd = sceIoDopen(path);
|
|
|
|
if (dfd < 0)
|
|
|
|
return dfd;
|
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->type = FILE_TYPE_UNKNOWN;
|
2018-09-13 10:00:30 +00:00
|
|
|
entry->is_symlink = 0;
|
2018-01-02 19:50:41 +00:00
|
|
|
fileListAddEntry(list, entry, sort);
|
|
|
|
}
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
int res = 0;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
do {
|
|
|
|
SceIoDirent dir;
|
|
|
|
memset(&dir, 0, sizeof(SceIoDirent));
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
res = sceIoDread(dfd, &dir);
|
|
|
|
if (res > 0) {
|
|
|
|
FileListEntry *entry = malloc(sizeof(FileListEntry));
|
2018-01-02 19:50:41 +00:00
|
|
|
if (entry) {
|
|
|
|
entry->is_folder = SCE_S_ISDIR(dir.d_stat.st_mode);
|
2018-09-13 10:00:30 +00:00
|
|
|
entry->is_symlink = 0;
|
|
|
|
entry->symlink = NULL;
|
|
|
|
|
2018-01-02 19:50:41 +00:00
|
|
|
if (entry->is_folder) {
|
2018-01-03 21:37:11 +00:00
|
|
|
entry->name_length = strlen(dir.d_name) + 1;
|
|
|
|
entry->name = malloc(entry->name_length + 1);
|
|
|
|
strcpy(entry->name, dir.d_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(dir.d_name);
|
|
|
|
entry->name = malloc(entry->name_length + 1);
|
|
|
|
strcpy(entry->name, dir.d_name);
|
2018-01-02 19:50:41 +00:00
|
|
|
entry->type = getFileType(entry->name);
|
|
|
|
list->files++;
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2018-09-13 10:00:30 +00:00
|
|
|
if (dir.d_stat.st_size <= SYMLINK_MAX_SIZE) {
|
|
|
|
char *p = malloc(strlen(path) + strlen(dir.d_name) + 2);
|
|
|
|
if (!p) {
|
|
|
|
return VITASHELL_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
snprintf(p, MAX_PATH_LENGTH - 1, "%s%s%s",
|
|
|
|
path, hasEndSlash(path) ? "" : "/", dir.d_name);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2018-09-13 10:00:30 +00:00
|
|
|
Symlink* symlink = malloc(sizeof(Symlink));
|
|
|
|
if (!symlink) {
|
|
|
|
return VITASHELL_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
int res = resolveSimLink(symlink, p);
|
|
|
|
if (res < 0) {
|
|
|
|
if (symlink)
|
|
|
|
free(symlink);
|
|
|
|
} else {
|
|
|
|
entry->is_symlink = 1;
|
|
|
|
entry->symlink = symlink;
|
|
|
|
}
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entry->size = dir.d_stat.st_size;
|
|
|
|
memcpy(&entry->ctime, (SceDateTime *) &dir.d_stat.st_ctime, sizeof(SceDateTime));
|
|
|
|
memcpy(&entry->mtime, (SceDateTime *) &dir.d_stat.st_mtime, sizeof(SceDateTime));
|
|
|
|
memcpy(&entry->atime, (SceDateTime *) &dir.d_stat.st_atime, sizeof(SceDateTime));
|
2016-08-23 12:50:39 +00:00
|
|
|
|
2018-01-02 19:50:41 +00:00
|
|
|
fileListAddEntry(list, entry, sort);
|
|
|
|
}
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
} while (res > 0);
|
2016-08-06 06:59:41 +00:00
|
|
|
|
2017-10-13 10:42:36 +00:00
|
|
|
sceIoDclose(dfd);
|
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
|
|
|
}
|
|
|
|
|
2017-02-12 15:27:43 +00:00
|
|
|
int fileListGetEntries(FileList *list, const char *path, int sort) {
|
2017-10-13 10:42:36 +00:00
|
|
|
if (!list)
|
2018-08-27 07:54:46 +00:00
|
|
|
return VITASHELL_ERROR_ILLEGAL_ADDR;
|
2017-10-13 10:42:36 +00:00
|
|
|
|
|
|
|
if (isInArchive()) {
|
2017-12-30 19:35:25 +00:00
|
|
|
return fileListGetArchiveEntries(list, path, sort);
|
2017-10-13 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcasecmp(path, HOME_PATH) == 0) {
|
|
|
|
return fileListGetDeviceEntries(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileListGetDirectoryEntries(list, path, sort);
|
2016-08-08 21:31:41 +00:00
|
|
|
}
|
2018-09-13 10:00:30 +00:00
|
|
|
|
|
|
|
// returns < 0 on error
|
|
|
|
int resolveSimLink(Symlink *symlink, const char *path) {
|
|
|
|
SceUID fd = sceIoOpen(path, SCE_O_RDONLY, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return VITASHELL_ERROR_SYMLINK_INTERNAL;
|
|
|
|
char magic[SYMLINK_HEADER_SIZE + 1];
|
|
|
|
magic[SYMLINK_HEADER_SIZE] = '\0';
|
|
|
|
|
|
|
|
if (sceIoRead(fd, &magic, SYMLINK_HEADER_SIZE) < SYMLINK_HEADER_SIZE) {
|
|
|
|
sceIoClose(fd);
|
|
|
|
return VITASHELL_ERROR_SYMLINK_INTERNAL;
|
|
|
|
}
|
|
|
|
if(memcmp(magic, symlink_header_bytes, SYMLINK_HEADER_SIZE) != 0) {
|
|
|
|
sceIoClose(fd);
|
|
|
|
return VITASHELL_ERROR_SYMLINK_INTERNAL;
|
|
|
|
}
|
|
|
|
char *resolve = (char *) malloc(MAX_PATH_LENGTH);
|
|
|
|
if (!resolve) {
|
|
|
|
sceIoClose(fd);
|
|
|
|
return VITASHELL_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
int bytes_read = sceIoRead(fd, resolve, MAX_PATH_LENGTH - 1);
|
|
|
|
sceIoClose(fd);
|
|
|
|
|
|
|
|
if (bytes_read <= 0) {
|
|
|
|
free(resolve);
|
|
|
|
return VITASHELL_ERROR_SYMLINK_INTERNAL;
|
|
|
|
}
|
|
|
|
resolve[bytes_read] = '\0';
|
|
|
|
SceIoStat io_stat;
|
|
|
|
memset(&io_stat, 0, sizeof(SceIoStat));
|
|
|
|
if (sceIoGetstat(resolve, &io_stat) < 0) {
|
|
|
|
free(resolve);
|
|
|
|
return VITASHELL_ERROR_SYMLINK_INTERNAL;
|
|
|
|
}
|
|
|
|
symlink->to_file = !SCE_S_ISDIR(io_stat.st_mode);
|
|
|
|
symlink->target_path = resolve;
|
|
|
|
symlink->target_path_length = bytes_read + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return < 0 on error
|
|
|
|
int createSymLink(const char *store_location, const char *target) {
|
|
|
|
SceUID fd = sceIoOpen(store_location, SCE_O_WRONLY | SCE_O_CREAT, 0777);
|
|
|
|
if (fd < 0) {
|
|
|
|
return VITASHELL_ERROR_SYMLINK_INTERNAL;
|
|
|
|
}
|
|
|
|
sceIoWrite(fd, (void*) &symlink_header_bytes, SYMLINK_HEADER_SIZE);
|
|
|
|
sceIoWrite(fd, (void*) target, strnlen(target, MAX_PATH_LENGTH));
|
|
|
|
sceIoClose(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get directory path from filename
|
|
|
|
// result has slash at the end
|
|
|
|
char * getBaseDirectory(const char * path) {
|
|
|
|
int i;
|
|
|
|
int sep_ind = -1;
|
|
|
|
int len = strlen(path);
|
|
|
|
if (len > MAX_PATH_LENGTH - 1 || len <= 0) return NULL;
|
|
|
|
for(i = len - 1; i >=0; i --) {
|
|
|
|
if (path[i] == '/' || path[i] == ':') {
|
|
|
|
sep_ind = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sep_ind == -1) return NULL;
|
|
|
|
|
|
|
|
char * res = (char *) malloc(MAX_PATH_LENGTH);
|
|
|
|
if (!res) return NULL;
|
|
|
|
|
|
|
|
strncpy(res, path, MAX_PATH_LENGTH);
|
|
|
|
res[sep_ind + 1] = '\0';
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns NULL when no filename found or error
|
|
|
|
// result is at most MAX_PATH_LEN
|
|
|
|
char * getFilename(const char *path) {
|
|
|
|
int i;
|
|
|
|
int sep_ind = -1;
|
|
|
|
int len = strlen(path);
|
|
|
|
if (len > MAX_PATH_LENGTH || len <= 0) return NULL;
|
|
|
|
if (path[len - 1] == '/' || path[len - 1] == ':') return NULL; // no file
|
|
|
|
|
|
|
|
for(i = len - 1; i >=0; i --) {
|
|
|
|
if (path[i] == '/' || path[i] == ':') {
|
|
|
|
sep_ind = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sep_ind == -1) return NULL;
|
|
|
|
char * res = (char *) malloc(MAX_PATH_LENGTH);
|
|
|
|
if (!res) return NULL;
|
|
|
|
|
|
|
|
int new_len = len - (sep_ind + 1);
|
|
|
|
strncpy(res, path + (sep_ind + 1), new_len); // dont copy separation char
|
|
|
|
if (new_len + 1 < MAX_PATH_LENGTH)
|
|
|
|
res[new_len] = '\0';
|
|
|
|
else
|
|
|
|
res[MAX_PATH_LENGTH - 1] = '\0';
|
|
|
|
return res;
|
|
|
|
}
|