1 /* This file is part of GDBM, the GNU data base manager.
2 Copyright (C) 2011, 2013 Free Software Foundation, Inc.
4 GDBM is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 GDBM is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
17 # include "autoconf.h"
18 # include "gdbmdefs.h"
25 print_datum (datum const *dat, unsigned char **bufptr,
26 size_t *bufsize, FILE *fp)
32 fprintf (fp, "#:len=%lu\n", (unsigned long) dat->dsize);
33 rc = _gdbm_base64_encode ((unsigned char*) dat->dptr, dat->dsize,
34 bufptr, bufsize, &len);
42 if (n > _GDBM_MAX_DUMP_LINE_LEN)
43 n = _GDBM_MAX_DUMP_LINE_LEN;
44 if (fwrite (p, n, 1, fp) != 1)
45 return GDBM_FILE_WRITE_ERROR;
54 _gdbm_dump_ascii (GDBM_FILE dbf, FILE *fp)
63 unsigned char *buffer = NULL;
67 fd = gdbm_fdesc (dbf);
69 return GDBM_FILE_STAT_ERROR;
73 fprintf (fp, "# GDBM dump file created by %s on %s",
74 gdbm_version, ctime (&t));
75 fprintf (fp, "#:version=1.0\n");
77 fprintf (fp, "#:file=%s\n", dbf->name);
78 fprintf (fp, "#:uid=%lu,", (unsigned long) st.st_uid);
79 pw = getpwuid (st.st_uid);
81 fprintf (fp, "user=%s,", pw->pw_name);
82 fprintf (fp, "gid=%lu,", (unsigned long) st.st_gid);
83 gr = getgrgid (st.st_gid);
85 fprintf (fp, "group=%s,", gr->gr_name);
86 fprintf (fp, "mode=%03o\n", st.st_mode & 0777);
87 fprintf (fp, "# End of header\n");
89 key = gdbm_firstkey (dbf);
94 datum data = gdbm_fetch (dbf, key);
97 if ((rc = print_datum (&key, &buffer, &bufsize, fp)) ||
98 (rc = print_datum (&data, &buffer, &bufsize, fp)))
106 nextkey = gdbm_nextkey (dbf, key);
115 /* FIXME: Something like that won't hurt, although load does not
117 fprintf (fp, "#:count=%lu\n", (unsigned long) count);
118 fprintf (fp, "# End of data\n");
127 gdbm_dump_to_file (GDBM_FILE dbf, FILE *fp, int format)
133 case GDBM_DUMP_FMT_BINARY:
134 rc = gdbm_export_to_file (dbf, fp) == -1;
137 case GDBM_DUMP_FMT_ASCII:
138 rc = _gdbm_dump_ascii (dbf, fp);
145 if (rc == 0 && ferror (fp))
146 rc = gdbm_errno = GDBM_FILE_WRITE_ERROR;
152 gdbm_dump (GDBM_FILE dbf, const char *filename, int fmt, int open_flags,
158 /* Only support GDBM_WCREAT or GDBM_NEWDB */
162 nfd = open (filename, O_WRONLY | O_CREAT | O_EXCL, mode);
165 gdbm_errno = GDBM_FILE_OPEN_ERROR;
170 nfd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
173 gdbm_errno = GDBM_FILE_OPEN_ERROR;
178 gdbm_errno = GDBM_BAD_OPEN_FLAGS;
182 fp = fdopen (nfd, "w");
186 gdbm_errno = GDBM_FILE_OPEN_ERROR;
189 rc = gdbm_dump_to_file (dbf, fp, fmt);