minitar/src/tar.c

137 lines
4.0 KiB
C
Raw Normal View History

2022-11-05 19:10:48 +00:00
#include "tar.h"
2022-11-06 10:02:26 +00:00
#include "minitar.h"
2022-12-31 11:48:53 +00:00
#include <stdio.h>
#include <string.h>
2022-11-05 19:10:48 +00:00
// all of these are defined in util.c
int minitar_read_header(struct minitar*, struct tar_header*);
2022-11-21 16:38:08 +00:00
int minitar_validate_header(const struct tar_header*);
void minitar_parse_metadata_from_tar_header(const struct tar_header*, struct minitar_entry_metadata*);
size_t minitar_align_up_to_block_size(size_t);
2022-11-05 17:52:51 +00:00
int minitar_open(const char* pathname, struct minitar* out)
2022-11-05 17:52:51 +00:00
{
FILE* fp =
fopen(pathname,
"rb"); // On some systems, opening the file in binary mode might be necessary to read the file properly.
if (!fp) return -1;
out->stream = fp;
return 0;
2022-11-05 17:52:51 +00:00
}
int minitar_close(struct minitar* mp)
{
return fclose(mp->stream);
2022-11-05 19:10:48 +00:00
}
// Try to read a valid header, and construct an entry from it. If the 512-byte block at the current read offset is not a
// valid header, valid is set to 0 so we can try again with the next block. In any other case, valid is set to 1. This
// helps distinguish valid return values, null pointers that should be returned to the user (for example, EOF), and
// invalid headers where we should just try again until we find a valid one.
static int minitar_try_to_read_valid_entry(struct minitar* mp, struct minitar_entry* out, int* valid)
2022-11-05 19:10:48 +00:00
{
struct tar_header hdr;
2022-11-22 16:28:34 +00:00
*valid = 1;
if (!minitar_read_header(mp, &hdr)) return -1;
2022-11-06 10:02:26 +00:00
if (!minitar_validate_header(&hdr))
2022-11-05 19:10:48 +00:00
{
*valid = 0;
return -1;
2022-11-05 19:10:48 +00:00
}
// Fetch the current read position (which is currently pointing to the start of the entry's contents), so we can
// return back to it when reading the contents of this entry using minitar_read_contents().
if (fgetpos(mp->stream, &out->_internal._mt_position)) return -1;
minitar_parse_metadata_from_tar_header(&hdr, &out->metadata);
if (out->metadata.size)
{
size_t size_in_archive = minitar_align_up_to_block_size(out->metadata.size);
if (fseek(mp->stream, size_in_archive,
SEEK_CUR)) // move over to the next block, skipping over the file contents
{
return -1;
}
}
return 0;
2022-11-05 19:10:48 +00:00
}
int minitar_read_entry(struct minitar* mp, struct minitar_entry* out)
2022-11-05 19:10:48 +00:00
{
int valid, result;
2022-11-05 19:10:48 +00:00
do {
result = minitar_try_to_read_valid_entry(mp, out, &valid);
2022-11-22 16:28:34 +00:00
} while (!valid); // Skip over invalid entries
2022-11-05 19:10:48 +00:00
return result;
}
2022-11-06 10:22:27 +00:00
void minitar_rewind(struct minitar* mp)
{
rewind(mp->stream);
}
int minitar_find_by_name(struct minitar* mp, const char* name, struct minitar_entry* out)
{
int rc;
do {
rc = minitar_read_entry(mp, out);
if (rc == 0)
{
if (!strcmp(out->metadata.name, name)) return 0;
}
} while (rc == 0);
return -1;
}
int minitar_find_by_path(struct minitar* mp, const char* path, struct minitar_entry* out)
{
int rc;
do {
rc = minitar_read_entry(mp, out);
if (rc == 0)
{
if (!strcmp(out->metadata.path, path)) return 0;
}
} while (rc == 0);
return -1;
}
int minitar_find_any_of(struct minitar* mp, enum minitar_file_type type, struct minitar_entry* out)
{
int rc;
do {
rc = minitar_read_entry(mp, out);
if (rc == 0)
{
if (out->metadata.type == type) return 0;
}
} while (rc == 0);
return -1;
}
size_t minitar_read_contents(struct minitar* mp, const struct minitar_entry* entry, char* buf, size_t max)
{
if (!max) return 0;
if (!entry->metadata.size) return 0;
fpos_t current_position;
2022-11-23 19:40:47 +00:00
// Save the current position
if (fgetpos(mp->stream, &current_position)) return 0;
2022-11-23 19:40:47 +00:00
// Move to the position stored in the entry
if (fsetpos(mp->stream, &entry->_internal._mt_position)) return 0;
2022-11-23 19:40:47 +00:00
2022-11-23 19:56:08 +00:00
// We refuse to read more than the size indicated by the archive
if (max > entry->metadata.size) max = entry->metadata.size;
size_t nread = fread(buf, 1, max, mp->stream);
if (ferror(mp->stream)) return 0;
2022-11-23 19:40:47 +00:00
// Restore the current position
if (fsetpos(mp->stream, &current_position)) return 0;
2022-11-23 19:40:47 +00:00
return nread;
}