gwenhywfar 5.10.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
29static 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 rv=lstat(fname, &st);
37#else
38 rv=stat(fname, &st);
39#endif
40 if (rv) {
41 DBG_ERROR(GSA_LOGDOMAIN, "stat(%s): %d (%s)",
42 fname, errno, strerror(errno));
43 fprintf(stderr, "Ignoring file \"%s\"\n", fname);
44 }
45 else {
46 /* always append this entry */
47 GWEN_StringList_AppendString(sl, fname, 0, 1);
48 if (recursive && S_ISDIR(st.st_mode)) {
49 GWEN_STRINGLIST *sll;
52 int rv;
53 char buffer[256];
54 GWEN_BUFFER *tbuf;
55 uint32_t pos;
56
57 /* add entries */
60 rv=GWEN_Directory_Open(d, fname);
61 if (rv<0) {
62 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
65 return rv;
66 }
67
68 while (0==GWEN_Directory_Read(d, buffer, sizeof(buffer))) {
69 if (strcmp(buffer, ".")!=0 &&
70 strcmp(buffer, "..")!=0)
71 GWEN_StringList_AppendString(sll, buffer, 0, 1);
72 }
73
76
77 /* recurse */
78 tbuf=GWEN_Buffer_new(0, 256, 0, 1);
79 GWEN_Buffer_AppendString(tbuf, fname);
81 pos=GWEN_Buffer_GetPos(tbuf);
83 while (se) {
84 const char *s;
85
87 if (s && *s) {
89 rv=addToList(GWEN_Buffer_GetStart(tbuf), recursive, sl);
90 if (rv<0) {
91 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
92 GWEN_Buffer_free(tbuf);
94 return rv;
95 }
96 }
97 GWEN_Buffer_Crop(tbuf, 0, pos);
99 } /* while se */
100 GWEN_Buffer_free(tbuf);
102 } /* if dir and recursive */
103 } /* if stat was ok */
104
105 return 0;
106}
107
108
109
110
111int add2Archive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
112{
113 GWEN_DB_NODE *db;
114 const char *aname;
115 GWEN_SAR *sr;
116 int rv;
117 int recursive;
118 int verbosity;
119 const GWEN_ARGS args[]= {
120 {
122 GWEN_ArgsType_Char, /* type */
123 "archive", /* name */
124 1, /* minnum */
125 1, /* maxnum */
126 "a", /* short option */
127 "archive", /* long option */
128 "Specify the archive file name", /* short description */
129 "Specify the archive file name" /* long description */
130 },
131 {
132 0, /* flags */
133 GWEN_ArgsType_Int, /* type */
134 "recursive", /* name */
135 0, /* minnum */
136 1, /* maxnum */
137 "r", /* short option */
138 "recursive", /* long option */
139 "add folders recursively", /* short description */
140 "add folders recursively" /* long description */
141 },
142 {
143 0, /* flags */
144 GWEN_ArgsType_Int, /* type */
145 "verbosity", /* name */
146 0, /* minnum */
147 10, /* maxnum */
148 "v", /* short option */
149 NULL, /* long option */
150 "set verbosity", /* short description */
151 "set verbosity" /* long description */
152 },
153 {
155 GWEN_ArgsType_Int, /* type */
156 "help", /* name */
157 0, /* minnum */
158 0, /* maxnum */
159 "h", /* short option */
160 "help", /* long option */
161 "Show this help screen", /* short description */
162 "Show this help screen" /* long description */
163 }
164 };
165
166 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
167 rv=GWEN_Args_Check(argc, argv, 1,
169 args,
170 db);
171 if (rv==GWEN_ARGS_RESULT_ERROR) {
172 fprintf(stderr, "ERROR: Could not parse arguments\n");
173 return 1;
174 }
175 else if (rv==GWEN_ARGS_RESULT_HELP) {
176 GWEN_BUFFER *ubuf;
177
178 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
179 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
180 fprintf(stderr, "ERROR: Could not create help string\n");
181 return 1;
182 }
183 fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
184 GWEN_Buffer_free(ubuf);
185 return 0;
186 }
187
188 aname=GWEN_DB_GetCharValue(db, "archive", 0, NULL);
189 assert(aname);
190
191 recursive=GWEN_DB_GetIntValue(db, "recursive", 0, 0);
192 verbosity=GWEN_DB_GetIntValue(db, "verbosity", 0, 0);
193
194 sr=GWEN_Sar_new();
195 rv=GWEN_Sar_OpenArchive(sr, aname,
198 if (rv<0) {
199 fprintf(stderr, "ERROR: Error opening archive (%d)\n", rv);
200 return 2;
201 }
202 else {
203 int i;
204 GWEN_STRINGLIST *sl;
206
208 for (i=0; ; i++) {
209 const char *fname;
210
211 fname=GWEN_DB_GetCharValue(db, "params", i, 0);
212 if (fname && *fname) {
213 rv=addToList(fname, recursive, sl);
214 if (rv<0) {
215 fprintf(stderr, "ERROR: Error adding entry \"%s\" to archive \"%s\" (%d)\n",
216 fname, aname, rv);
218 return 2;
219 }
220 }
221 else
222 break;
223 }
224
226 while (se) {
227 const char *s;
228
230 if (s && *s) {
231 rv=GWEN_Sar_AddFile(sr, s);
232 if (rv<0) {
233 fprintf(stderr, "ERROR: Error adding file \"%s\" to archive \"%s\" (%d)\n",
234 s, aname, rv);
236 GWEN_Sar_free(sr);
237 return 2;
238 }
239 if (verbosity>0) {
240 fprintf(stdout, "added \"%s\"\n", s);
241 }
242 }
244 } /* while se */
245
247
248 rv=GWEN_Sar_CloseArchive(sr, 0);
249 if (rv<0) {
250 fprintf(stderr, "ERROR: Error closing archive (%d)\n", rv);
251 return 2;
252 }
253
254 return 0;
255 }
256}
257
258
259
260
261
262
static int addToList(const char *fname, int recursive, GWEN_STRINGLIST *sl)
Definition: add.c:29
int add2Archive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: add.c:111
#define NULL
Definition: binreloc.c:300
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition: buffer.c:253
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
int GWEN_Buffer_Crop(GWEN_BUFFER *bf, uint32_t pos, uint32_t l)
Definition: buffer.c:947
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
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition: db.c:1163
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
GWENHYWFAR_API int GWEN_Directory_Read(GWEN_DIRECTORY *d, char *buffer, unsigned int len)
struct GWEN_DIRECTORY GWEN_DIRECTORY
Definition: directory.h:41
GWENHYWFAR_API int GWEN_Directory_Open(GWEN_DIRECTORY *d, const char *n)
GWENHYWFAR_API GWEN_DIRECTORY * GWEN_Directory_new(void)
GWENHYWFAR_API void GWEN_Directory_free(GWEN_DIRECTORY *d)
GWENHYWFAR_API int GWEN_Directory_Close(GWEN_DIRECTORY *d)
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
#define GSA_LOGDOMAIN
Definition: gsa/globals.h:27
#define GWEN_DIR_SEPARATOR_S
#define GWEN_LOGDOMAIN
Definition: logger.h:35
int GWEN_Sar_OpenArchive(GWEN_SAR *sr, const char *aname, GWEN_SYNCIO_FILE_CREATIONMODE cm, uint32_t acc)
Definition: sar.c:134
int GWEN_Sar_CloseArchive(GWEN_SAR *sr, int abandon)
Definition: sar.c:181
void GWEN_Sar_free(GWEN_SAR *sr)
Definition: sar.c:73
int GWEN_Sar_AddFile(GWEN_SAR *sr, const char *fname)
Definition: sar.c:716
GWEN_SAR * GWEN_Sar_new(void)
Definition: sar.c:50
struct GWEN_SAR GWEN_SAR
Definition: sar.h:37
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
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
Definition: src/base/args.h:54
#define GWEN_ARGS_FLAGS_HELP
Definition: src/base/args.h:52
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
@ GWEN_ArgsOutType_Txt
Definition: src/base/args.h:68
@ GWEN_ArgsType_Int
Definition: src/base/args.h:63
@ GWEN_ArgsType_Char
Definition: src/base/args.h:62
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
int GWEN_StringList_AppendString(GWEN_STRINGLIST *sl, const char *s, int take, int checkDouble)
Definition: stringlist.c:245
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:390
GWEN_STRINGLIST * GWEN_StringList_new(void)
Definition: stringlist.c:50
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
@ GWEN_SyncIo_File_CreationMode_OpenExisting
Definition: syncio_file.h:38
#define GWEN_SYNCIO_FILE_FLAGS_READ
Definition: syncio_file.h:53
#define GWEN_SYNCIO_FILE_FLAGS_WRITE
Definition: syncio_file.h:54