tarw.h

Minimalist header-only library for generating TAR files

NameSizeMode
..
tarw.h 4K -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
#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 {
  FILE *sink;

  const char *owner;
  const char *group;
  size_t owner_id;
  size_t group_id;
} TarWriter;

bool tarw_write_directory(TarWriter w, const char *path, uint16_t mode);
bool tarw_write_file(TarWriter w, const char *path, uint16_t mode,
                                  const char *buff, size_t buff_size);

#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
};

#ifdef TARW_IMPLEMENTATION
#define TARW_USTARW_INDICATOR "ustar"

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_write_directory(TarWriter w, const char *path, uint16_t mode)
{
  TarHeader h = tarw_make_header(path, mode,
                                 w.owner, w.group, w.owner_id, w.group_id,
                                 TARW_DIRECTORY, 0);

  if (fwrite((char*)&h, 1, sizeof(h), w.sink) != sizeof(h)) return false;
  return true;
}

bool tarw_write_file(TarWriter w, const char *path, uint16_t mode,
                                  const char *buff, size_t buff_size)
{
  TarHeader h = tarw_make_header(path, mode,
                                 w.owner, w.group, w.owner_id, w.group_id,
                                 TARW_NORMAL, buff_size);

  if (fwrite((char*)&h, 1, sizeof(h), w.sink) != sizeof(h)) return false;
  if (fwrite(buff, 1, buff_size, w.sink) != buff_size) return false;

  static const char zeros[TARW_CHUNK_SIZE] = {0};
  size_t chunks_ceil = (buff_size + (TARW_CHUNK_SIZE-1))/TARW_CHUNK_SIZE;
  size_t padding = chunks_ceil*TARW_CHUNK_SIZE - buff_size;
  if (fwrite(zeros, 1, padding, w.sink) != padding) return false;
  return true;
}

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