summaryrefslogtreecommitdiffstats
path: root/community-scripts/rename/atomic/bug1034/gt_read.c
blob: 7d07d59426f4c6596b2476b160aebc49a6cebb5c (plain)
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
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>

#include "gt.h"

static void
usage(char *progname)
{
	fprintf(stderr, "usage: %s node_count\n", progname);
	exit(1);
}

static void
dump_data(char *buf, size_t buflen)
{
	int i;

	fprintf(stderr, "raw data:  ");
	for (i = 0; i < buflen; i++) {
		int c = buf[i];

		if (isgraph(c)) {
			fprintf(stderr, "%c", c);
		} else if (isspace(c)) {
		switch (c) {
		case ' ':
			fprintf(stderr, " ");
			break;
		case '\f':
			fprintf(stderr, "\f");
			break;
		case '\n':
			fprintf(stderr, "\n");
			break;
		case '\r':
			fprintf(stderr, "\r");
			break;
		case '\t':
			fprintf(stderr, "\t");
			break;
		case '\v':
			fprintf(stderr, "\v");
			break;
		default:
			fprintf(stderr, "\%03o", c);
			break;
		}
		} else {
			fprintf(stderr, "\%03o", c);
		}
	}
	fprintf(stderr, "\n");
}

static void
touch_tree(char *rootpath, int node_count)
{
	char pathbuf[PATH_MAX];
	char databuf[PATH_MAX];
	int fd;
	int node;
	int i;
	int retval;

	for (node = 1; node <= node_count; node++) {
		for (i = 0; i < SUBDIR_FILES; i++) {
			snprintf(pathbuf, sizeof(pathbuf),
				"%s/%d/WA_RC_%d", rootpath, node, i);
			fd = open(pathbuf, O_RDONLY);
			if (0 > fd) {
				fprintf(stderr, "gt_read: %s: open %s\n",
					strerror(errno), pathbuf);
				continue;
			}
			retval = read(fd, databuf, sizeof(databuf));
			close(fd);
			if (0 > retval) {
				fprintf(stderr, "gt_read: %s: read %s\n",
					strerror(errno), pathbuf);
			} else if (retval != strlen(pathbuf)) {
				fprintf(stderr,
					"gt_read: incomplete read: read %s\n",
					pathbuf);
			} else {
				databuf[retval] = '\0';
				if (0 != strcmp(pathbuf, databuf)) {
					fprintf(stderr,
						"gt_read: bad data: read %s\n",
						pathbuf);
					dump_data(databuf, strlen(pathbuf));
				}
			}
		}
	}
}

int
main(int argc, char **argv)
{
	char *arg_ptr;
	long node_count;

	if (argc != 2) {
		usage(argv[0]);
	}

	node_count = strtol(argv[1], &arg_ptr, 10);
	if (*arg_ptr != '\0') {
		usage(argv[0]);
	}

	while (1) {
		int snooze;

		touch_tree(BASE_PATH, node_count);

		snooze = CYCLE_TIME + random()%CYCLE_JITTER;
		usleep(1000*snooze);
	}

	exit(0);
}