File: gfwhere.c

package info (click to toggle)
gfarm 2.3.2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,972 kB
  • ctags: 10,894
  • sloc: ansic: 84,510; sh: 13,707; java: 6,866; makefile: 2,286; python: 771; perl: 325; sql: 130; xml: 50; asm: 37; csh: 2
file content (174 lines) | stat: -rw-r--r-- 3,638 bytes parent folder | download
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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <gfarm/gfarm.h>
#include "gfarm_foreach.h"

char *program_name = "gfwhere";

static void
display_name(char *name)
{
	static int print_ln;

	if (print_ln)
		printf("\n");
	else
		print_ln = 1;

	printf("%s:\n", name);
}

static gfarm_error_t
display_copy(char *path, int do_not_display_name)
{
	int n, i;
	char **hosts;
	gfarm_error_t e;

	e = gfs_replica_list_by_name(path, &n, &hosts);
	if (e != GFARM_ERR_NO_ERROR)
		return (e);

	if (!do_not_display_name)
		display_name(path);

	i = 0;
	if (i < n)
		printf("%s", hosts[i++]);
	for (; i < n; ++i)
		printf(" %s", hosts[i]);
	printf("\n");

	for (i = 0; i < n; ++i)
		free(hosts[i]);
	free(hosts);

	return (e);
}

static gfarm_error_t
display_replica_catalog(char *path, struct gfs_stat *st, void *arg)
{
	gfarm_error_t e;
	gfarm_mode_t mode;
	int do_not_display_name = *(int *)arg;

	mode = st->st_mode;
	if (GFARM_S_ISDIR(mode))
		e = GFARM_ERR_IS_A_DIRECTORY;
	else if (!GFARM_S_ISREG(mode))
		e = GFARM_ERR_FUNCTION_NOT_IMPLEMENTED;
	else if (st->st_ncopy == 0 && st->st_size > 0)
		/* XXX - GFARM_ERR_NO_REPLICA */
		e = GFARM_ERR_NO_SUCH_OBJECT;
	else
		e = display_copy(path, do_not_display_name);

	if (e != GFARM_ERR_NO_ERROR)
		fprintf(stderr, "%s: %s\n", path, gfarm_error_string(e));
	return (e);
}

void
usage(void)
{
	fprintf(stderr, "Usage: %s [option] <path>...\n", program_name);
	fprintf(stderr, "option:\n");
	fprintf(stderr, "\t-r, -R\t\tdisplay subdirectories recursively\n");
	exit(1);
}

int
main(int argc, char **argv)
{
	int argc_save = argc;
	char **argv_save = argv;
	gfarm_error_t e, e_save = GFARM_ERR_NO_ERROR;
	int i, n, ch, opt_recursive = 0;
	gfarm_stringlist paths;
	gfs_glob_t types;

	if (argc >= 1)
		program_name = basename(argv[0]);

	while ((ch = getopt(argc, argv, "rR?")) != -1) {
		switch (ch) {
		case 'r':
		case 'R':
			opt_recursive = 1;
			break;
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	e = gfarm_initialize(&argc_save, &argv_save);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(1);
	}
	if (argc == 0) {
		usage();
	}

	e = gfarm_stringlist_init(&paths);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(EXIT_FAILURE);
	}
	e = gfs_glob_init(&types);
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < argc; i++)
		gfs_glob(argv[i], &paths, &types);
	gfs_glob_free(&types);

	n = gfarm_stringlist_length(&paths);
	for (i = 0; i < n; i++) {
		char *p = gfarm_stringlist_elem(&paths, i);
		struct gfs_stat st;
		int do_not_display = 0;

		if ((e = gfs_stat(p, &st)) != GFARM_ERR_NO_ERROR) {
			fprintf(stderr, "%s: %s\n", p, gfarm_error_string(e));
		} else {
			if (GFARM_S_ISREG(st.st_mode)) {
				do_not_display = (n == 1);
				e = display_replica_catalog(p, &st,
					&do_not_display);
			}
			else if (opt_recursive)
				e = gfarm_foreach_directory_hierarchy(
					display_replica_catalog, NULL, NULL,
					p, &do_not_display);
			else
				fprintf(stderr, "%s: not a file\n", p);
			gfs_stat_free(&st);
			if (e_save == GFARM_ERR_NO_ERROR)
				e_save = e;
		}
	}

	gfarm_stringlist_free_deeply(&paths);
	e = gfarm_terminate();
	if (e != GFARM_ERR_NO_ERROR) {
		fprintf(stderr, "%s: %s\n", program_name,
		    gfarm_error_string(e));
		exit(1);
	}
	return (e_save == GFARM_ERR_NO_ERROR ? 0 : 1);
}