gwenhywfar  5.14.1
add.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Sat Jun 25 2011
3  copyright : (C) 2011 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/sar.h>
18 #include <gwenhywfar/directory.h>
19 
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 
24 #include <errno.h>
25 #include <string.h>
26 
27 
28 
29 static int addToList(const char *fname, int recursive, GWEN_STRINGLIST *sl)
30 {
31  struct stat st;
32  int rv;
33 
34  /* stat file to be added */
35 //#if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
36 #if ((_BSD_SOURCE || _XOPEN_SOURCE >= 500 || (_XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED) || _POSIX_C_SOURCE >= 200112L) && !defined(__MINGW32__)) || defined(OS_DARWIN)
37  rv=lstat(fname, &st);
38 #else
39  rv=stat(fname, &st);
40 #endif
41  if (rv) {
42  DBG_ERROR(GSA_LOGDOMAIN, "stat(%s): %d (%s)",
43  fname, errno, strerror(errno));
44  fprintf(stderr, "Ignoring file \"%s\"\n", fname);
45  }
46  else {
47  /* always append this entry */
48  GWEN_StringList_AppendString(sl, fname, 0, 1);
49  if (recursive && S_ISDIR(st.st_mode)) {
50  GWEN_STRINGLIST *sll;
52  GWEN_DIRECTORY *d;
53  int rv;
54  char buffer[256];
55  GWEN_BUFFER *tbuf;
56  uint32_t pos;
57 
58  /* add entries */
59  sll=GWEN_StringList_new();
61  rv=GWEN_Directory_Open(d, fname);
62  if (rv<0) {
63  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
66  return rv;
67  }
68 
69  while (0==GWEN_Directory_Read(d, buffer, sizeof(buffer))) {
70  if (strcmp(buffer, ".")!=0 &&
71  strcmp(buffer, "..")!=0)
72  GWEN_StringList_AppendString(sll, buffer, 0, 1);
73  }
74 
77 
78  /* recurse */
79  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
80  GWEN_Buffer_AppendString(tbuf, fname);
82  pos=GWEN_Buffer_GetPos(tbuf);
84  while (se) {
85  const char *s;
86 
88  if (s && *s) {
89  GWEN_Buffer_AppendString(tbuf, s);
90  rv=addToList(GWEN_Buffer_GetStart(tbuf), recursive, sl);
91  if (rv<0) {
92  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
93  GWEN_Buffer_free(tbuf);
95  return rv;
96  }
97  }
98  GWEN_Buffer_Crop(tbuf, 0, pos);
100  } /* while se */
101  GWEN_Buffer_free(tbuf);
103  } /* if dir and recursive */
104  } /* if stat was ok */
105 
106  return 0;
107 }
108 
109 
110 
111 
112 int add2Archive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
113 {
114  GWEN_DB_NODE *db;
115  const char *aname;
116  GWEN_SAR *sr;
117  int rv;
118  int recursive;
119  int verbosity;
120  const GWEN_ARGS args[]= {
121  {
122  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
123  GWEN_ArgsType_Char, /* type */
124  "archive", /* name */
125  1, /* minnum */
126  1, /* maxnum */
127  "a", /* short option */
128  "archive", /* long option */
129  "Specify the archive file name", /* short description */
130  "Specify the archive file name" /* long description */
131  },
132  {
133  0, /* flags */
134  GWEN_ArgsType_Int, /* type */
135  "recursive", /* name */
136  0, /* minnum */
137  1, /* maxnum */
138  "r", /* short option */
139  "recursive", /* long option */
140  "add folders recursively", /* short description */
141  "add folders recursively" /* long description */
142  },
143  {
144  0, /* flags */
145  GWEN_ArgsType_Int, /* type */
146  "verbosity", /* name */
147  0, /* minnum */
148  10, /* maxnum */
149  "v", /* short option */
150  NULL, /* long option */
151  "set verbosity", /* short description */
152  "set verbosity" /* long description */
153  },
154  {
156  GWEN_ArgsType_Int, /* type */
157  "help", /* name */
158  0, /* minnum */
159  0, /* maxnum */
160  "h", /* short option */
161  "help", /* long option */
162  "Show this help screen", /* short description */
163  "Show this help screen" /* long description */
164  }
165  };
166 
167  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
168  rv=GWEN_Args_Check(argc, argv, 1,
170  args,
171  db);
172  if (rv==GWEN_ARGS_RESULT_ERROR) {
173  fprintf(stderr, "ERROR: Could not parse arguments\n");
174  return 1;
175  }
176  else if (rv==GWEN_ARGS_RESULT_HELP) {
177  GWEN_BUFFER *ubuf;
178 
179  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
180  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
181  fprintf(stderr, "ERROR: Could not create help string\n");
182  return 1;
183  }
184  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
185  GWEN_Buffer_free(ubuf);
186  return 0;
187  }
188 
189  aname=GWEN_DB_GetCharValue(db, "archive", 0, NULL);
190  assert(aname);
191 
192  recursive=GWEN_DB_GetIntValue(db, "recursive", 0, 0);
193  verbosity=GWEN_DB_GetIntValue(db, "verbosity", 0, 0);
194 
195  sr=GWEN_Sar_new();
196  rv=GWEN_Sar_OpenArchive(sr, aname,
199  if (rv<0) {
200  fprintf(stderr, "ERROR: Error opening archive (%d)\n", rv);
201  return 2;
202  }
203  else {
204  int i;
205  GWEN_STRINGLIST *sl;
207 
208  sl=GWEN_StringList_new();
209  for (i=0; ; i++) {
210  const char *fname;
211 
212  fname=GWEN_DB_GetCharValue(db, "params", i, 0);
213  if (fname && *fname) {
214  rv=addToList(fname, recursive, sl);
215  if (rv<0) {
216  fprintf(stderr, "ERROR: Error adding entry \"%s\" to archive \"%s\" (%d)\n",
217  fname, aname, rv);
219  return 2;
220  }
221  }
222  else
223  break;
224  }
225 
227  while (se) {
228  const char *s;
229 
231  if (s && *s) {
232  rv=GWEN_Sar_AddFile(sr, s);
233  if (rv<0) {
234  fprintf(stderr, "ERROR: Error adding file \"%s\" to archive \"%s\" (%d)\n",
235  s, aname, rv);
236  GWEN_Sar_CloseArchive(sr, 1);
237  GWEN_Sar_free(sr);
238  return 2;
239  }
240  if (verbosity>0) {
241  fprintf(stdout, "added \"%s\"\n", s);
242  }
243  }
245  } /* while se */
246 
248 
249  rv=GWEN_Sar_CloseArchive(sr, 0);
250  if (rv<0) {
251  fprintf(stderr, "ERROR: Error closing archive (%d)\n", rv);
252  return 2;
253  }
254 
255  return 0;
256  }
257 }
258 
259 
260 
261 
262 
263 
#define DBG_ERROR(dbg_logger, format,...)
Definition: debug.h:97
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
GWENHYWFAR_API void GWEN_Directory_free(GWEN_DIRECTORY *d)
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define GWEN_SYNCIO_FILE_FLAGS_WRITE
Definition: syncio_file.h:54
#define GWEN_DIR_SEPARATOR_S
#define GWEN_SYNCIO_FILE_FLAGS_READ
Definition: syncio_file.h:53
GWEN_SAR * GWEN_Sar_new(void)
Definition: sar.c:50
int GWEN_Sar_AddFile(GWEN_SAR *sr, const char *fname)
Definition: sar.c:716
static int addToList(const char *fname, int recursive, GWEN_STRINGLIST *sl)
Definition: add.c:29
int GWEN_Sar_CloseArchive(GWEN_SAR *sr, int abandon)
Definition: sar.c:181
#define NULL
Definition: binreloc.c:300
GWENHYWFAR_API int GWEN_Directory_Close(GWEN_DIRECTORY *d)
#define GWEN_LOGDOMAIN
Definition: logger.h:32
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition: buffer.c:253
GWENHYWFAR_API GWEN_DIRECTORY * GWEN_Directory_new(void)
#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
struct GWEN_DIRECTORY GWEN_DIRECTORY
Definition: directory.h:41
#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_StringList_AppendString(GWEN_STRINGLIST *sl, const char *s, int take, int checkDouble)
Definition: stringlist.c:245
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
#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
void GWEN_Sar_free(GWEN_SAR *sr)
Definition: sar.c:73
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_Buffer_Crop(GWEN_BUFFER *bf, uint32_t pos, uint32_t l)
Definition: buffer.c:950
int add2Archive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: add.c:112
int GWEN_Sar_OpenArchive(GWEN_SAR *sr, const char *aname, GWEN_SYNCIO_FILE_CREATIONMODE cm, uint32_t acc)
Definition: sar.c:134
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
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition: db.c:1163
#define DBG_INFO(dbg_logger, format,...)
Definition: debug.h:181
GWENHYWFAR_API int GWEN_Directory_Read(GWEN_DIRECTORY *d, char *buffer, unsigned int len)
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:50
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GSA_LOGDOMAIN
Definition: gsa/globals.h:27
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:992
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
struct GWEN_SAR GWEN_SAR
Definition: sar.h:37
GWENHYWFAR_API int GWEN_Directory_Open(GWEN_DIRECTORY *d, const char *n)