blob: 255add1bf54ff139c5e226aef675d9645152955a [file] [log] [blame]
Dan Willemsen6e63e0f2016-10-17 15:33:58 -07001// Copyright 2016 Google Inc. All rights reserved
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// +build ignore
16
17// This command will dump the contents of a kati stamp file into a more portable
18// format for use by other tools. For now, it just exports the files read.
19// Later, this will be expanded to include the Glob and Shell commands, but
20// those require a more complicated output format.
21
22#include <stdio.h>
23
24#include <string>
25
26#include "io.h"
27#include "log.h"
28#include "strutil.h"
29
30int main(int argc, char* argv[]) {
Dan Willemsen9f2154c2017-11-21 13:02:03 -080031 bool dump_files = false;
32 bool dump_env = false;
33
Dan Willemsen6e63e0f2016-10-17 15:33:58 -070034 if (argc == 1) {
Dan Willemsen9f2154c2017-11-21 13:02:03 -080035 fprintf(stderr, "Usage: ckati_stamp_dump [--env] [--files] <stamp>\n");
Dan Willemsen6e63e0f2016-10-17 15:33:58 -070036 return 1;
37 }
38
Dan Willemsen9f2154c2017-11-21 13:02:03 -080039 for (int i = 1; i < argc - 1; i++) {
40 const char* arg = argv[i];
41 if (!strcmp(arg, "--env")) {
42 dump_env = true;
43 } else if (!strcmp(arg, "--files")) {
44 dump_files = true;
45 } else {
46 fprintf(stderr, "Unknown option: %s", arg);
47 return 1;
48 }
49 }
50
51 if (!dump_files && !dump_env) {
52 dump_files = true;
53 }
54
55 FILE* fp = fopen(argv[argc - 1], "rb");
Dan Willemsen3ce083f2017-10-11 22:17:48 -070056 if (!fp)
Dan Willemsen6e63e0f2016-10-17 15:33:58 -070057 PERROR("fopen");
58
59 ScopedFile sfp(fp);
60 double gen_time;
61 size_t r = fread(&gen_time, sizeof(gen_time), 1, fp);
62 if (r != 1)
63 ERROR("Incomplete stamp file");
64
65 int num_files = LoadInt(fp);
66 if (num_files < 0)
67 ERROR("Incomplete stamp file");
68 for (int i = 0; i < num_files; i++) {
69 string s;
Dan Willemsenf09d21d2016-10-20 17:04:07 -070070 if (!LoadString(fp, &s))
Dan Willemsen6e63e0f2016-10-17 15:33:58 -070071 ERROR("Incomplete stamp file");
Dan Willemsen9f2154c2017-11-21 13:02:03 -080072 if (dump_files)
73 printf("%s\n", s.c_str());
74 }
75
76 int num_undefineds = LoadInt(fp);
77 if (num_undefineds < 0)
78 ERROR("Incomplete stamp file");
79 for (int i = 0; i < num_undefineds; i++) {
80 string s;
81 if (!LoadString(fp, &s))
82 ERROR("Incomplete stamp file");
83 if (dump_env)
84 printf("undefined: %s\n", s.c_str());
85 }
86
87 int num_envs = LoadInt(fp);
88 if (num_envs < 0)
89 ERROR("Incomplete stamp file");
90 for (int i = 0; i < num_envs; i++) {
91 string name;
92 string val;
93 if (!LoadString(fp, &name))
94 ERROR("Incomplete stamp file");
95 if (!LoadString(fp, &val))
96 ERROR("Incomplete stamp file");
97 if (dump_env)
98 printf("%s: %s\n", name.c_str(), val.c_str());
Dan Willemsen6e63e0f2016-10-17 15:33:58 -070099 }
100
101 return 0;
102}