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>
14 #include <ucw/trans.h>
24 static struct cf_section fbatomic_config = {
26 CF_UNS("Trace", &trace),
31 static void CONSTRUCTOR fbatomic_init_config(void)
33 cf_declare_section("FBAtomic", &fbatomic_config, 1);
38 #define FB_ATOMIC(f) ((struct fb_atomic *)(f))
39 #define TRACE(m...) do { if(trace) msg(L_DEBUG, "FB_ATOMIC: " m); } while(0)
41 struct fb_atomic_file {
50 fbatomic_internal_write(struct fastbuf *f)
52 struct fb_atomic_file *af = FB_ATOMIC(f)->af;
53 int size = f->bptr - f->buffer;
56 ASSERT(af->record_len < 0 || !(size % af->record_len));
57 int res = write(af->fd, f->buffer, size);
59 bthrow(f, "write", "Error writing %s: %m", f->name);
61 bthrow(f, "write", "Unexpected partial write to %s: written only %d bytes of %d", f->name, res, size);
67 fbatomic_spout(struct fastbuf *f)
69 if (f->bptr < f->bufend) /* Explicit flushes should be ignored */
72 struct fb_atomic *F = FB_ATOMIC(f);
75 uns written = f->bptr - f->buffer;
76 uns size = f->bufend - f->buffer + F->slack_size;
78 TRACE("Reallocating buffer for atomic file %s with slack %d", f->name, F->slack_size);
79 f->buffer = xrealloc(f->buffer, size);
80 f->bufend = f->buffer + size;
81 f->bptr = f->buffer + written;
83 F->expected_max_bptr = f->bufend - F->slack_size;
86 fbatomic_internal_write(f);
90 fbatomic_close(struct fastbuf *f)
92 struct fb_atomic_file *af = FB_ATOMIC(f)->af;
93 if (!(f->flags & FB_DEAD))
94 fbatomic_internal_write(f); /* Need to flush explicitly, because the file can be locked */
104 fbatomic_open(const char *name, struct fastbuf *master, uns bufsize, int record_len)
106 struct fb_atomic *F = xmalloc_zero(sizeof(*F));
107 struct fastbuf *f = &F->fb;
108 struct fb_atomic_file *af;
111 af = FB_ATOMIC(master)->af;
113 ASSERT(af->record_len == record_len);
117 int fd = ucw_open(name, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666);
119 trans_throw("ucw.fb.open", NULL, "Cannot create %s: %m", name);
120 af = xmalloc_zero(sizeof(*af) + strlen(name));
123 af->record_len = record_len;
124 af->locked = (record_len < 0);
125 strcpy(af->name, name);
128 if (record_len > 0 && bufsize % record_len)
129 bufsize += record_len - (bufsize % record_len);
130 f->buffer = xmalloc(bufsize);
131 f->bufend = f->buffer + bufsize;
132 F->slack_size = (record_len < 0) ? -record_len : 0;
133 ASSERT(bufsize > F->slack_size);
134 F->expected_max_bptr = f->bufend - F->slack_size;
135 f->bptr = f->bstop = f->buffer;
137 f->spout = fbatomic_spout;
138 f->close = fbatomic_close;
144 int main(int argc UNUSED, char **argv UNUSED)
146 struct fastbuf *f, *g;
148 // Always trace in the test
151 msg(L_INFO, "Testing block writes");
152 f = fbatomic_open("test", NULL, 16, 4);
153 for (u32 i=0; i<17; i++)
157 msg(L_INFO, "Testing interleaved var-size writes");
158 f = fbatomic_open("test2", NULL, 23, -5);
159 g = fbatomic_open("test2", f, 23, -5);
160 for (int i=0; i<100; i++)
162 struct fastbuf *x = (i%2) ? g : f;
163 bprintf(x, "%c<%d>\n", "fg"[i%2], ((259309*i) % 1000000) >> (i % 8));