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 "avro.h"
19 #include "avro_private.h"
20 #include <dirent.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 
should_test(char * d_name)24 int should_test(char *d_name)
25 {
26     // check filename extension
27     char *ext_pos = strrchr(d_name, '.');
28     if (ext_pos == NULL)
29     {
30         return 0;
31     }
32 
33     ptrdiff_t diff = d_name + strlen(d_name) - ext_pos;
34     char *substr = malloc(sizeof(char) * diff);
35     strncpy(substr, ext_pos + 1, diff - 1);
36     *(substr + diff - 1) = '\0';
37     if (strcmp(substr, "avro") != 0)
38     {
39         free(substr);
40         return 0;
41     }
42     free(substr);
43 
44     // check compression codec
45     char *codec_pos = strrchr(d_name, '_');
46     if (codec_pos == NULL)
47     {
48         return 1;
49     }
50     if (ext_pos < codec_pos)
51     {
52         return 0;
53     }
54 
55     diff = ext_pos - codec_pos;
56     substr = malloc(sizeof(char) * diff);
57     strncpy(substr, codec_pos + 1, diff - 1);
58     *(substr + diff - 1) = '\0';
59     if (strcmp(substr, "deflate") == 0 || strcmp(substr, "snappy") == 0)
60     {
61         free(substr);
62         return 1;
63     }
64     free(substr);
65 
66     return 0;
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71     if (argc != 2)
72     {
73         fprintf(stderr, "%s accepts just one input file\n", argv[0]);
74         return EXIT_FAILURE;
75     }
76 
77     DIR *dir;
78     if ((dir = opendir(argv[1])) == NULL)
79     {
80         fprintf(stderr, "The specified path is not a directory: %s\n", argv[1]);
81         return EXIT_FAILURE;
82     }
83 
84     struct dirent *ent;
85     while ((ent = readdir(dir)) != NULL)
86     {
87         avro_file_reader_t reader;
88         avro_value_t value;
89 
90         if (ent->d_type != DT_REG) continue;
91 
92         char *d_name = ent->d_name;
93         size_t d_name_len = strlen(d_name);
94         size_t size = strlen(argv[1]) + d_name_len + 2;
95         char* path = malloc(sizeof(char) * size);
96         sprintf(path, "%s/%s", argv[1], d_name);
97 
98         if (!should_test(d_name))
99         {
100             printf("Skipping file: %s\n", path);
101             free(path);
102             continue;
103         }
104         printf("Checking file: %s\n", path);
105 
106         if (avro_file_reader(path, &reader))
107         {
108             fprintf(stderr, "Failed to read from a file: %s\n", path);
109             free(path);
110             return EXIT_FAILURE;
111         }
112         avro_schema_t schema = avro_file_reader_get_writer_schema(reader);
113         avro_value_iface_t *iface = avro_generic_class_from_schema(schema);
114         avro_generic_value_new(iface, &value);
115 
116         int i, rval;
117         for (i = 0; (rval = avro_file_reader_read_value(reader, &value)) == 0; i++, avro_value_reset(&value));
118         if (rval != EOF)
119         {
120             fprintf(stderr, "Error(%d) occurred while reading file: %s\n", rval, path);
121             free(path);
122             return EXIT_FAILURE;
123         }
124         if (i == 0)
125         {
126             fprintf(stderr, "Input file %s is supposed to be non-empty\n", path);
127             free(path);
128             return EXIT_FAILURE;
129         }
130 
131         free(path);
132         avro_value_decref(&value);
133         avro_value_iface_decref(iface);
134         avro_schema_decref(schema);
135         avro_file_reader_close(reader);
136     }
137 
138     closedir(dir);
139 
140     return EXIT_SUCCESS;
141 }
142