gwenhywfar  4.99.8beta
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  GWEN_DB_NODE *db;
29  const char *folder;
30  GWEN_MDIGEST *md;
31  GWEN_STRINGLIST *sl;
33  GWEN_BUFFER *tbuf;
34  FILE *f;
35  int rv;
36  const GWEN_ARGS args[]={
37  {
38  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
39  GWEN_ArgsType_Char, /* type */
40  "folder", /* name */
41  1, /* minnum */
42  1, /* maxnum */
43  "d", /* short option */
44  "dir", /* long option */
45  "Specify folder to hash", /* short description */
46  "Specify folder to hash" /* long description */
47  },
48  {
50  GWEN_ArgsType_Int, /* type */
51  "help", /* name */
52  0, /* minnum */
53  0, /* maxnum */
54  "h", /* short option */
55  "help", /* long option */
56  "Show this help screen", /* short description */
57  "Show this help screen" /* long description */
58  }
59  };
60 
61  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
62  rv=GWEN_Args_Check(argc, argv, 1,
64  args,
65  db);
66  if (rv==GWEN_ARGS_RESULT_ERROR) {
67  fprintf(stderr, "ERROR: Could not parse arguments\n");
68  return 1;
69  }
70  else if (rv==GWEN_ARGS_RESULT_HELP) {
71  GWEN_BUFFER *ubuf;
72 
73  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
74  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
75  fprintf(stderr, "ERROR: Could not create help string\n");
76  return 1;
77  }
78  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
79  GWEN_Buffer_free(ubuf);
80  return 0;
81  }
82 
83  folder=GWEN_DB_GetCharValue(db, "folder", 0, NULL);
84  assert(folder);
85 
86  /* hash */
89  rv=GWEN_MDigest_HashFileTree(md, folder, "checksums.rmd", sl);
90  if (rv<0) {
91  fprintf(stderr, "ERROR: Could not hash folder tree (%d)\n", rv);
92  return 2;
93  }
94 
95  /* write checksum file */
96  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
97  GWEN_Buffer_AppendString(tbuf, folder);
98  GWEN_Buffer_AppendString(tbuf, GWEN_DIR_SEPARATOR_S "checksums.rmd");
99  f=fopen(GWEN_Buffer_GetStart(tbuf), "w+");
100  if (f==NULL) {
101  fprintf(stderr, "ERROR: Could not open output file: %s\n", strerror(errno));
102  return 2;
103  }
104 
106  while(se) {
107  const char *s;
108 
110  if (s && *s)
111  fprintf(f, "%s\n", s);
113  }
114  GWEN_MDigest_free(md);
116 
117  if (fclose(f)) {
118  fprintf(stderr, "ERROR: Could not close output file: %s\n", strerror(errno));
119  return 2;
120  }
121  GWEN_Buffer_free(tbuf);
122 
123  return 0;
124 }
125 
126 
127 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:51
int hashTree(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: hashtree.c:27
void GWEN_MDigest_free(GWEN_MDIGEST *md)
Definition: mdigest.c:53
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define GWEN_DIR_SEPARATOR_S
#define NULL
Definition: binreloc.c:290
#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:38
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:352
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:366
#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:57
int GWEN_MDigest_HashFileTree(GWEN_MDIGEST *md, const char *folder, const char *ignoreFile, GWEN_STRINGLIST *sl)
Definition: mdigest.c:461
GWENHYWFAR_API GWEN_MDIGEST * GWEN_MDigest_Rmd160_new(void)
Definition: mdigestgc.c:152
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:54
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:897
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1260
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
#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:359
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:46
#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:1014
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168