Commit e63412d7 e63412d72863c077b519e4df0b7627beefe0e2ed by Alain Magloire

file_stream.c mbx_unix.c mbx_unixscan.c include/public/io.h

1 parent 5059666f
...@@ -136,7 +136,7 @@ static int ...@@ -136,7 +136,7 @@ static int
136 _file_truncate (stream_t stream, off_t len) 136 _file_truncate (stream_t stream, off_t len)
137 { 137 {
138 struct _file_stream *fs = stream->owner; 138 struct _file_stream *fs = stream->owner;
139 if (ftruncate (fileno(fs->file), len) == -1) 139 if (ftruncate (fileno(fs->file), len) != 0)
140 return errno; 140 return errno;
141 return 0; 141 return 0;
142 } 142 }
...@@ -146,6 +146,7 @@ _file_size (stream_t stream, off_t *psize) ...@@ -146,6 +146,7 @@ _file_size (stream_t stream, off_t *psize)
146 { 146 {
147 struct _file_stream *fs = stream->owner; 147 struct _file_stream *fs = stream->owner;
148 struct stat stbuf; 148 struct stat stbuf;
149 fflush (fs->file);
149 if (fstat(fileno(fs->file), &stbuf) == -1) 150 if (fstat(fileno(fs->file), &stbuf) == -1)
150 return errno; 151 return errno;
151 if (psize) 152 if (psize)
...@@ -170,6 +171,16 @@ _file_get_fd (stream_t stream, int *pfd) ...@@ -170,6 +171,16 @@ _file_get_fd (stream_t stream, int *pfd)
170 } 171 }
171 172
172 static int 173 static int
174 _file_close (stream_t stream)
175 {
176 struct _file_stream *fs = stream->owner;
177 if (fs->file)
178 if (fclose (fs->file) != 0)
179 return errno;
180 return 0;
181 }
182
183 static int
173 _file_open (stream_t stream, const char *filename, int port, int flags) 184 _file_open (stream_t stream, const char *filename, int port, int flags)
174 { 185 {
175 struct _file_stream *fs = stream->owner; 186 struct _file_stream *fs = stream->owner;
...@@ -288,12 +299,12 @@ file_stream_create (stream_t *stream) ...@@ -288,12 +299,12 @@ file_stream_create (stream_t *stream)
288 ret = stream_create (stream, MU_STREAM_NO_CHECK, fs); 299 ret = stream_create (stream, MU_STREAM_NO_CHECK, fs);
289 if (ret != 0) 300 if (ret != 0)
290 { 301 {
291 fclose (fs->file);
292 free (fs); 302 free (fs);
293 return ret; 303 return ret;
294 } 304 }
295 305
296 stream_set_open (*stream, _file_open, fs); 306 stream_set_open (*stream, _file_open, fs);
307 stream_set_close (*stream, _file_close, fs);
297 stream_set_fd (*stream, _file_get_fd, fs); 308 stream_set_fd (*stream, _file_get_fd, fs);
298 stream_set_read (*stream, _file_read, fs); 309 stream_set_read (*stream, _file_read, fs);
299 stream_set_readline (*stream, _file_readline, fs); 310 stream_set_readline (*stream, _file_readline, fs);
......
...@@ -101,6 +101,7 @@ extern int stream_set_flags __P ((stream_t, int flags, void *owner)); ...@@ -101,6 +101,7 @@ extern int stream_set_flags __P ((stream_t, int flags, void *owner));
101 101
102 /* misc */ 102 /* misc */
103 extern int file_stream_create __P ((stream_t *stream)); 103 extern int file_stream_create __P ((stream_t *stream));
104 extern int mapfile_stream_create __P ((stream_t *stream));
104 extern int encoder_stream_create __P ((stream_t *stream, stream_t iostream, 105 extern int encoder_stream_create __P ((stream_t *stream, stream_t iostream,
105 const char *encoding)); 106 const char *encoding));
106 extern int decoder_stream_create __P ((stream_t *stream, stream_t iostream, 107 extern int decoder_stream_create __P ((stream_t *stream, stream_t iostream,
......
...@@ -346,7 +346,7 @@ static int ...@@ -346,7 +346,7 @@ static int
346 mailbox_unix_open (mailbox_t mbox, int flags) 346 mailbox_unix_open (mailbox_t mbox, int flags)
347 { 347 {
348 mailbox_unix_data_t mud; 348 mailbox_unix_data_t mud;
349 int status; 349 int status = 0;
350 350
351 if (mbox == NULL || 351 if (mbox == NULL ||
352 (mud = (mailbox_unix_data_t)mbox->data) == NULL) 352 (mud = (mailbox_unix_data_t)mbox->data) == NULL)
...@@ -359,15 +359,32 @@ mailbox_unix_open (mailbox_t mbox, int flags) ...@@ -359,15 +359,32 @@ mailbox_unix_open (mailbox_t mbox, int flags)
359 /* get a stream */ 359 /* get a stream */
360 if (mbox->stream == NULL) 360 if (mbox->stream == NULL)
361 { 361 {
362 status = file_stream_create (&(mbox->stream)); 362 /* FIXME: for small mbox we shout try to mmap() */
363
364 status = (flags & MU_STREAM_CREAT) || (flags & MU_STREAM_APPEND);
365 if (status == 0)
366 status = mapfile_stream_create (&(mbox->stream));
363 if (status != 0) 367 if (status != 0)
364 return status; 368 {
369 status = file_stream_create (&(mbox->stream));
370 if (status != 0)
371 return status;
372 }
373 status = stream_open (mbox->stream, mbox->name, 0, flags);
374 if (status != 0)
375 {
376 stream_destroy (&(mbox->stream), mbox);
377 return status;
378 }
365 } 379 }
366 status = stream_open (mbox->stream, mbox->name, 0, flags); 380 else
367 if (status != 0)
368 { 381 {
369 stream_destroy (&(mbox->stream), mbox); 382 status = stream_open (mbox->stream, mbox->name, 0, flags);
370 return status; 383 if (status != 0)
384 {
385 stream_destroy (&(mbox->stream), mbox);
386 return status;
387 }
371 } 388 }
372 389
373 /* Authentication */ 390 /* Authentication */
...@@ -765,10 +782,10 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -765,10 +782,10 @@ mailbox_unix_expunge (mailbox_t mbox)
765 clearerr (tempfile); 782 clearerr (tempfile);
766 783
767 /* flush/truncation */ 784 /* flush/truncation */
785 stream_flush (mbox->stream);
768 status = stream_truncate (mbox->stream, total); 786 status = stream_truncate (mbox->stream, total);
769 if (status != 0) 787 if (status != 0)
770 goto bailout; 788 goto bailout;
771 stream_flush (mbox->stream);
772 789
773 /* Don't remove the tmp mbox in case of errors */ 790 /* Don't remove the tmp mbox in case of errors */
774 remove (tmpmbox); 791 remove (tmpmbox);
......
...@@ -344,7 +344,6 @@ mailbox_unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif) ...@@ -344,7 +344,6 @@ mailbox_unix_scan0 (mailbox_t mbox, size_t msgno, size_t *pcount, int do_notif)
344 total, &n)) == 0 && n != 0) 344 total, &n)) == 0 && n != 0)
345 { 345 {
346 int nl; 346 int nl;
347 STRLEN(buf, n);
348 total += n; 347 total += n;
349 348
350 nl = (*buf == '\n') ? 1 : 0; 349 nl = (*buf == '\n') ? 1 : 0;
......