File: fifo.c

package info (click to toggle)
taper 6.9rb-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,444 kB
  • ctags: 1,604
  • sloc: ansic: 15,921; makefile: 248
file content (224 lines) | stat: -rw-r--r-- 4,414 bytes parent folder | download | duplicates (4)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* fifo.c */

#include "taper.h"


/* functions used by restore/verify */

PUBLIC void fifo_send_file_with_info (FILE *fdfifo, const struct file_info *fi,
	const char *file_name, const char *temp_name)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_FILE_INFO, fdfifo);
	fputsz (file_name, fdfifo);
	fputsz (temp_name, fdfifo);
	fwrite(fi, sizeof(*fi), 1, fdfifo);
	fflush (fdfifo);
}


PUBLIC void fifo_send_end (FILE *fdfifo)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_END, fdfifo);
	fflush (fdfifo);
}


PUBLIC void fifo_send_error (FILE *fdfifo)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_ERROR, fdfifo);
	fflush (fdfifo);
}


/* return value:
 *   1: fatal error, or end
 *   0:
 *	file_name = compressed_name = NULL -> error
 *	file_name != NULL, compressed_name != NULL -> everything ok
 */
PUBLIC int fifo_receive_file_with_info (FILE *fdfifo, struct file_info *fi,
	char **file_name, char **temp_name)
{
	char magic[100];
	static char fn[MAX_FNAME], tn[MAX_FNAME];

	int c;

	if (fgetsz (magic, 100, fdfifo) == NULL)
		goto fifo_error;
	if (strcmp (magic, FIFO_MAGIC) != 0)
		goto fifo_error;

	if ((c = getc (fdfifo)) == EOF)
		goto fifo_error;
	switch (c) {
	  case FIFO_FILE_INFO:
		if (fgetsz (fn, sizeof (fn), fdfifo) == NULL)
			goto fifo_error;
		if (fgetsz (tn, sizeof (tn), fdfifo) == NULL)
			goto fifo_error;
		if (fread (fi, sizeof(*fi), 1, fdfifo) != 1)
			goto fifo_error;
		*file_name = fn;
		*temp_name = tn;
		return 0;

	  case FIFO_END:
		return 1;		/* end */

	  case FIFO_ERROR:
		*file_name = *temp_name = NULL;
		return 0;		/* continue */

	  default:
		goto fifo_error;
	}

fifo_error:
	write_log ("ERROR: invalid FIFO data");
	return 1;			/* end */
}


/* functions used by backup */

PUBLIC void fifo_send_file (FILE *fdfifo, const char *file_name,
	const char *compressed_name)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_FILE, fdfifo);
	fputsz (file_name, fdfifo);
	fputsz (compressed_name, fdfifo);
	fflush (fdfifo);
}


PUBLIC void fifo_send_error_with_file (FILE *fdfifo, const char *file_name)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_ERROR_WITH_FILE, fdfifo);
	fputsz (file_name, fdfifo);
	fflush (fdfifo);
}


PUBLIC void fifo_send_disk_full (FILE *fdfifo)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_DISK_FULL, fdfifo);
	fflush (fdfifo);
}


PUBLIC void fifo_send_child_ok (FILE *fdfifo)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_CHILD_OK, fdfifo);
	fflush (fdfifo);
}


PUBLIC void fifo_send_no_child (FILE *fdfifo)
{
	fputsz (FIFO_MAGIC, fdfifo);
	putc (FIFO_NO_CHILD, fdfifo);
	fflush (fdfifo);
}


/* return value:
 *   1: fatal error, or no child
 *   0: child ok
 */
PUBLIC int fifo_receive_child_status (FILE *fdfifo)
{
	char magic[100];
	int c;

	if (fgetsz (magic, 100, fdfifo) == NULL)
		goto fifo_error;
	if (strcmp (magic, FIFO_MAGIC) != 0)
		goto fifo_error;

	if ((c = getc (fdfifo)) == EOF)
		goto fifo_error;
	switch (c) {
	  case FIFO_CHILD_OK:
		return 0;

	  case FIFO_NO_CHILD:
		return 1;

	  default:
		goto fifo_error;
	}

fifo_error:
	write_log ("ERROR: invalid FIFO data");
	return 1;
}


char fifo_disk_full_c;		/* to make a unique pointer */

/* return value:
 *   1: fatal error
 *   0:
 *	file_name = compressed_name = NULL -> EOF
 *	file_name = fifo_disk_full, compressed_name = NULL -> disk_full
 *	file_name != NULL, file_name != fifo_disk_full,
 *				compressed_name = NULL -> error
 *	file_name != NULL, compressed_name != NULL -> everything ok
 */
PUBLIC int fifo_receive_file (FILE *fdfifo, char **file_name,
	char **compressed_name)
{
	char magic[100];
	static char fn[MAX_FNAME], cn[MAX_FNAME];

	int c;

	if (fgetsz (magic, 100, fdfifo) == NULL) {
		if (ferror (fdfifo))
			return 1;
		*file_name = *compressed_name = NULL;
		return 0;
	}
	if (strcmp (magic, FIFO_MAGIC) != 0)
		goto fifo_error;

	if ((c = getc (fdfifo)) == EOF)
		goto fifo_error;
	switch (c) {
	  case FIFO_FILE:
		if (fgetsz (fn, sizeof (fn), fdfifo) == NULL)
			goto fifo_error;
		if (fgetsz (cn, sizeof (cn), fdfifo) == NULL)
			goto fifo_error;
		*file_name = fn;
		*compressed_name = cn;
		return 0;

	  case FIFO_ERROR_WITH_FILE:
		if (fgetsz (fn, sizeof (fn), fdfifo) == NULL)
			goto fifo_error;
		*file_name = fn;
		*compressed_name = NULL;
		return 0;

	  case FIFO_DISK_FULL:
		*file_name = fifo_disk_full;
		*compressed_name = NULL;
		return 0;

	  default:
		goto fifo_error;
	}

fifo_error:
	write_log ("ERROR: invalid FIFO data");
	return 1;
}