PKUOS - Pintos
Pintos source browser for PKU Operating System course
Data Structures | Functions | Variables
ustar.c File Reference
#include <ustar.h>
#include <limits.h>
#include <packed.h>
#include <stdio.h>
#include <string.h>
Include dependency graph for ustar.c:

Go to the source code of this file.

Data Structures

struct  ustar_header
 Header for ustar-format tar archive. More...
 

Functions

static unsigned int calculate_chksum (const struct ustar_header *h)
 Returns the checksum for the given ustar format HEADER. More...
 
static const char * strip_antisocial_prefixes (const char *file_name)
 Drop possibly dangerous prefixes from FILE_NAME and return the stripped name. More...
 
bool ustar_make_header (const char *file_name, enum ustar_type type, int size, char header[USTAR_HEADER_SIZE])
 Composes HEADER as a USTAR_HEADER_SIZE (512)-byte archive header in ustar format for a SIZE-byte file named FILE_NAME of the given TYPE. More...
 
static bool parse_octal_field (const char *s, size_t size, unsigned long int *value)
 Parses a SIZE-byte octal field in S in the format used by ustar format. More...
 
static bool is_all_zeros (const char *block, size_t cnt)
 Returns true if the CNT bytes starting at BLOCK are all zero, false otherwise. More...
 
const char * ustar_parse_header (const char header[USTAR_HEADER_SIZE], const char **file_name, enum ustar_type *type, int *size)
 Parses HEADER as a ustar-format archive header for a regular file or directory. More...
 

Variables

struct ustar_header PACKED
 

Function Documentation

◆ calculate_chksum()

static unsigned int calculate_chksum ( const struct ustar_header h)
static

Returns the checksum for the given ustar format HEADER.

Definition at line 37 of file ustar.c.

References ustar_header::chksum, offsetof, and USTAR_HEADER_SIZE.

Referenced by ustar_make_header(), and ustar_parse_header().

Here is the caller graph for this function:

◆ is_all_zeros()

static bool is_all_zeros ( const char *  block,
size_t  cnt 
)
static

Returns true if the CNT bytes starting at BLOCK are all zero, false otherwise.

Definition at line 167 of file ustar.c.

Referenced by ustar_parse_header().

Here is the caller graph for this function:

◆ parse_octal_field()

static bool parse_octal_field ( const char *  s,
size_t  size,
unsigned long int *  value 
)
static

Parses a SIZE-byte octal field in S in the format used by ustar format.

If successful, stores the field's value in VALUE and returns true; on failure, returns false.

ustar octal fields consist of a sequence of octal digits terminated by a space or a null byte. The ustar specification seems ambiguous as to whether these fields must be padded on the left with '0's, so we accept any field that fits in the available space, regardless of whether it fills the space.

Definition at line 130 of file ustar.c.

References s, ustar_header::size, and ULONG_MAX.

Referenced by ustar_parse_header().

Here is the caller graph for this function:

◆ strip_antisocial_prefixes()

static const char * strip_antisocial_prefixes ( const char *  file_name)
static

Drop possibly dangerous prefixes from FILE_NAME and return the stripped name.

An archive with file names that start with "/" or "../" could cause a naive tar extractor to write to arbitrary parts of the file system, not just the destination directory. We don't want to create such archives or be such a naive extractor.

The return value can be a suffix of FILE_NAME or a string literal.

Definition at line 66 of file ustar.c.

References file_name, memcmp(), strchr(), and strcmp().

Referenced by ustar_make_header(), and ustar_parse_header().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustar_make_header()

bool ustar_make_header ( const char *  file_name,
enum ustar_type  type,
int  size,
char  header[USTAR_HEADER_SIZE] 
)

Composes HEADER as a USTAR_HEADER_SIZE (512)-byte archive header in ustar format for a SIZE-byte file named FILE_NAME of the given TYPE.

The caller is responsible for writing the header to a file or device.

If successful, returns true. On failure (due to an excessively long file name), returns false.

Definition at line 83 of file ustar.c.

References ASSERT, calculate_chksum(), ustar_header::chksum, file_name, ustar_header::gid, ustar_header::gname, ustar_header::magic, memset(), ustar_header::mode, ustar_header::mtime, ustar_header::name, printf(), ustar_header::size, snprintf(), strip_antisocial_prefixes(), strlcpy(), strlen(), ustar_header::typeflag, ustar_header::uid, ustar_header::uname, USTAR_DIRECTORY, USTAR_HEADER_SIZE, USTAR_REGULAR, and ustar_header::version.

Referenced by fsutil_append(), and write_header().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustar_parse_header()

const char * ustar_parse_header ( const char  header[USTAR_HEADER_SIZE],
const char **  file_name,
enum ustar_type type,
int *  size 
)

Parses HEADER as a ustar-format archive header for a regular file or directory.

lib/ustar.h

If successful, stores the archived file's name in *FILE_NAME (as a pointer into HEADER or a string literal), its type in *TYPE, and its size in bytes in *SIZE, and returns a null pointer. On failure, returns a human-readable error message.

Definition at line 182 of file ustar.c.

References ASSERT, calculate_chksum(), ustar_header::chksum, file_name, INT_MAX, is_all_zeros(), ustar_header::magic, memcmp(), ustar_header::name, NULL, parse_octal_field(), ustar_header::prefix, ustar_header::size, strip_antisocial_prefixes(), ustar_header::typeflag, USTAR_DIRECTORY, USTAR_EOF, USTAR_HEADER_SIZE, USTAR_REGULAR, and ustar_header::version.

Referenced by fsutil_extract().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ PACKED

struct ustar_header PACKED

Referenced by read_partition_table().