Commit 400fdaac 400fdaaca90616a996f9a6fd230eb084aebb1bb8 by Sergey Poznyakoff

amd, pop3d: bugfixes.

* mailbox/amd.c (amd_body_stream_readdelim): Fix the logic.
* mailbox/message.c (_message_stream_seek): Fix seeks in
backward direction.
* pop3d/top.c: Fix output of the body.
1 parent c1d1ab07
...@@ -1648,7 +1648,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen, ...@@ -1648,7 +1648,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
1648 mu_body_t body = amdstr->body; 1648 mu_body_t body = amdstr->body;
1649 mu_message_t msg = mu_body_get_owner (body); 1649 mu_message_t msg = mu_body_get_owner (body);
1650 struct _amd_message *mhm = mu_message_get_owner (msg); 1650 struct _amd_message *mhm = mu_message_get_owner (msg);
1651 size_t nread = 0;
1652 int status = 0; 1651 int status = 0;
1653 1652
1654 amd_pool_open (mhm); 1653 amd_pool_open (mhm);
...@@ -1656,7 +1655,7 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen, ...@@ -1656,7 +1655,7 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
1656 if (buffer == NULL || buflen == 0) 1655 if (buffer == NULL || buflen == 0)
1657 { 1656 {
1658 if (pnread) 1657 if (pnread)
1659 *pnread = nread; 1658 *pnread = 0;
1660 return 0; 1659 return 0;
1661 } 1660 }
1662 1661
...@@ -1669,27 +1668,22 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen, ...@@ -1669,27 +1668,22 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
1669 1668
1670 status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off, 1669 status = mu_stream_seek (mhm->stream, mhm->body_start + amdstr->off,
1671 MU_SEEK_SET, NULL); 1670 MU_SEEK_SET, NULL);
1672 if (status) 1671 if (status == 0)
1673 { 1672 {
1674 buflen--; 1673 size_t nread = 0;
1675 while (buflen) 1674 size_t ln;
1676 {
1677 size_t ln, rdsize;
1678 1675
1679 ln = mhm->body_end - (mhm->body_start + amdstr->off); 1676 ln = mhm->body_end - (mhm->body_start + amdstr->off) + 1;
1680 if (ln > 0) 1677 if (ln > 0)
1681 { 1678 {
1682 rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen; 1679 size_t rdsize = ((size_t)ln < buflen) ? (size_t)ln : buflen;
1683 status = mu_stream_readdelim (mhm->stream, buffer, rdsize, 1680 status = mu_stream_readdelim (mhm->stream, buffer, rdsize,
1684 delim, &rdsize); 1681 delim, &nread);
1685 amdstr->off += nread; 1682 amdstr->off += rdsize;
1686 nread += rdsize;
1687 if (status)
1688 break;
1689 buflen -= rdsize;
1690 buffer += rdsize;
1691 }
1692 } 1683 }
1684
1685 if (pnread)
1686 *pnread = nread;
1693 } 1687 }
1694 1688
1695 mu_monitor_unlock (mhm->amd->mailbox->monitor); 1689 mu_monitor_unlock (mhm->amd->mailbox->monitor);
...@@ -1697,8 +1691,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen, ...@@ -1697,8 +1691,6 @@ amd_body_stream_readdelim (mu_stream_t is, char *buffer, size_t buflen,
1697 pthread_cleanup_pop (0); 1691 pthread_cleanup_pop (0);
1698 #endif 1692 #endif
1699 1693
1700 if (pnread)
1701 *pnread = nread;
1702 return status; 1694 return status;
1703 } 1695 }
1704 1696
......
...@@ -182,7 +182,17 @@ _message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos) ...@@ -182,7 +182,17 @@ _message_stream_seek (struct _mu_stream *str, mu_off_t off, mu_off_t *ppos)
182 return rc; 182 return rc;
183 /* fall through */ 183 /* fall through */
184 case _mss_body: 184 case _mss_body:
185 off -= hsize; 185 if (off > hsize)
186 off -= hsize;
187 else
188 {
189 mu_stream_destroy (&sp->transport);
190 sp->state = _mss_init;
191 rc = _check_stream_state (sp);
192 if (rc)
193 return rc;
194 }
195
186 break; 196 break;
187 197
188 default: 198 default:
......
...@@ -71,13 +71,13 @@ pop3d_top (char *arg) ...@@ -71,13 +71,13 @@ pop3d_top (char *arg)
71 { 71 {
72 char *buf = NULL; 72 char *buf = NULL;
73 size_t size = 0, n; 73 size_t size = 0, n;
74 while (lines > 0 && 74 for (; lines > 0 &&
75 mu_stream_getline (stream, &buf, &size, &n) == 0 && 75 mu_stream_getline (stream, &buf, &size, &n) == 0 &&
76 n > 0) 76 n > 0; lines--)
77 { 77 {
78 if (buf[0] == '.') 78 if (buf[0] == '.')
79 pop3d_outf ("."); 79 pop3d_outf (".");
80 pop3d_outf ("%s\n", buf); 80 pop3d_outf ("%s", buf);
81 } 81 }
82 mu_stream_destroy (&stream); 82 mu_stream_destroy (&stream);
83 free (buf); 83 free (buf);
......