Commit | Line | Data |
---|---|---|
68270f0f JRJ |
1 | /* |
2 | * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU Lesser General Public | |
6 | * License as published by the Free Software Foundation; version 2.1 of | |
7 | * the License. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * Lesser General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public | |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | /* | |
20 | * This script validate and xml from an xsd. | |
21 | * argv[1] Path of the xsd | |
22 | * argv[2] Path to the XML to be validated | |
23 | */ | |
24 | ||
68270f0f JRJ |
25 | #include <assert.h> |
26 | #include <ctype.h> | |
27 | #include <stdio.h> | |
28 | #include <stdlib.h> | |
29 | #include <string.h> | |
30 | #include <inttypes.h> | |
31 | #include <dirent.h> | |
32 | #include <unistd.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | ||
36 | #include <libxml/xmlschemas.h> | |
37 | #include <libxml/parser.h> | |
38 | ||
39 | #include <lttng/lttng-error.h> | |
40 | ||
41 | struct validation_ctx { | |
42 | xmlSchemaParserCtxtPtr parser_ctx; | |
43 | xmlSchemaPtr schema; | |
44 | xmlSchemaValidCtxtPtr schema_validation_ctx; | |
45 | }; | |
46 | ||
47 | enum command_err_code { | |
48 | CMD_SUCCESS = 0, | |
49 | CMD_ERROR | |
50 | }; | |
51 | ||
52 | static | |
53 | void xml_error_handler(void *ctx, const char *format, ...) | |
54 | { | |
55 | char *err_msg; | |
56 | va_list args; | |
57 | int ret; | |
58 | ||
59 | va_start(args, format); | |
60 | ret = vasprintf(&err_msg, format, args); | |
61 | va_end(args); | |
62 | if (ret == -1) { | |
63 | fprintf(stderr, "ERR: %s\n", | |
64 | "String allocation failed in xml error handle"); | |
65 | return; | |
66 | } | |
67 | ||
68 | fprintf(stderr, "XML Error: %s\n", err_msg); | |
69 | free(err_msg); | |
70 | } | |
71 | ||
72 | static | |
73 | void fini_validation_ctx( | |
74 | struct validation_ctx *ctx) | |
75 | { | |
76 | if (ctx->parser_ctx) { | |
77 | xmlSchemaFreeParserCtxt(ctx->parser_ctx); | |
78 | } | |
79 | ||
80 | if (ctx->schema) { | |
81 | xmlSchemaFree(ctx->schema); | |
82 | } | |
83 | ||
84 | if (ctx->schema_validation_ctx) { | |
85 | xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx); | |
86 | } | |
87 | ||
88 | memset(ctx, 0, sizeof(struct validation_ctx)); | |
89 | } | |
90 | ||
91 | static | |
92 | int init_validation_ctx( | |
93 | struct validation_ctx *ctx, char *xsd_path) | |
94 | { | |
95 | int ret; | |
96 | ||
97 | if (!xsd_path) { | |
98 | ret = -LTTNG_ERR_NOMEM; | |
99 | goto end; | |
100 | } | |
101 | ||
102 | ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path); | |
103 | if (!ctx->parser_ctx) { | |
104 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
105 | goto end; | |
106 | } | |
107 | xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler, | |
108 | xml_error_handler, NULL); | |
109 | ||
110 | ctx->schema = xmlSchemaParse(ctx->parser_ctx); | |
111 | if (!ctx->schema) { | |
112 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
113 | goto end; | |
114 | } | |
115 | ||
116 | ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema); | |
117 | if (!ctx->schema_validation_ctx) { | |
118 | ret = -LTTNG_ERR_LOAD_INVALID_CONFIG; | |
119 | goto end; | |
120 | } | |
121 | ||
122 | xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler, | |
123 | xml_error_handler, NULL); | |
124 | ret = 0; | |
125 | ||
126 | end: | |
127 | if (ret) { | |
128 | fini_validation_ctx(ctx); | |
129 | } | |
130 | return ret; | |
131 | } | |
132 | ||
133 | static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx) | |
134 | { | |
135 | int ret; | |
136 | xmlDocPtr doc = NULL; | |
137 | ||
138 | assert(xml_file_path); | |
139 | assert(ctx); | |
140 | ||
141 | /* Open the document */ | |
142 | doc = xmlParseFile(xml_file_path); | |
143 | if (!doc) { | |
144 | ret = LTTNG_ERR_MI_IO_FAIL; | |
145 | goto end; | |
146 | } | |
147 | ||
148 | /* Validate against the validation ctx (xsd) */ | |
149 | ret = xmlSchemaValidateDoc(ctx->schema_validation_ctx, doc); | |
150 | if (ret) { | |
151 | fprintf(stderr, "ERR: %s\n", "XML is not valid againt provided XSD"); | |
152 | ret = CMD_ERROR; | |
153 | goto end; | |
154 | } | |
155 | ||
156 | ret = CMD_SUCCESS; | |
157 | end: | |
158 | return ret; | |
159 | ||
160 | ||
161 | } | |
162 | int main(int argc, char **argv, char *env[]) | |
163 | { | |
164 | int ret; | |
a90d21b3 | 165 | struct validation_ctx ctx = { 0 }; |
68270f0f JRJ |
166 | |
167 | /* Check if we have all argument */ | |
168 | if (argc < 3) { | |
169 | fprintf(stderr, "ERR: %s\n", "Missing arguments"); | |
170 | ret = CMD_ERROR; | |
171 | goto end; | |
172 | } | |
173 | ||
174 | /* Check if xsd file exist */ | |
175 | ret = access(argv[1], F_OK); | |
176 | if (ret < 0) { | |
177 | fprintf(stderr, "ERR: %s\n", "Xsd path not valid"); | |
178 | goto end; | |
179 | } | |
180 | ||
181 | /* Check if xml to validate exist */ | |
182 | ret = access(argv[2], F_OK); | |
183 | if (ret < 0) { | |
184 | fprintf(stderr, "ERR: %s\n", "XML path not valid"); | |
185 | goto end; | |
186 | } | |
187 | ||
188 | /* initialize the validation ctx */ | |
189 | ret = init_validation_ctx(&ctx, argv[1]); | |
190 | if (ret) { | |
191 | goto end; | |
192 | } | |
193 | ||
194 | ret = validate_xml(argv[2], &ctx); | |
195 | ||
196 | fini_validation_ctx(&ctx); | |
197 | ||
198 | end: | |
199 | return ret; | |
200 | } |