forked from Starlink/cfitsio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drvrgsiftp.c
535 lines (438 loc) · 13.6 KB
/
drvrgsiftp.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* This file, drvrgsiftp.c contains driver routines for gsiftp files. */
/* Andrea Barisani <lcars@si.inaf.it> */
/* Taffoni Giuliano <taffoni@oats.inaf.it> */
#ifdef HAVE_NET_SERVICES
#ifdef HAVE_GSIFTP
#include <sys/types.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <setjmp.h>
#include "fitsio2.h"
#include <globus_ftp_client.h>
#define MAXLEN 1200
#define NETTIMEOUT 80
#define MAX_BUFFER_SIZE_R 1024
#define MAX_BUFFER_SIZE_W (64*1024)
static int gsiftpopen = 0;
static int global_offset = 0;
static int free_gsiftp_tmp=0;
static int gsiftp_get(char *filename, FILE **gsiftpfile, int num_streams);
static globus_mutex_t lock;
static globus_cond_t cond;
static globus_bool_t done;
static char *gsiftp_tmpfile;
static char *gsiftpurl = NULL;
static char gsiftp_tmpdir[MAXLEN];
static jmp_buf env; /* holds the jump buffer for setjmp/longjmp pairs */
static void signal_handler(int sig);
int gsiftp_init(void)
{
if (getenv("GSIFTP_TMPFILE")) {
gsiftp_tmpfile = getenv("GSIFTP_TMPFILE");
} else {
strncpy(gsiftp_tmpdir, "/tmp/gsiftp_XXXXXX", sizeof gsiftp_tmpdir);
if (mkdtemp(gsiftp_tmpdir) == NULL) {
ffpmsg("Cannot create temporary directory!");
return (FILE_NOT_OPENED);
}
gsiftp_tmpfile = malloc(strlen(gsiftp_tmpdir) + strlen("/gsiftp_buffer.tmp")+1);
gsiftp_tmpfile[0]=0;
free_gsiftp_tmp=1;
strcat(gsiftp_tmpfile, gsiftp_tmpdir);
strcat(gsiftp_tmpfile, "/gsiftp_buffer.tmp");
}
return file_init();
}
int gsiftp_shutdown(void)
{
free(gsiftpurl);
if (free_gsiftp_tmp)
free(gsiftp_tmpfile);
return file_shutdown();
}
int gsiftp_setoptions(int options)
{
return file_setoptions(options);
}
int gsiftp_getoptions(int *options)
{
return file_getoptions(options);
}
int gsiftp_getversion(int *version)
{
return file_getversion(version);
}
int gsiftp_checkfile(char *urltype, char *infile, char *outfile)
{
return file_checkfile(urltype, infile, outfile);
}
int gsiftp_open(char *filename, int rwmode, int *handle)
{
FILE *gsiftpfile;
int num_streams;
if (getenv("GSIFTP_STREAMS")) {
num_streams = (int)getenv("GSIFTP_STREAMS");
} else {
num_streams = 1;
}
if (rwmode) {
gsiftpopen = 2;
} else {
gsiftpopen = 1;
}
if (gsiftpurl)
free(gsiftpurl);
gsiftpurl = strdup(filename);
if (setjmp(env) != 0) {
ffpmsg("Timeout (gsiftp_open)");
goto error;
}
signal(SIGALRM, signal_handler);
alarm(NETTIMEOUT);
if (gsiftp_get(filename,&gsiftpfile,num_streams)) {
alarm(0);
ffpmsg("Unable to open gsiftp file (gsiftp_open)");
ffpmsg(filename);
goto error;
}
fclose(gsiftpfile);
signal(SIGALRM, SIG_DFL);
alarm(0);
return file_open(gsiftp_tmpfile, rwmode, handle);
error:
alarm(0);
signal(SIGALRM, SIG_DFL);
return (FILE_NOT_OPENED);
}
int gsiftp_create(char *filename, int *handle)
{
if (gsiftpurl)
free(gsiftpurl);
gsiftpurl = strdup(filename);
return file_create(gsiftp_tmpfile, handle);
}
int gsiftp_truncate(int handle, LONGLONG filesize)
{
return file_truncate(handle, filesize);
}
int gsiftp_size(int handle, LONGLONG *filesize)
{
return file_size(handle, filesize);
}
int gsiftp_flush(int handle)
{
FILE *gsiftpfile;
int num_streams;
if (getenv("GSIFTP_STREAMS")) {
num_streams = (int)getenv("GSIFTP_STREAMS");
} else {
num_streams = 1;
}
int rc = file_flush(handle);
if (gsiftpopen != 1) {
if (setjmp(env) != 0) {
ffpmsg("Timeout (gsiftp_write)");
goto error;
}
signal(SIGALRM, signal_handler);
alarm(NETTIMEOUT);
if (gsiftp_put(gsiftpurl,&gsiftpfile,num_streams)) {
alarm(0);
ffpmsg("Unable to open gsiftp file (gsiftp_flush)");
ffpmsg(gsiftpurl);
goto error;
}
fclose(gsiftpfile);
signal(SIGALRM, SIG_DFL);
alarm(0);
}
return rc;
error:
alarm(0);
signal(SIGALRM, SIG_DFL);
return (FILE_NOT_OPENED);
}
int gsiftp_seek(int handle, LONGLONG offset)
{
return file_seek(handle, offset);
}
int gsiftp_read(int hdl, void *buffer, long nbytes)
{
return file_read(hdl, buffer, nbytes);
}
int gsiftp_write(int hdl, void *buffer, long nbytes)
{
return file_write(hdl, buffer, nbytes);
}
int gsiftp_close(int handle)
{
unlink(gsiftp_tmpfile);
if (gsiftp_tmpdir)
rmdir(gsiftp_tmpdir);
return file_close(handle);
}
static void done_cb( void * user_arg,
globus_ftp_client_handle_t * handle,
globus_object_t * err)
{
if(err){
fprintf(stderr, "%s", globus_object_printable_to_string(err));
}
globus_mutex_lock(&lock);
done = GLOBUS_TRUE;
globus_cond_signal(&cond);
globus_mutex_unlock(&lock);
return;
}
static void data_cb_read( void * user_arg,
globus_ftp_client_handle_t * handle,
globus_object_t * err,
globus_byte_t * buffer,
globus_size_t length,
globus_off_t offset,
globus_bool_t eof)
{
if(err) {
fprintf(stderr, "%s", globus_object_printable_to_string(err));
}
else {
FILE* fd = (FILE*) user_arg;
int rc = fwrite(buffer, 1, length, fd);
if (ferror(fd)) {
printf("Read error in function data_cb_read; errno = %d\n", errno);
return;
}
if (!eof) {
globus_ftp_client_register_read(handle,
buffer,
MAX_BUFFER_SIZE_R,
data_cb_read,
(void*) fd);
}
}
return;
}
static void data_cb_write( void * user_arg,
globus_ftp_client_handle_t * handle,
globus_object_t * err,
globus_byte_t * buffer,
globus_size_t length,
globus_off_t offset,
globus_bool_t eof)
{
int curr_offset;
if(err) {
fprintf(stderr, "%s", globus_object_printable_to_string(err));
}
else {
if (!eof) {
FILE* fd = (FILE*) user_arg;
int rc;
globus_mutex_lock(&lock);
curr_offset = global_offset;
rc = fread(buffer, 1, MAX_BUFFER_SIZE_W, fd);
global_offset += rc;
globus_mutex_unlock(&lock);
if (ferror(fd)) {
printf("Read error in function data_cb_write; errno = %d\n", errno);
return;
}
globus_ftp_client_register_write(handle,
buffer,
rc,
curr_offset,
feof(fd) != 0,
data_cb_write,
(void*) fd);
} else {
globus_libc_free(buffer);
}
}
return;
}
int gsiftp_get(char *filename, FILE **gsiftpfile, int num_streams)
{
char gsiurl[MAXLEN];
globus_ftp_client_handle_t handle;
globus_ftp_client_operationattr_t attr;
globus_ftp_client_handleattr_t handle_attr;
globus_ftp_control_parallelism_t parallelism;
globus_ftp_control_layout_t layout;
globus_byte_t buffer[MAX_BUFFER_SIZE_R];
globus_size_t buffer_length = sizeof(buffer);
globus_result_t result;
globus_ftp_client_restart_marker_t restart;
globus_ftp_control_type_t filetype;
globus_module_activate(GLOBUS_FTP_CLIENT_MODULE);
globus_mutex_init(&lock, GLOBUS_NULL);
globus_cond_init(&cond, GLOBUS_NULL);
globus_ftp_client_handle_init(&handle, GLOBUS_NULL);
globus_ftp_client_handleattr_init(&handle_attr);
globus_ftp_client_operationattr_init(&attr);
layout.mode = GLOBUS_FTP_CONTROL_STRIPING_NONE;
globus_ftp_client_restart_marker_init(&restart);
globus_ftp_client_operationattr_set_mode(
&attr,
GLOBUS_FTP_CONTROL_MODE_EXTENDED_BLOCK);
if (num_streams >= 1)
{
parallelism.mode = GLOBUS_FTP_CONTROL_PARALLELISM_FIXED;
parallelism.fixed.size = num_streams;
globus_ftp_client_operationattr_set_parallelism(
&attr,
¶llelism);
}
globus_ftp_client_operationattr_set_layout(&attr,
&layout);
filetype = GLOBUS_FTP_CONTROL_TYPE_IMAGE;
globus_ftp_client_operationattr_set_type (&attr,
filetype);
globus_ftp_client_handle_init(&handle, &handle_attr);
done = GLOBUS_FALSE;
strcpy(gsiurl,"gsiftp://");
if (strlen(gsiurl)+strlen(filename) > MAXLEN-1)
{
ffpmsg("file name too long (gsiftp_get)");
return (FILE_NOT_OPENED);
}
strcat(gsiurl,filename);
*gsiftpfile = fopen(gsiftp_tmpfile,"w+");
if (!*gsiftpfile) {
ffpmsg("Unable to open temporary file!");
return (FILE_NOT_OPENED);
}
result = globus_ftp_client_get(&handle,
gsiurl,
&attr,
&restart,
done_cb,
0);
if(result != GLOBUS_SUCCESS) {
globus_object_t * err;
err = globus_error_get(result);
fprintf(stderr, "%s", globus_object_printable_to_string(err));
done = GLOBUS_TRUE;
}
else {
globus_ftp_client_register_read(&handle,
buffer,
buffer_length,
data_cb_read,
(void*) *gsiftpfile);
}
globus_mutex_lock(&lock);
while(!done) {
globus_cond_wait(&cond, &lock);
}
globus_mutex_unlock(&lock);
globus_ftp_client_handle_destroy(&handle);
globus_module_deactivate_all();
return 0;
}
int gsiftp_put(char *filename, FILE **gsiftpfile, int num_streams)
{
int i;
char gsiurl[MAXLEN];
globus_ftp_client_handle_t handle;
globus_ftp_client_operationattr_t attr;
globus_ftp_client_handleattr_t handle_attr;
globus_ftp_control_parallelism_t parallelism;
globus_ftp_control_layout_t layout;
globus_byte_t * buffer;
globus_size_t buffer_length = sizeof(buffer);
globus_result_t result;
globus_ftp_client_restart_marker_t restart;
globus_ftp_control_type_t filetype;
globus_module_activate(GLOBUS_FTP_CLIENT_MODULE);
globus_mutex_init(&lock, GLOBUS_NULL);
globus_cond_init(&cond, GLOBUS_NULL);
globus_ftp_client_handle_init(&handle, GLOBUS_NULL);
globus_ftp_client_handleattr_init(&handle_attr);
globus_ftp_client_operationattr_init(&attr);
layout.mode = GLOBUS_FTP_CONTROL_STRIPING_NONE;
globus_ftp_client_restart_marker_init(&restart);
globus_ftp_client_operationattr_set_mode(
&attr,
GLOBUS_FTP_CONTROL_MODE_EXTENDED_BLOCK);
if (num_streams >= 1)
{
parallelism.mode = GLOBUS_FTP_CONTROL_PARALLELISM_FIXED;
parallelism.fixed.size = num_streams;
globus_ftp_client_operationattr_set_parallelism(
&attr,
¶llelism);
}
globus_ftp_client_operationattr_set_layout(&attr,
&layout);
filetype = GLOBUS_FTP_CONTROL_TYPE_IMAGE;
globus_ftp_client_operationattr_set_type (&attr,
filetype);
globus_ftp_client_handle_init(&handle, &handle_attr);
done = GLOBUS_FALSE;
strcpy(gsiurl,"gsiftp://");
if (strlen(gsiurl)+strlen(filename) > MAXLEN-1)
{
ffpmsg("file name too long (gsiftp_put)");
return (FILE_NOT_OPENED);
}
strcat(gsiurl,filename);
*gsiftpfile = fopen(gsiftp_tmpfile,"r");
if (!*gsiftpfile) {
ffpmsg("Unable to open temporary file!");
return (FILE_NOT_OPENED);
}
result = globus_ftp_client_put(&handle,
gsiurl,
&attr,
&restart,
done_cb,
0);
if(result != GLOBUS_SUCCESS) {
globus_object_t * err;
err = globus_error_get(result);
fprintf(stderr, "%s", globus_object_printable_to_string(err));
done = GLOBUS_TRUE;
}
else {
int rc;
int curr_offset;
for (i = 0; i< 2 * num_streams && feof(*gsiftpfile) == 0; i++)
{
buffer = malloc(MAX_BUFFER_SIZE_W);
globus_mutex_lock(&lock);
curr_offset = global_offset;
rc = fread(buffer, 1, MAX_BUFFER_SIZE_W, *gsiftpfile);
global_offset += rc;
globus_mutex_unlock(&lock);
globus_ftp_client_register_write(
&handle,
buffer,
rc,
curr_offset,
feof(*gsiftpfile) != 0,
data_cb_write,
(void*) *gsiftpfile);
}
}
globus_mutex_lock(&lock);
while(!done) {
globus_cond_wait(&cond, &lock);
}
globus_mutex_unlock(&lock);
globus_ftp_client_handle_destroy(&handle);
globus_module_deactivate_all();
return 0;
}
static void signal_handler(int sig) {
switch (sig) {
case SIGALRM: /* process for alarm */
longjmp(env,sig);
default: {
/* Hmm, shouldn't have happend */
exit(sig);
}
}
}
#endif
#endif