2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1999 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
26 /* $Id: chunker.c,v 1.36 2006/08/24 11:23:32 martinea Exp $
28 * requests remote amandad processes to dump filesystems
40 #include "fileheader.h"
41 #include "amfeatures.h"
42 #include "server_util.h"
45 #include "timestamp.h"
46 #include "sockaddr-util.h"
56 #define CONNECT_TIMEOUT 5*60
58 #define STARTUP_TIMEOUT 60
61 int fd; /* file to flush to */
62 char *filename; /* name of what fd points to */
63 int filename_seq; /* for chunking */
64 off_t split_size; /* when to chunk */
65 off_t chunk_size; /* size of each chunk */
66 off_t use; /* size to use on this disk */
67 char buf[DISK_BLOCK_BYTES];
68 char *datain; /* data buffer markers */
73 static char *handle = NULL;
75 static char *errstr = NULL;
76 static int abort_pending;
77 static off_t dumpsize;
78 static unsigned long headersize;
79 static off_t dumpbytes;
80 static off_t filesize;
82 static char *hostname = NULL;
83 static char *diskname = NULL;
84 static char *qdiskname = NULL;
85 static char *options = NULL;
86 static char *progname = NULL;
88 static char *dumpdate = NULL;
89 static struct cmdargs *command_in_transit = NULL;
90 static char *chunker_timestamp = NULL;
92 static dumpfile_t file;
95 int main(int, char **);
96 static ssize_t write_tapeheader(int, dumpfile_t *);
97 static void databuf_init(struct databuf *, int, char *, off_t, off_t);
98 static int databuf_flush(struct databuf *);
100 static int startup_chunker(char *, off_t, off_t, struct databuf *, int *, int *);
101 static int do_chunk(int, struct databuf *, int, int);
103 /* we use a function pointer for full_write, so that we can "shim" in
104 * full_write_with_fake_enospc for testing */
105 static size_t (*db_full_write)(int fd, const void *buf, size_t count);
106 static size_t full_write_with_fake_enospc(int fd, const void *buf, size_t count);
107 static off_t fake_enospc_at_byte = -1;
114 static struct databuf db;
115 struct cmdargs *cmdargs;
118 char *filename = NULL;
119 off_t chunksize, use;
121 am_feature_t *their_features = NULL;
123 config_overrides_t *cfg_ovr = NULL;
124 char *cfg_opt = NULL;
130 * Configure program for internationalization:
131 * 1) Only set the message locale for now.
132 * 2) Set textdomain for all amanda related programs to "amanda"
133 * We don't want to be forced to support dozens of message catalogs.
135 setlocale(LC_MESSAGES, "C");
136 textdomain("amanda");
140 set_pname("chunker");
142 dbopen(DBG_SUBDIR_SERVER);
144 /* Don't die when child closes pipe */
145 signal(SIGPIPE, SIG_IGN);
147 add_amanda_log_handler(amanda_log_stderr);
148 add_amanda_log_handler(amanda_log_trace_log);
150 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
155 set_config_overrides(cfg_ovr);
156 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
158 if (config_errors(NULL) >= CFGERR_WARNINGS) {
159 config_print_errors();
160 if (config_errors(NULL) >= CFGERR_ERRORS) {
161 g_critical(_("errors processing config file"));
165 safe_cd(); /* do this *after* config_init() */
167 check_running_as(RUNNING_AS_DUMPUSER);
169 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
171 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
173 _("%s: pid %ld executable %s version %s\n"),
174 get_pname(), (long) getpid(),
178 /* now, make sure we are a valid user */
180 signal(SIGPIPE, SIG_IGN);
181 signal(SIGCHLD, SIG_IGN);
184 if(cmdargs->cmd == START) {
185 if(cmdargs->argc <= 1)
186 error(_("error [dumper START: not enough args: timestamp]"));
187 chunker_timestamp = newstralloc(chunker_timestamp, cmdargs->argv[1]);
190 log_add(L_INFO, "%s pid %ld", get_pname(), (long)getpid());
191 error(_("Didn't get START command"));
193 free_cmdargs(cmdargs);
195 /* set up a fake ENOSPC for testing purposes. Note that this counts
196 * headers as well as data written to disk. */
197 if (getenv("CHUNKER_FAKE_ENOSPC_AT")) {
198 char *env = getenv("CHUNKER_FAKE_ENOSPC_AT");
199 fake_enospc_at_byte = (off_t)atoi(env); /* these values are never > MAXINT */
200 db_full_write = full_write_with_fake_enospc;
201 g_debug("will trigger fake ENOSPC at byte %d", (int)fake_enospc_at_byte);
203 db_full_write = full_write;
209 switch(cmdargs->cmd) {
230 if(a >= cmdargs->argc) {
231 error(_("error [chunker PORT-WRITE: not enough args: handle]"));
234 handle = newstralloc(handle, cmdargs->argv[a++]);
236 if(a >= cmdargs->argc) {
237 error(_("error [chunker PORT-WRITE: not enough args: filename]"));
240 filename = newstralloc(filename, cmdargs->argv[a++]);
242 if(a >= cmdargs->argc) {
243 error(_("error [chunker PORT-WRITE: not enough args: hostname]"));
246 hostname = newstralloc(hostname, cmdargs->argv[a++]);
248 if(a >= cmdargs->argc) {
249 error(_("error [chunker PORT-WRITE: not enough args: features]"));
252 am_release_feature_set(their_features);
253 their_features = am_string_to_feature(cmdargs->argv[a++]);
254 if (!their_features) {
255 error(_("error [chunker PORT-WRITE: invalid feature string]"));
259 if(a >= cmdargs->argc) {
260 error(_("error [chunker PORT-WRITE: not enough args: diskname]"));
263 diskname = newstralloc(diskname, cmdargs->argv[a++]);
266 qdiskname = quote_string(diskname); /* qdiskname is a global */
268 if(a >= cmdargs->argc) {
269 error(_("error [chunker PORT-WRITE: not enough args: level]"));
272 level = atoi(cmdargs->argv[a++]);
274 if(a >= cmdargs->argc) {
275 error(_("error [chunker PORT-WRITE: not enough args: dumpdate]"));
278 dumpdate = newstralloc(dumpdate, cmdargs->argv[a++]);
280 if(a >= cmdargs->argc) {
281 error(_("error [chunker PORT-WRITE: not enough args: chunksize]"));
284 chunksize = OFF_T_ATOI(cmdargs->argv[a++]);
285 chunksize = am_floor(chunksize, (off_t)DISK_BLOCK_KB);
287 if(a >= cmdargs->argc) {
288 error(_("error [chunker PORT-WRITE: not enough args: progname]"));
291 progname = newstralloc(progname, cmdargs->argv[a++]);
293 if(a >= cmdargs->argc) {
294 error(_("error [chunker PORT-WRITE: not enough args: use]"));
297 use = am_floor(OFF_T_ATOI(cmdargs->argv[a++]), DISK_BLOCK_KB);
299 if(a >= cmdargs->argc) {
300 error(_("error [chunker PORT-WRITE: not enough args: options]"));
303 options = newstralloc(options, cmdargs->argv[a++]);
305 if(a != cmdargs->argc) {
306 error(_("error [chunker PORT-WRITE: too many args: %d != %d]"),
311 if ((header_fd = startup_chunker(filename, use, chunksize, &db,
312 &header_socket, &data_socket)) < 0) {
313 q = quote_string(vstrallocf(_("[chunker startup failed: %s]"), errstr));
314 putresult(TRYAGAIN, "%s %s\n", handle, q);
315 error("startup_chunker failed: %s", errstr);
317 command_in_transit = NULL;
318 if (header_fd >= 0 && do_chunk(header_fd, &db, header_socket, data_socket)) {
319 char kb_str[NUM_STR_SIZE];
320 char kps_str[NUM_STR_SIZE];
323 runtime = stopclock();
324 rt = g_timeval_to_double(runtime);
325 g_snprintf(kb_str, SIZEOF(kb_str), "%lld",
326 (long long)(dumpsize - (off_t)headersize));
327 g_snprintf(kps_str, SIZEOF(kps_str), "%3.1lf",
328 isnormal(rt) ? (double)dumpsize / rt : 0.0);
329 errstr = newvstrallocf(errstr, "sec %s kb %s kps %s",
330 walltime_str(runtime), kb_str, kps_str);
331 m = vstrallocf("[%s]", errstr);
334 free_cmdargs(cmdargs);
335 if(command_in_transit != NULL) {
336 cmdargs = command_in_transit;
337 command_in_transit = NULL;
341 switch(cmdargs->cmd) {
343 putresult(DONE, "%s %lld %s\n", handle,
344 (long long)(dumpsize - (off_t)headersize), q);
345 log_add(L_SUCCESS, "%s %s %s %d [%s]",
346 hostname, qdiskname, chunker_timestamp, level, errstr);
352 if(dumpsize > (off_t)DISK_BLOCK_KB) {
353 putresult(PARTIAL, "%s %lld %s\n", handle,
354 (long long)(dumpsize - (off_t)headersize),
356 log_add(L_PARTIAL, "%s %s %s %d [%s]",
357 hostname, qdiskname, chunker_timestamp, level, errstr);
360 errstr = newvstrallocf(errstr,
361 _("dumper returned %s"), cmdstr[cmdargs->cmd]);
363 m = vstrallocf("[%s]",errstr);
366 putresult(FAILED, "%s %s\n", handle, q);
367 log_add(L_FAIL, "%s %s %s %d [%s]",
368 hostname, qdiskname, chunker_timestamp, level, errstr);
373 } else if (header_fd != -2) {
375 m = vstrallocf("[%s]", errstr);
380 putresult(FAILED, "%s %s\n", handle, q);
382 log_add(L_FAIL, "%s %s %s %d [%s]",
383 hostname, qdiskname, chunker_timestamp, level, errstr);
391 if(cmdargs->argc >= 1) {
392 q = quote_string(cmdargs->argv[0]);
394 q = stralloc(_("(no input?)"));
396 putresult(BAD_COMMAND, "%s\n", q);
401 /* } while(cmdargs->cmd != QUIT); */
403 log_add(L_INFO, "pid-done %ld", (long)getpid());
406 amfree(chunker_timestamp);
414 free_cmdargs(cmdargs);
415 if (command_in_transit)
416 free_cmdargs(command_in_transit);
417 am_release_feature_set(their_features);
418 their_features = NULL;
422 return (0); /* exit */
426 * Returns a file descriptor to the incoming port
427 * on success, or -1 on error.
438 int header_fd, outfd;
439 char *tmp_filename, *pc;
440 in_port_t header_port, data_port;
441 int header_socket, data_socket;
443 struct addrinfo *res;
444 struct addrinfo *res_addr;
445 sockaddr_union *addr = NULL;
446 sockaddr_union data_addr;
450 if ((result = resolve_hostname("localhost", 0, &res, NULL) != 0)) {
451 errstr = newvstrallocf(errstr, _("could not resolve localhost: %s"),
452 gai_strerror(result));
455 for (res_addr = res; res_addr != NULL; res_addr = res_addr->ai_next) {
456 g_debug("ra: %s\n", str_sockaddr((sockaddr_union*)res_addr->ai_addr));
457 if (res_addr->ai_family == AF_INET) {
458 addr = (sockaddr_union *)res_addr->ai_addr;
463 addr = (sockaddr_union *)res->ai_addr;
464 g_debug("addr: %s\n", str_sockaddr(addr));
467 header_socket = stream_server(SU_GET_FAMILY(addr), &header_port, 0,
469 data_socket = stream_server(SU_GET_FAMILY(addr), &data_port, 0,
471 copy_sockaddr(&data_addr, addr);
473 SU_SET_PORT(&data_addr, data_port);
475 if (res) freeaddrinfo(res);
477 if (header_socket < 0) {
478 errstr = vstrallocf(_("error creating header stream server: %s"), strerror(errno));
483 if (data_socket < 0) {
484 errstr = vstrallocf(_("error creating data stream server: %s"), strerror(errno));
485 aclose(header_socket);
489 putresult(PORT, "%d %s\n", header_port, str_sockaddr(&data_addr));
491 header_fd = stream_accept(header_socket, CONNECT_TIMEOUT, 0,
493 if (header_fd == -1) {
494 errstr = vstrallocf(_("error accepting header stream: %s"),
496 aclose(header_socket);
501 tmp_filename = vstralloc(filename, ".tmp", NULL);
502 pc = strrchr(tmp_filename, '/');
503 g_assert(pc != NULL);
505 mkholdingdir(tmp_filename);
507 if ((outfd = open(tmp_filename, O_RDWR|O_CREAT|O_TRUNC, 0600)) < 0) {
508 int save_errno = errno;
509 char *m = vstrallocf(_("holding file \"%s\": %s"),
513 errstr = quote_string(m);
515 amfree(tmp_filename);
518 if(save_errno == ENOSPC) {
519 putresult(NO_ROOM, "%s %lld\n",
520 handle, (long long)use);
526 amfree(tmp_filename);
527 databuf_init(db, outfd, filename, use, chunksize);
529 *headersocket = header_socket;
530 *datasocket = data_socket;
543 char header_buf[DISK_BLOCK_BYTES];
547 dumpsize = dumpbytes = filesize = (off_t)0;
549 memset(header_buf, 0, sizeof(header_buf));
552 * The first thing we should receive is the file header, which we
553 * need to save into "file", as well as write out. Later, the
554 * chunk code will rewrite it.
556 nread = full_read(header_fd, header_buf, SIZEOF(header_buf));
558 aclose(header_socket);
559 if (nread != sizeof(header_buf)) {
561 errstr = vstrallocf(_("cannot read header: %s"), strerror(errno));
563 errstr = vstrallocf(_("cannot read header: got %zd bytes instead of %zd"),
564 nread, sizeof(header_buf));
569 parse_file_header(header_buf, &file, (size_t)nread);
570 if(write_tapeheader(db->fd, &file)) {
571 int save_errno = errno;
572 char *m = vstrallocf(_("write_tapeheader file %s: %s"),
573 db->filename, strerror(errno));
574 errstr = quote_string(m);
576 if(save_errno == ENOSPC) {
577 putresult(NO_ROOM, "%s %lld\n", handle,
578 (long long)(db->use+db->split_size-dumpsize));
583 dumpsize += (off_t)DISK_BLOCK_KB;
584 filesize = (off_t)DISK_BLOCK_KB;
585 headersize += DISK_BLOCK_KB;
587 /* open the data socket */
588 data_fd = stream_accept(data_socket, CONNECT_TIMEOUT, 0, STREAM_BUFSIZE);
591 errstr = vstrallocf(_("error accepting data stream: %s"),
598 * We've written the file header. Now, just write data until the
601 while ((nread = full_read(data_fd, db->buf,
602 (size_t)(db->datalimit - db->datain))) > 0) {
604 while(db->dataout < db->datain) {
605 if(!databuf_flush(db)) {
612 while(db->dataout < db->datain) {
613 if(!databuf_flush(db)) {
619 if(dumpbytes > (off_t)0) {
620 dumpsize += (off_t)1; /* count partial final KByte */
621 filesize += (off_t)1;
629 * Initialize a databuf. Takes a writeable file descriptor.
640 db->filename = stralloc(filename);
641 db->filename_seq = (off_t)0;
642 db->chunk_size = chunk_size;
643 db->split_size = (db->chunk_size > use) ? use : db->chunk_size;
644 db->use = (use > db->split_size) ? use - db->split_size : (off_t)0;
645 db->datain = db->dataout = db->buf;
646 db->datalimit = db->buf + SIZEOF(db->buf);
651 * Write out the buffer to the backing file
657 struct cmdargs *cmdargs = NULL;
659 size_t size_to_write;
662 char *arg_filename = NULL;
663 char *new_filename = NULL;
664 char *tmp_filename = NULL;
665 char sequence[NUM_STR_SIZE];
667 filetype_t save_type;
673 * If there's no data, do nothing.
675 if (db->dataout >= db->datain) {
680 * See if we need to split this file.
682 while (db->split_size > (off_t)0 && dumpsize >= db->split_size) {
683 if( db->use == (off_t)0 ) {
685 * Probably no more space on this disk. Request some more.
687 putresult(RQ_MORE_DISK, "%s\n", handle);
689 if(command_in_transit == NULL &&
690 (cmdargs->cmd == DONE || cmdargs->cmd == TRYAGAIN || cmdargs->cmd == FAILED)) {
691 command_in_transit = cmdargs;
694 if(cmdargs->cmd == CONTINUE) {
702 a = 2; /* skip CONTINUE and serial */
704 if(a >= cmdargs->argc) {
705 error(_("error [chunker CONTINUE: not enough args: filename]"));
708 arg_filename = newstralloc(arg_filename, cmdargs->argv[a++]);
710 if(a >= cmdargs->argc) {
711 error(_("error [chunker CONTINUE: not enough args: chunksize]"));
714 db->chunk_size = OFF_T_ATOI(cmdargs->argv[a++]);
715 db->chunk_size = am_floor(db->chunk_size, (off_t)DISK_BLOCK_KB);
717 if(a >= cmdargs->argc) {
718 error(_("error [chunker CONTINUE: not enough args: use]"));
721 db->use = OFF_T_ATOI(cmdargs->argv[a++]);
723 if(a != cmdargs->argc) {
724 error(_("error [chunker CONTINUE: too many args: %d != %d]"),
729 if(strcmp(db->filename, arg_filename) == 0) {
731 * Same disk, so use what room is left up to the
732 * next chunk boundary or the amount we were given,
735 left_in_chunk = db->chunk_size - filesize;
736 if(left_in_chunk > db->use) {
737 db->split_size += db->use;
740 db->split_size += left_in_chunk;
741 db->use -= left_in_chunk;
743 if(left_in_chunk > (off_t)0) {
745 * We still have space in this chunk.
751 * Different disk, so use new file.
753 db->filename = newstralloc(db->filename, arg_filename);
755 } else if(cmdargs->cmd == ABORT) {
757 errstr = newstralloc(errstr, cmdargs->argv[1]);
758 putresult(ABORT_FINISHED, "%s\n", handle);
762 if(cmdargs->argc >= 1) {
763 q = quote_string(cmdargs->argv[0]);
765 q = stralloc(_("(no input?)"));
767 error(_("error [bad command after RQ-MORE-DISK: \"%s\"]"), q);
773 * Time to use another file.
777 * First, open the new chunk file, and give it a new header
778 * that has no cont_filename pointer.
780 g_snprintf(sequence, SIZEOF(sequence), "%d", db->filename_seq);
781 new_filename = newvstralloc(new_filename,
786 tmp_filename = newvstralloc(tmp_filename,
790 pc = strrchr(tmp_filename, '/');
791 g_assert(pc != NULL); /* Only a problem if db->filename has no /. */
793 mkholdingdir(tmp_filename);
795 newfd = open(tmp_filename, O_RDWR|O_CREAT|O_TRUNC, 0600);
797 int save_errno = errno;
800 if(save_errno == ENOSPC) {
801 putresult(NO_ROOM, "%s %lld\n", handle,
802 (long long)(db->use+db->split_size-dumpsize));
803 db->use = (off_t)0; /* force RQ_MORE_DISK */
804 db->split_size = dumpsize;
807 m = vstrallocf(_("creating chunk holding file \"%s\": %s"),
810 errstr = quote_string(m);
816 save_type = file.type;
817 file.type = F_CONT_DUMPFILE;
818 file.cont_filename[0] = '\0';
819 if(write_tapeheader(newfd, &file)) {
820 int save_errno = errno;
824 if(save_errno == ENOSPC) {
825 if (unlink(tmp_filename) < 0) {
826 g_debug("could not delete '%s'; ignoring", tmp_filename);
828 putresult(NO_ROOM, "%s %lld\n", handle,
829 (long long)(db->use+db->split_size-dumpsize));
830 db->use = (off_t)0; /* force RQ_MORE DISK */
831 db->split_size = dumpsize;
832 file.type = save_type;
835 m = vstrallocf(_("write_tapeheader file %s: %s"),
838 errstr = quote_string(m);
845 * Now, update the header of the current file to point
846 * to the next chunk, and then close it.
848 if (lseek(db->fd, (off_t)0, SEEK_SET) < (off_t)0) {
849 char *m = vstrallocf(_("lseek holding file %s: %s"),
852 errstr = quote_string(m);
859 file.type = save_type;
860 strncpy(file.cont_filename, new_filename, SIZEOF(file.cont_filename));
861 file.cont_filename[SIZEOF(file.cont_filename)-1] = '\0';
862 if(write_tapeheader(db->fd, &file)) {
863 char * m = vstrallocf(_("write_tapeheader file \"%s\": %s"),
866 errstr = quote_string(m);
869 unlink(tmp_filename);
873 file.type = F_CONT_DUMPFILE;
876 * Now shift the file descriptor.
883 * Update when we need to chunk again
885 if(db->use <= (off_t)DISK_BLOCK_KB) {
887 * Cheat and use one more block than allowed so we can make
890 db->split_size += (off_t)(2 * DISK_BLOCK_KB);
892 } else if(db->chunk_size > db->use) {
893 db->split_size += db->use;
896 db->split_size += db->chunk_size;
897 db->use -= db->chunk_size;
901 amfree(tmp_filename);
902 amfree(new_filename);
903 dumpsize += (off_t)DISK_BLOCK_KB;
904 filesize = (off_t)DISK_BLOCK_KB;
905 headersize += DISK_BLOCK_KB;
910 * Write out the buffer
912 size_to_write = (size_t)(db->datain - db->dataout);
913 written = db_full_write(db->fd, db->dataout, size_to_write);
915 db->dataout += written;
916 dumpbytes += (off_t)written;
918 dumpsize += (dumpbytes / (off_t)1024);
919 filesize += (dumpbytes / (off_t)1024);
921 if (written < size_to_write) {
922 if (errno != ENOSPC) {
923 char *m = vstrallocf(_("data write: %s"), strerror(errno));
924 errstr = quote_string(m);
931 * NO-ROOM is informational only. Later, RQ_MORE_DISK will be
932 * issued to use another holding disk.
934 putresult(NO_ROOM, "%s %lld\n", handle,
935 (long long)(db->use+db->split_size-dumpsize));
936 db->use = (off_t)0; /* force RQ_MORE_DISK */
937 db->split_size = dumpsize;
940 if (db->datain == db->dataout) {
942 * We flushed the whole buffer so reset to use it all.
944 db->datain = db->dataout = db->buf;
950 free_cmdargs(cmdargs);
951 amfree(new_filename);
952 /*@i@*/ amfree(tmp_filename);
953 amfree(arg_filename);
959 * Send an Amanda dump header to the output file and set file->blocksize
969 file->blocksize = DISK_BLOCK_BYTES;
970 if (debug_chunker > 1)
971 dump_dumpfile_t(file);
972 buffer = build_header(file, NULL, DISK_BLOCK_BYTES);
973 if (!buffer) /* this shouldn't happen */
974 error(_("header does not fit in %zd bytes"), (size_t)DISK_BLOCK_BYTES);
976 written = db_full_write(outfd, buffer, DISK_BLOCK_BYTES);
978 if(written == DISK_BLOCK_BYTES) return 0;
980 /* fake ENOSPC when we get a short write without errno set */
988 full_write_with_fake_enospc(
995 //g_debug("HERE %zd %zd", count, (size_t)fake_enospc_at_byte);
997 if (count <= (size_t)fake_enospc_at_byte) {
998 fake_enospc_at_byte -= count;
999 return full_write(fd, buf, count);
1002 /* if we get here, the caller has requested a size that is less
1003 * than fake_enospc_at_byte. */
1004 count = fake_enospc_at_byte;
1005 g_debug("returning fake ENOSPC");
1007 if (fake_enospc_at_byte) {
1008 rc = full_write(fd, buf, fake_enospc_at_byte);
1009 if (rc == (size_t)fake_enospc_at_byte) {
1010 /* full_write succeeded, so fake a failure */
1014 /* no bytes to write; just fake an error */
1019 /* switch back to calling full_write directly */
1020 fake_enospc_at_byte = -1;
1021 db_full_write = full_write;