1 1.1 christos /* Id: dba_write.c,v 1.3 2016/08/05 23:15:08 schwarze Exp */ 2 1.1 christos /* 3 1.1 christos * Copyright (c) 2016 Ingo Schwarze <schwarze (at) openbsd.org> 4 1.1 christos * 5 1.1 christos * Permission to use, copy, modify, and distribute this software for any 6 1.1 christos * purpose with or without fee is hereby granted, provided that the above 7 1.1 christos * copyright notice and this permission notice appear in all copies. 8 1.1 christos * 9 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 1.1 christos * 17 1.1 christos * Low-level functions for serializing allocation-based data to disk. 18 1.1 christos * The interface is defined in "dba_write.h". 19 1.1 christos */ 20 1.1 christos #include "config.h" 21 1.1 christos 22 1.1 christos #include <assert.h> 23 1.1 christos #if HAVE_ENDIAN 24 1.1 christos #include <endian.h> 25 1.1 christos #elif HAVE_SYS_ENDIAN 26 1.1 christos #include <sys/endian.h> 27 1.1 christos #elif HAVE_NTOHL 28 1.1 christos #include <arpa/inet.h> 29 1.1 christos #endif 30 1.1 christos #if HAVE_ERR 31 1.1 christos #include <err.h> 32 1.1 christos #endif 33 1.1 christos #include <errno.h> 34 1.1 christos #include <fcntl.h> 35 1.1 christos #include <stdint.h> 36 1.1 christos #include <stdio.h> 37 1.1 christos 38 1.1 christos #include "dba_write.h" 39 1.1 christos 40 1.1 christos static FILE *ofp; 41 1.1 christos 42 1.1 christos 43 1.1 christos int 44 1.1 christos dba_open(const char *fname) 45 1.1 christos { 46 1.1 christos ofp = fopen(fname, "w"); 47 1.1 christos return ofp == NULL ? -1 : 0; 48 1.1 christos } 49 1.1 christos 50 1.1 christos int 51 1.1 christos dba_close(void) 52 1.1 christos { 53 1.1 christos return fclose(ofp) == EOF ? -1 : 0; 54 1.1 christos } 55 1.1 christos 56 1.1 christos int32_t 57 1.1 christos dba_tell(void) 58 1.1 christos { 59 1.1 christos long pos; 60 1.1 christos 61 1.1 christos if ((pos = ftell(ofp)) == -1) 62 1.1 christos err(1, "ftell"); 63 1.1 christos if (pos >= INT32_MAX) { 64 1.1 christos errno = EOVERFLOW; 65 1.1 christos err(1, "ftell = %ld", pos); 66 1.1 christos } 67 1.1 christos return pos; 68 1.1 christos } 69 1.1 christos 70 1.1 christos void 71 1.1 christos dba_seek(int32_t pos) 72 1.1 christos { 73 1.1 christos if (fseek(ofp, pos, SEEK_SET) == -1) 74 1.1 christos err(1, "fseek(%d)", pos); 75 1.1 christos } 76 1.1 christos 77 1.1 christos int32_t 78 1.1 christos dba_align(void) 79 1.1 christos { 80 1.1 christos int32_t pos; 81 1.1 christos 82 1.1 christos pos = dba_tell(); 83 1.1 christos while (pos & 3) { 84 1.1 christos dba_char_write('\0'); 85 1.1 christos pos++; 86 1.1 christos } 87 1.1 christos return pos; 88 1.1 christos } 89 1.1 christos 90 1.1 christos int32_t 91 1.1 christos dba_skip(int32_t nmemb, int32_t sz) 92 1.1 christos { 93 1.1 christos const int32_t out[5] = {0, 0, 0, 0, 0}; 94 1.1 christos int32_t i, pos; 95 1.1 christos 96 1.1 christos assert(sz >= 0); 97 1.1 christos assert(nmemb > 0); 98 1.1 christos assert(nmemb <= 5); 99 1.1 christos pos = dba_tell(); 100 1.1 christos for (i = 0; i < sz; i++) 101 1.1 christos if (nmemb - fwrite(&out, sizeof(out[0]), nmemb, ofp)) 102 1.1 christos err(1, "fwrite"); 103 1.1 christos return pos; 104 1.1 christos } 105 1.1 christos 106 1.1 christos void 107 1.1 christos dba_char_write(int c) 108 1.1 christos { 109 1.1 christos if (putc(c, ofp) == EOF) 110 1.1 christos err(1, "fputc"); 111 1.1 christos } 112 1.1 christos 113 1.1 christos void 114 1.1 christos dba_str_write(const char *str) 115 1.1 christos { 116 1.1 christos if (fputs(str, ofp) == EOF) 117 1.1 christos err(1, "fputs"); 118 1.1 christos dba_char_write('\0'); 119 1.1 christos } 120 1.1 christos 121 1.1 christos void 122 1.1 christos dba_int_write(int32_t i) 123 1.1 christos { 124 1.1 christos i = htobe32(i); 125 1.1 christos if (fwrite(&i, sizeof(i), 1, ofp) != 1) 126 1.1 christos err(1, "fwrite"); 127 1.1 christos } 128