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 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* dump2tar - tool to dump files and command output into a tar archive
*
* TAR file generation
*
* Copyright IBM Corp. 2016, 2017
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdio.h>
#include <string.h>
#include "buffer.h"
#include "idcache.h"
#include "misc.h"
#include "tar.h"
#define LONGLINK "././@LongLink"
#define TYPE_LONGLINK 'K'
#define TYPE_LONGNAME 'L'
#define BLOCKSIZE 512
#if __has_attribute(nonstring)
# define __nonstring __attribute__ ((nonstring))
#else
# define __nonstring
#endif
/* Basic TAR header */
struct tar_header {
char name[100] __nonstring;
char mode[8];
char uid[8];
char gid[8];
char size[12];
char mtime[12];
char chksum[8];
char typeflag;
char linkname[100] __nonstring;
char magic[6];
char version[2];
char uname[32];
char gname[32];
char devmajor[8];
char devminor[8];
char prefix[155];
};
/* Store the octal value of @value to at most @len bytes at @dest */
static void set_octal(char *dest, size_t len, unsigned long value)
{
int i;
dest[len - 1] = 0;
for (i = len - 2; i >= 0; i--) {
dest[i] = '0' + (value & 7);
value >>= 3;
}
}
/* Store time @value to at most @len bytes at @dest */
static void set_time(char *dest, size_t len, time_t value)
{
time_t max = (1ULL << (3 * (len - 1))) - 1;
if (value >= 0 && value <= max) {
set_octal(dest, len, value);
return;
}
for (; len > 0; len--) {
dest[len - 1] = value & 0xff;
value >>= 8;
}
dest[0] |= 0x80;
}
#define SET_FIELD(obj, name, value) \
set_octal((obj)->name, sizeof((obj)->name), (unsigned long) (value))
#define SET_TIME_FIELD(obj, name, value) \
set_time((obj)->name, sizeof((obj)->name), (time_t) (value))
#define SET_STR_FIELD(obj, name, value) \
strncpy((obj)->name, (value), sizeof((obj)->name))
/* Initialize the tar file @header with the provided data */
static void init_header(struct tar_header *header, const char *filename,
const char *link, size_t len, struct stat *stat,
char type)
{
unsigned int i, checksum;
unsigned char *c;
memset(header, 0, sizeof(*header));
/* Fill in header fields */
SET_STR_FIELD(header, name, filename);
if (link)
SET_STR_FIELD(header, linkname, link);
SET_FIELD(header, size, len);
if (stat) {
SET_FIELD(header, mode, stat->st_mode & 07777);
SET_FIELD(header, uid, stat->st_uid);
SET_FIELD(header, gid, stat->st_gid);
SET_TIME_FIELD(header, mtime, stat->st_mtime);
uid_to_name(stat->st_uid, header->uname, sizeof(header->uname));
gid_to_name(stat->st_gid, header->gname, sizeof(header->gname));
} else {
SET_FIELD(header, mode, 0644);
SET_FIELD(header, uid, 0);
SET_FIELD(header, gid, 0);
SET_TIME_FIELD(header, mtime, 0);
uid_to_name(0, header->uname, sizeof(header->uname));
gid_to_name(0, header->gname, sizeof(header->gname));
}
header->typeflag = type;
memcpy(header->magic, "ustar ", sizeof(header->magic));
memcpy(header->version, " ", sizeof(header->version));
/* Calculate checksum */
memset(header->chksum, ' ', sizeof(header->chksum));
checksum = 0;
c = (unsigned char *) header;
for (i = 0; i < sizeof(*header); i++)
checksum += c[i];
snprintf(header->chksum, 7, "%06o", checksum);
}
/* Emit zero bytes via @emit_cb to pad @len to a multiple of BLOCKSIZE */
static int emit_padding(emit_cb_t emit_cb, void *data, size_t len)
{
size_t pad = BLOCKSIZE - len % BLOCKSIZE;
char zeroes[BLOCKSIZE];
if (len % BLOCKSIZE > 0) {
memset(zeroes, 0, BLOCKSIZE);
return emit_cb(data, zeroes, pad);
}
return 0;
}
/* Emit @len bytes at @addr via @emit_cb and pad data to BLOCKSIZE with zero
* bytes */
static int emit_data(emit_cb_t emit_cb, void *data, void *addr, size_t len)
{
int rc;
if (len == 0)
return 0;
rc = emit_cb(data, addr, len);
if (rc)
return rc;
return emit_padding(emit_cb, data, len);
}
/* Emit a tar header via @emit_cb */
static int emit_header(emit_cb_t emit_cb, void *data, char *filename,
char *link, size_t len, struct stat *stat, char type)
{
struct tar_header header;
size_t namelen = strlen(filename);
size_t linklen;
int rc;
/* /proc can contain unreadable links which causes tar to complain
* during extract - use a dummy value to handle this more gracefully */
if (link && !*link)
link = " ";
linklen = link ? strlen(link) : 0;
if (linklen > sizeof(header.linkname)) {
rc = emit_header(emit_cb, data, LONGLINK, NULL, linklen + 1,
NULL, TYPE_LONGLINK);
if (rc)
return rc;
rc = emit_data(emit_cb, data, link, linklen + 1);
if (rc)
return rc;
}
if (namelen > sizeof(header.name)) {
rc = emit_header(emit_cb, data, LONGLINK, NULL, namelen + 1,
NULL, TYPE_LONGNAME);
if (rc)
return rc;
rc = emit_data(emit_cb, data, filename, namelen + 1);
if (rc)
return rc;
}
init_header(&header, filename, link, len, stat, type);
return emit_data(emit_cb, data, &header, sizeof(header));
}
struct emit_content_cb_data {
emit_cb_t emit_cb;
void *data;
size_t len;
int rc;
};
/* Callback for emitting a single chunk of data of a buffer */
static int emit_content_cb(void *data, void *addr, size_t len)
{
struct emit_content_cb_data *cb_data = data;
if (len > cb_data->len)
len = cb_data->len;
cb_data->len -= len;
cb_data->rc = cb_data->emit_cb(cb_data->data, addr, len);
if (cb_data->rc || cb_data->len == 0)
return 1;
return 0;
}
/* Emit at most @len bytes of contents of @buffer via @emit_cb and pad output
* to BLOCKSIZE with zero bytes */
static int emit_content(emit_cb_t emit_cb, void *data, struct buffer *buffer,
size_t len)
{
struct emit_content_cb_data cb_data;
cb_data.emit_cb = emit_cb;
cb_data.data = data;
cb_data.len = len;
cb_data.rc = 0;
buffer_iterate(buffer, emit_content_cb, &cb_data);
if (cb_data.rc)
return cb_data.rc;
return emit_padding(emit_cb, data, buffer->total);
}
/* Convert file meta data and content specified as @content into a
* stream of bytes that is reported via the @emit_cb callback. @data is
* passed through to the callback for arbitrary use. */
int tar_emit_file_from_buffer(char *filename, char *link, size_t len,
struct stat *stat, char type,
struct buffer *content, emit_cb_t emit_cb,
void *data)
{
int rc;
DBG("emit tar file=%s type=%d len=%zu", filename, type, len);
rc = emit_header(emit_cb, data, filename, link, len, stat, type);
if (rc)
return rc;
if (content)
rc = emit_content(emit_cb, data, content, len);
return rc;
}
/* Convert file meta data and content specified as @addr and @len into a
* stream of bytes that is reported via the @emit_cb callback. @data is
* passed through to the callback for arbitrary use. */
int tar_emit_file_from_data(char *filename, char *link, size_t len,
struct stat *stat, char type, void *addr,
emit_cb_t emit_cb, void *data)
{
int rc;
DBG("emit tar file=%s type=%d len=%zu", filename, type, len);
rc = emit_header(emit_cb, data, filename, link, len, stat, type);
if (rc)
return rc;
if (addr)
rc = emit_data(emit_cb, data, addr, len);
return rc;
}
|