File: gfcp.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 (192 lines) | stat: -rw-r--r-- 3,993 bytes parent folder | download | duplicates (2)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * $Id: gfcp.c 2373 2006-01-12 00:54:22Z soda $
 *
 * Copy a file
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <gfarm/gfarm.h>

char *program_name = "gfcp";

void
usage()
{
	fprintf(stderr, "Usage: %s [option] <input_file> <output_file>\n",
		program_name);
	fprintf(stderr, "option:\n");
	fprintf(stderr, "\t-p\t\t\tpreserve file status\n");
	fprintf(stderr, "\t-I fragment-index\tspecify a fragment index\n");
	exit(1);
}

#define BUFSIZE	65536

int
main(int argc, char *argv[])
{
	int flag_preserve = 0;
	char *e, c, *input, *output, *gfarm_index = NULL;
	GFS_File igf, ogf;
	struct gfs_stat gstat;
	gfarm_mode_t mode;
	struct gfarm_timespec gtspec[2];
	extern char *optarg;
	extern int optind;

	e = gfarm_initialize(&argc, &argv);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(1);
	}

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

	while ((c = getopt(argc, argv, "hI:p?")) != -1) {
		switch (c) {
		case 'p':
			flag_preserve = 1;
			break;
		case 'I':
			gfarm_index = optarg;
			break;
		case 'h':
		case '?':
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 2 || argc > 3 /* XXX */)
		usage();
	output = argv[argc - 1];
	--argc;

	e = gfs_stat(output, &gstat);
	if (e == NULL) {
		if (GFARM_S_ISDIR(gstat.st_mode)) {
			fprintf(stderr, "%s: is a directory, "
				"not supported yet\n", output);
			exit(1);
		}
		/*
		 * XXX - gfs_stat() may return a non-null pointer because
		 * a process in the same parallel process might create
		 * it but a different file fragment already.
		 */
		/* fprintf(stderr, "%s: already exist\n", output); */
		gfs_stat_free(&gstat);
	}

	e = gfs_realpath(*argv, &input);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", *argv, e);
		exit(1);
	}
	e = gfs_stat(input, &gstat);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", input, e);
		exit(1);
	}
	if (flag_preserve) {
		mode = gstat.st_mode;
		gtspec[0] = gstat.st_atimespec;
		gtspec[1] = gstat.st_mtimespec;
	}
	else
		mode = 0666;
	gfs_stat_free(&gstat);

	/** **/

	e = gfs_pio_open(input, GFARM_FILE_RDONLY, &igf);
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", input, e);
		exit(1);
	}
	if (gfarm_index == NULL) {
		e = gfs_pio_set_view_local(igf, GFARM_FILE_SEQUENTIAL);
		if (e != NULL) {
			fprintf(stderr, "%s: set_view_local(%s): %s\n",
				program_name, input, e);
			exit(1);
		}
	}
	else {
		e = gfs_pio_set_view_section(
			igf, gfarm_index, NULL, GFARM_FILE_SEQUENTIAL);
		if (e != NULL) {
			fprintf(stderr, "%s: set_view_section(%s, %s): %s\n",
				program_name, input, gfarm_index, e);
			exit(1);
		}
	}

	/** **/

	e = gfs_pio_create(output, GFARM_FILE_WRONLY|GFARM_FILE_TRUNC, mode,
	    &ogf);
	if (e != NULL) {
		fprintf(stderr, "%s: cannot open %s: %s\n",
			program_name, output, e);
		exit(1);
	}
	if (gfarm_index == NULL) {
		e = gfs_pio_set_view_local(ogf, GFARM_FILE_SEQUENTIAL);
		if (e != NULL) {
			fprintf(stderr, "%s: set_view_local(%s): %s\n",
				program_name, output, e);
			exit(1);
		}
	}
	else {
		e = gfs_pio_set_view_section(
			ogf, gfarm_index, NULL, GFARM_FILE_SEQUENTIAL);
		if (e != NULL) {
			fprintf(stderr, "%s: set_view_section(%s, %s): %s\n",
				program_name, output, gfarm_index, e);
			exit(1);
		}
	}

	/* copy this fragment */
	for (;;) {
		int rv, wv;
		char buffer[BUFSIZE];
		e = gfs_pio_read(igf, buffer, sizeof(buffer), &rv);
		if (e != NULL || rv == 0)
			break;
		e = gfs_pio_write(ogf, buffer, rv, &wv);
		if (e != NULL)
			break;
	}
	if (e != NULL)
		fprintf(stderr, "%s\n", e);

	e = gfs_pio_close(igf);
	if (e != NULL)
		fprintf(stderr, "%s: close failed: %s\n", input, e);
	e = gfs_pio_close(ogf);
	if (e != NULL)
		fprintf(stderr, "%s: close failed: %s\n", output, e);

	if (flag_preserve) {
		e = gfs_utimes(output, gtspec);
		if (e != NULL)
			fprintf(stderr, "%s: utimes failed: %s\n", output, e);
	}

	e = gfarm_terminate();
	if (e != NULL) {
		fprintf(stderr, "%s: %s\n", program_name, e);
		exit(1);
	}

	return (0);
}