Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:cleanup zdbr
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:7894bb786f8f0ce5bdce794d5ee1666a55850a62
User & Date: brandon 2016-06-12 15:11:36
Context
2016-06-12
15:11
pull in more gnulib libs check-in: ee981fdc85 user: brandon tags: trunk
15:11
cleanup zdbr check-in: 7894bb786f user: brandon tags: trunk
14:07
remove unnecessary headers check-in: 56b3b1a867 user: brandon tags: trunk
Changes

Changes to src/zdbr.c.

18
19
20
21
22
23
24

25
26
27
28
29
30
31
..
39
40
41
42
43
44
45

46

47

48
49

50
51

52
53

54
55

56
57
58
59
60
61
62
..
72
73
74
75
76
77
78








79







80
81
82
83
84
85
86
...
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
...
198
199
200
201
202
203
204
205
206
207
208
209



210
211

212
213
214
215
216
217
218
219


220
221
222
223
224
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <config.h>
#include <string.h>

#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include "zdb.h"

const char *program_name = "zdbr";
................................................................................
  "many records from the database, some fragmentation can occur, in which case "
  "it is recommended to reorganize the database. If the 'reorganize' argument "
  "is passed, the database will be reorganized after any records have been "
  "removed";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {

  {"input", 'i', "FILE", 0, "Read queries from a file"},

  {"reorganize", 'r', 0, 0, "Reorganize the database"},

  {"sync", 's', 0, 0, "Automatically synchronize all database operations to the"
   " disk"},

  {"no-lock", 'l', 0, 0, "Do not perform any locking of the database file"},
#ifdef HAVE_GDBM

  {"no-mmap", 'n', 0, 0, "Do not memory-map the database file"},
#endif

  {"verbose", 'v', 0, 0, "Print extra information."},
  {0}

};

struct arguments
{
  char *args[1];
  char *input_file;
  bool reorg;
................................................................................
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;

