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