7 static char *rcsid_get_file_c = "$Header$";
13 #include <sys/param.h>
19 extern CONNECTION conn;
22 extern int code, errno;
24 extern int have_authorization, have_file, done;
37 * linear checksum of bytes
40 * (initial protocol already done)
41 * <<< (int)code (can we accept the file?)
47 * >>> (STRING)data (last data block)
48 * <<< (int)code (from read, write, checksum verify)
52 * 0 for success, 1 for failure
55 * perform initial preparations and receive file as
56 * a single string, storing it into <pathname>
61 get_file(pathname, file_size, checksum)
69 if (!have_authorization) {
70 reject_call(SMSU_NO_AUTH);
73 if (done) /* re-initialize data */
76 (void) unlink(pathname);
77 /* open file descriptor */
78 fd = open(pathname, O_CREAT|O_EXCL|O_WRONLY, 0700);
81 sprintf(buf, "%s: creating file %s (get_file)",
82 error_message(code), pathname);
84 report_error("reporting file creation error (get_file)");
87 /* check to see if we've got the disk space */
89 while (n_written < file_size) {
91 n_wrote = write(fd, buf, sizeof(buf));
94 sprintf(buf, "%s: verifying free disk space for %s (get_file)",
95 error_message(code), pathname);
97 /* do all we can to free the space */
98 (void) unlink(pathname);
99 (void) ftruncate(fd, 0);
101 report_error("reporting test-write error (get_file)");
104 n_written += n_wrote;
108 lose("sending okay for file transfer (get_file)");
110 while (n_written < file_size && code == 0) {
111 int n_got = get_block(fd, file_size - n_written);
113 /* get_block has already printed a message */
118 if (n_written != file_size)
120 lose("receiving data");
123 code = connection_errno(conn);
124 report_error("reading file (get_file)");
128 ftruncate(fd, file_size);
131 /* validate checksum */
132 fd = open(pathname, O_RDONLY, 0);
135 report_error("re-opening file for checksum verification");
138 found_checksum = checksum_fd(fd);
139 if (checksum != found_checksum) {
140 code = SMSU_CHECKSUM;
141 com_err(whoami, code, ": expected = %d, found = %d",
142 checksum, found_checksum);
143 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)");