TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
tar.h
Go to the documentation of this file.
1/*
2 * TARFS - Immutable (read-only) filesystem for embedded systems.
3 *
4 * Copyright (c) 2024-2026 Viacheslav Logunov
5 * SPDX-License-Identifier: MMIT
6 *
7 * Author:
8 * Viacheslav Logunov <vvb333007@gmail.com>
9 *
10 * Project:
11 * https://github.com/vvb333007/tarfs
12 *
13 * @file tar.h
14 * @brief Public tar file API
15 */
16#pragma once
18#include "config.h"
19
25typedef enum __attribute__((packed)) {
26
27 TART_AFILE = '\0',
28 TART_FILE = '0',
29
30 TART_HARDLINK = '1',
31 TART_SYMLINK = '2',
32 TART_DIR = '5',
33
34 TART_PAX = 'x',
35
36 // Ignored by TARFS
37 TART_CHRDEV = '3',
38 TART_BLKDEV = '4',
39 TART_FIFO = '6',
40 TART_CONT = '7',
41 TART_PAX_G = 'g',
42
43 /* Not a real type, used as a return value to indicate a bad type */
44 TART_BAD = 255,
45
47
54
55struct tarhdr {
56
57 const char name[100];
58 const char mode[8];
59 const char uid[8];
60 const char gid[8];
61 const char size[12];
62 const char mtime[12];
63 char checksum[8];
64 const tart_t type;
65 const char link_name[100];
66 const char magic[6];
67 const char version[2];
68 const char user[32];
69 const char group[32];
70 const char major[8];
71 const char minor[8];
72 const char prefix[155];
73 char zero;
74#if CONFIG_TARFS_INTEGRITY
75 char md[3];
77 char digest[8];
78#else
79 const char pad[11];
80#endif
81
82} __attribute__((packed));
83
84typedef struct tarhdr tarhdr_t;
85
86#ifdef __cplusplus
87extern "C" {
88#endif
89
90/* Checked TAR String == "CTS", a byte sequence within a TAR archive terminated
91 * by '\0', '\r', or '\n'.
92 * Unchecked TAR String == "UTS", a byte sequence that may not be terminated.
93 * C string == a byte sequence terminated by '\0'.
94 *
95 * Strings stored in a TAR archive may or may not include a terminating
96 * character. For this reason, TARFS provides a set of string helper
97 * functions that mimic the behavior of the standard string.h functions.
98 *
99 * When operating on an Unchecked TAR String (UTS), the caller must provide
100 * a pointer to the end of the string (e.g. tar_strcmp(), tar_strlen(), ...).
101 */
102
103
121int tar_strcmp(const char *s1, const char *s1_end, const char *s2);
122
123/*
124 * strncmp() for two CTS, len is the hard limit
125 *
126 */
127int tar_strncmp(const char *s1, const char *s2, size_t len);
128
129
144int tar_strlen(const char *s1, const char *s1_end);
145
164char *tar_strdup1(const char *s1, const char *s1_end);
165
166/* Parse an octal number from a TAR field.
167 *
168 * Unlike strtol(), the input is not required to be NUL-terminated.
169 * Parsing stops after max_len characters or at the first non-octal
170 * character.
171 *
172 * @param p buffer start
173 * @param max_len buffer length in bytes
174 * @return decimal value
175 */
176uint32_t tar_octal(const char *p, size_t max_len);
177
178#if CONFIG_TARFS_LOG
179/* Devel only:
180 * Displays CTS `buf`; `end` is the UTS_limit
181 *
182 * We dont go past the `end` pointer even if we didnt find any line
183 * terminator
184 */
185void tar_print(const char *buf, const char *end);
186#endif /* CONFIG_TARFS_LOG */
187
188
202bool tar_badhdr(tarhdr_t const * hdr);
203
212int tar_getnino(const uint8_t *tar_start, size_t tar_length);
213
214
253bool tar_rootdir(const uint8_t *tar_start, size_t tar_length, char *base_dir, size_t base_dir_len);
254
255
256
257/* Calculate the checksum of a TAR header.
258 *
259 * While the checksum is being calculated, the checksum field itself
260 * must be treated as eight ASCII space characters, as required by the
261 * TAR format specification.
262 *
263 * The checksum field occupies bytes [148..156).
264 *
265 * @param hdr Pointer to the TAR header.
266 * @return Header checksum.
267 */
268uint32_t tar_hdrsum(const tarhdr_t *hdr);
269
270
275bool tar_baddata(struct tarhdr const *hdr, size_t size);
276
277
278#ifdef TARSUM_BUILD
292int tar_addsum(uint8_t *tar_start, size_t tar_length);
293#endif /* TARSUM_BUILD */
294
295
296#ifdef __cplusplus
297};
298#endif
299
300_Static_assert(sizeof(tarhdr_t) == 512, "sizeof(tarhdr_t) != 512, code review is required");
301_Static_assert(sizeof(tart_t) == 1, "sizeof(tar_type_t) != 1, code review is required");
For TAR files with modified PADDING field (see tarsum.c TARFS Checksum Utility): The type and meaning...
Definition tar.h:55
const char group[32]
Definition tar.h:69
const char minor[8]
Definition tar.h:71
const char pad[11]
Definition tar.h:79
const char version[2]
Definition tar.h:67
const char mtime[12]
Definition tar.h:62
const tart_t type
Definition tar.h:64
char zero
Definition tar.h:73
const char size[12]
Definition tar.h:61
const char name[100]
Definition tar.h:57
const char prefix[155]
Definition tar.h:72
const char major[8]
Definition tar.h:70
const char uid[8]
Definition tar.h:59
char checksum[8]
Definition tar.h:63
const char magic[6]
Definition tar.h:66
const char mode[8]
Definition tar.h:58
const char user[32]
Definition tar.h:68
const char gid[8]
Definition tar.h:60
const char link_name[100]
Definition tar.h:65
int tar_strncmp(const char *s1, const char *s2, size_t len)
Definition tar.c:90
char * tar_strdup1(const char *s1, const char *s1_end)
Duplicate a TAR string as a regular NUL-terminated C string.
Definition tar.c:171
uint32_t tar_octal(const char *p, size_t max_len)
Definition tar.c:193
uint32_t tar_hdrsum(const tarhdr_t *hdr)
Definition tar.c:221
bool tar_badhdr(tarhdr_t const *hdr)
Validate a TAR header.
Definition tar.c:249
int tar_getnino(const uint8_t *tar_start, size_t tar_length)
Quick run through the tarfile to count number of inodes we have to create Bad blocks are skipped; ino...
Definition tar.c:306
tart_t
Definition tar.h:46
int tar_strcmp(const char *s1, const char *s1_end, const char *s2)
Compare an UTS/CTS to a CTS.
Definition tar.c:48
enum __attribute__((packed))
"TART" == "TAR Type" tarfile entry (header) types, 1-byte wide.
Definition tar.h:25
int tar_strlen(const char *s1, const char *s1_end)
Return the length of a TAR string.
Definition tar.c:131
bool tar_baddata(struct tarhdr const *hdr, size_t size)
Verify CRC64 checksums stored in a TAR archive, if present.
Definition tar.c:529
const char size[12]
Definition tar.h:5
bool tar_rootdir(const uint8_t *tar_start, size_t tar_length, char *base_dir, size_t base_dir_len)
Detect the archive root directory.
Definition tar.c:372