From 86e8aeb5bb66ae5625002533b529752a7814e6c0 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 22 Jun 2020 15:09:59 +0200 Subject: Added archive extracting --- src/record.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/record.c (limited to 'src/record.c') diff --git a/src/record.c b/src/record.c new file mode 100644 index 0000000..be78978 --- /dev/null +++ b/src/record.c @@ -0,0 +1,60 @@ +#include "tar.h" + +int record_write(int fd, char *s, size_t size) +{ + char buf[RECORD_SIZE]; + + while (size > RECORD_SIZE) + { + memcpy(buf, s, RECORD_SIZE); + s += RECORD_SIZE; + size -= RECORD_SIZE; + if (write(fd, buf, RECORD_SIZE) == -1) + { + perror(NULL); + return -1; + } + } + bzero(buf, RECORD_SIZE); + memcpy(buf, s, size); + if (write(fd, buf, RECORD_SIZE) == -1) + { + perror(NULL); + return -1; + } + return (0); +} + +int record_write_blank(int fd, size_t count) +{ + char buf[RECORD_SIZE] = {0}; + while (count-- > 0) + { + if (write(fd, buf, RECORD_SIZE) == -1) + { + perror(NULL); + return -1; + } + } + return 0; +} + +int record_read(int fd, char record[RECORD_SIZE]) +{ + int ret = read(fd, record, RECORD_SIZE); + if (ret == -1) + { + perror(NULL); + return -1; + } + if (ret != RECORD_SIZE) + return -1; + return 0; +} + +static char g_record_blank[RECORD_SIZE] = {0}; + +bool record_is_blank(char record[RECORD_SIZE]) +{ + return memcmp(record, g_record_blank, RECORD_SIZE) == 0; +} -- cgit