Files
fpk/tools/fpack.c
T

261 lines
5.5 KiB
C

#if !defined(_WIN32)
#define _POSIX_C_SOURCE 200809L
#endif
#include "fpk.h"
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#define PROGRAM_NAME "fpack.exe"
#else
#include <time.h>
#define PROGRAM_NAME "fpack"
#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)
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'},
{"progress", no_argument, NULL, 'p'},
{"quiet", no_argument, NULL, 'q'},
{"verbose", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
static char *dup_str(const char *s)
{
size_t len = strlen(s) + 1;
char *out = malloc(len);
memcpy(out, s, len);
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 && IS_PATH_SEP(path[end - 1]))
end--;
start = end;
while (start > 0 && !IS_PATH_SEP(path[start - 1]))
start--;
out = malloc(end - start + 1);
memcpy(out, path + start, end - start);
out[end - start] = '\0';
return out;
}
static char *derive_output_name(const char *input_dir)
{
char *base = basename_dup(input_dir);
size_t len = strlen(base);
char *out = malloc(len + 5);
memcpy(out, base, len);
memcpy(out + len, ".fpk", 5);
free(base);
return out;
}
static int on_add(const char *path, uint64_t size, void *ctx)
{
(void)ctx;
entry_count++;
total_bytes += size;
if (flags & FPACK_VERBOSE)
printf(" %s (%lu bytes)\n", path, (unsigned long)size);
if (flags & FPACK_PROGRESS) {
printf("\rpacked %lu files (%lu bytes)",
(unsigned long)entry_count, (unsigned long)total_bytes);
fflush(stdout);
}
return !interrupted;
}
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;
const char *input_dir;
char *output;
struct fpk_writer *w;
int c;
double start_time;
while ((c = getopt_long(argc, argv, "o:pqvh", long_opts, NULL)) != -1) {
switch (c) {
case 'o':
output_arg = optarg;
break;
case 'p':
flags |= FPACK_PROGRESS;
break;
case 'q':
flags |= FPACK_QUIET;
break;
case 'v':
flags |= FPACK_VERBOSE;
break;
case 'h':
help();
break;
default:
usage();
}
}
if (optind != argc - 1)
usage();
input_dir = argv[optind];
output = output_arg ? dup_str(output_arg) : derive_output_name(input_dir);
start_time = now_seconds();
w = fpk_writer_open(output);
if (!w) {
fprintf(stderr, "fpack: cannot create: %s\n", output);
free(output);
return 1;
}
signal(SIGINT, handle_sigint);
if (fpk_writer_add_dir(w, input_dir, on_add, NULL) != 0) {
fpk_writer_close(w);
if (interrupted) {
remove(output);
fprintf(stderr, "\nfpack: interrupted, removed incomplete archive: %s\n", output);
free(output);
return 130;
}
fprintf(stderr, "fpack: failed while packing: %s\n", input_dir);
free(output);
return 1;
}
if (entry_count == 0) {
fprintf(stderr, "fpack: no files found in: %s\n", input_dir);
fpk_writer_close(w);
free(output);
return 1;
}
if (fpk_writer_finalize(w) != 0) {
fprintf(stderr, "fpack: failed to finalize: %s\n", output);
fpk_writer_close(w);
free(output);
return 1;
}
fpk_writer_close(w);
if (flags & FPACK_PROGRESS)
printf("\n");
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;
}