aboutsummaryrefslogtreecommitdiff
path: root/src/record.c
blob: be78978008e2a5135a03cac9c46d4865c5f500c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}