  switch (key)
    {
    case 'i':








      arguments->input_file = arg;







      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = { options, parse_opt, args_doc, doc };



int
remrecords (void *db, FILE *input, bool verbose)
{
  /*
   * This function removes records from the database from queries
   * in a file object.
   */

  size_t linelen = 512;
  size_t key_size;
  char *lineptr = (char *) malloc (linelen);
  char *key_str;





  /* query the database for each line of input */
  while (getline (&lineptr, &linelen, input) != EOF)
    {


      key_size = strlen (lineptr);
      if (lineptr[key_size - 1] == '\n')
        key_size -= 1;
      if (key_size <= 0)
        error (0, errno, "Empty key value");
      key_str = (char *) malloc (sizeof (char) * (key_size+1));
      if (!key_str)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Virtual memory exhausted");
        }
      /* get rid of the newline */
      strncpy (key_str, lineptr, sizeof (char) * (key_size));
      key_str[key_size] = '\0';
      if (zdb_remove (db, key_str, key_size, verbose))
        error (0, errno, "Key does not exist in the database: %s", key_str);
      free (key_str);
    }
  free (lineptr);
  return (0);
}

void
termination_handler (int signum)
{
  printf ("Interrupt caught, closing database\n");
  if (zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
  else
    exit (EXIT_SUCCESS);
}

int
main (int argc, char **argv)
{
  void *database;
  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;


  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = "-";
  arguments.reorg = false;

  argp_parse (&argp, argc, argv, 0, 0, &arguments);



  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
................................................................................
  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (strcmp (arguments.input_file, "-") == 0)
    input = stdin;
  else
    input = fopen (arguments.input_file, "r");
  if (!input)



    error (EXIT_FAILURE, errno,
           "Failed to open file %s", arguments.input_file);

  remrecords (database, input, arguments.verbose);

  /* if the "reorganize" argument was passed, reorganize  */
  /* the database (GDBM only) */
  if (arguments.reorg != 0)
    zdb_reorganize (database);

  fclose (input);


  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}







>







 







>
|
>
|
>
|
<
>
|

>
|

>
|
<
>







 







>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>







 







|
>
>




<
<
<
<
<





>
>
>
>



>
>



|





|












|
|


|








<



>







|


|
>
>







 







|




>
>
>
|
|
>




|



>
>





18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
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
..
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
...
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
...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
   along with zeptodb.  If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE

#include <config.h>
#include <string.h>
#include <stdio.h>
#include "error.h"
#include "argp.h"
#include <signal.h>
#include <stdbool.h>
#include "zdb.h"

const char *program_name = "zdbr";
................................................................................
  "many records from the database, some fragmentation can occur, in which case "
  "it is recommended to reorganize the database. If the 'reorganize' argument "
  "is passed, the database will be reorganized after any records have been "
  "removed";
static char args_doc[] = "DATABASE";

static struct argp_option options[] = {
  {"input", (int) 'i', "FILE", 0,
   "Read queries from a file", 0},
  {"reorganize", (int) 'r', (const char *)NULL, 0,
   "Reorganize the database", 0},
  {"sync", (int) 's', (const char *)NULL, 0,
   "Automatically synchronize all database operations to the disk", 0},

  {"no-lock", (int) 'l', (const char *)NULL, 0,
   "Do not perform any locking of the database file", 0},
#ifdef HAVE_GDBM
  {"no-mmap", (int) 'n', (const char *)NULL, 0,
   "Do not memory-map the database file", 0},
#endif
  {"verbose", (int) 'v', (const char *)NULL, 0,
   "Print extra information.", 0},

  {0, 0, 0, 0, 0, 0}
};

struct arguments
{
  char *args[1];
  char *input_file;
  bool reorg;
................................................................................
parse_opt (int key, char *arg, struct argp_state *state)
{
  struct arguments *arguments = state->input;

  switch (key)
    {
    case 'i':
      if (arguments->input_file)
        free (arguments->input_file);
      if (!arg)
        {
          arguments->input_file = NULL;
        }
      else
        {
          arguments->input_file =
            (char *) malloc (sizeof (char) * (strlen (arg)+1));
          if (!arguments->input_file)
            error (EXIT_FAILURE, errno,
                   "Failed to allocate memory");
          strncpy (arguments->input_file, arg, strlen (arg));
          arguments->input_file[strlen (arg)] = '\0';
        }
      break;
    case 'l':
      arguments->no_lock = true;
      break;
#ifdef HAVE_GDBM
    case 'n':
      arguments->no_mmap = true;
................................................................................
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp argp = {options, parse_opt, args_doc, doc,
                           (const struct argp_child *)NULL,
                           NULL, (const char *)NULL};

int
remrecords (void *db, FILE *input, bool verbose)
{





  size_t linelen = 512;
  size_t key_size;
  char *lineptr = (char *) malloc (linelen);
  char *key_str;

  if (!lineptr)
    error (EXIT_FAILURE, errno,
           "Failed to allocate memory");

  /* query the database for each line of input */
  while (getline (&lineptr, &linelen, input) != EOF)
    {
      if (!lineptr)
          error (EXIT_FAILURE, errno, "Failed to read line");
      key_size = strlen (lineptr);
      if (lineptr[key_size - 1] == '\n')
        key_size -= 1;
      if (key_size == 0)
        error (0, errno, "Empty key value");
      key_str = (char *) malloc (sizeof (char) * (key_size+1));
      if (!key_str)
        {
          free (lineptr);
          error (EXIT_FAILURE, errno, "Failed to allocate memory");
        }
      /* get rid of the newline */
      strncpy (key_str, lineptr, sizeof (char) * (key_size));
      key_str[key_size] = '\0';
      if (zdb_remove (db, key_str, key_size, verbose))
        error (0, errno, "Key does not exist in the database: %s", key_str);
      free (key_str);
    }
  free (lineptr);
  return (0);
}

static void
termination_handler (int signum __attribute__ ((unused)))
{
  printf ("Interrupt caught, closing database\n");
  if (database && zdb_close (database, false))
    error (EXIT_FAILURE, errno, "Failed to close database");
  else
    exit (EXIT_SUCCESS);
}

int
main (int argc, char **argv)
{

  FILE *input;
  struct arguments arguments;
  int mode = ZDB_WRITER;
  error_t argp_result;

  arguments.verbose = false;
  arguments.sync = false;
  arguments.no_lock = false;
#ifdef HAVE_GDBM
  arguments.no_mmap = false;
#endif
  arguments.input_file = NULL;
  arguments.reorg = false;

  argp_result = argp_parse (&argp, argc, argv, 0, 0, &arguments);
  if (argp_result)
    error (EXIT_FAILURE, argp_result, "Failed to parse arguments");

  if (signal (SIGINT, termination_handler) == SIG_IGN)
    signal (SIGINT, SIG_IGN);
  if (signal (SIGHUP, termination_handler) == SIG_IGN)
    signal (SIGHUP, SIG_IGN);
  if (signal (SIGTERM, termination_handler) == SIG_IGN)
    signal (SIGTERM, SIG_IGN);
................................................................................
  /* open the database */
  database = zdb_open (arguments.args[0], mode, arguments.verbose);
  if (!database)
    error (EXIT_FAILURE, errno,
           "Failed to open database %s", arguments.args[0]);

  /* if an input file was provided, use it, otherwise use stdin */
  if (!arguments.input_file || strcmp (arguments.input_file, "-") == 0)
    input = stdin;
  else
    input = fopen (arguments.input_file, "r");
  if (!input)
    {
      if (arguments.input_file)
        free (arguments.input_file);
      error (EXIT_FAILURE, errno,
             "Failed to open file %s", arguments.input_file);
    }
  remrecords (database, input, arguments.verbose);

  /* if the "reorganize" argument was passed, reorganize  */
  /* the database (GDBM only) */
  if (arguments.reorg)
    zdb_reorganize (database);

  fclose (input);
  if (arguments.input_file)
    free (arguments.input_file);
  if (zdb_close (database, arguments.verbose))
    error (EXIT_FAILURE, errno,
           "Failed to close database %s", arguments.args[0]);
  exit (EXIT_SUCCESS);
}