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