~ubuntu-branches/debian/sid/thunar/sid

« back to all changes in this revision

Viewing changes to .pc/01_use-system-tdb.patch/tdb/tdbtool.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez, Yves-Alexis Perez, Lionel Le Folgoc
  • Date: 2011-06-18 23:23:52 UTC
  • mfrom: (1.2.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110618232352-57ua2sh3kg722xem
Tags: 1.2.2-1
[ Yves-Alexis Perez ]
* New upstream release.
  - load network stuff later to speed up start (Xfce #7373).  closes: #626200
    lp: #775117
  - fixed Dutch translation                                       lp: #781048
* debian/patches:
  - 01_use-system-td dropped, included upstream.
  - 02_thunar-icon-naming-spec-compliance dropped, don't replace stock icons
    even if they aren't part of the spec.
  - 03_Don-t-interpret-file-display-names-as-format-strings dropped,
    included upstream.
* debian/control:
  - drop build-dep on xfce4-dev-tools, libtool and gtk-doc-tools
* debian/rules:
  - don't run xdt-autogen anymore.

[ Lionel Le Folgoc ]
* debian/patches:
  - 01_retrieve-the-translated-desktop-file-name.patch: fixes untranslated
    .desktop display name.
  - series: refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$ */
2
 
/*-
3
 
 * Copyright (c) 1999-2000 Andrew Tridgell
4
 
 * Copyright (c) 2000      Paul `Rusty' Russell
5
 
 * Copyright (c) 2000      Jeremy Allison
6
 
 * Copyright (c) 2001      Andrew Esh
7
 
 * Copyright (c) 2005      Benedikt Meurer <benny@xfce.org>
8
 
 *
9
 
 * This library is free software; you can redistribute it and/or
10
 
 * modify it under the terms of the GNU Library General Public
11
 
 * License as published by the Free Software Foundation; either
12
 
 * version 2 of the License, or (at your option) any later version.
13
 
 *
14
 
 * This library is distributed in the hope that it will be useful,
15
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
 * Library General Public License for more details.
18
 
 *
19
 
 * You should have received a copy of the GNU Library General Public
20
 
 * License along with this library; if not, write to the
21
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22
 
 * Boston, MA 02111-1307, USA.
23
 
 *
24
 
 * This file was originally part of the tdb library, which in turn is
25
 
 * part of the Samba suite, a Unix SMB/CIFS implementation.
26
 
 */
27
 
 
28
 
#ifdef HAVE_CONFIG_H
29
 
#include <config.h>
30
 
#endif
31
 
 
32
 
#ifdef HAVE_SYS_TYPES_H
33
 
#include <sys/types.h>
34
 
#endif
35
 
#ifdef HAVE_SYS_MMAN_H
36
 
#include <sys/mman.h>
37
 
#endif
38
 
#ifdef HAVE_SYS_STAT_H
39
 
#include <sys/stat.h>
40
 
#endif
41
 
#ifdef HAVE_SYS_TIME_H
42
 
#include <sys/time.h>
43
 
#endif
44
 
 
45
 
#ifdef HAVE_CTYPE_H
46
 
#include <ctype.h>
47
 
#endif
48
 
#ifdef HAVE_ERRNO_H
49
 
#include <errno.h>
50
 
#endif
51
 
#ifdef HAVE_FCNTL_H
52
 
#include <fcntl.h>
53
 
#endif
54
 
#ifdef HAVE_MEMORY_H
55
 
#include <memory.h>
56
 
#endif
57
 
#ifdef HAVE_STDLIB_H
58
 
#include <stdlib.h>
59
 
#endif
60
 
#include <stdio.h>
61
 
#ifdef HAVE_STRING_H
62
 
#include <string.h>
63
 
#endif
64
 
#ifdef HAVE_TIME_H
65
 
#include <time.h>
66
 
#endif
67
 
#ifdef HAVE_UNISTD_H
68
 
#include <unistd.h>
69
 
#endif
70
 
 
71
 
#include <tdb/tdb.h>
72
 
 
73
 
/* a tdb tool for manipulating a tdb database */
74
 
 
75
 
#define FSTRING_LEN 256
76
 
typedef char fstring[FSTRING_LEN];
77
 
 
78
 
typedef struct connections_key {
79
 
  pid_t pid;
80
 
  int cnum;
81
 
  fstring name;
82
 
} connections_key;
83
 
 
84
 
typedef struct connections_data {
85
 
  int magic;
86
 
  pid_t pid;
87
 
  int cnum;
88
 
  uid_t uid;
89
 
  gid_t gid;
90
 
  char name[24];
91
 
  char addr[24];
92
 
  char machine[128];
93
 
  time_t start;
94
 
} connections_data;
95
 
 
96
 
static TDB_CONTEXT *tdb;
97
 
FILE *pDumpFile;
98
 
 
99
 
static int print_rec(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
100
 
 
101
 
static char *get_token(int startover)
102
 
{
103
 
  static char tmp[1024];
104
 
  static char *cont = NULL;
105
 
  char *insert, *start;
106
 
  char *k = strtok(NULL, " ");
107
 
 
108
 
  if (!k)
109
 
    return NULL;
110
 
 
111
 
  if (startover)
112
 
    start = tmp;
113
 
  else
114
 
    start = cont;
115
 
 
116
 
  strcpy(start, k);
117
 
  insert = start + strlen(start) - 1;
118
 
  while (*insert == '\\') {
119
 
    *insert++ = ' ';
120
 
    k = strtok(NULL, " ");
121
 
    if (!k)
122
 
      break;
123
 
    strcpy(insert, k);
124
 
    insert = start + strlen(start) - 1;
125
 
  }
126
 
 
127
 
  /* Get ready for next call */
128
 
  cont = start + strlen(start) + 1;
129
 
  return start;
130
 
}
131
 
 
132
 
static int open_dump_file(void)
133
 
{
134
 
  int retval = 0;
135
 
  char *tok = get_token(0);
136
 
  if (!tok) {
137
 
    pDumpFile = stdout;
138
 
  } else {
139
 
    pDumpFile = fopen(tok, "w");
140
 
    
141
 
    if (pDumpFile == NULL) {
142
 
      printf("File Open Failed! --  %s", tok);
143
 
      retval = 1;
144
 
    } else {
145
 
      printf("Writing to file: %s\n", tok);
146
 
    }
147
 
  }
148
 
    return(retval);
149
 
}
150
 
 
151
 
static void close_dump_file(void)
152
 
{
153
 
  if(pDumpFile != NULL && pDumpFile != stdout) {
154
 
    fclose(pDumpFile);
155
 
  }
156
 
}
157
 
 
158
 
 static void print_asc(unsigned char *buf,int len)
159
 
{
160
 
  int i;
161
 
 
162
 
  /* We're probably printing ASCII strings so don't try to display
163
 
     the trailing NULL character. */
164
 
 
165
 
  if (buf[len - 1] == 0)
166
 
          len--;
167
 
 
168
 
  for (i=0;i<len;i++)
169
 
    fprintf(pDumpFile,"%c",isprint(buf[i])?buf[i]:'.');
170
 
}
171
 
 
172
 
static void print_data(unsigned char *buf,int len)
173
 
{
174
 
  int i=0;
175
 
  if (len<=0) return;
176
 
  fprintf(pDumpFile,"[%03X] ",i);
177
 
  for (i=0;i<len;) {
178
 
    fprintf(pDumpFile,"%02X ",(int)buf[i]);
179
 
    i++;
180
 
    if (i%8 == 0) fprintf(pDumpFile," ");
181
 
    if (i%16 == 0) {      
182
 
      print_asc(&buf[i-16],8); fprintf(pDumpFile," ");
183
 
      print_asc(&buf[i-8],8); fprintf(pDumpFile,"\n");
184
 
      if (i<len) fprintf(pDumpFile,"[%03X] ",i);
185
 
    }
186
 
  }
187
 
  if (i%16) {
188
 
    int n;
189
 
    
190
 
    n = 16 - (i%16);
191
 
    fprintf(pDumpFile," ");
192
 
    if (n>8) fprintf(pDumpFile," ");
193
 
    while (n--) fprintf(pDumpFile,"   ");
194
 
    
195
 
    n = i%16;
196
 
    if (n > 8) n = 8;
197
 
    print_asc(&buf[i-(i%16)],n); fprintf(pDumpFile," ");
198
 
    n = (i%16) - n;
199
 
    if (n>0) print_asc(&buf[i-n],n); 
200
 
    fprintf(pDumpFile,"\n");    
201
 
  }
202
 
}
203
 
 
204
 
static void help(void)
205
 
{
206
 
  printf(
207
 
"tdbtool:\n"
208
 
"  create    dbname     : create a database\n"
209
 
"  open      dbname     : open an existing database\n"
210
 
"  erase                : erase the database\n"
211
 
"  dump      dumpname   : dump the database as strings\n"
212
 
"  insert    key  data  : insert a record\n"
213
 
"  store     key  data  : store a record (replace)\n"
214
 
"  show      key        : show a record by key\n"
215
 
"  delete    key        : delete a record by key\n"
216
 
"  list                 : print the database hash table and freelist\n"
217
 
"  free                 : print the database freelist\n"
218
 
"  1 | first            : print the first record\n"
219
 
"  n | next             : print the next record\n"
220
 
"  q | quit             : terminate\n"
221
 
"  \\n                   : repeat 'next' command\n");
222
 
}
223
 
 
224
 
static void terror(char *why)
225
 
{
226
 
  printf("%s\n", why);
227
 
}
228
 
 
229
 
static void create_tdb(void)
230
 
{
231
 
  char *tok = get_token(1);
232
 
  if (!tok) {
233
 
    help();
234
 
    return;
235
 
  }
236
 
  if (tdb) tdb_close(tdb);
237
 
  tdb = tdb_open(tok, 0, TDB_CLEAR_IF_FIRST,
238
 
           O_RDWR | O_CREAT | O_TRUNC, 0600);
239
 
  if (!tdb) {
240
 
    printf("Could not create %s: %s\n", tok, strerror(errno));
241
 
  }
242
 
}
243
 
 
244
 
static void open_tdb(void)
245
 
{
246
 
  char *tok = get_token(1);
247
 
  if (!tok) {
248
 
    help();
249
 
    return;
250
 
  }
251
 
  if (tdb) tdb_close(tdb);
252
 
  tdb = tdb_open(tok, 0, 0, O_RDWR, 0600);
253
 
  if (!tdb) {
254
 
    printf("Could not open %s: %s\n", tok, strerror(errno));
255
 
  }
256
 
}
257
 
 
258
 
static void insert_tdb(void)
259
 
{
260
 
  char *k = get_token(1);
261
 
  char *d = get_token(0);
262
 
  TDB_DATA key, dbuf;
263
 
 
264
 
  if (!k || !d) {
265
 
    help();
266
 
    return;
267
 
  }
268
 
 
269
 
  key.dptr = k;
270
 
  key.dsize = strlen(k)+1;
271
 
  dbuf.dptr = d;
272
 
  dbuf.dsize = strlen(d)+1;
273
 
 
274
 
  if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
275
 
    terror("insert failed");
276
 
  }
277
 
}
278
 
 
279
 
static void store_tdb(void)
280
 
{
281
 
  char *k = get_token(1);
282
 
  char *d = get_token(0);
283
 
  TDB_DATA key, dbuf;
284
 
 
285
 
  if (!k || !d) {
286
 
    help();
287
 
    return;
288
 
  }
289
 
 
290
 
  key.dptr = k;
291
 
  key.dsize = strlen(k)+1;
292
 
  dbuf.dptr = d;
293
 
  dbuf.dsize = strlen(d)+1;
294
 
 
295
 
  printf("Storing key:\n");
296
 
  print_rec(tdb, key, dbuf, NULL);
297
 
 
298
 
  if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
299
 
    terror("store failed");
300
 
  }
301
 
}
302
 
 
303
 
static void show_tdb(void)
304
 
{
305
 
  char *k = get_token(1);
306
 
  TDB_DATA key, dbuf;
307
 
 
308
 
  if (!k) {
309
 
    help();
310
 
    return;
311
 
  }
312
 
 
313
 
  key.dptr = k;
314
 
  key.dsize = strlen(k)+1;
315
 
 
316
 
  dbuf = tdb_fetch(tdb, key);
317
 
  if (!dbuf.dptr) {
318
 
    terror("fetch failed");
319
 
    return;
320
 
  }
321
 
  /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
322
 
  print_rec(tdb, key, dbuf, NULL);
323
 
}
324
 
 
325
 
static void delete_tdb(void)
326
 
{
327
 
  char *k = get_token(1);
328
 
  TDB_DATA key;
329
 
 
330
 
  if (!k) {
331
 
    help();
332
 
    return;
333
 
  }
334
 
 
335
 
  key.dptr = k;
336
 
  key.dsize = strlen(k)+1;
337
 
 
338
 
  if (tdb_delete(tdb, key) != 0) {
339
 
    terror("delete failed");
340
 
  }
341
 
}
342
 
 
343
 
static int print_rec(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf, void *state)
344
 
{
345
 
  fprintf(pDumpFile,"\nkey %u bytes\n", (unsigned) key.dsize);
346
 
  print_asc((unsigned char*)key.dptr, key.dsize);
347
 
  fprintf(pDumpFile,"\ndata %u bytes\n", (unsigned) dbuf.dsize);
348
 
  print_data((unsigned char*)dbuf.dptr, dbuf.dsize);
349
 
  return 0;
350
 
}
351
 
 
352
 
static int print_key(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf, void *state)
353
 
{
354
 
  print_asc((unsigned char*)key.dptr, key.dsize);
355
 
  printf("\n");
356
 
  return 0;
357
 
}
358
 
 
359
 
static int total_bytes;
360
 
 
361
 
static int traverse_fn(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf, void *state)
362
 
{
363
 
  total_bytes += dbuf.dsize;
364
 
  return 0;
365
 
}
366
 
 
367
 
static void info_tdb(void)
368
 
{
369
 
  int count;
370
 
  total_bytes = 0;
371
 
  if ((count = tdb_traverse(tdb, traverse_fn, NULL) == -1))
372
 
    printf("Error = %s\n", tdb_errorstr(tdb));
373
 
  else
374
 
    printf("%d records totalling %d bytes\n", count, total_bytes);
375
 
}
376
 
 
377
 
static char *tdb_getline(char *prompt)
378
 
{
379
 
  static char line[1024];
380
 
  char *p;
381
 
  fputs(prompt, stdout);
382
 
  line[0] = 0;
383
 
  p = fgets(line, sizeof(line)-1, stdin);
384
 
  if (p) p = strchr(p, '\n');
385
 
  if (p) *p = 0;
386
 
  pDumpFile = stdout;
387
 
  return p?line:NULL;
388
 
}
389
 
 
390
 
static int do_delete_fn(TDB_CONTEXT *context, TDB_DATA key, TDB_DATA dbuf,
391
 
                     void *state)
392
 
{
393
 
    return tdb_delete(context, key);
394
 
}
395
 
 
396
 
static void first_record(TDB_CONTEXT *context, TDB_DATA *pkey)
397
 
{
398
 
  TDB_DATA dbuf;
399
 
  *pkey = tdb_firstkey(context);
400
 
  
401
 
  dbuf = tdb_fetch(context, *pkey);
402
 
  if (!dbuf.dptr) terror("fetch failed");
403
 
  /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
404
 
  print_rec(context, *pkey, dbuf, NULL);
405
 
}
406
 
 
407
 
static void next_record(TDB_CONTEXT *context, TDB_DATA *pkey)
408
 
{
409
 
  TDB_DATA dbuf;
410
 
  *pkey = tdb_nextkey(context, *pkey);
411
 
  
412
 
  dbuf = tdb_fetch(context, *pkey);
413
 
  if (!dbuf.dptr) 
414
 
    terror("fetch failed");
415
 
  else
416
 
    /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
417
 
    print_rec(context, *pkey, dbuf, NULL);
418
 
}
419
 
 
420
 
int main(int argc, char *argv[])
421
 
{
422
 
    int bIterate = 0, ignore;
423
 
    char *line;
424
 
    char *tok;
425
 
  TDB_DATA iterate_kbuf;
426
 
 
427
 
    if (argv[1]) {
428
 
  static char tmp[1024];
429
 
        sprintf(tmp, "open %s", argv[1]);
430
 
        tok=strtok(tmp," ");
431
 
        open_tdb();
432
 
    }
433
 
 
434
 
    while ((line = tdb_getline("tdb> "))) {
435
 
 
436
 
        /* Shell command */
437
 
        
438
 
        if (line[0] == '!') {
439
 
            ignore = system(line + 1);
440
 
            continue;
441
 
        }
442
 
        
443
 
        if ((tok = strtok(line," "))==NULL) {
444
 
           if (bIterate)
445
 
              next_record(tdb, &iterate_kbuf);
446
 
           continue;
447
 
        }
448
 
        if (strcmp(tok,"create") == 0) {
449
 
            bIterate = 0;
450
 
            create_tdb();
451
 
            continue;
452
 
        } else if (strcmp(tok,"open") == 0) {
453
 
            open_tdb();
454
 
            continue;
455
 
        } else if ((strcmp(tok, "q") == 0) ||
456
 
                   (strcmp(tok, "quit") == 0)) {
457
 
            break;
458
 
  }
459
 
            
460
 
        /* all the rest require a open database */
461
 
        if (!tdb) {
462
 
            bIterate = 0;
463
 
            terror("database not open");
464
 
            help();
465
 
            continue;
466
 
        }
467
 
            
468
 
        if (strcmp(tok,"insert") == 0) {
469
 
            bIterate = 0;
470
 
            insert_tdb();
471
 
        } else if (strcmp(tok,"store") == 0) {
472
 
            bIterate = 0;
473
 
            store_tdb();
474
 
        } else if (strcmp(tok,"show") == 0) {
475
 
            bIterate = 0;
476
 
            show_tdb();
477
 
        } else if (strcmp(tok,"erase") == 0) {
478
 
            bIterate = 0;
479
 
            tdb_traverse(tdb, do_delete_fn, NULL);
480
 
        } else if (strcmp(tok,"delete") == 0) {
481
 
            bIterate = 0;
482
 
            delete_tdb();
483
 
        } else if (strcmp(tok,"dump") == 0) {
484
 
            bIterate = 0;
485
 
      if(open_dump_file() == 0) { /* open file */
486
 
        tdb_traverse(tdb, print_rec, NULL);
487
 
        close_dump_file(); /* close file */
488
 
      }
489
 
      pDumpFile = stdout;
490
 
        } else if (strcmp(tok,"list") == 0) {
491
 
            tdb_dump_all(tdb);
492
 
        } else if (strcmp(tok, "free") == 0) {
493
 
            tdb_printfreelist(tdb);
494
 
        } else if (strcmp(tok,"info") == 0) {
495
 
            info_tdb();
496
 
        } else if ( (strcmp(tok, "1") == 0) ||
497
 
                    (strcmp(tok, "first") == 0)) {
498
 
            bIterate = 1;
499
 
            first_record(tdb, &iterate_kbuf);
500
 
        } else if ((strcmp(tok, "n") == 0) ||
501
 
                   (strcmp(tok, "next") == 0)) {
502
 
            next_record(tdb, &iterate_kbuf);
503
 
        } else if ((strcmp(tok, "keys") == 0)) {
504
 
                bIterate = 0;
505
 
                tdb_traverse(tdb, print_key, NULL);
506
 
        } else {
507
 
            help();
508
 
        }
509
 
    }
510
 
 
511
 
    if (tdb) tdb_close(tdb);
512
 
 
513
 
    return 0;
514
 
}