~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to chirp/src/chirp_put.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include <stdio.h>
 
9
#include <errno.h>
 
10
#include <string.h>
 
11
#include <stdlib.h>
 
12
#include <dirent.h>
 
13
#include <time.h>
 
14
#include <sys/stat.h>
 
15
 
 
16
#include "chirp_reli.h"
 
17
#include "chirp_recursive.h"
 
18
#include "chirp_stream.h"
 
19
 
 
20
#include "debug.h"
 
21
#include "auth_all.h"
 
22
#include "stringtools.h"
 
23
#include "xmalloc.h"
 
24
#include "full_io.h"
 
25
 
 
26
#if CCTOOLS_OPSYS_CYGWIN || CCTOOLS_OPSYS_DARWIN || CCTOOLS_OPSYS_FREEBSD
 
27
#define fopen64 fopen
 
28
#define open64 open
 
29
#define lseek64 lseek
 
30
#define stat64 stat
 
31
#define fstat64 fstat
 
32
#define lstat64 lstat
 
33
#define fseeko64 fseeko
 
34
#endif
 
35
 
 
36
static int timeout=3600;
 
37
static int buffer_size=65536;
 
38
 
 
39
static void show_version( const char *cmd )
 
40
{
 
41
        printf("%s version %d.%d.%d built by %s@%s on %s at %s\n",cmd,CCTOOLS_VERSION_MAJOR,CCTOOLS_VERSION_MINOR,CCTOOLS_VERSION_MICRO,BUILD_USER,BUILD_HOST,__DATE__,__TIME__);
 
42
}
 
43
 
 
44
static void show_help( const char *cmd )
 
45
{
 
46
        printf("use: %s [options] <local-file> <hostname[:port]> <remote-file>\n",cmd);
 
47
        printf("where options are:\n");
 
48
        printf(" -a <flag>  Require this authentication mode.\n");
 
49
        printf(" -b <size>  Set transfer buffer size. (default is %d bytes)\n",buffer_size);
 
50
        printf(" -d <flag>  Enable debugging for this subsystem.\n");
 
51
        printf(" -f         Follow input file like tail -f.\n");
 
52
        printf(" -t <time>  Timeout for failure. (default is %ds)\n",timeout);
 
53
        printf(" -v         Show program version.\n");
 
54
        printf(" -h         This message.\n");
 
55
}
 
56
 
 
57
int main( int argc, char *argv[] )
 
58
{       
 
59
        int did_explicit_auth = 0;
 
60
        int follow_mode = 0;
 
61
        int whole_file_mode = 1;
 
62
        const char *hostname, *source_file, *target_file;
 
63
        time_t stoptime;
 
64
        FILE *file;
 
65
        char c;
 
66
 
 
67
        debug_config(argv[0]);
 
68
 
 
69
        while((c=getopt(argc,argv,"a:b:d:ft:vh"))!=(char)-1) {
 
70
                switch(c) {
 
71
                        case 'a':
 
72
                                auth_register_byname(optarg);
 
73
                                did_explicit_auth = 1;
 
74
                                break;
 
75
                        case 'b':
 
76
                                buffer_size = atoi(optarg);
 
77
                                break;
 
78
                        case 'd':
 
79
                                debug_flags_set(optarg);
 
80
                                break;
 
81
                        case 'f':
 
82
                                follow_mode = 1;
 
83
                                break;
 
84
                        case 't':
 
85
                                timeout = string_time_parse(optarg);
 
86
                                break;
 
87
                        case 'v':
 
88
                                show_version(argv[0]);
 
89
                                exit(0);
 
90
                                break;
 
91
                        case 'h':
 
92
                                show_help(argv[0]);
 
93
                                exit(0);
 
94
                                break;
 
95
 
 
96
                }
 
97
        }
 
98
 
 
99
        if(!did_explicit_auth) auth_register_all();
 
100
 
 
101
        if( (argc-optind)<3 ) {
 
102
                show_help(argv[0]);
 
103
                exit(0);
 
104
        }
 
105
 
 
106
        source_file = argv[optind];
 
107
        hostname = argv[optind+1];
 
108
        target_file = argv[optind+2];
 
109
        stoptime = time(0) + timeout;
 
110
 
 
111
        if(!strcmp(source_file,"-")) {
 
112
                file = stdin;
 
113
        source_file = "/dev/stdin";
 
114
        } else {
 
115
                file = fopen(source_file,"r");
 
116
                if(!file) {
 
117
                        fprintf(stderr,"chirp_put: couldn't open %s: %s\n",source_file,strerror(errno));
 
118
                        return 1;
 
119
                }
 
120
        }
 
121
 
 
122
        if(follow_mode) whole_file_mode = 0;
 
123
 
 
124
        if(whole_file_mode) {
 
125
                int result = chirp_recursive_put(hostname,source_file,target_file,stoptime);
 
126
                if(result<0) {
 
127
                        fprintf(stderr,"chirp_put: couldn't put %s to host %s: %s\n",source_file,hostname,strerror(errno));
 
128
                        return 1;
 
129
                } else {
 
130
                        return 0;
 
131
                }
 
132
        } else {
 
133
                struct chirp_stream *stream;
 
134
                char *buffer = xxmalloc(buffer_size);
 
135
                INT64_T ractual, wactual;
 
136
 
 
137
                stream = chirp_stream_open(hostname,target_file,CHIRP_STREAM_WRITE,stoptime);
 
138
                if(!stream) {
 
139
                        fprintf(stderr,"chirp_put: couldn't open %s for writing: %s\n",target_file,strerror(errno));
 
140
                        return 1;
 
141
                }
 
142
 
 
143
                while(1) {
 
144
                        ractual = full_fread(file,buffer,buffer_size);
 
145
                        if(ractual==0) {
 
146
                                if(follow_mode) {
 
147
                                        debug(D_DEBUG,"waiting for more data...");
 
148
                                        sleep(1);
 
149
                                        continue;
 
150
                                } else {
 
151
                                        break;
 
152
                                }
 
153
                        }
 
154
                        wactual = chirp_stream_write(stream,buffer,ractual,stoptime);
 
155
                        if(wactual!=ractual) {
 
156
                                fprintf(stderr,"chirp_put: couldn't write to %s: %s\n",target_file,strerror(errno));
 
157
                                return 1;
 
158
                        }
 
159
                }
 
160
                chirp_stream_close(stream,stoptime);
 
161
                return 0;
 
162
        }
 
163
}