TARFS 0.1.5
Read-only TAR filesystem for ESP32
Loading...
Searching...
No Matches
tar.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "os.h"
#include "fs.h"
#include "tar.h"
#include "hash.h"

Go to the source code of this file.

Functions

int tar_strcmp (const char *s1, const char *s1_end, const char *s2)
 Compare an UTS/CTS to a CTS.
int tar_strncmp (const char *s1, const char *s2, size_t len)
int tar_strlen (const char *s1, const char *s1_end)
 Return the length of a TAR string.
void tar_strcpy (char *dst, const char *src)
char * tar_strdup1 (const char *s1, const char *s1_end)
 Duplicate a TAR string as a regular NUL-terminated C string.
uint32_t tar_octal (const char *p, size_t max_len)
uint32_t tar_hdrsum (tarhdr_t const *hdr)
bool tar_badhdr (tarhdr_t const *hdr)
 Validate a TAR header.
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.
bool tar_rootdir (const uint8_t *tar_start, size_t tar_length, char *base_dir, size_t out_len)
 Detect the archive root directory.
bool tar_baddata (struct tarhdr const *hdr, size_t size)
 Verify CRC64 checksum stored in a TAR archive, if present.

Function Documentation

◆ tar_baddata()

bool tar_baddata ( struct tarhdr const * hdr,
size_t size )

Verify CRC64 checksum stored in a TAR archive, if present.

Verify CRC64 checksums stored in a TAR archive, if present.

hdr must pass tar_badhdr() check!!

Verify CRC64 checksum stored in a TAR archive, if present.

hdr must pass tar_badhdr() check!!

Definition at line 529 of file tar.c.

References hash64(), and size.

Referenced by inode_populate(), tarf_open(), and tarfs_fsck().

◆ tar_badhdr()

bool tar_badhdr ( tarhdr_t const * hdr)

Validate a TAR header.

Checks:

  • entry type is supported/recognized;
  • reserved padding byte is zero;
  • checksum field contains only valid TAR characters;
  • header checksum matches the calculated value.
Parameters
hdrPointer to a TAR header.
Returns
true if the header is invalid, corrupted, or unsupported. In this case no inode should be created for this entry.

Checks:

  • entry type is supported/recognized;
  • reserved padding byte is zero;
  • checksum field contains only valid TAR characters;
  • header checksum matches the calculated value.
  • entry size is within the mmaped memory range.
Parameters
hdrPointer to a TAR header.
Returns
true if the header is invalid, corrupted, or unsupported. In this case no inode should be created for this entry.

Definition at line 249 of file tar.c.

References tar_hdrsum(), and tar_octal().

Referenced by inode_populate(), tar_getnino(), tar_rootdir(), and tarfs_fsck().

◆ tar_getnino()

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.

Quick run through the tarfile to count number of inodes we have to create Bad blocks are skipped; inodes_count = number_of_links + number_files + number_of_directoris.

Bad blocks are skipped; inodes_count = number_of_links + number_files + number_of_directoris

Quick run through the tarfile to count number of inodes we have to create.

Parameters
tar_startAddress where the TAR archive is mapped.
tar_lengthSize of the mapped archive.
Returns
Number of inodes required for this filesystem

Definition at line 306 of file tar.c.

References log, size, tar_badhdr(), and tar_octal().

Referenced by inode_mount().

◆ tar_hdrsum()

uint32_t tar_hdrsum ( tarhdr_t const * hdr)

Definition at line 221 of file tar.c.

Referenced by tar_badhdr().

◆ tar_octal()

uint32_t tar_octal ( const char * p,
size_t max_len )

◆ tar_rootdir()

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.

This function may choose wrong directory name if filesystem is damaged

The detected directory is used both as the filesystem mount point and as the path prefix to strip from every inode. For example, if the archive contains:

my_archive/
my_archive/file.txt
my_archive/dir/test.txt

then the filesystem will expose:

/file.txt
/dir/test.txt

Detection relies on the following assumptions:

  • the first directory entry (type 5) is the archive root;
  • all archive entries reside under that directory.

These assumptions are not verified. It is the caller's responsibility to provide a well-formed TAR archive.

Since the root directory becomes the mount point, it must satisfy the ESP-IDF mount-point limitations (currently no more than 16 characters, excluding the terminating NUL).

The root directory name always fits into tarhdr->name, so it is guaranteed to be NUL-terminated and never uses the TAR 'prefix' field.

Parameters
tar_startAddress where the TAR archive is mapped.
tar_lengthSize of the mapped archive.
base_dirOutput buffer receiving the detected root directory.
base_dir_lenOutput buffer size. Recommended size is 100 bytes
Returns
true if a root directory was found.

Definition at line 372 of file tar.c.

References log, size, tar_badhdr(), tar_octal(), tar_strlen(), and tarfs_os_mp_maxlen().

Referenced by tarfs_mount_memory().

◆ tar_strcmp()

int tar_strcmp ( const char * s1,
const char * s1_end,
const char * s2 )

Compare an UTS/CTS to a CTS.

TAR string fields are not required to be NUL-terminated. A string ends at the first '\0', '\r' or '
', or at s1_end if no terminator appears before that point.

Parameters
s1TAR string.
s1_endPointer to the byte immediately following the last valid character in s1, or NULL if s1 is guaranteed to contain a TAR string terminator.
s2Regular NUL-terminated C string.
Returns
<0 if s1 is lexicographically less than s2, >0 if s1 is lexicographically greater than s2, 0 if both strings are equal.

Definition at line 48 of file tar.c.

◆ tar_strcpy()

void tar_strcpy ( char * dst,
const char * src )

Definition at line 153 of file tar.c.

◆ tar_strdup1()

char * tar_strdup1 ( const char * s1,
const char * s1_end )

Duplicate a TAR string as a regular NUL-terminated C string.

The returned buffer contains one additional byte after the terminating NUL, allowing a single character (typically '/') to be appended without reallocating. This is used during link resolution.

string\0\0 ^_— Here one could append one character to extend a string by one character

Parameters
s1TAR string.
s1_endPointer to the byte immediately following the last valid character in s1, or NULL if s1 contains a TAR string terminator.
Returns
Newly allocated NUL-NULL-terminated copy of the string, or NULL on allocation failure.

Definition at line 171 of file tar.c.

References tar_strlen(), and tarfs_os_malloc().

Referenced by inode_populate(), tard_fdopendir(), and tarf_open().

◆ tar_strlen()

int tar_strlen ( const char * s1,
const char * s1_end )

Return the length of a TAR string.

A TAR string ends at the first '\0', '\r' or '
'. If none of these characters is present before s1_end, then s1_end is treated as the end of the string.

Parameters
s1TAR string.
s1_endPointer to the byte immediately following the last valid character in s1, or NULL if s1 contains a TAR string terminator.
Returns
Length of the string, excluding the terminator.

Definition at line 131 of file tar.c.

Referenced by inode_populate(), tar_rootdir(), tar_strdup1(), and tard_readdir().

◆ tar_strncmp()

int tar_strncmp ( const char * s1,
const char * s2,
size_t len )

Definition at line 90 of file tar.c.