2 * UCW Library -- Atomic Buffered Write to Files
4 * (c) 2006 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "ucw/fastbuf.h"
23 static struct cf_section fbatomic_config = {
25 CF_UNS("Trace", &trace)
29 static void CONSTRUCTOR fbatomic_init_config(void)
31 cf_declare_section("FBAtomic", &fbatomic_config, 1);
36 #define TRACE(m...) do { if(trace) msg(L_DEBUG, "FB_ATOMIC: " m); } while(0)
38 struct fb_atomic_file {
47 fbatomic_internal_write(struct fastbuf *f)
49 struct fb_atomic_file *af = FB_ATOMIC(f)->af;
50 int size = f->bptr - f->buffer;
53 ASSERT(af->record_len < 0 || !(size % af->record_len));
54 int res = write(af->fd, f->buffer, size);
56 die("Error writing %s: %m", f->name);
58 die("Unexpected partial write to %s: written only %d bytes of %d", f->name, res, size);
64 fbatomic_spout(struct fastbuf *f)
66 if (f->bptr < f->bufend) /* Explicit flushes should be ignored */
69 struct fb_atomic *F = FB_ATOMIC(f);
72 uns written = f->bptr - f->buffer;
73 uns size = f->bufend - f->buffer + F->slack_size;
75 TRACE("Reallocating buffer for atomic file %s with slack %d", f->name, F->slack_size);
76 f->buffer = xrealloc(f->buffer, size);
77 f->bufend = f->buffer + size;
78 f->bptr = f->buffer + written;
79 F->expected_max_bptr = f->bufend - F->slack_size;
82 fbatomic_internal_write(f);
86 fbatomic_close(struct fastbuf *f)
88 struct fb_atomic_file *af = FB_ATOMIC(f)->af;
89 fbatomic_internal_write(f); /* Need to flush explicitly, because the file can be locked */
99 fbatomic_open(const char *name, struct fastbuf *master, uns bufsize, int record_len)
101 struct fb_atomic *F = xmalloc_zero(sizeof(*F));
102 struct fastbuf *f = &F->fb;
103 struct fb_atomic_file *af;
106 af = FB_ATOMIC(master)->af;
108 ASSERT(af->record_len == record_len);
112 af = xmalloc_zero(sizeof(*af) + strlen(name));
113 if ((af->fd = ucw_open(name, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666)) < 0)
114 die("Cannot create %s: %m", name);
116 af->record_len = record_len;
117 af->locked = (record_len < 0);
118 strcpy(af->name, name);
121 if (record_len > 0 && bufsize % record_len)
122 bufsize += record_len - (bufsize % record_len);
123 f->buffer = xmalloc(bufsize);
124 f->bufend = f->buffer + bufsize;
125 F->slack_size = (record_len < 0) ? -record_len : 0;
126 ASSERT(bufsize > F->slack_size);
127 F->expected_max_bptr = f->bufend - F->slack_size;
128 f->bptr = f->bstop = f->buffer;
130 f->spout = fbatomic_spout;
131 f->close = fbatomic_close;
137 int main(int argc UNUSED, char **argv UNUSED)
139 struct fastbuf *f, *g;
141 // Always trace in the test
144 msg(L_INFO, "Testing block writes");
145 f = fbatomic_open("test", NULL, 16, 4);
146 for (u32 i=0; i<17; i++)
150 msg(L_INFO, "Testing interleaved var-size writes");
151 f = fbatomic_open("test2", NULL, 23, -5);
152 g = fbatomic_open("test2", f, 23, -5);
153 for (int i=0; i<100; i++)
155 struct fastbuf *x = (i%2) ? g : f;
156 bprintf(x, "%c<%d>\n", "fg"[i%2], ((259309*i) % 1000000) >> (i % 8));