Compare commits

...
5 Commits
2 changed files with 172 additions and 21 deletions
+81 -6
View File
@@ -12,15 +12,56 @@
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#define PROGRAM_NAME "fpack.exe"
#define fpack_fseek64(f, off, whence) _fseeki64((f), (off), (whence))
#define fpack_ftell64(f) _ftelli64((f))
#else
#include <time.h>
#define PROGRAM_NAME "fpack"
#define fpack_fseek64(f, off, whence) fseeko((f), (off_t)(off), (whence))
#define fpack_ftell64(f) ftello((f))
#endif
static double now_seconds(void)
{
#ifdef _WIN32
LARGE_INTEGER freq, count;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&count);
return (double)count.QuadPart / (double)freq.QuadPart;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
#endif
}
static void format_duration(double secs, char *buf, size_t bufsize)
{
unsigned long total_ms = (unsigned long)(secs * 1000.0 + 0.5);
unsigned long ms = total_ms % 1000;
unsigned long total_s = total_ms / 1000;
unsigned long s = total_s % 60;
unsigned long total_m = total_s / 60;
unsigned long m = total_m % 60;
unsigned long total_h = total_m / 60;
unsigned long h = total_h % 24;
unsigned long d = total_h / 24;
if (d > 0)
sprintf(buf, "%lud %luh %lum %lus", d, h, m, s);
else if (h > 0)
sprintf(buf, "%luh %lum %lus", h, m, s);
else if (m > 0)
sprintf(buf, "%lum %lu.%03lus", m, s, ms);
else
sprintf(buf, "%lu.%03lus", s, ms);
buf[bufsize - 1] = '\0';
}
#define FPACK_QUIET (1u << 0)
#define FPACK_VERBOSE (1u << 1)
#define FPACK_PROGRESS (1u << 2)
@@ -34,6 +75,7 @@ static struct option long_opts[] = {
{"progress", no_argument, NULL, 'p'},
{"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
@@ -59,17 +101,23 @@ static char *join_path(const char *dir, const char *name)
return out;
}
#ifdef _WIN32
#define IS_PATH_SEP(c) ((c) == '/' || (c) == '\\')
#else
#define IS_PATH_SEP(c) ((c) == '/')
#endif
static char *basename_dup(const char *path)
{
size_t end = strlen(path);
size_t start;
char *out;
while (end > 0 && path[end - 1] == '/')
while (end > 0 && IS_PATH_SEP(path[end - 1]))
end--;
start = end;
while (start > 0 && path[start - 1] != '/')
while (start > 0 && !IS_PATH_SEP(path[start - 1]))
start--;
out = malloc(end - start + 1);
@@ -224,9 +272,25 @@ static int add_dir(struct fpk_writer *w, const char *dir, size_t base_len)
static void usage(void)
{
fprintf(stderr, "usage: %s [-o output.fpk] [-p] [-q] [-v] input_dir\n", PROGRAM_NAME);
fprintf(stderr, "try '%s --help' for more information\n", PROGRAM_NAME);
exit(1);
}
static void help(void)
{
printf("usage: %s [options] input_dir\n", PROGRAM_NAME);
printf("\n");
printf("Pack a directory tree into an fpk archive.\n");
printf("\n");
printf("options:\n");
printf(" -o, --output=FILE write the archive to FILE instead of the derived name\n");
printf(" -p, --progress print packing progress\n");
printf(" -q, --quiet suppress the final summary line\n");
printf(" -v, --verbose print each packed entry\n");
printf(" -h, --help show this help and exit\n");
exit(0);
}
int main(int argc, char **argv)
{
const char *output_arg = NULL;
@@ -235,8 +299,9 @@ int main(int argc, char **argv)
size_t base_len;
struct fpk_writer *w;
int c;
double start_time;
while ((c = getopt_long(argc, argv, "o:pqv", long_opts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "o:pqvh", long_opts, NULL)) != -1) {
switch (c) {
case 'o':
output_arg = optarg;
@@ -250,6 +315,9 @@ int main(int argc, char **argv)
case 'v':
flags |= FPACK_VERBOSE;
break;
case 'h':
help();
break;
default:
usage();
}
@@ -261,8 +329,10 @@ int main(int argc, char **argv)
input_dir = argv[optind];
output = output_arg ? dup_str(output_arg) : derive_output_name(input_dir);
start_time = now_seconds();
base_len = strlen(input_dir);
while (base_len > 0 && input_dir[base_len - 1] == '/')
while (base_len > 0 && IS_PATH_SEP(input_dir[base_len - 1]))
base_len--;
w = fpk_writer_open(output);
@@ -297,8 +367,13 @@ int main(int argc, char **argv)
if (flags & FPACK_PROGRESS)
printf("\n");
if (!(flags & FPACK_QUIET))
printf("wrote: %s (%lu entries)\n", output, (unsigned long)entry_count);
if (!(flags & FPACK_QUIET)) {
char dur[64];
format_duration(now_seconds() - start_time, dur, sizeof(dur));
printf("wrote: %s (%lu entries) in %s\n", output,
(unsigned long)entry_count, dur);
}
free(output);
return 0;
+91 -15
View File
@@ -1,3 +1,7 @@
#if !defined(_WIN32)
#define _POSIX_C_SOURCE 200809L
#endif
#include "fpk.h"
#include <errno.h>
#include <getopt.h>
@@ -8,17 +12,54 @@
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
#define MKDIR(path) _mkdir(path)
#else
#define MKDIR(path) mkdir(path, 0755)
#endif
#ifdef _WIN32
#define PROGRAM_NAME "funpack.exe"
#else
#include <time.h>
#define MKDIR(path) mkdir(path, 0755)
#define PROGRAM_NAME "funpack"
#endif
static double now_seconds(void)
{
#ifdef _WIN32
LARGE_INTEGER freq, count;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&count);
return (double)count.QuadPart / (double)freq.QuadPart;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
#endif
}
static void format_duration(double secs, char *buf, size_t bufsize)
{
unsigned long total_ms = (unsigned long)(secs * 1000.0 + 0.5);
unsigned long ms = total_ms % 1000;
unsigned long total_s = total_ms / 1000;
unsigned long s = total_s % 60;
unsigned long total_m = total_s / 60;
unsigned long m = total_m % 60;
unsigned long total_h = total_m / 60;
unsigned long h = total_h % 24;
unsigned long d = total_h / 24;
if (d > 0)
sprintf(buf, "%lud %luh %lum %lus", d, h, m, s);
else if (h > 0)
sprintf(buf, "%luh %lum %lus", h, m, s);
else if (m > 0)
sprintf(buf, "%lum %lu.%03lus", m, s, ms);
else
sprintf(buf, "%lu.%03lus", s, ms);
buf[bufsize - 1] = '\0';
}
#define FUNPACK_LIST (1u << 0)
#define FUNPACK_PROGRESS (1u << 1)
#define FUNPACK_QUIET (1u << 2)
@@ -32,6 +73,7 @@ static struct option long_opts[] = {
{"progress", no_argument, NULL, 'p'},
{"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
@@ -57,17 +99,23 @@ static char *join_path(const char *dir, const char *name)
return out;
}
#ifdef _WIN32
#define IS_PATH_SEP(c) ((c) == '/' || (c) == '\\')
#else
#define IS_PATH_SEP(c) ((c) == '/')
#endif
static char *basename_dup(const char *path)
{
size_t end = strlen(path);
size_t start;
char *out;
while (end > 0 && path[end - 1] == '/')
while (end > 0 && IS_PATH_SEP(path[end - 1]))
end--;
start = end;
while (start > 0 && path[start - 1] != '/')
while (start > 0 && !IS_PATH_SEP(path[start - 1]))
start--;
out = malloc(end - start + 1);
@@ -119,19 +167,20 @@ static int extract_entry(const char *out_dir, const fpk_entry_t *entry,
const void *data, size_t size)
{
char *full_path = join_path(out_dir, entry->path);
char *dir_part = dup_str(full_path);
char *slash = strrchr(dir_part, '/');
FILE *fp;
if (slash) {
if (strchr(entry->path, '/') != NULL) {
char *dir_part = dup_str(full_path);
char *slash = strrchr(dir_part, '/');
*slash = '\0';
if (!mkdir_p(dir_part)) {
free(dir_part);
free(full_path);
return 0;
}
}
free(dir_part);
}
fp = fopen(full_path, "wb");
if (!fp) {
@@ -155,9 +204,26 @@ static int extract_entry(const char *out_dir, const fpk_entry_t *entry,
static void usage(void)
{
fprintf(stderr, "usage: %s [-d output_dir] [-l] [-p] [-q] [-v] input.fpk\n", PROGRAM_NAME);
fprintf(stderr, "try '%s --help' for more information\n", PROGRAM_NAME);
exit(1);
}
static void help(void)
{
printf("usage: %s [options] input.fpk\n", PROGRAM_NAME);
printf("\n");
printf("Extract files from an fpk archive.\n");
printf("\n");
printf("options:\n");
printf(" -d, --dir=DIR extract into DIR instead of the archive's derived name\n");
printf(" -l, --list list archive entries and exit, without extracting\n");
printf(" -p, --progress print extraction progress\n");
printf(" -q, --quiet suppress the final summary line\n");
printf(" -v, --verbose print each extracted entry\n");
printf(" -h, --help show this help and exit\n");
exit(0);
}
int main(int argc, char **argv)
{
const char *dir_arg = NULL;
@@ -167,8 +233,9 @@ int main(int argc, char **argv)
uint64_t count;
uint64_t i;
int c;
double start_time;
while ((c = getopt_long(argc, argv, "d:lpqv", long_opts, NULL)) != -1) {
while ((c = getopt_long(argc, argv, "d:lpqvh", long_opts, NULL)) != -1) {
switch (c) {
case 'd':
dir_arg = optarg;
@@ -185,6 +252,9 @@ int main(int argc, char **argv)
case 'v':
flags |= FUNPACK_VERBOSE;
break;
case 'h':
help();
break;
default:
usage();
}
@@ -195,6 +265,8 @@ int main(int argc, char **argv)
input = argv[optind];
start_time = now_seconds();
pak = fpk_open(input);
if (!pak) {
fprintf(stderr, "funpack: cannot open: %s\n", input);
@@ -260,9 +332,13 @@ int main(int argc, char **argv)
if ((flags & FUNPACK_PROGRESS) && count > 0)
printf("\n");
if (!(flags & FUNPACK_QUIET))
printf("unpacked: %s -> %s (%lu entries)\n", input, out_dir,
(unsigned long)count);
if (!(flags & FUNPACK_QUIET)) {
char dur[64];
format_duration(now_seconds() - start_time, dur, sizeof(dur));
printf("unpacked: %s -> %s (%lu entries) in %s\n", input, out_dir,
(unsigned long)count, dur);
}
free(out_dir);
return 0;