gwenhywfar  5.10.1
hashtree.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Tue Jun 01 2010
3  copyright : (C) 2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #include "globals.h"
15 
16 #include <gwenhywfar/debug.h>
17 #include <gwenhywfar/ct.h>
18 #include <gwenhywfar/ctplugin.h>
19 #include <gwenhywfar/text.h>
20 #include <gwenhywfar/mdigest.h>
21 
22 
23 
24 
25 
26 
27 int hashTree(GWEN_DB_NODE *dbArgs, int argc, char **argv)
28 {
29  GWEN_DB_NODE *db;
30  const char *folder;
31  GWEN_MDIGEST *md;
32  GWEN_STRINGLIST *sl;
34  GWEN_BUFFER *tbuf;
35  FILE *f;
36  int rv;
37  const GWEN_ARGS args[]= {
38  {
39  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
40  GWEN_ArgsType_Char, /* type */
41  "folder", /* name */
42  1, /* minnum */
43  1, /* maxnum */
44  "d", /* short option */
45  "dir", /* long option */
46  "Specify folder to hash", /* short description */
47  "Specify folder to hash" /* long description */
48  },
49  {
51  GWEN_ArgsType_Int, /* type */
52  "help", /* name */
53  0, /* minnum */
54  0, /* maxnum */
55  "h", /* short option */
56  "help", /* long option */
57  "Show this help screen", /* short description */
58  "Show this help screen" /* long description */
59  }
60  };
61 
62  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
63  rv=GWEN_Args_Check(argc, argv, 1,
65  args,
66  db);
67  if (rv==GWEN_ARGS_RESULT_ERROR) {
68  fprintf(stderr, "ERROR: Could not parse arguments\n");
69  return 1;
70  }
71  else if (rv==GWEN_ARGS_RESULT_HELP) {
72  GWEN_BUFFER *ubuf;
73 
74  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
75  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
76  fprintf(stderr, "ERROR: Could not create help string\n");
77  return 1;
78  }
79  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
80  GWEN_Buffer_free(ubuf);
81  return 0;
82  }
83 
84  folder=GWEN_DB_GetCharValue(db, "folder", 0, NULL);
85  assert(folder);
86 
87  /* hash */
90  rv=GWEN_MDigest_HashFileTree(md, folder, "checksums.rmd", sl);
91  if (rv<0) {
92  fprintf(stderr, "ERROR: Could not hash folder tree (%d)\n", rv);
93  return 2;
94  }
95 
96  /* write checksum file */
97  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
98  GWEN_Buffer_AppendString(tbuf, folder);
99  GWEN_Buffer_AppendString(tbuf, GWEN_DIR_SEPARATOR_S "checksums.rmd");
100  f=fopen(GWEN_Buffer_GetStart(tbuf), "w+");
101  if (f==NULL) {
102  fprintf(stderr, "ERROR: Could not open output file: %s\n", strerror(errno));
103  return 2;
104  }
105 
107  while (se) {
108  const char *s;
109 
111  if (s && *s)
112  fprintf(f, "%s\n", s);
114  }
115  GWEN_MDigest_free(md);
117 
118  if (fclose(f)) {
119  fprintf(stderr, "ERROR: Could not close output file: %s\n", strerror(errno));
120  return 2;
121  }
122  GWEN_Buffer_free(tbuf);
123 
124  return 0;
125 }
126 
127 
128 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
int hashTree(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: hashtree.c:27
void GWEN_MDigest_free(GWEN_MDIGEST *md)
Definition: mdigest.c:54
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define GWEN_DIR_SEPARATOR_S
#define NULL
Definition: binreloc.c:300
#define GWEN_ARGS_FLAGS_HELP
Definition: src/base/args.h:52
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:390
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
int GWEN_MDigest_HashFileTree(GWEN_MDIGEST *md, const char *folder, const char *ignoreFile, GWEN_STRINGLIST *sl)
Definition: mdigest.c:525
GWENHYWFAR_API GWEN_MDIGEST * GWEN_MDigest_Rmd160_new(void)
Definition: mdigestgc.c:158
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
struct GWEN_MDIGEST GWEN_MDIGEST
Definition: mdigest.h:25
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
Definition: src/base/args.h:54
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition: db.c:971
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1381
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
int GWEN_Args_Check(int argc, char **argv, int startAt, uint32_t mode, const GWEN_ARGS *args, GWEN_DB_NODE *db)
Definition: src/base/args.c:45
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:50
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168