aboutsummaryrefslogtreecommitdiffstats
path: root/tapeio.c
blob: 02a7250b05e8a612c390b3485d5076d0dcd7442e (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "config.h"
#include "err.h"
#include "strutil.h"
#include "tapeio.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>

#ifdef NEED_STRINGS
#include <strings.h>
#endif

RCSID("$Id: tapeio.c,v 1.2 2000/02/22 22:50:33 shmit Exp $");

int
mt_rewind(int fd)
{
	struct mtop mt;

	mt.mt_op = MTREW;
	mt.mt_count = 1;
	return ioctl(fd, MTIOCTOP, &mt);
}

int
mt_fsf(int fd, int count)
{
	struct mtop mt;

	mt.mt_op = MTFSF;
	mt.mt_count = count;
	return ioctl(fd, MTIOCTOP, &mt);
}

int
mt_weof(int fd, int count)
{
	struct mtop mt;

	mt.mt_op = MTWEOF;
	mt.mt_count = count;
	return ioctl(fd, MTIOCTOP, &mt);
}

int
readlabel(int fd, tapelabel_t *label)
{
	char buffer[HEADERSIZE], date[MAXLINE];
	char *p;
	struct tm tm;
	ssize_t readb;

	do {
		readb = read(fd, buffer, sizeof(buffer));
		if (readb == -1) {
			err("Taper: couldn't read label from tape: %s.\n",
			    strerror(errno));
			return -1;
		}
	} while (readb);

	p = getword(buffer, label->labelstr);
	getword(p, date);
	sscanf(date, "%02d%02d%04d", &tm.tm_mday, &tm.tm_mon, &tm.tm_year);
	tm.tm_mon--; tm.tm_year -= 1900; tm.tm_isdst = -1;

	label->date = mktime(&tm);

	if (mt_fsf(fd, 0) == -1) {
		err("Taper: couldn't fast-forward past label: %s.\n",
		    strerror(errno));
		return -1;
	}

	return 0;
}

int
writelabel(int fd, tapelabel_t *label)
{
	char buffer[HEADERSIZE];
	struct tm *tm;
	int wroteb;

	tm = localtime(&label->date);
	snprintf(buffer, sizeof(buffer), "%s %02d%02d%04d", label->labelstr,
		 tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900);

	wroteb = write(fd, buffer, sizeof(buffer));
	if (wroteb == -1)
		return -1;

	return mt_weof(fd, 1);
}

int
readheader(int fd, fileheader_t *header)
{
	char buffer[HEADERSIZE];
	ssize_t readb;

	do {
		readb = read(fd, buffer, sizeof(buffer));
		if (readb == -1)
			return -1;
	} while (readb);

	bzero(header, sizeof(header));
	if (!strncmp(TAPER_START, buffer, sizeof(buffer)))
		header->type = STARTMARK;
	else if (!strncmp(TAPER_STOP, buffer, sizeof(buffer)))
		header->type = STOPMARK;
	else {
		char *vol;
		struct tm tm;

		header->type = FILEMARK;
		vol = index(buffer, ':');
		if (vol) {
			*vol = '\0';
			sscanf(vol+1, "%s %02d%02d%04d", header->vol,
			       &tm.tm_mday, &tm.tm_mon, &tm.tm_year);
			tm.tm_mon--; tm.tm_year -= 1900; tm.tm_isdst = -1;
			header->date = mktime(&tm);
		}
		strncpy(header->host, buffer, sizeof(header->host));
	}

	return mt_fsf(fd, 0);
}

int
writeheader(int fd, fileheader_t *header)
{
	char buffer[HEADERSIZE];
	struct tm *tm;
	ssize_t wroteb;

	switch (header->type) {
	case STARTMARK:
		strncpy(buffer, TAPER_START, sizeof(buffer));
		break;
	case STOPMARK:
		strncpy(buffer, TAPER_STOP, sizeof(buffer));
		break;
	case FILEMARK:
		tm = localtime(&header->date);
		snprintf(buffer, sizeof(buffer), "%s:%s %02d%02d%04d",
			 header->host, header->vol,
			 tm->tm_mday, tm->tm_mon+1, tm->tm_year+1900);
		break;
	}

	wroteb = write(fd, buffer, sizeof(buffer));
	if (wroteb == -1)
		return -1;

	return mt_weof(fd, 1);
}