Commit de5a84ae de5a84ae4a3a5c8f8ffa096dcd0cb40ddec9f217 by Alain Magloire

attribute.c attribute.h attribute0.h header.c header.h

 	header0.h io.c io.h io0.h mailbox.c mailbox0.h mbx_unix.c
 	message.c message.h message0.h rfc822.c

message_t is a pure interface.
1 parent c77752c0
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
22 #include <errno.h> 22 #include <errno.h>
23 23
24 int 24 int
25 attribute_init (attribute_t *pattr) 25 attribute_init (attribute_t *pattr, void *owner)
26 { 26 {
27 attribute_t attr; 27 attribute_t attr;
28 if (pattr == NULL) 28 if (pattr == NULL)
...@@ -30,19 +30,24 @@ attribute_init (attribute_t *pattr) ...@@ -30,19 +30,24 @@ attribute_init (attribute_t *pattr)
30 attr = calloc (1, sizeof(*attr)); 30 attr = calloc (1, sizeof(*attr));
31 if (attr == NULL) 31 if (attr == NULL)
32 return ENOMEM; 32 return ENOMEM;
33 attr->owner = owner;
33 *pattr = attr; 34 *pattr = attr;
34 return 0; 35 return 0;
35 } 36 }
36 37
37 void 38 void
38 attribute_destroy (attribute_t *pattr) 39 attribute_destroy (attribute_t *pattr, void *owner)
39 { 40 {
40 if (pattr && *pattr) 41 if (pattr && *pattr)
41 { 42 {
42 attribute_t attr = *pattr; 43 attribute_t attr = *pattr;
43 /* no owner we really can free it */ 44
44 if (! attr->message) 45 attr->ref_count--;
45 free (*pattr); 46 if ((attr->owner && attr->owner == owner) ||
47 (attr->owner == NULL && attr->ref_count <= 0))
48 {
49 free (attr);
50 }
46 /* loose the link */ 51 /* loose the link */
47 *pattr = NULL; 52 *pattr = NULL;
48 } 53 }
...@@ -249,20 +254,11 @@ attribute_copy (attribute_t dest, attribute_t src) ...@@ -249,20 +254,11 @@ attribute_copy (attribute_t dest, attribute_t src)
249 } 254 }
250 255
251 int 256 int
252 attribute_set_owner (attribute_t attr, message_t *msg) 257 attribute_get_owner (attribute_t attr, void **powner)
253 {
254 if (attr == NULL)
255 return EINVAL;
256 attr->message = msg;
257 return 0;
258 }
259
260 int
261 attribute_get_owner (attribute_t attr, message_t *msg)
262 { 258 {
263 if (attr == NULL) 259 if (attr == NULL)
264 return EINVAL; 260 return EINVAL;
265 if (msg) 261 if (powner)
266 *msg = attr->message; 262 *powner = attr->owner;
267 return 0; 263 return 0;
268 } 264 }
......
...@@ -33,8 +33,8 @@ extern "C" { ...@@ -33,8 +33,8 @@ extern "C" {
33 struct _attribute; 33 struct _attribute;
34 typedef struct _attribute * attribute_t; 34 typedef struct _attribute * attribute_t;
35 35
36 extern int attribute_init __P ((attribute_t *)); 36 extern int attribute_init __P ((attribute_t *, void *owner));
37 extern void attribute_destroy __P ((attribute_t *)); 37 extern void attribute_destroy __P ((attribute_t *, void *owner));
38 38
39 extern int attribute_is_seen __P ((attribute_t)); 39 extern int attribute_is_seen __P ((attribute_t));
40 extern int attribute_is_answered __P ((attribute_t)); 40 extern int attribute_is_answered __P ((attribute_t));
......
...@@ -46,13 +46,13 @@ extern "C" { ...@@ -46,13 +46,13 @@ extern "C" {
46 struct _attribute 46 struct _attribute
47 { 47 {
48 size_t flag; 48 size_t flag;
49 void *message; 49 void *owner;
50 int ref_count;
50 }; 51 };
51 52
52 /* not user visible ?? */ 53 /* not user visible ?? */
53 extern int attribute_copy __P ((attribute_t dst, attribute_t src)); 54 extern int attribute_copy __P ((attribute_t dst, attribute_t src));
54 extern int attribute_set_owner __P ((attribute_t attr, message_t *msg)); 55 extern int attribute_get_owner __P ((attribute_t attr, void **owner));
55 extern int attribute_get_owner __P ((attribute_t attr, message_t *msg));
56 56
57 #ifdef __cplusplus 57 #ifdef __cplusplus
58 } 58 }
......
...@@ -26,18 +26,19 @@ ...@@ -26,18 +26,19 @@
26 #include <errno.h> 26 #include <errno.h>
27 27
28 int 28 int
29 header_init (header_t *ph, const char *blurb, size_t len, int flag) 29 header_init (header_t *ph, const char *blurb, size_t len,
30 int flag, void *owner)
30 { 31 {
31 if (flag != MU_HEADER_RFC822) 32 if (flag != MU_HEADER_RFC822)
32 return ENOTSUP; 33 return ENOTSUP;
33 return rfc822_init (ph, blurb, len); 34 return rfc822_init (ph, blurb, len, owner);
34 } 35 }
35 36
36 void 37 void
37 header_destroy (header_t *ph) 38 header_destroy (header_t *ph, void *owner)
38 { 39 {
39 if (ph && *ph) 40 if (ph && *ph)
40 (*ph)->_destroy (ph); 41 (*ph)->_destroy (ph, owner);
41 } 42 }
42 43
43 /* stub functions */ 44 /* stub functions */
......
...@@ -69,8 +69,8 @@ struct _header; ...@@ -69,8 +69,8 @@ struct _header;
69 typedef struct _header * header_t; 69 typedef struct _header * header_t;
70 70
71 extern int header_init __P ((header_t *, const char *blurb, 71 extern int header_init __P ((header_t *, const char *blurb,
72 size_t ln, int flag)); 72 size_t ln, int flag, void *owner));
73 extern void header_destroy __P ((header_t *)); 73 extern void header_destroy __P ((header_t *, void *owner));
74 74
75 extern int header_set_value __P ((header_t, const char *fn, 75 extern int header_set_value __P ((header_t, const char *fn,
76 const char *fv, size_t n, int replace)); 76 const char *fv, size_t n, int replace));
......
...@@ -48,15 +48,17 @@ struct _header ...@@ -48,15 +48,17 @@ struct _header
48 size_t num; 48 size_t num;
49 /* Data */ 49 /* Data */
50 void *data; 50 void *data;
51 /* owner ? */
52 void *message;
53 /* streams */ 51 /* streams */
54 istream_t is; 52 istream_t is;
55 ostream_t os; 53 ostream_t os;
56 54
55 /* owner ? */
56 void *owner;
57 int ref_count;
58
57 /* Functions */ 59 /* Functions */
58 int (*_init) __P ((header_t *, const char *, size_t)); 60 int (*_init) __P ((header_t *, const char *, size_t, void *owner));
59 void (*_destroy) __P ((header_t *)); 61 void (*_destroy) __P ((header_t *, void *owner));
60 int (*_set_value) __P ((header_t, const char *fn, const char *fv, 62 int (*_set_value) __P ((header_t, const char *fn, const char *fv,
61 size_t n, int replace)); 63 size_t n, int replace));
62 int (*_get_value) __P ((header_t, const char *fn, char *fv, 64 int (*_get_value) __P ((header_t, const char *fn, char *fv,
...@@ -72,8 +74,9 @@ struct _header ...@@ -72,8 +74,9 @@ struct _header
72 }; 74 };
73 75
74 /* rfc822 */ 76 /* rfc822 */
75 extern int rfc822_init __P ((header_t *ph, const char *blurb, size_t len)); 77 extern int rfc822_init __P ((header_t *ph, const char *blurb,
76 extern void rfc822_destroy __P ((header_t *ph)); 78 size_t len, void *owner));
79 extern void rfc822_destroy __P ((header_t *ph, void *owner));
77 80
78 #ifdef _cpluscplus 81 #ifdef _cpluscplus
79 } 82 }
......
...@@ -22,7 +22,9 @@ ...@@ -22,7 +22,9 @@
22 #include <stdio.h> 22 #include <stdio.h>
23 23
24 int 24 int
25 istream_init (istream_t *pis) 25 istream_init (istream_t *pis, int (*_read)
26 __P ((istream_t, char *, size_t, off_t, ssize_t *)),
27 void *owner)
26 { 28 {
27 istream_t is; 29 istream_t is;
28 if (pis == NULL) 30 if (pis == NULL)
...@@ -30,12 +32,16 @@ istream_init (istream_t *pis) ...@@ -30,12 +32,16 @@ istream_init (istream_t *pis)
30 is = calloc (1, sizeof (*is)); 32 is = calloc (1, sizeof (*is));
31 if (is == NULL) 33 if (is == NULL)
32 return ENOMEM; 34 return ENOMEM;
35 is->owner = owner;
36 is->_read = _read;
33 *pis = is; 37 *pis = is;
34 return 0; 38 return 0;
35 } 39 }
36 40
37 int 41 int
38 ostream_init (ostream_t *pos) 42 ostream_init (ostream_t *pos, int (*_write)
43 __P ((ostream_t, const char *, size_t, off_t, ssize_t *)),
44 void *owner)
39 { 45 {
40 ostream_t os; 46 ostream_t os;
41 if (pos == NULL) 47 if (pos == NULL)
...@@ -43,60 +49,54 @@ ostream_init (ostream_t *pos) ...@@ -43,60 +49,54 @@ ostream_init (ostream_t *pos)
43 os = calloc (1, sizeof (*os)); 49 os = calloc (1, sizeof (*os));
44 if (os == NULL) 50 if (os == NULL)
45 return ENOMEM; 51 return ENOMEM;
52 os->owner = owner;
53 os->_write = _write;
46 *pos = os; 54 *pos = os;
47 return 0; 55 return 0;
48 } 56 }
49 57
50 void 58 void
51 istream_destroy (istream_t *pis) 59 istream_destroy (istream_t *pis, void *owner)
52 { 60 {
53 free (pis); 61 if (pis && *pis)
62 {
63 istream_t is = *pis;
64 is->ref_count--;
65 if ((is->owner && is->owner == owner) ||
66 (is->owner == NULL && is->ref_count <= 0))
67 free (pis);
68 *pis = NULL;
69 }
54 } 70 }
55 71
56 void 72 void
57 ostream_destroy (ostream_t *pos) 73 ostream_destroy (ostream_t *pos, void *owner)
58 { 74 {
59 free (pos); 75 if (pos && (*pos))
76 {
77 ostream_t os = *pos;
78 os->ref_count--;
79 if ((os->owner && os->owner == owner) ||
80 (os->owner == NULL && os->ref_count <= 0))
81 free (*pos);
82 *pos = NULL;
83 }
60 } 84 }
61 85
62 int 86 int
63 istream_set_read (istream_t is, ssize_t (*read) 87 istream_read (istream_t is, char *buf, size_t count,
64 __P ((istream_t, char *, size_t, off_t))) 88 off_t offset, ssize_t *pnread)
65 { 89 {
66 if (is == NULL) 90 if (is == NULL || is->_read == NULL)
67 return EINVAL; 91 return EINVAL;
68 is->_read = read; 92 return is->_read (is, buf, count, offset, pnread);
69 return 0;
70 } 93 }
71 94
72 int 95 int
73 ostream_set_write (ostream_t os, ssize_t (*write) 96 ostream_write (ostream_t os, const char *buf, size_t count,
74 __P ((ostream_t, const char *, size_t, off_t))) 97 off_t offset, ssize_t *pnwrite)
75 {
76 if (os == NULL)
77 return EINVAL;
78 os->_write = write;
79 return 0;
80 }
81
82 ssize_t
83 istream_read (istream_t is, char *buf, size_t count, off_t offset)
84 {
85 if (is == NULL || is->_read == NULL)
86 {
87 errno = EINVAL;
88 return -1;
89 }
90 return is->_read (is, buf, count, offset);
91 }
92
93 ssize_t
94 ostream_write (ostream_t os, const char *buf, size_t count, off_t offset)
95 { 98 {
96 if (os == NULL || os->_write == NULL) 99 if (os == NULL || os->_write == NULL)
97 { 100 return EINVAL;
98 errno = EINVAL; 101 return os->_write (os, buf, count, offset, pnwrite);
99 return -1;
100 }
101 return os->_write (os, buf, count, offset);
102 } 102 }
......
...@@ -33,13 +33,14 @@ extern "C" { ...@@ -33,13 +33,14 @@ extern "C" {
33 #endif /*__P */ 33 #endif /*__P */
34 34
35 struct _istream; 35 struct _istream;
36 typedef struct _istream * istream_t; 36 typedef struct _istream *istream_t;
37 struct _ostream; 37 struct _ostream;
38 typedef struct _ostream * ostream_t; 38 typedef struct _ostream *ostream_t;
39 39
40 extern ssize_t istream_read __P ((istream_t, char *, size_t, off_t)); 40 extern int istream_read __P ((istream_t, char *, size_t, off_t, ssize_t *));
41 41
42 extern ssize_t ostream_write __P ((ostream_t, const char *, size_t, off_t)); 42 extern int ostream_write __P ((ostream_t, const char *, size_t,
43 off_t, ssize_t *));
43 44
44 #ifdef __cplusplus 45 #ifdef __cplusplus
45 } 46 }
......
...@@ -36,20 +36,28 @@ struct _istream ...@@ -36,20 +36,28 @@ struct _istream
36 { 36 {
37 /* owner of the stream can not be a specific type */ 37 /* owner of the stream can not be a specific type */
38 void *owner; 38 void *owner;
39 ssize_t (*_read) __P ((istream_t, char *, size_t, off_t)); 39 int ref_count;
40 int (*_read) __P ((istream_t, char *, size_t, off_t, ssize_t *));
40 }; 41 };
41 42
42 struct _ostream 43 struct _ostream
43 { 44 {
44 /* owner of the stream can not be a specific type */ 45 /* owner of the stream can not be a specific type */
45 void *owner; 46 void *owner;
46 ssize_t (*_write) __P ((ostream_t, const char *, size_t, off_t)); 47 int ref_count;
48 int (*_write) __P ((ostream_t, const char *, size_t, off_t, ssize_t *));
47 }; 49 };
48 50
49 extern int istream_init __P ((istream_t *)); 51 extern int istream_init __P ((istream_t *,
50 extern void istream_destroy __P ((istream_t *)); 52 int (*_read) __P ((istream_t, char *,
51 extern int ostream_init __P ((ostream_t *)); 53 size_t, off_t, ssize_t *)),
52 extern void ostream_destroy __P ((ostream_t *)); 54 void *owner));
55 extern void istream_destroy __P ((istream_t *, void *owner));
56 extern int ostream_init __P ((ostream_t *,
57 int (*_write) __P ((ostream_t, const char *,
58 size_t, off_t, ssize_t *)),
59 void *owner));
60 extern void ostream_destroy __P ((ostream_t *, void *owner));
53 61
54 #ifdef __cplusplus 62 #ifdef __cplusplus
55 } 63 }
......
...@@ -112,17 +112,9 @@ mailbox_append_message (mailbox_t mbox, message_t msg) ...@@ -112,17 +112,9 @@ mailbox_append_message (mailbox_t mbox, message_t msg)
112 int 112 int
113 mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) 113 mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
114 { 114 {
115 int status; 115 if (mbox == NULL || mbox->_get_message == NULL)
116 if (mbox == NULL || pmsg == NULL)
117 return EINVAL; 116 return EINVAL;
118 if (mailbox_is_deleted (mbox, msgno)) 117 return mbox->_get_message (mbox, msgno, pmsg);
119 return EINVAL;
120 status = message_init (pmsg);
121 if (status != 0)
122 return status;
123 (*pmsg)->mailbox = mbox;
124 (*pmsg)->num = msgno;
125 return 0;
126 } 118 }
127 119
128 int 120 int
...@@ -194,24 +186,6 @@ mailbox_get_size (mailbox_t mbox, size_t msgno, size_t *h, size_t *b) ...@@ -194,24 +186,6 @@ mailbox_get_size (mailbox_t mbox, size_t msgno, size_t *h, size_t *b)
194 return mbox->_get_size (mbox, msgno, h, b); 186 return mbox->_get_size (mbox, msgno, h, b);
195 } 187 }
196 188
197 ssize_t
198 mailbox_get_header (mailbox_t mbox, size_t msgno, char *h,
199 size_t len, off_t off, int *err)
200 {
201 if (mbox == NULL || mbox->_get_header == NULL)
202 return ENOSYS;
203 return mbox->_get_header (mbox, msgno, h, len, off, err);
204 }
205
206 ssize_t
207 mailbox_get_body (mailbox_t mbox, size_t msgno, char *b,
208 size_t len, off_t off, int *err)
209 {
210 if (mbox == NULL || mbox->_get_body == NULL)
211 return ENOSYS;
212 return mbox->_get_body (mbox, msgno, b, len, off, err);
213 }
214
215 /* locking */ 189 /* locking */
216 int 190 int
217 mailbox_set_locker (mailbox_t mbox, locker_t locker) 191 mailbox_set_locker (mailbox_t mbox, locker_t locker)
...@@ -251,18 +225,3 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth) ...@@ -251,18 +225,3 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth)
251 return 0; 225 return 0;
252 } 226 }
253 227
254 int
255 mailbox_get_attribute (mailbox_t mbox, size_t msgno, attribute_t *pattr)
256 {
257 if (mbox == NULL || mbox->_get_attribute == NULL)
258 return ENOSYS;
259 return mbox->_get_attribute (mbox, msgno, pattr);
260 }
261
262 int
263 mailbox_set_attribute (mailbox_t mbox, size_t msgno, attribute_t attr)
264 {
265 if (mbox == NULL || mbox->_set_attribute == NULL)
266 return ENOSYS;
267 return mbox->_set_attribute (mbox, msgno, attr);
268 }
......
...@@ -38,17 +38,10 @@ extern "C" { ...@@ -38,17 +38,10 @@ extern "C" {
38 struct _mailbox 38 struct _mailbox
39 { 39 {
40 /* Data */ 40 /* Data */
41
42 char *name; 41 char *name;
43 FILE *file;
44 message_t *messages;
45 size_t messages_num;
46 off_t size;
47
48 auth_t auth; 42 auth_t auth;
49 locker_t locker; 43 locker_t locker;
50 url_t url; 44 url_t url;
51
52 int (*_progress) __P ((int, void *arg)); 45 int (*_progress) __P ((int, void *arg));
53 void *progress_arg; 46 void *progress_arg;
54 47
...@@ -80,15 +73,6 @@ struct _mailbox ...@@ -80,15 +73,6 @@ struct _mailbox
80 int (*_num_deleted) __P ((mailbox_t, size_t *)); 73 int (*_num_deleted) __P ((mailbox_t, size_t *));
81 int (*_get_size) __P ((mailbox_t, size_t msgno, 74 int (*_get_size) __P ((mailbox_t, size_t msgno,
82 size_t *h, size_t *b)); 75 size_t *h, size_t *b));
83
84 ssize_t (*_get_header) __P ((mailbox_t, size_t msgno, char *h,
85 size_t len, off_t off, int *err));
86 ssize_t (*_get_body) __P ((mailbox_t, size_t msgno, char *b,
87 size_t len, off_t off, int *err));
88 int (*_get_attribute) __P ((mailbox_t mbox, size_t msgno,
89 attribute_t *attr));
90 int (*_set_attribute) __P ((mailbox_t mbox, size_t msgno,
91 attribute_t attr));
92 }; 76 };
93 77
94 /* private */ 78 /* private */
......
...@@ -40,23 +40,23 @@ extern "C" { ...@@ -40,23 +40,23 @@ extern "C" {
40 struct _message; 40 struct _message;
41 typedef struct _message *message_t; 41 typedef struct _message *message_t;
42 42
43 extern int message_init __P ((message_t *)); 43 extern int message_init __P ((message_t *, void *owner));
44 extern void message_destroy __P ((message_t *)); 44 extern void message_destroy __P ((message_t *, void *owner));
45 45
46 extern int message_get_header __P ((message_t, header_t *)); 46 extern int message_get_header __P ((message_t, header_t *));
47 extern int message_set_header __P ((message_t, header_t)); 47 extern int message_set_header __P ((message_t, header_t, void *owner));
48 48
49 extern int message_get_istream __P ((message_t, istream_t *)); 49 extern int message_get_istream __P ((message_t, istream_t *));
50 extern int message_set_istream __P ((message_t, istream_t *)); 50 extern int message_set_istream __P ((message_t, istream_t, void *owner));
51 extern int message_get_ostream __P ((message_t, ostream_t *)); 51 extern int message_get_ostream __P ((message_t, ostream_t *));
52 extern int message_set_ostream __P ((message_t, ostream_t *)); 52 extern int message_set_ostream __P ((message_t, ostream_t, void *owner));
53 53
54 extern int message_is_multipart __P ((message_t)); 54 extern int message_is_multipart __P ((message_t));
55 55
56 extern int message_get_size __P ((message_t, size_t *)); 56 extern int message_get_size __P ((message_t, size_t *));
57 57
58 extern int message_get_attribute __P ((message_t, attribute_t *)); 58 extern int message_get_attribute __P ((message_t, attribute_t *));
59 extern int message_set_attribute __P ((message_t, attribute_t)); 59 extern int message_set_attribute __P ((message_t, attribute_t, void *owner));
60 60
61 extern int message_clone __P ((message_t, message_t *)); 61 extern int message_clone __P ((message_t, message_t *));
62 62
......
...@@ -42,27 +42,27 @@ typedef struct _body * body_t; ...@@ -42,27 +42,27 @@ typedef struct _body * body_t;
42 /* forward declaration */ 42 /* forward declaration */
43 struct _message 43 struct _message
44 { 44 {
45 /* whos is the owner, only mailbox can own messages */
46 mailbox_t mailbox;
47 header_t header; 45 header_t header;
48 istream_t is; 46 istream_t is;
49 ostream_t os; 47 ostream_t os;
50 body_t body; 48 body_t body;
51 size_t num;
52 attribute_t attribute; 49 attribute_t attribute;
53 char *header_buffer; 50 size_t num;
54 off_t header_offset; 51
52 /* who is the owner */
53 void *owner;
54 int ref_count;
55 55
56 int (*_get_header) __P ((message_t msg, header_t *hdr)); 56 int (*_get_header) __P ((message_t msg, header_t *hdr));
57 int (*_set_header) __P ((message_t msg, header_t hdr)); 57 int (*_set_header) __P ((message_t msg, header_t hdr, void *owner));
58 58
59 int (*_get_attribute) __P ((message_t msg, attribute_t *attr)); 59 int (*_get_attribute) __P ((message_t msg, attribute_t *attr));
60 int (*_set_attribute) __P ((message_t msg, attribute_t attr)); 60 int (*_set_attribute) __P ((message_t msg, attribute_t attr, void *owner));
61 61
62 int (*_get_istream) __P ((message_t msg, istream_t *)); 62 int (*_get_istream) __P ((message_t msg, istream_t *));
63 int (*_set_istream) __P ((message_t msg, istream_t)); 63 int (*_set_istream) __P ((message_t msg, istream_t, void *owner));
64 int (*_get_ostream) __P ((message_t msg, ostream_t *)); 64 int (*_get_ostream) __P ((message_t msg, ostream_t *));
65 int (*_set_ostream) __P ((message_t msg, ostream_t)); 65 int (*_set_ostream) __P ((message_t msg, ostream_t, void *owner));
66 66
67 int (*_size) __P ((message_t msg, size_t *size)); 67 int (*_size) __P ((message_t msg, size_t *size));
68 68
......
...@@ -37,7 +37,8 @@ static int rfc822_entry_value (header_t h, size_t num, char *buf, ...@@ -37,7 +37,8 @@ static int rfc822_entry_value (header_t h, size_t num, char *buf,
37 size_t buflen, size_t *total); 37 size_t buflen, size_t *total);
38 static int rfc822_get_istream (header_t h, istream_t *pis); 38 static int rfc822_get_istream (header_t h, istream_t *pis);
39 static int rfc822_get_ostream (header_t h, ostream_t *pos); 39 static int rfc822_get_ostream (header_t h, ostream_t *pos);
40 static ssize_t rfc822_read (istream_t is, char *buf, size_t buflen, off_t off); 40 static int rfc822_read (istream_t is, char *buf, size_t buflen,
41 off_t off, ssize_t *pnread);
41 42
42 struct _rfc822 43 struct _rfc822
43 { 44 {
...@@ -51,13 +52,14 @@ typedef struct _rfc822 * rfc822_t; ...@@ -51,13 +52,14 @@ typedef struct _rfc822 * rfc822_t;
51 52
52 53
53 int 54 int
54 rfc822_init (header_t *ph, const char *blurb, size_t len) 55 rfc822_init (header_t *ph, const char *blurb, size_t len, void *owner)
55 { 56 {
56 header_t h; 57 header_t h;
57 int status; 58 int status;
58 h = calloc (1, sizeof (*h)); 59 h = calloc (1, sizeof (*h));
59 if (h == NULL) 60 if (h == NULL)
60 return ENOMEM; 61 return ENOMEM;
62 h->owner = owner;
61 h->_init = rfc822_init; 63 h->_init = rfc822_init;
62 h->_destroy = rfc822_destroy; 64 h->_destroy = rfc822_destroy;
63 h->_parse = rfc822_parse; 65 h->_parse = rfc822_parse;
...@@ -77,37 +79,32 @@ rfc822_init (header_t *ph, const char *blurb, size_t len) ...@@ -77,37 +79,32 @@ rfc822_init (header_t *ph, const char *blurb, size_t len)
77 } 79 }
78 80
79 void 81 void
80 rfc822_destroy (header_t *ph) 82 rfc822_destroy (header_t *ph, void *owner)
81 { 83 {
82 if (ph && *ph) 84 if (ph && *ph)
83 { 85 {
84 header_t h = *ph; 86 header_t h = *ph;
85 /* own by a message */ 87
86 if (h->message) 88 /* if destroy is call always decremente */
87 { 89 h->ref_count--;
88 *ph = NULL; 90
89 return; 91 /* can we destroy ? */
90 } 92 if ((h->owner && h->owner == owner) ||
91 /* is the istream own by us */ 93 (h->owner == NULL && h->ref_count <= 0))
92 if (h->is && h->is->owner == h)
93 {
94 h->is->owner = NULL;
95 istream_destroy (&(h->is));
96 }
97 /* is the ostream own by us */
98 if (h->os && h->os->owner == h)
99 {
100 h->os->owner = NULL;
101 ostream_destroy (&(h->os));
102 }
103 if (h->data)
104 { 94 {
105 rfc822_t rfc = (rfc822_t)h->data; 95 /* io */
106 free (rfc->hdr); 96 istream_destroy (&(h->is), owner);
107 free (rfc->blurb); 97 ostream_destroy (&(h->os), owner);
108 free (rfc); 98
99 if (h->data)
100 {
101 rfc822_t rfc = (rfc822_t)h->data;
102 free (rfc->hdr);
103 free (rfc->blurb);
104 free (rfc);
105 }
106 free (h);
109 } 107 }
110 free (h);
111 *ph = NULL; 108 *ph = NULL;
112 } 109 }
113 } 110 }
...@@ -342,8 +339,9 @@ rfc822_entry_value (header_t h, size_t num, char *buf, ...@@ -342,8 +339,9 @@ rfc822_entry_value (header_t h, size_t num, char *buf,
342 return 0; 339 return 0;
343 } 340 }
344 341
345 static ssize_t 342 static int
346 rfc822_read (istream_t is, char *buf, size_t buflen, off_t off) 343 rfc822_read (istream_t is, char *buf, size_t buflen,
344 off_t off, ssize_t *pnread)
347 { 345 {
348 header_t h; 346 header_t h;
349 rfc822_t rfc = NULL; 347 rfc822_t rfc = NULL;
...@@ -351,10 +349,7 @@ rfc822_read (istream_t is, char *buf, size_t buflen, off_t off) ...@@ -351,10 +349,7 @@ rfc822_read (istream_t is, char *buf, size_t buflen, off_t off)
351 349
352 if (is == NULL || (h = (header_t)is->owner) == NULL || 350 if (is == NULL || (h = (header_t)is->owner) == NULL ||
353 (rfc = (rfc822_t)h->data) == NULL) 351 (rfc = (rfc822_t)h->data) == NULL)
354 { 352 return EINVAL;
355 errno = EINVAL;
356 return -1;
357 }
358 353
359 len = rfc->blurb_len - off; 354 len = rfc->blurb_len - off;
360 if ((rfc->blurb_len - off) > 0) 355 if ((rfc->blurb_len - off) > 0)
...@@ -368,7 +363,9 @@ rfc822_read (istream_t is, char *buf, size_t buflen, off_t off) ...@@ -368,7 +363,9 @@ rfc822_read (istream_t is, char *buf, size_t buflen, off_t off)
368 else 363 else
369 len = 0; 364 len = 0;
370 365
371 return len; 366 if (pnread)
367 *pnread = len;
368 return 0;
372 } 369 }
373 370
374 int 371 int
...@@ -381,7 +378,7 @@ rfc822_get_istream (header_t h, istream_t *pis) ...@@ -381,7 +378,7 @@ rfc822_get_istream (header_t h, istream_t *pis)
381 if (h->is) 378 if (h->is)
382 *pis = h->is; 379 *pis = h->is;
383 380
384 err = istream_init (&(h->is)); 381 err = istream_init (&(h->is), rfc822_read, h->owner);
385 if (err != 0) 382 if (err != 0)
386 return err; 383 return err;
387 /* tell the world this is ours */ 384 /* tell the world this is ours */
......