1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to you under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.  See the License for the specific language governing
15  * permissions and limitations under the License.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #ifdef _WIN32
23 #include <unistd.h>
24 #endif
25 
26 #include "avro.h"
27 
28 int process_file(const char *in_filename, const char *out_filename)
29 {
30 	avro_file_reader_t  reader;
31 	avro_file_writer_t  writer;
32 
33 	if (in_filename == NULL) {
34 		if (avro_file_reader_fp(stdin, "<stdin>", 0, &reader)) {
35 			fprintf(stderr, "Error opening <stdin>:\n  %s\n",
36 				avro_strerror());
37 			return 1;
38 		}
39 	} else {
40 		if (avro_file_reader(in_filename, &reader)) {
41 			fprintf(stderr, "Error opening %s:\n  %s\n",
42 				in_filename, avro_strerror());
43 			return 1;
44 		}
45 	}
46 
47 	avro_schema_t  wschema;
48 	wschema = avro_file_reader_get_writer_schema(reader);
49 
50 	/* Check that the reader schema is the same as the writer schema */
51 	{
52 		avro_schema_t oschema;
53 		avro_file_reader_t oreader;
54 
55 		if (avro_file_reader(out_filename, &oreader)) {
56 			fprintf(stderr, "Error opening %s:\n   %s\n",
57 					out_filename, avro_strerror());
58 			avro_file_reader_close(reader);
59 			return 1;
60 		}
61 
62 		oschema = avro_file_reader_get_writer_schema(oreader);
63 
64 		if (avro_schema_equal(oschema, wschema) == 0) {
65 			fprintf(stderr, "Error: reader and writer schema are not equal.\n");
66 			avro_file_reader_close(oreader);
67 			avro_file_reader_close(reader);
68 			return 1;
69 		}
70 
71 		avro_file_reader_close(oreader);
72 		avro_schema_decref(oschema);
73 	}
74 
75 	if (avro_file_writer_open(out_filename, &writer)) {
76 		fprintf(stderr, "Error opening %s:\n   %s\n",
77 				out_filename, avro_strerror());
78 		avro_file_reader_close(reader);
79 		return 1;
80 	}
81 
82 	avro_value_iface_t  *iface;
83 	avro_value_t  value;
84 
85 	iface = avro_generic_class_from_schema(wschema);
86 	avro_generic_value_new(iface, &value);
87 
88 	while (avro_file_reader_read_value(reader, &value) == 0) {
89 		if (avro_file_writer_append_value(writer, &value)) {
90 			fprintf(stderr, "Error writing to %s:\n  %s\n",
91 				out_filename, avro_strerror());
92 			return 1;
93 		}
94 		avro_value_reset(&value);
95 	}
96 
97 	avro_file_reader_close(reader);
98 	avro_file_writer_close(writer);
99 	avro_value_decref(&value);
100 	avro_value_iface_decref(iface);
101 	avro_schema_decref(wschema);
102 
103 	return 0;
104 }
105 
106 static void usage(void)
107 {
108 	fprintf(stderr,
109 		"Usage: avroappend [<input avro file>] <output avro file>\n");
110 }
111 
112 static int check_filenames(const char *in_filename, const char *out_filename)
113 {
114 	if (in_filename == NULL) {
115 		return 0;
116 	}
117 
118 	struct stat in_stat;
119 	struct stat out_stat;
120 
121 	if (stat(in_filename, &in_stat) == -1) {
122 		fprintf(stderr, "stat error on %s: %s\n", in_filename, strerror(errno));
123 		return 2;
124 	}
125 
126 	if (stat(out_filename, &out_stat) == -1) {
127 		fprintf(stderr, "stat error on %s: %s\n", out_filename, strerror(errno));
128 		return 2;
129 	}
130 
131 	if (in_stat.st_dev == out_stat.st_dev && in_stat.st_ino == out_stat.st_ino) {
132 		return 1;
133 	}
134 
135 	return 0;
136 }
137 
138 int main(int argc, char **argv)
139 {
140 	char *in_filename;
141 	char *out_filename;
142 
143 	argc--;
144 	argv++;
145 
146 	if (argc == 2) {
147 		in_filename = argv[0];
148 		out_filename = argv[1];
149 	} else if (argc == 1) {
150 		in_filename = NULL;
151 		out_filename = argv[0];
152 	} else {
153 		fprintf(stderr, "Not enough arguments\n\n");
154 		usage();
155 		exit(1);
156 	}
157 
158 	int ret = check_filenames(in_filename, out_filename);
159 
160 	if (ret == 1) {
161 		fprintf(stderr, "Files are the same.\n");
162 	}
163 
164 	if (ret > 0) {
165 		exit(1);
166 	}
167 
168 	exit(process_file(in_filename, out_filename));
169 }
170