1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* Copyright © 2013-2020 Inria. All rights reserved.
* See COPYING in top-level directory.
*/
#include "private/autogen/config.h"
#include "hwloc.h"
#include "hwloc/diff.h"
#include "misc.h"
void usage(const char *callname __hwloc_attribute_unused, FILE *where)
{
fprintf(where, "Usage: hwloc-patch [options] [<old.xml> | refname] [<diff.xml> | -] [<output.xml>]\n");
fprintf(where, "Options:\n");
fprintf(where, " -R --reverse Reverse the sense of the difference\n");
fprintf(where, " --version Report version and exit\n");
}
static int hwloc_diff_read(const char *inputdiff,
hwloc_topology_diff_t *firstdiffp, char **refnamep)
{
size_t buflen, offset, readlen;
char *buffer, *tmp;
size_t ret;
int err;
if (strcmp(inputdiff, "-"))
return hwloc_topology_diff_load_xml(inputdiff, firstdiffp, refnamep);
buflen = 4096;
buffer = malloc(buflen+1); /* one more byte for the ending \0 */
if (!buffer)
goto out;
offset = 0; readlen = buflen;
while (1) {
ret = fread(buffer+offset, 1, readlen, stdin);
offset += ret;
buffer[offset] = 0;
if (ret != readlen)
break;
buflen *= 2;
tmp = realloc(buffer, buflen+1);
if (!tmp) {
fprintf(stderr, "Failed to realloc buffer for reading diff.\n");
goto out_with_buffer;
}
buffer = tmp;
readlen = buflen/2;
}
err = hwloc_topology_diff_load_xmlbuffer(buffer, (int)(offset+1), firstdiffp, refnamep);
free(buffer);
return err;
out_with_buffer:
free(buffer);
out:
return -1;
}
int main(int argc, char *argv[])
{
hwloc_topology_t topo;
hwloc_topology_diff_t firstdiff = NULL;
unsigned long flags = HWLOC_TOPOLOGY_FLAG_INCLUDE_DISALLOWED | HWLOC_TOPOLOGY_FLAG_IMPORT_SUPPORT;
unsigned long patchflags = 0;
char *callname, *input, *inputdiff, *output = NULL, *refname = NULL;
int err;
callname = argv[0];
/* skip argv[0], handle options */
argc--;
argv++;
hwloc_utils_check_api_version(callname);
if (!getenv("HWLOC_XML_VERBOSE"))
putenv((char *) "HWLOC_XML_VERBOSE=1");
while (argc && *argv[0] == '-') {
if (!strcmp (argv[0], "-R") || !strcmp (argv[0], "--reverse")) {
patchflags ^= HWLOC_TOPOLOGY_DIFF_APPLY_REVERSE;
} else if (!strcmp (argv[0], "--version")) {
printf("%s %s\n", callname, HWLOC_VERSION);
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[0], "-h") || !strcmp(argv[0], "--help")) {
usage(callname, stdout);
exit(EXIT_SUCCESS);
} else {
fprintf(stderr, "Unrecognized options: %s\n", argv[0]);
usage(callname, stderr);
exit(EXIT_FAILURE);
}
argc--;
argv++;
}
if (argc < 2) {
usage(callname, stderr);
exit(EXIT_FAILURE);
}
input = argv[0];
inputdiff = argv[1];
argc -= 2;
argv += 2;
if (argc >= 1) {
output = argv[0];
argc--;
argv++;
}
/* load the diff and get the refname */
err = hwloc_diff_read(inputdiff, &firstdiff, &refname);
if (err < 0) {
fprintf(stderr, "Failed to load XML topology diff %s\n", inputdiff);
goto out;
}
/* load the input topology */
hwloc_topology_init(&topo);
hwloc_topology_set_all_types_filter(topo, HWLOC_TYPE_FILTER_KEEP_ALL);
hwloc_topology_set_flags(topo, flags);
if (!strcmp(input, "refname")) {
/* use the diff refname as input */
if (!refname) {
fprintf(stderr, "Couldn't find the reference topology name from the input diff %s\n", inputdiff);
goto out_with_topo;
}
err = hwloc_topology_set_xml(topo, refname);
if (err < 0) {
fprintf(stderr, "Failed to load XML topology %s (from input diff %s refname)\n", refname, inputdiff);
goto out_with_topo;
}
} else {
/* use the given input */
err = hwloc_topology_set_xml(topo, input);
if (err < 0) {
fprintf(stderr, "Failed to load XML topology %s\n", input);
goto out_with_topo;
}
}
err = hwloc_topology_load(topo);
if (err < 0) {
fprintf(stderr, "Failed to load topology\n");
goto out_with_topo;
}
err = hwloc_topology_diff_apply(topo, firstdiff, patchflags);
if (err < 0) {
fprintf(stderr, "Failed to%s apply topology diff %s, failed for hunk #%d hunk\n",
(patchflags & HWLOC_TOPOLOGY_DIFF_APPLY_REVERSE) ? " reverse" : "",
inputdiff, -err);
goto out_with_topo;
}
err = hwloc_topology_export_xml(topo, output ? output : input, 0);
if (err < 0) {
fprintf(stderr, "Failed to export patched topology %s\n", output);
goto out_with_topo;
}
hwloc_topology_destroy(topo);
hwloc_topology_diff_destroy(firstdiff);
exit(EXIT_SUCCESS);
out_with_topo:
hwloc_topology_destroy(topo);
hwloc_topology_diff_destroy(firstdiff);
out:
exit(EXIT_FAILURE);
}
|