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>
22 extern CONNECTION conn;
25 extern int code, errno;
27 extern int have_authorization, have_file, done;
40 * linear checksum of bytes
43 * (initial protocol already done)
44 * <<< (int)code (can we accept the file?)
50 * >>> (STRING)data (last data block)
51 * <<< (int)code (from read, write, checksum verify)
55 * 0 for success, 1 for failure
58 * perform initial preparations and receive file as
59 * a single string, storing it into <pathname>
64 get_file(pathname, file_size, checksum)
72 if (!have_authorization) {
73 reject_call(SMS_PERM);
76 if (done) /* re-initialize data */
79 (void) unlink(pathname);
80 /* open file descriptor */
81 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, 0700);
84 sprintf(buf, "%s: creating file %s (get_file)",
85 error_message(code), pathname);
87 report_error("reporting file creation error (get_file)");
90 /* check to see if we've got the disk space */
92 while (n_written < file_size) {
94 n_wrote = write(fd, buf, sizeof(buf));
97 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
98 error_message(code), pathname);
100 /* do all we can to free the space */
101 (void) unlink(pathname);
102 (void) ftruncate(fd, 0);
104 report_error("reporting test-write error (get_file)");
107 n_written += n_wrote;
111 lose("sending okay for file transfer (get_file)");
113 while (n_written < file_size && code == 0) {
114 int n_got = get_block(fd, file_size - n_written);
116 /* get_block has already printed a message */
121 if (n_written != file_size)
123 lose("receiving data");
126 code = connection_errno(conn);
127 report_error("reading file (get_file)");
131 ftruncate(fd, file_size);
134 /* validate checksum */
135 found_checksum = checksum_file(pathname);
136 if (checksum != found_checksum) {
137 code = SMS_MISSINGFILE;
138 com_err(whoami, code, ": expected = %d, found = %d",
139 checksum, found_checksum);
140 report_error("checksum error");
143 /* send ack or nack */
146 code = connection_errno(conn);
147 (void) unlink(pathname);
148 lose("sending ok after file transfer (get_file)");
155 get_block(fd, max_size)
162 code = receive_object(conn, (char *)&data, STRING_T);
164 code = connection_errno(conn);
165 lose("receiving data file (get_file)");
167 n_read = MIN(MAX_STRING_SIZE(data), max_size);
170 register int n_wrote;
171 n_wrote = write(fd, STRING_DATA(data)+n,
175 sprintf(buf, "%s: writing file (get_file)", error_message(code));
178 report_error("reporting write error (get_file)");