Compare commits

...
2 Commits
2 changed files with 225 additions and 42 deletions
+156 -40
View File
@@ -6,6 +6,7 @@
#include "fpk.h"
#include <dirent.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -13,6 +14,7 @@
#ifdef _WIN32
#include <windows.h>
#include <wchar.h>
#define PROGRAM_NAME "fpack.exe"
#define fpack_fseek64(f, off, whence) _fseeki64((f), (off), (whence))
#define fpack_ftell64(f) _ftelli64((f))
@@ -23,6 +25,36 @@
#define fpack_ftell64(f) ftello((f))
#endif
#ifdef _WIN32
static wchar_t *utf8_to_wide(const char *s)
{
int wlen = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
wchar_t *w;
if (wlen <= 0)
return NULL;
w = malloc((size_t)wlen * sizeof(wchar_t));
if (!w)
return NULL;
MultiByteToWideChar(CP_UTF8, 0, s, -1, w, wlen);
return w;
}
static char *wide_to_utf8(const wchar_t *w)
{
int len = WideCharToMultiByte(CP_UTF8, 0, w, -1, NULL, 0, NULL, NULL);
char *s;
if (len <= 0)
return NULL;
s = malloc((size_t)len);
if (!s)
return NULL;
WideCharToMultiByte(CP_UTF8, 0, w, -1, s, len, NULL, NULL);
return s;
}
#endif
static double now_seconds(void)
{
#ifdef _WIN32
@@ -69,6 +101,13 @@ static void format_duration(double secs, char *buf, size_t bufsize)
static unsigned char flags;
static uint64_t entry_count;
static uint64_t total_bytes;
static volatile sig_atomic_t interrupted;
static void handle_sigint(int sig)
{
(void)sig;
interrupted = 1;
}
static struct option long_opts[] = {
{"output", required_argument, NULL, 'o'},
@@ -151,8 +190,16 @@ static void *read_file(const char *path, size_t *out_size)
FILE *fp;
int64_t len;
void *buf;
#ifdef _WIN32
wchar_t *wpath = utf8_to_wide(path);
if (!wpath)
return NULL;
fp = _wfopen(wpath, L"rb");
free(wpath);
#else
fp = fopen(path, "rb");
#endif
if (!fp)
return NULL;
@@ -183,6 +230,105 @@ static void *read_file(const char *path, size_t *out_size)
return buf;
}
static int add_file(struct fpk_writer *w, const char *path, size_t base_len)
{
const char *rel_path = path + base_len;
void *data;
size_t size;
char *norm;
if (interrupted)
return 0;
while (*rel_path == '/')
rel_path++;
norm = dup_str(rel_path);
normalize_path(norm);
data = read_file(path, &size);
if (!data) {
fprintf(stderr, "fpack: cannot read: %s\n", path);
free(norm);
return 0;
}
if (fpk_writer_add(w, norm, data, size) != 0) {
fprintf(stderr, "fpack: cannot add: %s\n", norm);
free(data);
free(norm);
return 0;
}
entry_count++;
total_bytes += size;
if (flags & FPACK_VERBOSE)
printf(" %s (%lu bytes)\n", norm, (unsigned long)size);
if (flags & FPACK_PROGRESS) {
printf("\rpacked %lu files (%lu bytes)",
(unsigned long)entry_count, (unsigned long)total_bytes);
fflush(stdout);
}
free(data);
free(norm);
return 1;
}
#ifdef _WIN32
static int add_dir(struct fpk_writer *w, const char *dir, size_t base_len)
{
char *pattern = join_path(dir, "*");
wchar_t *wpattern = utf8_to_wide(pattern);
WIN32_FIND_DATAW fd;
HANDLE h;
free(pattern);
if (!wpattern) {
fprintf(stderr, "fpack: cannot open directory: %s\n", dir);
return 0;
}
h = FindFirstFileW(wpattern, &fd);
free(wpattern);
if (h == INVALID_HANDLE_VALUE) {
fprintf(stderr, "fpack: cannot open directory: %s\n", dir);
return 0;
}
do {
char *name = wide_to_utf8(fd.cFileName);
char *path;
if (!name || name[0] == '.') {
free(name);
continue;
}
path = join_path(dir, name);
free(name);
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
if (!add_dir(w, path, base_len)) {
free(path);
FindClose(h);
return 0;
}
} else if (!add_file(w, path, base_len)) {
free(path);
FindClose(h);
return 0;
}
free(path);
} while (FindNextFileW(h, &fd));
FindClose(h);
return 1;
}
#else
static int add_dir(struct fpk_writer *w, const char *dir, size_t base_len)
{
DIR *d;
@@ -216,50 +362,11 @@ static int add_dir(struct fpk_writer *w, const char *dir, size_t base_len)
return 0;
}
} else if (S_ISREG(st.st_mode)) {
const char *rel_path = path + base_len;
void *data;
size_t size;
char *norm;
while (*rel_path == '/')
rel_path++;
norm = dup_str(rel_path);
normalize_path(norm);
data = read_file(path, &size);
if (!data) {
fprintf(stderr, "fpack: cannot read: %s\n", path);
free(norm);
if (!add_file(w, path, base_len)) {
free(path);
closedir(d);
return 0;
}
if (fpk_writer_add(w, norm, data, size) != 0) {
fprintf(stderr, "fpack: cannot add: %s\n", norm);
free(data);
free(norm);
free(path);
closedir(d);
return 0;
}
entry_count++;
total_bytes += size;
if (flags & FPACK_VERBOSE)
printf(" %s (%lu bytes)\n", norm, (unsigned long)size);
if (flags & FPACK_PROGRESS) {
printf("\rpacked %lu files (%lu bytes)",
(unsigned long)entry_count,
(unsigned long)total_bytes);
fflush(stdout);
}
free(data);
free(norm);
}
free(path);
@@ -268,6 +375,7 @@ static int add_dir(struct fpk_writer *w, const char *dir, size_t base_len)
closedir(d);
return 1;
}
#endif
static void usage(void)
{
@@ -342,8 +450,16 @@ int main(int argc, char **argv)
return 1;
}
signal(SIGINT, handle_sigint);
if (!add_dir(w, input_dir, base_len)) {
fpk_writer_close(w);
if (interrupted) {
remove(output);
fprintf(stderr, "\nfpack: interrupted, removed incomplete archive: %s\n", output);
free(output);
return 130;
}
free(output);
return 1;
}
+69 -2
View File
@@ -5,6 +5,7 @@
#include "fpk.h"
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -13,7 +14,7 @@
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
#define MKDIR(path) _mkdir(path)
#include <wchar.h>
#define PROGRAM_NAME "funpack.exe"
#else
#include <time.h>
@@ -21,6 +22,42 @@
#define PROGRAM_NAME "funpack"
#endif
#ifdef _WIN32
static wchar_t *utf8_to_wide(const char *s)
{
int wlen = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0);
wchar_t *w;
if (wlen <= 0)
return NULL;
w = malloc((size_t)wlen * sizeof(wchar_t));
if (!w)
return NULL;
MultiByteToWideChar(CP_UTF8, 0, s, -1, w, wlen);
return w;
}
static int win_mkdir(const char *path)
{
wchar_t *wpath = utf8_to_wide(path);
int ok;
if (!wpath)
return -1;
ok = CreateDirectoryW(wpath, NULL);
free(wpath);
if (ok)
return 0;
if (GetLastError() == ERROR_ALREADY_EXISTS) {
errno = EEXIST;
return -1;
}
errno = 0;
return -1;
}
#define MKDIR(path) win_mkdir(path)
#endif
static double now_seconds(void)
{
#ifdef _WIN32
@@ -66,6 +103,13 @@ static void format_duration(double secs, char *buf, size_t bufsize)
#define FUNPACK_VERBOSE (1u << 3)
static unsigned char flags;
static volatile sig_atomic_t interrupted;
static void handle_sigint(int sig)
{
(void)sig;
interrupted = 1;
}
static struct option long_opts[] = {
{"dir", required_argument, NULL, 'd'},
@@ -168,6 +212,9 @@ static int extract_entry(const char *out_dir, const fpk_entry_t *entry,
{
char *full_path = join_path(out_dir, entry->path);
FILE *fp;
#ifdef _WIN32
wchar_t *wpath;
#endif
if (strchr(entry->path, '/') != NULL) {
char *dir_part = dup_str(full_path);
@@ -182,7 +229,13 @@ static int extract_entry(const char *out_dir, const fpk_entry_t *entry,
free(dir_part);
}
#ifdef _WIN32
wpath = utf8_to_wide(full_path);
fp = wpath ? _wfopen(wpath, L"wb") : NULL;
free(wpath);
#else
fp = fopen(full_path, "wb");
#endif
if (!fp) {
fprintf(stderr, "funpack: cannot create: %s\n", full_path);
free(full_path);
@@ -295,10 +348,17 @@ int main(int argc, char **argv)
return 1;
}
signal(SIGINT, handle_sigint);
for (i = 0; i < count; i++) {
const fpk_entry_t *entry = fpk_entry_at(pak, i);
size_t size;
void *data = fpk_read(pak, entry, &size);
void *data;
if (interrupted)
break;
data = fpk_read(pak, entry, &size);
if (!data) {
fprintf(stderr, "funpack: cannot read: %s\n", entry->path);
@@ -332,6 +392,13 @@ int main(int argc, char **argv)
if ((flags & FUNPACK_PROGRESS) && count > 0)
printf("\n");
if (interrupted) {
fprintf(stderr, "funpack: interrupted, %lu of %lu entries extracted\n",
(unsigned long)i, (unsigned long)count);
free(out_dir);
return 130;
}
if (!(flags & FUNPACK_QUIET)) {
char dur[64];