tarw.h

Minimalist header-only library for generating TAR files

NameSizeMode
..
tarw.h 6K -rw-r--r--
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef TARW_H_
#define TARW_H_

#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

typedef struct {
  const char *owner;
  const char *group;
  size_t owner_id;
  size_t group_id;

  char   *data;
  size_t cursor;
  size_t capacity;
} TarWriter;

bool tarw_add_directory(TarWriter *w, const char *path, uint16_t mode);
bool tarw_add_file(TarWriter *w, const char *path, uint16_t mode,
                                 const char *buff, size_t buff_size);
bool tarw_write(TarWriter w, FILE *f);

void tarw_free(TarWriter *w);

#define TARW_CHUNK_SIZE 512

/* UStar TAR file header */
typedef struct {
  char file_path[100];

  // all encoded as null-terminated octal strings
  char file_mode[8];
  char owner_id[8];
  char group_id[8];
  char file_size[12];
  char last_modification_time[12]; // seconds since unix epoch

  char    checksum[8]; // null-terminated octal string
  uint8_t type_flag;

  char name_of_linked_file[100];

  char ustarw_indicator[6];
  char ustarw_version[2];

  char owner_name[32];
  char group_name[32];

  char device_major_number[8];
  char device_minor_number[8];
  char file_name_prefix[155];

  char padding[12]; // padding to make the header 512 bytes
} TarHeader;

static_assert(sizeof(TarHeader) == TARW_CHUNK_SIZE,
              "TarHeader struct has the wrong size");

enum {
  TARW_NORMAL           = '0',
  TARW_HARD_LINK        = '1', // TODO: unsupported
  TARW_SYMLINK          = '2', // TODO: unsupported
  TARW_CHARACTER_DEVIDE = '3', // TODO: unsupported
  TARW_BLOCK_DEVICE     = '4', // TODO: unsupported
  TARW_DIRECTORY        = '5',
  TARW_PIPE             = '6', // TODO: unsupported
};

// = Implementation ===========================================================
#ifdef TARW_IMPLEMENTATION

#define TARW_USTARW_INDICATOR "ustar"


#if !defined(TARW_REALLOC) || !defined(TARW_FREE)
#include <stdlib.h>
#endif

/*
 * NOTE: should behave like realloc from libc:
 *       - returns `NULL` on fail
 *       - if `p` is `NULL` then behave like malloc
 */
#ifndef TARW_REALLOC
#define TARW_REALLOC(p, size) realloc((p), (size))
#endif // TARW_REALLOC

#ifndef TARW_FREE
#define TARW_FREE(p) free((p))
#endif // TARW_FREE


#ifdef defined(TARW_EMIT_ASSERTS) && !defined(TARW_ASSERT)
#define TARW_ASSERT(cond, msg) assert((cond) && (msg))
#endif

void tarw_header_set_checksum(TarHeader *h)
{
  uint8_t *data = (uint8_t*)h;

  size_t checksum = 0;
  // the checksum field should be set to "  ...  " for computing the checksum
  memset(&h->checksum, ' ', sizeof(h->checksum));
  for (size_t i = 0; i < sizeof(*h); i++) checksum += (size_t)data[i];

  snprintf((char*)&h->checksum, sizeof(h->checksum), "%zo", checksum);
}

