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 extern CONNECTION conn;
26 extern int code, errno;
28 extern int have_authorization, have_file, done;
41 * linear checksum of bytes
44 * (initial protocol already done)
45 * <<< (int)code (can we accept the file?)
51 * >>> (STRING)data (last data block)
52 * <<< (int)code (from read, write, checksum verify)
56 * 0 for success, 1 for failure
59 * perform initial preparations and receive file as
60 * a single string, storing it into <pathname>
65 get_file(pathname, file_size, checksum)
73 if (!have_authorization) {
74 reject_call(SMS_PERM);
77 if (done) /* re-initialize data */
80 (void) unlink(pathname);
81 /* open file descriptor */
82 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, 0700);
85 sprintf(buf, "%s: creating file %s (get_file)",
86 error_message(code), pathname);
88 report_error("reporting file creation error (get_file)");
91 /* check to see if we've got the disk space */
93 while (n_written < file_size) {
95 n_wrote = write(fd, buf, sizeof(buf));
98 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
99 error_message(code), pathname);
101 /* do all we can to free the space */
102 (void) unlink(pathname);
103 (void) ftruncate(fd, 0);
105 report_error("reporting test-write error (get_file)");
108 n_written += n_wrote;
112 lose("sending okay for file transfer (get_file)");
114 while (n_written < file_size && code == 0) {
115 int n_got = get_block(fd, file_size - n_written);
117 /* get_block has already printed a message */
122 if (n_written != file_size)
124 lose("receiving data");
127 code = connection_errno(conn);
128 report_error("reading file (get_file)");
132 ftruncate(fd, file_size);
135 /* validate checksum */
136 found_checksum = checksum_file(pathname);
137 if (checksum != found_checksum) {
138 code = SMS_MISSINGFILE;
139 com_err(whoami, code, ": expected = %d, found = %d",
140 checksum, found_checksum);
141 report_error("checksum error");
144 /* send ack or nack */
147 code = connection_errno(conn);
148 (void) unlink(pathname);
149 lose("sending ok after file transfer (get_file)");
156 get_block(fd, max_size)
163 code = receive_object(conn, (char *)&data, STRING_T);
165 code = connection_errno(conn);
166 lose("receiving data file (get_file)");
168 n_read = MIN(MAX_STRING_SIZE(data), max_size);
171 register int n_wrote;
172 n_wrote = write(fd, STRING_DATA(data)+n,
176 sprintf(buf, "%s: writing file (get_file)", error_message(code));
179 report_error("reporting write error (get_file)");