- SORT_TRACE("Copying %jd bytes to output file", (uintmax_t) sbuck_size(b));
- struct fastbuf *src = sbuck_read(b);
- struct fastbuf *dest = sbuck_write(join);
- bbcopy(src, dest, ~0U);
- sbuck_drop(b);
+ if (!sbuck_has_file(join))
+ {
+ // The final bucket doesn't have any file associated yet, so replace
+ // it with the new bucket.
+ SORT_XTRACE("Replaced final bucket");
+ b->flags |= SBF_FINAL;
+ sbuck_drop(join);
+ }
+ else
+ {
+ SORT_TRACE("Copying %jd bytes to output file", (uintmax_t) sbuck_size(b));
+ struct fastbuf *src = sbuck_read(b);
+ struct fastbuf *dest = sbuck_write(join);
+ bbcopy(src, dest, ~0U);
+ sbuck_drop(b);
+ }