add changelog
[platform/upstream/gdbm.git] / src / gdbmexp.c
1 /* gdbmexp.c - Export a GDBM database. */
2
3 /* This file is part of GDBM, the GNU data base manager.
4    Copyright (C) 2007, 2011, 2013 Free Software Foundation, Inc.
5
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)
9    any later version.
10
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.
15
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/>.   */
18
19 /* Include system configuration before all else. */
20 # include "autoconf.h"
21 # include <arpa/inet.h>
22
23 # include "gdbmdefs.h"
24 # include "gdbm.h"
25
26 int
27 gdbm_export_to_file (GDBM_FILE dbf, FILE *fp)
28 {
29   unsigned long size;
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";
33   int count = 0;
34
35   /* Write out the text header. */
36   if (fwrite (header1, strlen (header1), 1, fp) != 1)
37     goto write_fail;
38   if (fwrite (gdbm_version, strlen (gdbm_version), 1, fp) != 1)
39     goto write_fail;
40   if (fwrite (header2, strlen (header2), 1, fp) != 1)
41     goto write_fail;
42
43   /* For each item in the database, write out a record to the file. */
44   key = gdbm_firstkey (dbf);
45
46   while (key.dptr != NULL)
47     {
48       data = gdbm_fetch (dbf, key);
49       if (data.dptr != NULL)
50         {
51           /* Add the data to the new file. */
52           size = htonl (key.dsize);
53           if (fwrite (&size, sizeof (size), 1, fp) != 1)
54             goto write_fail;
55           if (fwrite (key.dptr, key.dsize, 1, fp) != 1)
56             goto write_fail;
57
58           size = htonl (data.dsize);
59           if (fwrite (&size, sizeof (size), 1, fp) != 1)
60             goto write_fail;
61           if (fwrite (data.dptr, data.dsize, 1, fp) != 1)
62             goto write_fail;
63         }
64       nextkey = gdbm_nextkey (dbf, key);
65       free (key.dptr);
66       free (data.dptr);
67       key = nextkey;
68       
69       count++;
70     }
71   
72   return count;
73   
74  write_fail:
75   
76   gdbm_errno = GDBM_FILE_WRITE_ERROR;
77   return -1;
78 }
79
80 int
81 gdbm_export (GDBM_FILE dbf, const char *exportfile, int flags, int mode)
82 {
83   int nfd, rc;
84   FILE *fp;
85   
86   /* Only support GDBM_WCREAT or GDBM_NEWDB */
87   switch (flags)
88     {
89     case GDBM_WRCREAT:
90       nfd = open (exportfile, O_WRONLY | O_CREAT | O_EXCL, mode);
91       if (nfd == -1)
92         {
93           gdbm_errno = GDBM_FILE_OPEN_ERROR;
94           return -1;
95         }
96       break;
97     case GDBM_NEWDB:
98       nfd = open (exportfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
99       if (nfd == -1)
100         {
101           gdbm_errno = GDBM_FILE_OPEN_ERROR;
102           return -1;
103         }
104       break;
105     default:
106 #ifdef GDBM_BAD_OPEN_FLAGS
107       gdbm_errno = GDBM_BAD_OPEN_FLAGS;
108 #else
109       gdbm_errno = GDBM_FILE_OPEN_ERROR;
110 #endif
111       return -1;
112   }
113
114   fp = fdopen (nfd, "w");
115   if (!fp)
116     {
117       close (nfd);
118       gdbm_errno = GDBM_FILE_OPEN_ERROR;
119       return -1;
120     }
121         
122   rc = gdbm_export_to_file (dbf, fp);
123   fclose (fp);
124   return rc;
125 }