5 /* (c) Copyright 1988 by the Massachusetts Institute of Technology. */
6 /* For copying and distribution information, please see the file */
7 /* <mit-copyright.h>. */
10 static char *rcsid_get_file_c = "$Header$";
13 #include <mit-copyright.h>
17 #include <sys/param.h>
23 #define MIN(a,b) (((a) < (b))?(a):(b))
26 extern CONNECTION conn;
29 extern int code, errno;
31 extern int have_authorization, have_file, done;
44 * linear checksum of bytes
47 * (initial protocol already done)
48 * <<< (int)code (can we accept the file?)
54 * >>> (STRING)data (last data block)
55 * <<< (int)code (from read, write, checksum verify)
59 * 0 for success, 1 for failure
62 * perform initial preparations and receive file as
63 * a single string, storing it into <pathname>
68 get_file(pathname, file_size, checksum)
76 if (!have_authorization) {
80 if (done) /* re-initialize data */
83 (void) unlink(pathname);
84 /* open file descriptor */
85 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, 0700);
88 sprintf(buf, "%s: creating file %s (get_file)",
89 error_message(code), pathname);
91 report_error("reporting file creation error (get_file)");
94 /* check to see if we've got the disk space */
96 while (n_written < file_size) {
98 n_wrote = write(fd, buf, sizeof(buf));
101 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
102 error_message(code), pathname);
104 /* do all we can to free the space */
105 (void) unlink(pathname);
106 (void) ftruncate(fd, 0);
108 report_error("reporting test-write error (get_file)");
111 n_written += n_wrote;
115 lose("sending okay for file transfer (get_file)");
117 while (n_written < file_size && code == 0) {
118 int n_got = get_block(fd, file_size - n_written);
120 /* get_block has already printed a message */
125 if (n_written != file_size)
127 lose("receiving data");
130 code = connection_errno(conn);
131 report_error("reading file (get_file)");
135 ftruncate(fd, file_size);
138 /* validate checksum */
139 found_checksum = checksum_file(pathname);
140 if (checksum != found_checksum) {
141 code = MR_MISSINGFILE;
142 com_err(whoami, code, ": expected = %d, found = %d",
143 checksum, found_checksum);
144 report_error("checksum error");
147 /* send ack or nack */
150 code = connection_errno(conn);
151 (void) unlink(pathname);
152 lose("sending ok after file transfer (get_file)");
159 get_block(fd, max_size)
166 code = receive_object(conn, (char *)&data, STRING_T);
168 code = connection_errno(conn);
169 lose("receiving data file (get_file)");
171 n_read = MIN(MAX_STRING_SIZE(data), max_size);
174 register int n_wrote;
175 n_wrote = write(fd, STRING_DATA(data)+n,
179 sprintf(buf, "%s: writing file (get_file)", error_message(code));
182 report_error("reporting write error (get_file)");