* of the GNU Lesser General Public License.
*/
-#include "ucw/lib.h"
-#include "ucw/fastbuf.h"
-#include "ucw/lfs.h"
-#include "ucw/conf.h"
+#include <ucw/lib.h>
+#include <ucw/fastbuf.h>
+#include <ucw/io.h>
+#include <ucw/conf.h>
+#include <ucw/trans.h>
#include <string.h>
#include <fcntl.h>
#endif
+#define FB_ATOMIC(f) ((struct fb_atomic *)(f))
#define TRACE(m...) do { if(trace) msg(L_DEBUG, "FB_ATOMIC: " m); } while(0)
struct fb_atomic_file {
ASSERT(af->record_len < 0 || !(size % af->record_len));
int res = write(af->fd, f->buffer, size);
if (res < 0)
- die("Error writing %s: %m", f->name);
+ bthrow(f, "write", "Error writing %s: %m", f->name);
if (res != size)
- die("Unexpected partial write to %s: written only %d bytes of %d", f->name, res, size);
+ bthrow(f, "write", "Unexpected partial write to %s: written only %d bytes of %d", f->name, res, size);
f->bptr = f->buffer;
}
}
f->buffer = xrealloc(f->buffer, size);
f->bufend = f->buffer + size;
f->bptr = f->buffer + written;
+ f->bstop = f->buffer;
F->expected_max_bptr = f->bufend - F->slack_size;
}
else
fbatomic_close(struct fastbuf *f)
{
struct fb_atomic_file *af = FB_ATOMIC(f)->af;
- fbatomic_internal_write(f); /* Need to flush explicitly, because the file can be locked */
+ if (!(f->flags & FB_DEAD))
+ fbatomic_internal_write(f); /* Need to flush explicitly, because the file can be locked */
if (!--af->use_count)
{
close(af->fd);
}
else
{
+ int fd = ucw_open(name, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666);
+ if (fd < 0)
+ trans_throw("ucw.fb.open", NULL, "Cannot create %s: %m", name);
af = xmalloc_zero(sizeof(*af) + strlen(name));
- if ((af->fd = ucw_open(name, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666)) < 0)
- die("Cannot create %s: %m", name);
+ af->fd = fd;
af->use_count = 1;
af->record_len = record_len;
af->locked = (record_len < 0);