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 <errno.h>
19 #include <getopt.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "avro.h"
26 #include "avro_private.h"
27
28
29 /* The compression codec to use. */
30 static const char *codec = "null";
31
32 /* The block size to use. */
33 static size_t block_size = 0;
34
35 /*-- PROCESSING A FILE --*/
36
37 static void
process_file(const char * in_filename,const char * out_filename)38 process_file(const char *in_filename, const char *out_filename)
39 {
40 avro_file_reader_t reader;
41 avro_file_writer_t writer;
42
43 if (in_filename == NULL) {
44 if (avro_file_reader_fp(stdin, "<stdin>", 0, &reader)) {
45 fprintf(stderr, "Error opening <stdin>:\n %s\n",
46 avro_strerror());
47 exit(1);
48 }
49 } else {
50 if (avro_file_reader(in_filename, &reader)) {
51 fprintf(stderr, "Error opening %s:\n %s\n",
52 in_filename, avro_strerror());
53 exit(1);
54 }
55 }
56
57 avro_schema_t wschema;
58 avro_value_iface_t *iface;
59 avro_value_t value;
60 int rval;
61
62 wschema = avro_file_reader_get_writer_schema(reader);
63 iface = avro_generic_class_from_schema(wschema);
64 avro_generic_value_new(iface, &value);
65
66 if (avro_file_writer_create_with_codec
67 (out_filename, wschema, &writer, codec, block_size)) {
68 fprintf(stderr, "Error creating %s:\n %s\n",
69 out_filename, avro_strerror());
70 exit(1);
71 }
72
73 while ((rval = avro_file_reader_read_value(reader, &value)) == 0) {
74 if (avro_file_writer_append_value(writer, &value)) {
75 fprintf(stderr, "Error writing to %s:\n %s\n",
76 out_filename, avro_strerror());
77 exit(1);
78 }
79 avro_value_reset(&value);
80 }
81
82 if (rval != EOF) {
83 fprintf(stderr, "Error reading value: %s", avro_strerror());
84 }
85
86 avro_file_reader_close(reader);
87 avro_file_writer_close(writer);
88 avro_value_decref(&value);
89 avro_value_iface_decref(iface);
90 avro_schema_decref(wschema);
91 }
92
93
94 /*-- MAIN PROGRAM --*/
95
96 static struct option longopts[] = {
97 { "block-size", required_argument, NULL, 'b' },
98 { "codec", required_argument, NULL, 'c' },
99 { NULL, 0, NULL, 0 }
100 };
101
usage(void)102 static void usage(void)
103 {
104 fprintf(stderr,
105 "Usage: avromod [--codec=<compression codec>]\n"
106 " [--block-size=<block size>]\n"
107 " [<input avro file>]\n"
108 " <output avro file>\n");
109 }
110
111 static void
parse_block_size(const char * optarg)112 parse_block_size(const char *optarg)
113 {
114 unsigned long ul;
115 char *end;
116
117 ul = strtoul(optarg, &end, 10);
118 if ((ul == 0 && end == optarg) ||
119 (ul == ULONG_MAX && errno == ERANGE)) {
120 fprintf(stderr, "Invalid block size: %s\n\n", optarg);
121 usage();
122 exit(1);
123 }
124 block_size = ul;
125 }
126
127
main(int argc,char ** argv)128 int main(int argc, char **argv)
129 {
130 char *in_filename;
131 char *out_filename;
132
133 int ch;
134 while ((ch = getopt_long(argc, argv, "b:c:", longopts, NULL)) != -1) {
135 switch (ch) {
136 case 'b':
137 parse_block_size(optarg);
138 break;
139
140 case 'c':
141 codec = optarg;
142 break;
143
144 default:
145 usage();
146 exit(1);
147 }
148 }
149
150 argc -= optind;
151 argv += optind;
152
153 if (argc == 2) {
154 in_filename = argv[0];
155 out_filename = argv[1];
156 } else if (argc == 1) {
157 in_filename = NULL;
158 out_filename = argv[0];
159 } else {
160 fprintf(stderr, "Can't read from multiple input files.\n");
161 usage();
162 exit(1);
163 }
164
165 /* Process the data file */
166 process_file(in_filename, out_filename);
167 return 0;
168 }
169