aboutsummaryrefslogtreecommitdiff
path: root/src/gdbmdump.c
blob: a5396b59ec3f3a17f91fc62dcfa9c52bdd30efb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* This file is part of GDBM, the GNU data base manager.
   Copyright (C) 2011, 2013, 2016-2020 Free Software Foundation, Inc.

   GDBM is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GDBM is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */

# include "autoconf.h"
# include "gdbmdefs.h"
# include "gdbm.h"
# include <pwd.h>
# include <grp.h>
# include <time.h>

static int
print_datum (datum const *dat, unsigned char **bufptr,
	     size_t *bufsize, FILE *fp)
{
  int rc;
  size_t len;
  unsigned char *p;
  
  fprintf (fp, "#:len=%lu\n", (unsigned long) dat->dsize);
  rc = _gdbm_base64_encode ((unsigned char*) dat->dptr, dat->dsize,
			    bufptr, bufsize, &len);
  if (rc)
    return rc;
  
  p = *bufptr;
  while (len)
    {
      size_t n = len;
      if (n > _GDBM_MAX_DUMP_LINE_LEN)
	n = _GDBM_MAX_DUMP_LINE_LEN;
      if (fwrite (p, n, 1, fp) != 1)
	return GDBM_FILE_WRITE_ERROR;
      fputc ('\n', fp);
      len -= n;
      p += n;
    }
  return 0;
}

int
_gdbm_dump_ascii (GDBM_FILE dbf, FILE *fp)
{
  time_t t;
  int fd;
  struct stat st;
  struct passwd *pw;
  struct group *gr;
  datum key;
  size_t count = 0;
  unsigned char *buffer = NULL;
  size_t bufsize = 0;
  int rc = 0;

  fd = gdbm_fdesc (dbf);
  if (fstat (fd, &st))
    return GDBM_FILE_STAT_ERROR;

  /* Print header */
  time (&t);
  fprintf (fp, "# GDBM dump file created by %s on %s",
	   gdbm_version, ctime (&t));
  fprintf (fp, "#:version=1.0\n");

  fprintf (fp, "#:file=%s\n", dbf->name);
  fprintf (fp, "#:uid=%lu,", (unsigned long) st.st_uid);
  pw = getpwuid (st.st_uid);
  if (pw)
    fprintf (fp, "user=%s,", pw->pw_name);
  fprintf (fp, "gid=%lu,", (unsigned long) st.st_gid);
  gr = getgrgid (st.st_gid);
  if (gr)
    fprintf (fp, "group=%s,", gr->gr_name);
  fprintf (fp, "mode=%03o\n", st.st_mode & 0777);
  fprintf (fp, "# End of header\n");
  
  key = gdbm_firstkey (dbf);

  while (key.dptr)
    {
      datum nextkey;
      datum data = gdbm_fetch (dbf, key);
      if (data.dptr)
 	{
	  if ((rc = print_datum (&key, &buffer, &bufsize, fp)) ||
	      (rc = print_datum (&data, &buffer, &bufsize, fp)))
	    {
	      free (key.dptr);
	      free (data.dptr);
	      GDBM_SET_ERRNO (dbf, rc, FALSE);
	      break;
	    }
 	}
      else
	break;
      nextkey = gdbm_nextkey (dbf, key);
      free (key.dptr);
      free (data.dptr);
      key = nextkey;
      count++;
    }

  /* FIXME: Something like that won't hurt, although load does not
     use it currently. */
  fprintf (fp, "#:count=%lu\n", (unsigned long) count);
  fprintf (fp, "# End of data\n");
  
  if (rc == 0)
    {
      rc = gdbm_last_errno (dbf);
      if (rc == GDBM_ITEM_NOT_FOUND)
	{
	  gdbm_clear_error (dbf);
	  gdbm_errno = GDBM_NO_ERROR;
	  rc = 0;
	}
    }
  free (buffer);

  return rc ? -1 : 0;
}

int
gdbm_dump_to_file (GDBM_FILE dbf, FILE *fp, int format)
{
  int rc;
  
  /* Return immediately if the database needs recovery */	
  GDBM_ASSERT_CONSISTENCY (dbf, -1);
  
  switch (format)
    {
    case GDBM_DUMP_FMT_BINARY:
      rc = gdbm_export_to_file (dbf, fp) == -1;
      break;

    case GDBM_DUMP_FMT_ASCII:
      rc = _gdbm_dump_ascii (dbf, fp);
      break;

    default:
      GDBM_SET_ERRNO (NULL, GDBM_BAD_OPEN_FLAGS, FALSE);
      return EINVAL;
    }
  
  if (rc == 0 && ferror (fp))
    {
      GDBM_SET_ERRNO (NULL, GDBM_FILE_WRITE_ERROR, FALSE);
      rc = -1;
    }

  return rc;
}

int
gdbm_dump (GDBM_FILE dbf, const char *filename, int fmt, int open_flags,
	   int mode)
{
  int nfd, rc;
  FILE *fp;
  
  /* Return immediately if the database needs recovery */	
  GDBM_ASSERT_CONSISTENCY (dbf, -1);
  
  /* Only support GDBM_WCREAT or GDBM_NEWDB */
  switch (open_flags)
    {
    case GDBM_WRCREAT:
      nfd = open (filename, O_WRONLY | O_CREAT | O_EXCL, mode);
      if (nfd == -1)
	{
	  GDBM_SET_ERRNO (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
	  return -1;
	}
      break;
    case GDBM_NEWDB:
      nfd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode);
      if (nfd == -1)
	{
	  GDBM_SET_ERRNO (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
	  return -1;
	}
      break;
    default:
      GDBM_SET_ERRNO (NULL, GDBM_BAD_OPEN_FLAGS, FALSE);
      return -1;
  }

  fp = fdopen (nfd, "w");
  if (!fp)
    {
      close (nfd);
      GDBM_SET_ERRNO (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
      return -1;
    }
  rc = gdbm_dump_to_file (dbf, fp, fmt);
  fclose (fp);
  return rc;
}



Return to:

Send suggestions and report system problems to the System administrator.