seahag/haggis.c

985 lines
26 KiB
C
Raw Normal View History

2023-07-20 01:06:38 -04:00
/* _,.---._ .-._ .--.-. ,--.--------.
2023-07-19 22:38:59 -04:00
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
2023-07-20 01:06:38 -04:00
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
2023-07-19 22:38:59 -04:00
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
2023-07-20 01:06:38 -04:00
* |==|,| | -|==| , '=' |==| - _ | |==|- |
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
* _ __ ,---. .-._ .=-.-. _,.----.
2023-07-19 22:38:59 -04:00
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
2023-07-20 01:06:38 -04:00
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
2023-07-19 22:38:59 -04:00
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
2023-07-20 01:06:38 -04:00
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
2023-07-19 22:38:59 -04:00
* `--`---' `--` `--`./ `--``--`-`
*
* @(#)Copyright (c) 2023, Nathan D. Fisher.
*
* This is free software. It comes with NO WARRANTY.
* Permission to use, modify and distribute this source code
* is granted subject to the following conditions.
* 1/ that the above copyright notice and this notice
* are preserved in all copies and that due credit be given
* to the author.
* 2/ that any changes to this code are clearly commented
* as such so that the author does not get blamed for bugs
* other than his own.
*/
#include "haggis.h"
#include "mq.h"
2023-10-03 13:04:50 -04:00
#include <assert.h> // assert
#include <errno.h> // errno
#include <libgen.h> // dirname
#include <limits.h> // PATH_MAX
2023-10-03 13:04:50 -04:00
#include <stddef.h> // size_t
#include <stdint.h> // uint<x>_t
2023-08-07 18:55:47 -04:00
2023-07-23 23:42:51 -04:00
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sha.h>
#include <sha256.h>
2023-10-03 13:04:50 -04:00
#include <sys/types.h> // dev_t, makedev, major, minor
2023-07-23 23:42:51 -04:00
#elif defined(__NetBSD__) || defined(__OpenBSD__)
#include <sha1.h>
#include <sha2.h>
2023-10-03 13:04:50 -04:00
#include <sys/types.h> // dev_t, makedev, major, minor
2023-07-23 23:42:51 -04:00
#elif defined(__linux__)
#include <sha1.h>
#include <sha2.h>
#include <sys/sysmacros.h> // major, minor, dev_t
#endif /* if defined (__FreeBSD__) */
#include <md5.h>
#include <stdio.h> // fopen, fread, fwrite, FILE
#include <stdlib.h> // free, malloc, calloc
2023-10-03 13:04:50 -04:00
#include <string.h> // memcpy, strnlen, strndup
#include <unistd.h> // access, dirname, geteuid, readlink
#include <sys/stat.h> // stat, lstat, mkdir, mknod
2023-07-19 22:38:59 -04:00
#include "haggis_private.h"
2023-07-19 22:38:59 -04:00
2023-07-20 01:06:38 -04:00
static unsigned char header[7] = {0x89, 'h', 'a', 'g', 'g', 'i', 's'};
int haggis_store_header(FILE *stream) {
if (fwrite(header, 1, 7, stream) < 7)
return 1;
return 0;
}
int haggis_check_header(FILE *stream) {
unsigned char *buf[7];
if (fread(buf, 1, 7, stream) < 7)
return 1;
if (memcmp(buf, header, 7))
return 2;
2023-08-09 15:28:02 -04:00
return 0;
2023-07-20 01:06:38 -04:00
}
void haggis_device_init(dev_t rdev, haggis_device *dev) {
2023-08-07 18:55:47 -04:00
dev->major.val = (uint32_t)major(rdev);
dev->minor.val = (uint32_t)minor(rdev);
}
int haggis_store_device(FILE *stream, haggis_device *dev) {
if (fwrite(dev->major.bytes, 1, 4, stream) != 4)
return 1;
if (fwrite(dev->minor.bytes, 1, 4, stream) != 4)
return 1;
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_load_device(FILE *stream, haggis_device *dev) {
if (fread(dev->major.bytes, 1, 4, stream) != 4)
return 1;
if (fread(dev->minor.bytes, 1, 4, stream) != 4)
return 1;
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_store_cksum(FILE *stream, haggis_checksum *cksum) {
u8 flag;
2023-07-20 01:06:38 -04:00
switch (cksum->tag) {
case md5:
flag = 0;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
if (fwrite(cksum->sum.md5, 1, 16, stream) != 16)
2023-07-20 01:06:38 -04:00
return 1;
break;
case sha1:
flag = 1;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
if (fwrite(cksum->sum.sha1, 1, 20, stream) != 20)
2023-07-20 01:06:38 -04:00
return 1;
break;
case sha256:
flag = 2;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
if (fwrite(cksum->sum.sha256, 1, 32, stream) != 32)
2023-07-20 01:06:38 -04:00
return 1;
break;
case skip:
flag = 3;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
break;
}
return 0;
}
int haggis_load_cksum(FILE *stream, haggis_checksum *cksum) {
u8 flag;
2023-07-20 01:06:38 -04:00
if (fread(&flag, 1, 1, stream) != 1)
return 1;
switch (flag) {
case md5:
cksum->tag = 0;
if (fread(&cksum->sum.md5, 1, 16, stream) != 16)
2023-07-20 01:06:38 -04:00
return 1;
break;
case sha1:
cksum->tag = 1;
if (fread(&cksum->sum.sha1, 1, 20, stream) != 20)
2023-07-20 01:06:38 -04:00
return 1;
break;
case sha256:
cksum->tag = 2;
if (fread(&cksum->sum.sha256, 1, 32, stream) != 32)
2023-07-20 01:06:38 -04:00
return 1;
break;
case skip:
cksum->tag = 3;
break;
}
return 0;
}
2023-08-09 15:28:02 -04:00
void init_md5(haggis_file *f) {
MD5_CTX ctx;
f->cksum.tag = md5;
MD5Init(&ctx);
MD5Update(&ctx, f->data, (size_t)f->len.val);
MD5Final(f->cksum.sum.md5, &ctx);
}
int validate_md5(haggis_file *file) {
MD5_CTX ctx;
u8 digest[16];
MD5Init(&ctx);
2023-07-23 23:42:51 -04:00
MD5Update(&ctx, file->data, (size_t)file->len.val);
MD5Final(digest, &ctx);
if (memcmp(file->cksum.sum.md5, digest, sizeof(digest)))
return 2;
return 0;
}
2023-07-23 23:42:51 -04:00
#if defined(__FreeBSD__) || defined(__DragonFly__)
2023-08-09 15:28:02 -04:00
void init_sha1(haggis_file *f) {
SHA1_CTX ctx;
f->cksum.tag = sha1;
SHA1_Init(&ctx);
SHA1_Update(&ctx, f->data, (size_t)f->len.val);
SHA1_Final(f->cksum.sum.sha1, &ctx);
}
int validate_sha1(haggis_file *file) {
SHA1_CTX ctx;
u8 digest[20];
SHA1_Init(&ctx);
2023-07-23 23:42:51 -04:00
SHA1_Update(&ctx, file->data, (size_t)file->len.val);
SHA1_Final(digest, &ctx);
if (memcmp(file->cksum.sum.sha1, digest, sizeof(digest)))
return 2;
return 0;
}
2023-07-23 23:42:51 -04:00
#elif defined(__linux__) || defined(__NetBSD__) || defined(__OpenBSD__)
2023-08-09 15:28:02 -04:00
void init_sha1(haggis_file *f) {
SHA1_CTX ctx;
f->cksum.tag = sha1;
SHA1Init(&ctx);
SHA1Update(&ctx, f->data, (size_t)f->len.val);
SHA1Final(f->cksum.sum.sha1, &ctx);
2023-08-09 15:28:02 -04:00
}
int validate_sha1(haggis_file *file) {
SHA1_CTX ctx;
u8 digest[20];
SHA1Init(&ctx);
2023-07-23 23:42:51 -04:00
SHA1Update(&ctx, file->data, (size_t)file->len.val);
SHA1Final(digest, &ctx);
if (memcmp(file->cksum.sum.sha1, digest, sizeof(digest)))
return 2;
return 0;
}
#endif /* if defined (__FreeBSD__) */
2023-07-23 23:42:51 -04:00
#if defined(__FreeBSD__) || defined(__DragonFly) || defined(__NetBSD__)
2023-08-09 15:28:02 -04:00
void init_sha256(haggis_file *f) {
SHA256_CTX ctx;
f->cksum.tag = sha256;
SHA256_Init(&ctx);
SHA256_Update(&ctx, f->data, (size_t)f->len.val);
SHA256_Final(f->cksum.sum.sha256, &ctx);
}
int validate_sha256(haggis_file *file) {
SHA256_CTX ctx;
u8 digest[32];
SHA256_Init(&ctx);
2023-07-23 23:42:51 -04:00
SHA256_Update(&ctx, file->data, (size_t)file->len.val);
SHA256_Final(digest, &ctx);
if (memcmp(file->cksum.sum.sha256, digest, sizeof(digest)))
return 2;
return 0;
}
2023-07-23 23:42:51 -04:00
#elif defined(__linux__) || defined(__OpenBSD__)
2023-08-09 15:28:02 -04:00
void init_sha256(haggis_file *f) {
SHA2_CTX ctx;
f->cksum.tag = sha256;
SHA256Init(&ctx);
SHA256Update(&ctx, f->data, (size_t)f->len.val);
SHA256Final(f->cksum.sum.sha256, &ctx);
2023-08-09 15:28:02 -04:00
}
int validate_sha256(haggis_file *file) {
SHA2_CTX ctx;
u8 digest[32];
SHA256Init(&ctx);
2023-07-23 23:42:51 -04:00
SHA256Update(&ctx, file->data, (size_t)file->len.val);
SHA256Final(digest, &ctx);
if (memcmp(file->cksum.sum.sha256, digest, sizeof(digest)))
return 2;
return 0;
}
#endif /* if defined (__FreeBSD__) */
2023-08-09 15:28:02 -04:00
void haggis_init_cksum(haggis_file *f, haggis_algorithm a) {
switch (a) {
case md5:
init_md5(f);
break;
case sha1:
init_sha1(f);
break;
case sha256:
init_sha256(f);
break;
case skip:
break;
}
}
int haggis_validate_cksum(haggis_file *file) {
switch (file->cksum.tag) {
case md5:
2023-07-23 23:42:51 -04:00
return validate_md5(file);
case sha1:
2023-07-23 23:42:51 -04:00
return validate_sha1(file);
case sha256:
2023-07-23 23:42:51 -04:00
return validate_sha256(file);
case skip:
return 0;
}
2023-07-20 01:06:38 -04:00
return 0;
}
2023-08-09 15:28:02 -04:00
int haggis_file_init(char *path, haggis_file *hf, haggis_algorithm a) {
2023-08-07 18:55:47 -04:00
FILE *f;
long len;
f = fopen(path, "r");
if (f == NULL) return 2;
2023-08-07 18:55:47 -04:00
if (fseek(f, 0, SEEK_END) == -1) {
fclose(f);
return 2;
2023-08-07 18:55:47 -04:00
}
len = ftell(f);
if (len == -1) {
fclose(f);
return 2;
2023-08-07 18:55:47 -04:00
}
hf->len.val = (uint64_t)len;
rewind(f);
hf->data = calloc(1, (size_t)len);
2023-08-07 18:55:47 -04:00
if (fread(hf->data, 1, (size_t)len, f) != (size_t)len) {
free(hf->data);
fclose(f);
return 1;
2023-08-07 18:55:47 -04:00
}
fclose(f);
2023-08-09 15:28:02 -04:00
haggis_init_cksum(hf, a);
return 0;
2023-08-07 18:55:47 -04:00
}
void haggis_file_deinit(haggis_file *f) {
if (f->data != NULL) free(f->data);
}
int haggis_store_file(FILE *stream, haggis_file *file) {
if (store_u64(stream, &file->len) != 8)
return 1;
if (haggis_store_cksum(stream, &file->cksum) != 0)
return 1;
int res = fwrite(file->data, 1, (size_t)file->len.val, stream);
if (res != (size_t)file->len.val)
return 1;
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_load_file(FILE *stream, haggis_file *f) {
if (load_u64(stream, &f->len) != 8)
return 1;
if (haggis_load_cksum(stream, &f->cksum) != 0)
return 1;
f->data = calloc(1, (size_t)f->len.val);
if (f->data == NULL)
2023-07-25 19:50:46 -04:00
return -1;
int res = fread(f->data, 1, (size_t)f->len.val, stream);
if (res != (size_t)f->len.val) {
fprintf(stderr, "Error reading file:\n length: %i\n expected: %lu\n",
res, f->len.val);
free(f->data);
return 1;
2023-07-23 23:42:51 -04:00
}
if (haggis_validate_cksum(f)) {
free(f->data);
2023-07-23 23:42:51 -04:00
return 1;
}
return 0;
}
void haggis_filename_init(char *target, haggis_filename *fname) {
2023-08-07 18:55:47 -04:00
size_t len;
len = strlen(target);
2023-08-07 18:55:47 -04:00
fname->len.val = (uint16_t)len;
fname->name = target;
}
void haggis_filename_deinit(haggis_filename *fname) {
if (fname->name != NULL) free(fname->name);
2023-08-07 18:55:47 -04:00
}
int haggis_load_filename(FILE *stream, haggis_filename *n) {
2023-10-03 13:04:50 -04:00
u16 len;
char *name;
2023-07-23 23:42:51 -04:00
len.val = 0;
2023-07-29 13:51:37 -04:00
if (fread(len.bytes, 1, 2, stream) != 2) return 2;
n->len = len;
name = calloc(1, (size_t)len.val);
2023-07-29 13:51:37 -04:00
if (name == NULL) return -1;
if (fread(name, 1, (size_t)len.val, stream) != (size_t)len.val) {
free(name);
2023-07-23 23:42:51 -04:00
return 2;
}
n->name = name;
2023-07-23 23:42:51 -04:00
return 0;
}
2023-07-29 13:51:37 -04:00
int haggis_store_filename(FILE *stream, haggis_filename *n) {
if (fwrite(n->len.bytes, 1, 2, stream) != 2) return 2;
if (fwrite(n->name, 1, (size_t)n->len.val, stream) != (size_t)n->len.val)
return 2;
return 0;
}
int haggis_load_filetype(FILE *stream, haggis_typeflag tag, haggis_filetype *file) {
2023-07-23 23:42:51 -04:00
switch (tag) {
case normal:
file->tag = 0;
return haggis_load_file(stream, &file->f_type.file);
2023-07-23 23:42:51 -04:00
case hardlink:
return haggis_load_filename(stream, &file->f_type.target);
2023-07-23 23:42:51 -04:00
file->tag = 1;
case softlink:
return haggis_load_filename(stream, &file->f_type.target);
2023-07-23 23:42:51 -04:00
file->tag = 2;
case directory:
file->tag = 3;
break;
2023-07-23 23:42:51 -04:00
case character:
file->tag = 4;
return haggis_load_device(stream, &file->f_type.dev);
2023-07-23 23:42:51 -04:00
case block:
file->tag = 5;
return haggis_load_device(stream, &file->f_type.dev);
2023-07-23 23:42:51 -04:00
case fifo:
file->tag = 6;
break;
2023-07-23 23:42:51 -04:00
case eof:
file->tag = 7;
break;
}
2023-07-20 01:06:38 -04:00
return 0;
}
int haggis_store_filetype(FILE *stream, haggis_filetype *filetype) {
u8 flag;
switch (filetype->tag) {
case normal:
flag = 0;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
if (haggis_store_file(stream, &filetype->f_type.file) != 0)
return 1;
break;
case hardlink:
flag = 1;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
return haggis_store_filename(stream, &filetype->f_type.target);
case softlink:
flag = 2;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
return haggis_store_filename(stream, &filetype->f_type.target);
case directory:
flag = 3;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
break;
case character:
flag = 4;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
if (haggis_store_device(stream, &filetype->f_type.dev) != 0)
return 1;
break;
case block:
flag = 5;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
if (haggis_store_device(stream, &filetype->f_type.dev) != 0)
return 1;
break;
case fifo:
flag = 6;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
break;
case eof:
flag = 7;
if (fwrite(&flag, 1, 1, stream) != 1)
return 1;
break;
};
return 0;
}
haggis_typeflag haggis_filetype_from_mode(u16 mode) {
u8 mask = 07 << 5;
int filetype = (int)((mode.bytes[0] & mask) >> 5);
return filetype;
}
u16 haggis_derive_mode(u16 raw, haggis_filetype *ft) {
2023-07-29 13:51:37 -04:00
u16 mode;
mode.val = ((uint16_t)ft->tag << 5) & raw.val;
2023-07-29 13:51:37 -04:00
return mode;
}
2023-08-07 18:55:47 -04:00
void haggis_node_deinit(haggis_node *node) {
if (node == NULL) return;
if (node->name.name != NULL) haggis_filename_deinit(&node->name);
switch (node->filetype.tag) {
2023-08-07 18:55:47 -04:00
case normal:
if (node->filetype.f_type.file.data != NULL) {
free(node->filetype.f_type.file.data);
2023-08-07 18:55:47 -04:00
}
break;
case hardlink:
case softlink:
if (node->filetype.f_type.target.name != NULL) {
haggis_filename_deinit(&node->filetype.f_type.target);
2023-08-07 18:55:47 -04:00
}
break;
case character:
case block:
case directory:
case fifo:
case eof:
break;
};
free(node);
}
int haggis_init_hardlink_node(
haggis_node *node,
char *target,
haggis_linkmap *map,
haggis_mq *mq
) {
haggis_message_body body;
haggis_msg *msg;
node->filetype.tag = hardlink;
haggis_filename_init(target, &node->filetype.f_type.target);
2023-11-12 19:04:40 -05:00
body.f_name = strndup(node->name.name, PATH_MAX);
msg = haggis_msg_init(NodeCreated, body);
if (msg == NULL)
return 1;
haggis_mq_push(mq, msg);
return 0;
}
int haggis_init_file_node(
haggis_node *node,
struct stat *st,
haggis_algorithm a,
haggis_linkmap *map,
haggis_mq *mq
) {
haggis_message_body body;
haggis_msg *msg;
char *target;
int res;
if (st->st_nlink > 1) {
target = haggis_linkmap_get_or_add(map, st->st_ino, node->name.name);
if (target != NULL) {
haggis_init_hardlink_node(node, target, map, mq);
return 0;
}
}
2023-09-19 20:46:48 -04:00
node->filetype.tag = normal;
res = haggis_file_init(node->name.name, &node->filetype.f_type.file, a);
if (res != 0) {
haggis_node_deinit(node);
return 1;
}
2023-11-12 19:04:40 -05:00
body.f_name = strndup(node->name.name, PATH_MAX);
msg = haggis_msg_init(NodeCreated, body);
haggis_mq_push(mq, msg);
return 0;
}
2023-09-19 20:46:48 -04:00
int haggis_init_dev_node(
haggis_node *node,
struct stat *st,
haggis_linkmap *map,
haggis_mq *mq
) {
haggis_message_body body;
haggis_msg *msg;
char *target;
if (st->st_nlink > 1) {
target = haggis_linkmap_get_or_add(map, st->st_ino, node->name.name);
if (target != NULL) {
haggis_init_hardlink_node(node, target, map, mq);
return 0;
}
}
haggis_device_init(st->st_rdev, &node->filetype.f_type.dev);
body.f_name = strndup(node->name.name, PATH_MAX);
2023-09-19 20:46:48 -04:00
msg = haggis_msg_init(NodeCreated, body);
haggis_mq_push(mq, msg);
return 0;
}
haggis_node* haggis_create_node(
char *file,
haggis_algorithm a,
haggis_linkmap *map,
haggis_mq *mq)
{
u16 mode;
char *target;
char pathbuf[PATH_MAX];
int res;
size_t namlen;
haggis_node *node;
haggis_message_body body;
haggis_msg *msg;
struct stat st;
node = calloc(1, sizeof(haggis_node));
if (node == NULL) return NULL;
if (lstat(file, &st) != 0) {
free(node);
return NULL;
}
errno = 0;
if (file[0] == '/') {
namlen = strnlen(file, PATH_MAX - 1);
node->name.name = strndup(&file[1], namlen);
} else {
namlen = strnlen(file, PATH_MAX) + 1;
node->name.name = strndup(file, namlen);
}
if (errno == ENOMEM) {
haggis_node_deinit(node);
return NULL;
}
node->name.len.val = (uint16_t)namlen;
2023-09-13 16:41:59 -04:00
node->uid.val = (uint32_t)st.st_uid;
node->gid.val = (uint32_t)st.st_gid;
node->mtime.val = (uint64_t)st.st_mtim.tv_sec;
mode.val = (uint16_t)(st.st_mode & 07777);
node->mode = mode;
2023-09-19 20:46:48 -04:00
if (S_ISBLK(st.st_mode)) {
node->filetype.tag = block;
res = haggis_init_dev_node(node, &st, map, mq);
if (res != 0)
return NULL;
} else if (S_ISCHR(st.st_mode)) {
node->filetype.tag = character;
res = haggis_init_dev_node(node, &st, map, mq);
if (res != 0)
return NULL;
} else if (S_ISDIR(st.st_mode)) {
node->filetype.tag = directory;
body.f_name = strndup(file, PATH_MAX);
msg = haggis_msg_init(NodeCreated, body);
haggis_mq_push(mq, msg);
2023-09-19 20:46:48 -04:00
} else if (S_ISFIFO(st.st_mode)) {
node->filetype.tag = fifo;
if (st.st_nlink > 1) {
target = haggis_linkmap_get_or_add(map, st.st_ino, file);
if (target != NULL) {
haggis_init_hardlink_node(node, target, map, mq);
return node;
2023-08-07 18:55:47 -04:00
}
2023-09-19 20:46:48 -04:00
}
2023-10-05 10:40:12 -04:00
body.f_name = strndup(file, PATH_MAX);
msg = haggis_msg_init(NodeCreated, body);
haggis_mq_push(mq, msg);
2023-09-19 20:46:48 -04:00
} else if (S_ISLNK(st.st_mode)) {
node->filetype.tag = softlink;
ssize_t res = readlink(file, pathbuf, PATH_MAX);
if (res == -1) {
haggis_node_deinit(node);
return NULL;
}
target = malloc(res + 1);
memcpy(target, pathbuf, (unsigned long)res);
haggis_filename_init(target, &node->filetype.f_type.target);
body.f_name = file;
msg = haggis_msg_init(NodeCreated, body);
haggis_mq_push(mq, msg);
} else if (S_ISREG(st.st_mode)) {
node->filetype.tag = normal;
res = haggis_init_file_node(node, &st, a, map, mq);
if (res != 0)
return NULL;
} else {
free(node);
return NULL;
}
return node;
}
2023-09-19 20:46:48 -04:00
char* get_full_path(haggis_filename *fname, char *basedir) {
char *path;
int pathlen;
if (basedir == NULL) {
path = calloc(1, (int)fname->len.val + 1);
if (path == NULL)
return NULL;
memcpy(path, fname->name, fname->len.val);
} else {
if (fname->name[0] == '/') {
pathlen = strnlen(basedir, PATH_MAX) + (int)fname->len.val + 1;
path = calloc(1, pathlen);
if (path == NULL)
return NULL;
snprintf(path, pathlen, "%s%s", basedir, fname->name);
} else {
pathlen = strnlen(basedir, PATH_MAX) + (int)fname->len.val + 2;
path = calloc(1, pathlen);
if (path == NULL)
return NULL;
snprintf(path, pathlen, "%s/%s", basedir, fname->name);
}
}
return path;
}
int mkdir_p(char *dir) {
struct stat st;
if (!stat(dir, &st))
return 0;
if (mkdir_p(dirname(strndup(dir, PATH_MAX))))
return 1;
return mkdir(dir, 0755);
}
2023-09-17 19:09:44 -04:00
int haggis_extract_dev(haggis_node *node, char *basedir) {
2023-09-19 20:46:48 -04:00
dev_t dev;
2023-10-03 13:04:50 -04:00
mode_t mode = 0;
2023-09-19 20:46:48 -04:00
char *path;
int ret, major, minor;
2023-09-19 20:46:48 -04:00
assert(geteuid() == 0);
2023-09-17 19:09:44 -04:00
assert(node->filetype.tag == block || node->filetype.tag == character);
2023-09-19 20:46:48 -04:00
path = get_full_path(&node->name, basedir);
if (path == NULL)
return errno;
if (mkdir_p(dirname(strndup(path, PATH_MAX)))) {
free(path);
return 2;
}
major = (int)node->filetype.f_type.dev.major.val;
minor = (int)node->filetype.f_type.dev.minor.val;
dev = makedev(major, minor);
if (node->filetype.tag == block) {
mode = (mode_t)node->mode.val | S_IFBLK;
} else if (node->filetype.tag == character) {
mode = (mode_t)node->mode.val | S_IFCHR;
}
2023-09-19 20:46:48 -04:00
ret = mknod(path, mode, dev);
free(path);
return ret;
2023-09-17 19:09:44 -04:00
}
int haggis_extract_fifo(haggis_node *node, char *basedir) {
2023-09-19 20:46:48 -04:00
mode_t mode;
char *path;
int ret;
2023-09-17 19:09:44 -04:00
assert(node->filetype.tag == fifo);
2023-09-19 20:46:48 -04:00
path = get_full_path(&node->name, basedir);
if (path == NULL)
return 1;
if (mkdir_p(dirname(strndup(path, PATH_MAX)))) {
free(path);
return 2;
}
2023-09-19 20:46:48 -04:00
mode = (mode_t)node->mode.val;
ret = mkfifo(path, mode);
free(path);
if (ret !=0)
return errno;
2023-09-17 19:09:44 -04:00
return 0;
}
int haggis_extract_symlink(haggis_node *node, char *basedir) {
char *path, *target;
2023-09-19 20:46:48 -04:00
int ret;
2023-09-17 19:09:44 -04:00
assert(node->filetype.tag == softlink);
2023-09-19 20:46:48 -04:00
path = get_full_path(&node->name, basedir);
if (path == NULL)
return 1;
if (mkdir_p(dirname(strndup(path, PATH_MAX)))) {
free(path);
return 2;
}
target = node->filetype.f_type.target.name;
ret = symlink(target, path);
2023-09-19 20:46:48 -04:00
free(path);
if (ret != 0)
return errno;
2023-09-17 19:09:44 -04:00
return 0;
}
int haggis_extract_hardlink(haggis_node *node, char *basedir) {
2023-09-19 20:46:48 -04:00
char *path, *target;
int ret;
FILE *fd;
2023-09-17 19:09:44 -04:00
assert(node->filetype.tag == hardlink);
2023-09-19 20:46:48 -04:00
path = get_full_path(&node->name, basedir);
if (path == NULL)
return 1;
if (mkdir_p(dirname(strndup(path, PATH_MAX)))) {
free(path);
return 2;
}
2023-09-19 20:46:48 -04:00
target = get_full_path(&node->filetype.f_type.target, basedir);
if (target == NULL) {
free(path);
return 1;
}
if (access(target, F_OK) == -1) {
fd = fopen(target, "w");
if (mkdir_p(dirname(strndup(target, PATH_MAX)))) {
free(path);
return 2;
}
2023-09-19 20:46:48 -04:00
if (fd == NULL) {
free(path);
free(target);
return errno;
}
fclose(fd);
}
ret = link(target, path);
free(path);
free(target);
if (ret != 0)
return errno;
2023-09-17 19:09:44 -04:00
return 0;
}
int haggis_extract_dir(haggis_node *node, char *basedir) {
char *path;
2023-09-17 19:09:44 -04:00
assert(node->filetype.tag == directory);
path = get_full_path(&node->name, basedir);
if (mkdir_p(path)) {
free(path);
return errno;
}
free(path);
2023-09-17 19:09:44 -04:00
return 0;
}
char* haggis_extract_file(haggis_node *node, char *basedir) {
char *path;
FILE *fd;
size_t len;
int ret;
2023-09-17 19:09:44 -04:00
assert(node->filetype.tag == normal);
path = get_full_path(&node->name, basedir);
if (path == NULL) {
return NULL;
}
if (mkdir_p(dirname(strndup(path, PATH_MAX)))) {
free(path);
return NULL;
}
fd = fopen(path, "w");
if (fd == NULL) {
free(path);
return NULL;
}
len = (size_t)node->filetype.f_type.file.len.val;
ret = fwrite(node->filetype.f_type.file.data, len, 1, fd);
if (ret != len) {
free(path);
fclose(fd);
return NULL;
}
fflush(fd);
fclose(fd);
return path;
2023-09-17 19:09:44 -04:00
}
int haggis_extract_node(char *basedir, haggis_node *node, haggis_mq *mq) {
haggis_msg *msg;
char *path, *dir, *fullpath;
int ret;
path = get_full_path(&node->name, basedir);
dir = dirname(strndup(path, PATH_MAX));
mkdir_p(dir);
2023-09-17 19:09:44 -04:00
switch (node->filetype.tag) {
case block:
case character:
if (geteuid() == 0) {
ret = haggis_extract_dev(node, basedir);
} else {
ret = -1;
msg = calloc(1, sizeof(haggis_msg));
if (msg == NULL) return 2;
msg->tag = DevNodeSkipped;
msg->body.f_name = strndup(node->name.name, PATH_MAX);
haggis_mq_push(mq, msg);
}
break;
2023-09-17 19:09:44 -04:00
case fifo:
ret = haggis_extract_fifo(node, basedir);
break;
2023-09-17 19:09:44 -04:00
case softlink:
return haggis_extract_symlink(node, basedir);
case hardlink:
ret = haggis_extract_hardlink(node, basedir);
break;
2023-09-17 19:09:44 -04:00
case directory:
ret = haggis_extract_dir(node, basedir);
break;
2023-09-17 19:09:44 -04:00
case normal:
fullpath = haggis_extract_file(node, basedir);
if (fullpath == NULL) {
ret = errno;
}
break;
2023-09-17 19:09:44 -04:00
case eof:
return 0;
}
if (geteuid() == 0) {
ret = chown(path, (uid_t)node->uid.val, (gid_t)node->gid.val);
if (ret != 0) {
free(path);
return errno;
}
}
ret = chmod(path, (mode_t)node->mode.val);
msg = calloc(1, sizeof(haggis_msg));
if (msg == NULL)
return 2;
msg->tag = NodeExtracted;
msg->body.f_name = strndup(node->name.name, PATH_MAX);
haggis_mq_push(mq, msg);
return ret;
2023-07-19 22:38:59 -04:00
}
2023-07-20 01:06:38 -04:00
int haggis_load_node(FILE *stream, haggis_node *node) {
int res;
u16 mode;
haggis_typeflag tag;
mode.val = 0;
res = haggis_load_filename(stream, &node->name);
if (res)
return res;
res = load_u32(stream, &node->uid);
if (res != 4)
return 2;
res = load_u32(stream, &node->gid);
if (res != 4)
return 2;
res = load_u64(stream, &node->mtime);
if (res != 8)
return 2;
res = load_u16(stream, &mode);
if (res != 2)
return 2;
tag = haggis_filetype_from_mode(mode);
node->mode.bytes[0] = mode.bytes[0] & 037;
node->mode.bytes[1] = mode.bytes[1];
res = haggis_load_filetype(stream, tag, &node->filetype);
if (res)
return res;
return 0;
}
int haggis_store_node(FILE *stream, haggis_node *node) {
u16 mode;
if (haggis_store_filename(stream, &node->name) != (size_t)(node->name.len.val) + 2)
2023-07-29 13:51:37 -04:00
return 2;
if (store_u32(stream, &node->uid) != 4) return 2;
if (store_u32(stream, &node->gid) != 4) return 2;
if (store_u64(stream, &node->mtime) != 8) return 2;
mode = haggis_derive_mode(node->mode, &node->filetype);
if (store_u16(stream, &mode) != 2) return 2;
return haggis_store_filetype(stream, &node->filetype);
2023-07-20 01:06:38 -04:00
}