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, uid;
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, mode)
77 if (!have_authorization) {
81 if (done) /* re-initialize data */
83 if (setreuid(0, uid) < 0) {
84 com_err(whoami, errno, "Unable to setuid to %d\n", uid);
88 if (!config_lookup("noclobber"))
89 (void) unlink(pathname);
90 /* open file descriptor */
91 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, mode);
94 sprintf(buf, "%s: creating file %s (get_file)",
95 error_message(code), pathname);
97 report_error("reporting file creation error (get_file)");
99 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
104 /* check to see if we've got the disk space */
106 while (n_written < file_size) {
107 register int n_wrote;
108 n_wrote = write(fd, buf, sizeof(buf));
111 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
112 error_message(code), pathname);
114 /* do all we can to free the space */
115 (void) unlink(pathname);
116 (void) ftruncate(fd, 0);
118 report_error("reporting test-write error (get_file)");
120 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
125 n_written += n_wrote;
129 lose("sending okay for file transfer (get_file)");
131 while (n_written < file_size && code == 0) {
132 int n_got = get_block(fd, file_size - n_written);
134 /* get_block has already printed a message */
137 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
143 if (n_written != file_size)
145 lose("receiving data");
148 code = connection_errno(conn);
149 report_error("reading file (get_file)");
151 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
157 ftruncate(fd, file_size);
161 com_err(whoami, errno, "Unable to setuid back to %d\n", 0);
164 /* validate checksum */
165 found_checksum = checksum_file(pathname);
166 if (checksum != found_checksum) {
167 code = MR_MISSINGFILE;
168 com_err(whoami, code, ": expected = %d, found = %d",
169 checksum, found_checksum);
170 report_error("checksum error");
173 /* send ack or nack */
176 code = connection_errno(conn);
177 (void) unlink(pathname);
178 lose("sending ok after file transfer (get_file)");
185 get_block(fd, max_size)
192 code = receive_object(conn, (char *)&data, STRING_T);
194 code = connection_errno(conn);
195 lose("receiving data file (get_file)");
197 n_read = MIN(MAX_STRING_SIZE(data), max_size);
200 register int n_wrote;
201 n_wrote = write(fd, STRING_DATA(data)+n,
205 sprintf(buf, "%s: writing file (get_file)", error_message(code));
208 report_error("reporting write error (get_file)");