1 /* gdbmexp.c - Export a GDBM database. */
3 /* This file is part of GDBM, the GNU data base manager.
4 Copyright (C) 2007, 2011, 2013 Free Software Foundation, Inc.
6 GDBM is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GDBM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
19 /* Include system configuration before all else. */
20 # include "autoconf.h"
21 # include <arpa/inet.h>
23 # include "gdbmdefs.h"
27 gdbm_export_to_file (GDBM_FILE dbf, FILE *fp)
30 datum key, nextkey, data;
31 const char *header1 = "!\r\n! GDBM FLAT FILE DUMP -- THIS IS NOT A TEXT FILE\r\n! ";
32 const char *header2 = "\r\n!\r\n";
35 /* Write out the text header. */
36 if (fwrite (header1, strlen (header1), 1, fp) != 1)
38 if (fwrite (gdbm_version, strlen (gdbm_version), 1, fp) != 1)
40 if (fwrite (header2, strlen (header2), 1, fp) != 1)
43 /* For each item in the database, write out a record to the file. */
44 key = gdbm_firstkey (dbf);
46 while (key.dptr != NULL)
48 data = gdbm_fetch (dbf, key);
49 if (data.dptr != NULL)
51 /* Add the data to the new file. */
52 size = htonl (key.dsize);
53 if (fwrite (&size, sizeof (size), 1, fp) != 1)
55 if (fwrite (key.dptr, key.dsize, 1, fp) != 1)
58 size = htonl (data.dsize);
59 if (fwrite (&size, sizeof (size), 1, fp) != 1)
61 if (fwrite (data.dptr, data.dsize, 1, fp) != 1)
64 nextkey = gdbm_nextkey (dbf, key);
76 gdbm_errno = GDBM_FILE_WRITE_ERROR;
81 gdbm_export (GDBM_FILE dbf, const char *exportfile, int flags, int mode)
86 /* Only support GDBM_WCREAT or GDBM_NEWDB */
90 nfd = open (exportfile, O_WRONLY | O_CREAT | O_EXCL, mode);
93 gdbm_errno = GDBM_FILE_OPEN_ERROR;
98 nfd = open (exportfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
101 gdbm_errno = GDBM_FILE_OPEN_ERROR;
106 #ifdef GDBM_BAD_OPEN_FLAGS
107 gdbm_errno = GDBM_BAD_OPEN_FLAGS;
109 gdbm_errno = GDBM_FILE_OPEN_ERROR;
114 fp = fdopen (nfd, "w");
118 gdbm_errno = GDBM_FILE_OPEN_ERROR;
122 rc = gdbm_export_to_file (dbf, fp);