aboutsummaryrefslogtreecommitdiff
path: root/contrib/recovery/recover_directory.c
blob: 13f83fd106bad4773e9d90f4287ec753eba7b65c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (c) 2016-2021, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under both the BSD-style license (found in the
 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
 * in the COPYING file in the root directory of this source tree).
 * You may select, at your option, one of the above-listed licenses.
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define ZSTD_STATIC_LINKING_ONLY
#include "util.h"
#include "zstd.h"

#define CHECK(cond, ...)                                                       \
  do {                                                                         \
    if (!(cond)) {                                                             \
      fprintf(stderr, "%s:%d CHECK(%s) failed: ", __FILE__, __LINE__, #cond);  \
      fprintf(stderr, "" __VA_ARGS__);                                         \
      fprintf(stderr, "\n");                                                   \
      exit(1);                                                                 \
    }                                                                          \
  } while (0)

static void usage(char const *program) {
  fprintf(stderr, "USAGE: %s FILE.zst PREFIX\n", program);
  fprintf(stderr, "FILE.zst: A zstd compressed file with multiple frames\n");
  fprintf(stderr, "PREFIX:   The output prefix. Uncompressed files will be "
                  "created named ${PREFIX}0 ${PREFIX}1...\n\n");
  fprintf(stderr, "This program takes concatenated zstd frames and "
                  "decompresses them into individual files.\n");
  fprintf(stderr, "E.g. files created with a command like: zstd -r directory "
                  "-o file.zst\n");
}

typedef struct {
  char *data;
  size_t size;
  size_t frames;
  size_t maxFrameSize;
} ZstdFrames;

static ZstdFrames readFile(char const *fileName) {
  U64 const fileSize = UTIL_getFileSize(fileName);
  CHECK(fileSize != UTIL_FILESIZE_UNKNOWN, "Unknown file size!");

  char *const data = (char *)malloc(fileSize);
  CHECK(data != NULL, "Allocation failed");

  FILE *file = fopen(fileName, "rb");
  CHECK(file != NULL, "fopen failed");

  size_t const readSize = fread(data, 1, fileSize, file);
  CHECK(readSize == fileSize, "fread failed");

  fclose(file);
  ZstdFrames frames;
  frames.data = (char *)data;
  frames.size = fileSize;
  frames.frames = 0;

  size_t index;
  size_t maxFrameSize = 0;
  for (index = 0; index < fileSize;) {
    size_t const frameSize =
        ZSTD_findFrameCompressedSize(data + index, fileSize - index);
    CHECK(!ZSTD_isError(frameSize), "Bad zstd frame: %s",
          ZSTD_getErrorName(frameSize));
    if (frameSize > maxFrameSize)
      maxFrameSize = frameSize;
    frames.frames += 1;
    index += frameSize;
  }
  CHECK(index == fileSize, "Zstd file corrupt!");
  frames.maxFrameSize = maxFrameSize;

  return frames;
}

static int computePadding(size_t numFrames) {
  return snprintf(NULL, 0, "%u", (unsigned)numFrames);
}

int main(int argc, char **argv) {
  if (argc != 3) {
    usage(argv[0]);
    exit(1);
  }
  char const *const zstdFile = argv[1];
  char const *const prefix = argv[2];

  ZstdFrames frames = readFile(zstdFile);

  if (frames.frames <= 1) {
    fprintf(
        stderr,
        "%s only has %u zstd frame. Simply use `zstd -d` to decompress it.\n",
        zstdFile, (unsigned)frames.frames);
    exit(1);
  }

  int const padding = computePadding(frames.frames - 1);

  size_t const outFileNameSize = strlen(prefix) + padding + 1;
  char* outFileName = malloc(outFileNameSize);
  CHECK(outFileName != NULL, "Allocation failure");

  size_t const bufferSize = 128 * 1024;
  void *buffer = malloc(bufferSize);
  CHECK(buffer != NULL, "Allocation failure");

  ZSTD_DCtx* dctx = ZSTD_createDCtx();
  CHECK(dctx != NULL, "Allocation failure");

  fprintf(stderr, "Recovering %u files...\n", (unsigned)frames.frames);

  size_t index;
  size_t frame = 0;
  for (index = 0; index < frames.size; ++frame) {
    size_t const frameSize =
        ZSTD_findFrameCompressedSize(frames.data + index, frames.size - index);

    int const ret = snprintf(outFileName, outFileNameSize, "%s%0*u", prefix, padding, (unsigned)frame);
    CHECK(ret >= 0 && (size_t)ret <= outFileNameSize, "snprintf failed!");

    FILE* outFile = fopen(outFileName, "wb");
    CHECK(outFile != NULL, "fopen failed");

    ZSTD_DCtx_reset(dctx, ZSTD_reset_session_only);
    ZSTD_inBuffer in = {frames.data + index, frameSize, 0};
    while (in.pos < in.size) {
        ZSTD_outBuffer out = {buffer, bufferSize, 0};
        CHECK(!ZSTD_isError(ZSTD_decompressStream(dctx, &out, &in)), "decompression failed");
        size_t const writeSize = fwrite(out.dst, 1, out.pos, outFile);
        CHECK(writeSize == out.pos, "fwrite failed");
    }
    fclose(outFile);
    fprintf(stderr, "Recovered %s\n", outFileName);
    index += frameSize;
  }
  fprintf(stderr, "Complete\n");

  free(outFileName);
  ZSTD_freeDCtx(dctx);
  free(buffer);
  free(frames.data);
  return 0;
}