gwenhywfar  4.99.8beta
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  struct stat st;
31  int rv;
32 
33  /* stat file to be added */
34 #if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
35  rv=lstat(fname, &st);
36 #else
37  rv=stat(fname, &st);
38 #endif
39  if (rv) {
40  DBG_ERROR(GSA_LOGDOMAIN, "stat(%s): %d (%s)",
41  fname, errno, strerror(errno));
42  fprintf(stderr, "Ignoring file \"%s\"\n", fname);
43  }
44  else {
45  /* always append this entry */
46  GWEN_StringList_AppendString(sl, fname, 0, 1);
47  if (recursive && S_ISDIR(st.st_mode)) {
48  GWEN_STRINGLIST *sll;
50  GWEN_DIRECTORY *d;
51  int rv;
52  char buffer[256];
53  GWEN_BUFFER *tbuf;
54  uint32_t pos;
55 
56  /* add entries */
57  sll=GWEN_StringList_new();
59  rv=GWEN_Directory_Open(d, fname);
60  if (rv<0) {
61  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
64  return rv;
65  }
66 
67  while(0==GWEN_Directory_Read(d, buffer, sizeof(buffer))) {
68  if (strcmp(buffer, ".")!=0 &&
69  strcmp(buffer, "..")!=0)
70  GWEN_StringList_AppendString(sll, buffer, 0, 1);
71  }
72 
75 
76  /* recurse */
77  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
78  GWEN_Buffer_AppendString(tbuf, fname);
80  pos=GWEN_Buffer_GetPos(tbuf);
82  while(se) {
83  const char *s;
84 
86  if (s && *s) {
87  GWEN_Buffer_AppendString(tbuf, s);
88  rv=addToList(GWEN_Buffer_GetStart(tbuf), recursive, sl);
89  if (rv<0) {
90  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
91  GWEN_Buffer_free(tbuf);
93  return rv;
94  }
95  }
96  GWEN_Buffer_Crop(tbuf, 0, pos);
98  } /* while se */
99  GWEN_Buffer_free(tbuf);
101  } /* if dir and recursive */
102  } /* if stat was ok */
103 
104  return 0;
105 }
106 
107 
108 
109 
110 int add2Archive(GWEN_DB_NODE *dbArgs, int argc, char **argv) {
111  GWEN_DB_NODE *db;
112  const char *aname;
113  GWEN_SAR *sr;
114  int rv;
115  int recursive;
116  int verbosity;
117  const GWEN_ARGS args[]={
118  {
119  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
120  GWEN_ArgsType_Char, /* type */
121  "archive", /* name */
122  1, /* minnum */
123  1, /* maxnum */
124  "a", /* short option */
125  "archive", /* long option */
126  "Specify the archive file name", /* short description */
127  "Specify the archive file name" /* long description */
128  },
129  {
130  0, /* flags */
131  GWEN_ArgsType_Int, /* type */
132  "recursive", /* name */
133  0, /* minnum */
134  1, /* maxnum */
135  "r", /* short option */
136  "recursive", /* long option */
137  "add folders recursively", /* short description */
138  "add folders recursively" /* long description */
139  },
140  {
141  0, /* flags */
142  GWEN_ArgsType_Int, /* type */
143  "verbosity", /* name */
144  0, /* minnum */
145  10, /* maxnum */
146  "v", /* short option */
147  NULL, /* long option */
148  "set verbosity", /* short description */
149  "set verbosity" /* long description */
150  },
151  {
153  GWEN_ArgsType_Int, /* type */
154  "help", /* name */
155  0, /* minnum */
156  0, /* maxnum */
157  "h", /* short option */
158  "help", /* long option */
159  "Show this help screen", /* short description */
160  "Show this help screen" /* long description */
161  }
162  };
163 
164  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
165  rv=GWEN_Args_Check(argc, argv, 1,
167  args,
168  db);
169  if (rv==GWEN_ARGS_RESULT_ERROR) {
170  fprintf(stderr, "ERROR: Could not parse arguments\n");
171  return 1;
172  }
173  else if (rv==GWEN_ARGS_RESULT_HELP) {
174  GWEN_BUFFER *ubuf;
175 
176  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
177  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
178  fprintf(stderr, "ERROR: Could not create help string\n");
179  return 1;
180  }
181  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
182  GWEN_Buffer_free(ubuf);
183  return 0;
184  }
185 
186  aname=GWEN_DB_GetCharValue(db, "archive", 0, NULL);
187  assert(aname);
188 
189  recursive=GWEN_DB_GetIntValue(db, "recursive", 0, 0);
190  verbosity=GWEN_DB_GetIntValue(db, "verbosity", 0, 0);
191 
192  sr=GWEN_Sar_new();
193  rv=GWEN_Sar_OpenArchive(sr, aname,
196  if (rv<0) {
197  fprintf(stderr, "ERROR: Error opening archive (%d)\n", rv);
198  return 2;
199  }
200  else {
201  int i;
202  GWEN_STRINGLIST *sl;
204 
205  sl=GWEN_StringList_new();
206  for (i=0; ; i++) {
207  const char *fname;
208 
209  fname=GWEN_DB_GetCharValue(db, "params", i, 0);
210  if (fname && *fname) {
211  rv=addToList(fname, recursive, sl);
212  if (rv<0) {
213  fprintf(stderr, "ERROR: Error adding entry \"%s\" to archive \"%s\" (%d)\n",
214  fname, aname, rv);
216  return 2;
217  }
218  }
219  else
220  break;
221  }
222 
224  while(se) {
225  const char *s;
226 
228  if (s && *s) {
229  rv=GWEN_Sar_AddFile(sr, s);
230  if (rv<0) {
231  fprintf(stderr, "ERROR: Error adding file \"%s\" to archive \"%s\" (%d)\n",
232  s, aname, rv);
233  GWEN_Sar_CloseArchive(sr, 1);
234  GWEN_Sar_free(sr);
235  return 2;
236  }
237  if (verbosity>0) {
238  fprintf(stdout, "added \"%s\"\n", s);
239  }
240  }
242  } /* while se */
243 
245 
246  rv=GWEN_Sar_CloseArchive(sr, 0);
247  if (rv<0) {
248  fprintf(stderr, "ERROR: Error closing archive (%d)\n", rv);
249  return 2;
250  }
251 
252  return 0;
253  }
254 }
255 
256 
257 
258 
259 
260 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
GWENHYWFAR_API void GWEN_Directory_free(GWEN_DIRECTORY *d)
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:51
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:704
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:176
#define NULL
Definition: binreloc.c:290
GWENHYWFAR_API int GWEN_Directory_Close(GWEN_DIRECTORY *d)
#define GWEN_LOGDOMAIN
Definition: logger.h:35
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition: buffer.c:239
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: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
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:57
int GWEN_StringList_AppendString(GWEN_STRINGLIST *sl, const char *s, int take, int checkDouble)
Definition: stringlist.c:230
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:54
#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
void GWEN_Sar_free(GWEN_SAR *sr)
Definition: sar.c:71
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_Buffer_Crop(GWEN_BUFFER *bf, uint32_t pos, uint32_t l)
Definition: buffer.c:973
int add2Archive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: add.c:110
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
int GWEN_Sar_OpenArchive(GWEN_SAR *sr, const char *aname, GWEN_SYNCIO_FILE_CREATIONMODE cm, uint32_t acc)
Definition: sar.c:130
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
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition: db.c:1048
GWENHYWFAR_API int GWEN_Directory_Read(GWEN_DIRECTORY *d, char *buffer, unsigned int len)
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:46
#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:1014
#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)