TarHeader tarw_make_header(const char *path,  uint16_t mode,
                           const char *owner, const char *group,
                           size_t owner_id,   size_t group_id,
                           uint8_t type_flag, size_t file_size)
{
  TarHeader h = {0};

  snprintf((char*)&h.file_path, sizeof(h.file_path), "%s",     path);
  snprintf((char*)&h.file_mode, sizeof(h.file_mode), "%07o",   mode & 0x7fff);
  snprintf((char*)&h.owner_id,  sizeof(h.owner_id),  "%07zo",  owner_id);
  snprintf((char*)&h.group_id,  sizeof(h.group_id),  "%07zo",  group_id);
  snprintf((char*)&h.file_size, sizeof(h.file_size), "%011zo", file_size);
  // set h.last_modification_time to the UNIX epoch
  memset((char*)&h.last_modification_time, '0',
         sizeof(h.last_modification_time)-1);

  h.type_flag = type_flag;

  snprintf((char*)&h.ustarw_indicator, sizeof(h.ustarw_indicator), TARW_USTARW_INDICATOR);

  snprintf((char*)&h.owner_name, sizeof(h.owner_name), "%s", owner);
  snprintf((char*)&h.group_name, sizeof(h.group_name), "%s", group);
  tarw_header_set_checksum(&h);

  return h;
}

bool tarw__reserve_bytes(TarWriter *w, size_t bytes)
{
  if (w->capacity - w->cursor >= bytes) return true;

  size_t new_capacity = 2 * w->capacity;
  if (new_capacity - w->cursor < bytes) new_capacity = w->capacity + bytes;

  void *new_data = TARW_REALLOC(w->data, new_capacity);
  if (new_data == NULL) return false;
  w->data     = new_data;
  w->capacity = new_capacity;

  return true;
}

/*
 * NOTE: it is only safe to call this function when buff fits inside w->data!
 */
void tarw__write_bytes(TarWriter *w, const char *buff, size_t buff_size)
{
#ifdef TARW_EMIT_ASSERTS
  TARW_ASSERT(w->capactiry - w->cursor >= buff_size, "buffer overflow");
#endif // TARW_EMIT_ASSERTS
  memcpy(w->data + w->cursor, buff, buff_size);
  w->cursor += buff_size;
}

/*
 * NOTE: it is only safe to call this function when buff fits inside w->data!
 */
void tarw__write_zeros(TarWriter *w, size_t n)
{
#ifdef TARW_EMIT_ASSERTS
  TARW_ASSERT(w->capactiry - w->cursor >= buff_size, "buffer overflow");
#endif // TARW_EMIT_ASSERTS
  memset(w->data + w->cursor, 0, n);
  w->cursor += n;
}

bool tarw_add_directory(TarWriter *w, const char *path, uint16_t mode)
{
  if (!tarw__reserve_bytes(w, TARW_CHUNK_SIZE)) return false;

  TarHeader h = tarw_make_header(path, mode,
                                 w->owner, w->group, w->owner_id, w->group_id,
                                 TARW_DIRECTORY, 0);

  tarw__write_bytes(w, (char*)&h, TARW_CHUNK_SIZE);
  return true;
}

bool tarw_add_file(TarWriter *w, const char *path, uint16_t mode,
                                 const char *buff, size_t buff_size)
{
  size_t chunks_ceil     = (buff_size + (TARW_CHUNK_SIZE-1))/TARW_CHUNK_SIZE;
  size_t padding         = chunks_ceil*TARW_CHUNK_SIZE - buff_size;
  size_t capacity_needed = TARW_CHUNK_SIZE + buff_size + padding;
  if (!tarw__reserve_bytes(w, capacity_needed)) return false;

  TarHeader h = tarw_make_header(path, mode,
                                 w->owner, w->group, w->owner_id, w->group_id,
                                 TARW_NORMAL, buff_size);

  tarw__write_bytes(w, (char*)&h, sizeof(h));
  tarw__write_bytes(w, buff, buff_size);
  tarw__write_zeros(w, padding);

  return true;
}

bool tarw_write(TarWriter w, FILE *f)
{
  if (fwrite(w.data, 1, w.cursor, f) != w.cursor) return false;
  return true;
}

void tarw_free(TarWriter *w)
{
  if (w->data) {
    TARW_FREE(w->data);
    w->data = NULL;
  }
  w->capacity = 0;
  w->cursor   = 0;
}

#endif // TARW_IMPLEMENTATION
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // TARW_H_