aboutsummaryrefslogtreecommitdiff
path: root/include/tar/tar.h
blob: 128464edf931a0bc7ff257041657bbd574e36e31 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * tar.h
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#ifndef TAR_H
#define TAR_H

#include "config.h"
#include "compat.h"
#include "io/istream.h"
#include "io/ostream.h"

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

typedef struct sparse_map_t {
	struct sparse_map_t *next;
	sqfs_u64 offset;
	sqfs_u64 count;
} sparse_map_t;

typedef struct tar_xattr_t {
	struct tar_xattr_t *next;
	char *key;
	sqfs_u8 *value;
	size_t value_len;
	char data[];
} tar_xattr_t;

typedef struct {
	char *name;
	char *link_target;
	sparse_map_t *sparse;
	sqfs_u64 actual_size;
	sqfs_u64 record_size;
	bool unknown_record;
	bool is_hard_link;
	tar_xattr_t *xattr;

	sqfs_u16 mode;
	sqfs_u64 uid;
	sqfs_u64 gid;
	sqfs_u64 devno;
	sqfs_s64 mtime;
} tar_header_decoded_t;

#ifdef __cplusplus
extern "C" {
#endif

/*
  Returns < 0 on failure, > 0 if cannot encode, 0 on success.
  Prints error/warning messages to stderr.

  The counter is an incremental record counter used if additional
  headers need to be generated.
*/
int write_tar_header(ostream_t *fp, const struct stat *sb, const char *name,
		     const char *slink_target, const tar_xattr_t *xattr,
		     unsigned int counter);

int write_hard_link(ostream_t *fp, const struct stat *sb, const char *name,
		    const char *target, unsigned int counter);

/* calcuate and skip the zero padding */
int skip_padding(istream_t *fp, sqfs_u64 size);

/* round up to block size and skip the entire entry */
int skip_entry(istream_t *fp, sqfs_u64 size);

int read_header(istream_t *fp, tar_header_decoded_t *out);

void clear_header(tar_header_decoded_t *hdr);

/*
  Write zero bytes to an output file to padd it to the tar record size.
  Returns 0 on success. On failure, prints error message to stderr.
*/
int padd_file(ostream_t *fp, sqfs_u64 size);

void free_sparse_list(sparse_map_t *sparse);

void free_xattr_list(tar_xattr_t *list);

#ifdef __cplusplus
}
#endif

#endif /* TAR_H */