269
LOG(log_maxdebug, logtype_dsi, "dsi_stream_write: sending %u bytes", length);
269
LOG(log_maxdebug, logtype_dsi, "dsi_stream_write(send: %zd bytes): START", length);
271
271
if (dsi->flags & DSI_DISCONNECTED)
317
318
#ifdef WITH_SENDFILE
318
319
ssize_t dsi_stream_read_file(DSI *dsi, int fromfd, off_t offset, const size_t length)
323
LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sending %u bytes", length);
326
LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file(send %zd bytes): START", length);
325
328
if (dsi->flags & DSI_DISCONNECTED)
331
334
while (written < length) {
332
len = sys_sendfile(dsi->socket, fromfd, &offset, length - written);
335
len = sys_sendfile(dsi->socket, fromfd, &pos, length - written);
335
338
if (errno == EINTR)
337
if (errno == EINVAL || errno == ENOSYS)
340
if (errno == EINVAL || errno == ENOSYS) {
340
344
if (errno == EAGAIN || errno == EWOULDBLOCK) {
345
#if defined(SOLARIS) || defined(FREEBSD)
347
/* we actually have sent sth., adjust counters and keep trying */
341
353
if (dsi_peek(dsi)) {
342
354
/* can't go back to blocking mode, exit, the next read
343
355
will return with an error and afpd will die.
353
365
/* afpd is going to exit */
355
return -1; /* I think we're at EOF here... */
361
373
dsi->write_count += written;
377
LOG(log_maxdebug, logtype_dsi, "dsi_stream_read_file: sent: %zd", written);
426
LOG(log_maxdebug, logtype_dsi, "dsi_stream_send: %u bytes",
427
length ? length : sizeof(block));
443
LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): START", length);
429
445
if (dsi->flags & DSI_DISCONNECTED)
439
455
sizeof(dsi->header.dsi_reserved));
441
457
if (!length) { /* just write the header */
458
LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): DSI header, no data", sizeof(block));
442
459
length = (dsi_stream_write(dsi, block, sizeof(block), 0) == sizeof(block));
443
460
return length; /* really 0 on failure, 1 on success */
482
499
iov[1].iov_len -= len;
503
LOG(log_maxdebug, logtype_dsi, "dsi_stream_send(%u bytes): END", length);
486
505
unblock_sig(dsi);
491
/* ---------------------------------------
492
* read data. function on success. 0 on failure. data length gets
493
* stored in length variable. this should really use size_t's, but
494
* that would require changes elsewhere. */
495
int dsi_stream_receive(DSI *dsi, void *buf, const size_t ilength,
511
* Read DSI command and data
513
* @param dsi (rw) DSI handle
515
* @return DSI function on success, 0 on failure
517
int dsi_stream_receive(DSI *dsi)
498
519
char block[DSI_BLOCKSIZ];
500
LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: %u bytes", ilength);
521
LOG(log_maxdebug, logtype_dsi, "dsi_stream_receive: START");
502
523
if (dsi->flags & DSI_DISCONNECTED)
509
530
dsi->header.dsi_flags = block[0];
510
531
dsi->header.dsi_command = block[1];
511
/* FIXME, not the right place,
512
but we get a server disconnect without reason in the log
515
LOG(log_error, logtype_dsi, "dsi_stream_receive: invalid packet, fatal");
533
if (dsi->header.dsi_command == 0)
519
memcpy(&dsi->header.dsi_requestID, block + 2,
520
sizeof(dsi->header.dsi_requestID));
536
memcpy(&dsi->header.dsi_requestID, block + 2, sizeof(dsi->header.dsi_requestID));
521
537
memcpy(&dsi->header.dsi_code, block + 4, sizeof(dsi->header.dsi_code));
522
538
memcpy(&dsi->header.dsi_len, block + 8, sizeof(dsi->header.dsi_len));
523
memcpy(&dsi->header.dsi_reserved, block + 12,
524
sizeof(dsi->header.dsi_reserved));
539
memcpy(&dsi->header.dsi_reserved, block + 12, sizeof(dsi->header.dsi_reserved));
525
540
dsi->clientID = ntohs(dsi->header.dsi_requestID);
527
542
/* make sure we don't over-write our buffers. */
528
*rlength = min(ntohl(dsi->header.dsi_len), ilength);
529
if (dsi_stream_read(dsi, buf, *rlength) != *rlength)
543
dsi->cmdlen = min(ntohl(dsi->header.dsi_len), DSI_CMDSIZ);
544
if (dsi_stream_read(dsi, dsi->commands, dsi->cmdlen) != dsi->cmdlen)
547
LOG(log_debug, logtype_dsi, "dsi_stream_receive: DSI cmdlen: %zd", dsi->cmdlen);