xref: /aosp_15_r20/system/extras/ioshark/dump_ioshark_filenames.c (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
17*288bf522SAndroid Build Coastguard Worker #include <sys/time.h>
18*288bf522SAndroid Build Coastguard Worker #include <sys/types.h>
19*288bf522SAndroid Build Coastguard Worker #include <unistd.h>
20*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
21*288bf522SAndroid Build Coastguard Worker #include <signal.h>
22*288bf522SAndroid Build Coastguard Worker #include <string.h>
23*288bf522SAndroid Build Coastguard Worker #include <sys/stat.h>
24*288bf522SAndroid Build Coastguard Worker #include <errno.h>
25*288bf522SAndroid Build Coastguard Worker #include <fcntl.h>
26*288bf522SAndroid Build Coastguard Worker #include <ctype.h>
27*288bf522SAndroid Build Coastguard Worker #include "ioshark.h"
28*288bf522SAndroid Build Coastguard Worker 
29*288bf522SAndroid Build Coastguard Worker /*
30*288bf522SAndroid Build Coastguard Worker  * Real simple utility that just extracts and dumps the IOshark filenames
31*288bf522SAndroid Build Coastguard Worker  * one per line from the ioshark_filenames file. Useful in debugging.
32*288bf522SAndroid Build Coastguard Worker  */
33*288bf522SAndroid Build Coastguard Worker int
main(int argc,char ** argv)34*288bf522SAndroid Build Coastguard Worker main(int argc __attribute__((unused)), char **argv)
35*288bf522SAndroid Build Coastguard Worker {
36*288bf522SAndroid Build Coastguard Worker 	char *progname;
37*288bf522SAndroid Build Coastguard Worker 	static FILE *filename_cache_fp;
38*288bf522SAndroid Build Coastguard Worker 	struct stat st;
39*288bf522SAndroid Build Coastguard Worker 	struct ioshark_filename_struct *filename_cache;
40*288bf522SAndroid Build Coastguard Worker 	int filename_cache_num_entries;
41*288bf522SAndroid Build Coastguard Worker 	size_t filename_cache_size;
42*288bf522SAndroid Build Coastguard Worker 	int i;
43*288bf522SAndroid Build Coastguard Worker 
44*288bf522SAndroid Build Coastguard Worker 	progname = argv[0];
45*288bf522SAndroid Build Coastguard Worker 	if (stat("ioshark_filenames", &st) < 0) {
46*288bf522SAndroid Build Coastguard Worker 		fprintf(stderr, "%s Can't stat ioshark_filenames file\n",
47*288bf522SAndroid Build Coastguard Worker 			progname);
48*288bf522SAndroid Build Coastguard Worker 			exit(EXIT_FAILURE);
49*288bf522SAndroid Build Coastguard Worker 	}
50*288bf522SAndroid Build Coastguard Worker 	filename_cache_num_entries = st.st_size /
51*288bf522SAndroid Build Coastguard Worker 		sizeof(struct ioshark_filename_struct);
52*288bf522SAndroid Build Coastguard Worker 	filename_cache_fp = fopen("ioshark_filenames", "r");
53*288bf522SAndroid Build Coastguard Worker 	if (filename_cache_fp == NULL) {
54*288bf522SAndroid Build Coastguard Worker 		fprintf(stderr, "%s Cannot open ioshark_filenames file\n",
55*288bf522SAndroid Build Coastguard Worker 			progname);
56*288bf522SAndroid Build Coastguard Worker 		exit(EXIT_FAILURE);
57*288bf522SAndroid Build Coastguard Worker 	}
58*288bf522SAndroid Build Coastguard Worker 	/* Preallocate a fixed size of entries */
59*288bf522SAndroid Build Coastguard Worker 	filename_cache_size = filename_cache_num_entries + 1024;
60*288bf522SAndroid Build Coastguard Worker 	filename_cache = calloc(filename_cache_size,
61*288bf522SAndroid Build Coastguard Worker 				sizeof(struct ioshark_filename_struct));
62*288bf522SAndroid Build Coastguard Worker 	if (filename_cache == NULL) {
63*288bf522SAndroid Build Coastguard Worker 		fprintf(stderr, "%s Can't allocate memory - this is fatal\n",
64*288bf522SAndroid Build Coastguard Worker 			__func__);
65*288bf522SAndroid Build Coastguard Worker 		exit(EXIT_FAILURE);
66*288bf522SAndroid Build Coastguard Worker 	}
67*288bf522SAndroid Build Coastguard Worker 	if (fread(filename_cache,
68*288bf522SAndroid Build Coastguard Worker 		  sizeof(struct ioshark_filename_struct),
69*288bf522SAndroid Build Coastguard Worker 		  filename_cache_num_entries,
70*288bf522SAndroid Build Coastguard Worker 		  filename_cache_fp) != (size_t)filename_cache_num_entries) {
71*288bf522SAndroid Build Coastguard Worker 		fprintf(stderr, "%s Can't read ioshark_filenames file\n",
72*288bf522SAndroid Build Coastguard Worker 			progname);
73*288bf522SAndroid Build Coastguard Worker 		exit(EXIT_FAILURE);
74*288bf522SAndroid Build Coastguard Worker 	}
75*288bf522SAndroid Build Coastguard Worker        for (i = 0 ; i < filename_cache_num_entries ; i++) {
76*288bf522SAndroid Build Coastguard Worker 	       printf("%s\n", filename_cache[i].path);
77*288bf522SAndroid Build Coastguard Worker        }
78*288bf522SAndroid Build Coastguard Worker        free(filename_cache);
79*288bf522SAndroid Build Coastguard Worker        fclose(filename_cache_fp);
80*288bf522SAndroid Build Coastguard Worker }
81