lpd,lpr: send/receive ACKs after filenames, not only after file bodies.
lpqr_main 1114 1149 +35 lpd_main 748 768 +20
This commit is contained in:
parent
e99a92dc2f
commit
c445758708
@ -236,6 +236,9 @@ int lpd_main(int argc UNUSED_PARAM, char *argv[])
|
|||||||
fd = xopen(queue, O_RDWR | O_APPEND);
|
fd = xopen(queue, O_RDWR | O_APPEND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// signal OK
|
||||||
|
safe_write(STDOUT_FILENO, "", 1);
|
||||||
|
|
||||||
// copy the file
|
// copy the file
|
||||||
real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len);
|
real_len = bb_copyfd_size(STDIN_FILENO, fd, expected_len);
|
||||||
if (real_len != expected_len) {
|
if (real_len != expected_len) {
|
||||||
@ -243,7 +246,7 @@ int lpd_main(int argc UNUSED_PARAM, char *argv[])
|
|||||||
expected_len, real_len);
|
expected_len, real_len);
|
||||||
goto err_exit;
|
goto err_exit;
|
||||||
}
|
}
|
||||||
// get ACK and see whether it is NUL (ok)
|
// get EOF indicator, see whether it is NUL (ok)
|
||||||
// (and don't trash s[0]!)
|
// (and don't trash s[0]!)
|
||||||
if (safe_read(STDIN_FILENO, &s[1], 1) != 1 || s[1] != 0) {
|
if (safe_read(STDIN_FILENO, &s[1], 1) != 1 || s[1] != 0) {
|
||||||
// don't send error msg to peer - it obviously
|
// don't send error msg to peer - it obviously
|
||||||
@ -262,6 +265,9 @@ int lpd_main(int argc UNUSED_PARAM, char *argv[])
|
|||||||
|
|
||||||
free(s);
|
free(s);
|
||||||
close(fd); // NB: can do close(-1). Who cares?
|
close(fd); // NB: can do close(-1). Who cares?
|
||||||
|
|
||||||
|
// NB: don't do "signal OK" write here, it will be done
|
||||||
|
// at the top of the loop
|
||||||
} // while (1)
|
} // while (1)
|
||||||
|
|
||||||
err_exit:
|
err_exit:
|
||||||
|
@ -149,6 +149,7 @@ int lpqr_main(int argc UNUSED_PARAM, char *argv[])
|
|||||||
|
|
||||||
// process files
|
// process files
|
||||||
do {
|
do {
|
||||||
|
unsigned cflen;
|
||||||
int dfd;
|
int dfd;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char *c;
|
char *c;
|
||||||
@ -194,23 +195,30 @@ int lpqr_main(int argc UNUSED_PARAM, char *argv[])
|
|||||||
);
|
);
|
||||||
// delete possible "\nX\n" patterns
|
// delete possible "\nX\n" patterns
|
||||||
c = controlfile;
|
c = controlfile;
|
||||||
|
cflen = (unsigned)strlen(controlfile);
|
||||||
while ((c = strchr(c, '\n')) != NULL) {
|
while ((c = strchr(c, '\n')) != NULL) {
|
||||||
c++;
|
if (c[1] && c[2] == '\n') {
|
||||||
while (c[0] && c[1] == '\n')
|
/* can't use strcpy, results are undefined */
|
||||||
memmove(c, c+2, strlen(c+1)); /* strlen(c+1) == strlen(c+2) + 1 */
|
memmove(c, c+2, cflen - (c-controlfile) - 1);
|
||||||
|
cflen -= 2;
|
||||||
|
} else {
|
||||||
|
c++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// send control file
|
// send control file
|
||||||
if (opts & LPR_V)
|
if (opts & LPR_V)
|
||||||
bb_error_msg("sending control file");
|
bb_error_msg("sending control file");
|
||||||
|
/* "Acknowledgement processing must occur as usual
|
||||||
|
* after the command is sent." */
|
||||||
|
fdprintf(fd, "\x2" "%u c%s\n", cflen, remote_filename);
|
||||||
|
get_response_or_say_and_die(fd, "sending control file");
|
||||||
/* "Once all of the contents have
|
/* "Once all of the contents have
|
||||||
* been delivered, an octet of zero bits is sent as
|
* been delivered, an octet of zero bits is sent as
|
||||||
* an indication that the file being sent is complete.
|
* an indication that the file being sent is complete.
|
||||||
* A second level of acknowledgement processing
|
* A second level of acknowledgement processing
|
||||||
* must occur at this point." */
|
* must occur at this point." */
|
||||||
fdprintf(fd, "\x2" "%u c%s\n" "%s" "%c",
|
full_write(fd, controlfile, cflen + 1); /* writes NUL byte too */
|
||||||
(unsigned)strlen(controlfile),
|
|
||||||
remote_filename, controlfile, '\0');
|
|
||||||
get_response_or_say_and_die(fd, "sending control file");
|
get_response_or_say_and_die(fd, "sending control file");
|
||||||
|
|
||||||
// send data file, with name "dfaXXX"
|
// send data file, with name "dfaXXX"
|
||||||
@ -219,6 +227,7 @@ int lpqr_main(int argc UNUSED_PARAM, char *argv[])
|
|||||||
st.st_size = 0; /* paranoia: fstat may theoretically fail */
|
st.st_size = 0; /* paranoia: fstat may theoretically fail */
|
||||||
fstat(dfd, &st);
|
fstat(dfd, &st);
|
||||||
fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
|
fdprintf(fd, "\x3" "%"OFF_FMT"u d%s\n", st.st_size, remote_filename);
|
||||||
|
get_response_or_say_and_die(fd, "sending data file");
|
||||||
if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
|
if (bb_copyfd_size(dfd, fd, st.st_size) != st.st_size) {
|
||||||
// We're screwed. We sent less bytes than we advertised.
|
// We're screwed. We sent less bytes than we advertised.
|
||||||
bb_error_msg_and_die("local file changed size?!");
|
bb_error_msg_and_die("local file changed size?!");
|
||||||
|
Loading…
Reference in New Issue
Block a user