gwenhywfar 5.10.1
extract.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
19
20
21
22int extractArchive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
23{
24 GWEN_DB_NODE *db;
25 const char *aname;
26 GWEN_SAR *sr;
27 int rv;
28 int verbosity;
29 const GWEN_ARGS args[]= {
30 {
32 GWEN_ArgsType_Char, /* type */
33 "archive", /* name */
34 1, /* minnum */
35 1, /* maxnum */
36 "a", /* short option */
37 "archive", /* long option */
38 "Specify the archive file name", /* short description */
39 "Specify the archive file name" /* long description */
40 },
41 {
42 0, /* flags */
43 GWEN_ArgsType_Int, /* type */
44 "verbosity", /* name */
45 0, /* minnum */
46 10, /* maxnum */
47 "v", /* short option */
48 NULL, /* long option */
49 "set verbosity", /* short description */
50 "set verbosity" /* long description */
51 },
52 {
54 GWEN_ArgsType_Int, /* type */
55 "help", /* name */
56 0, /* minnum */
57 0, /* maxnum */
58 "h", /* short option */
59 "help", /* long option */
60 "Show this help screen", /* short description */
61 "Show this help screen" /* long description */
62 }
63 };
64
65 db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
66 rv=GWEN_Args_Check(argc, argv, 1,
68 args,
69 db);
70 if (rv==GWEN_ARGS_RESULT_ERROR) {
71 fprintf(stderr, "ERROR: Could not parse arguments\n");
72 return 1;
73 }
74 else if (rv==GWEN_ARGS_RESULT_HELP) {
75 GWEN_BUFFER *ubuf;
76
77 ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
78 if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
79 fprintf(stderr, "ERROR: Could not create help string\n");
80 return 1;
81 }
82 fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
83 GWEN_Buffer_free(ubuf);
84 return 0;
85 }
86
87 aname=GWEN_DB_GetCharValue(db, "archive", 0, NULL);
88 assert(aname);
89
90 verbosity=GWEN_DB_GetIntValue(db, "verbosity", 0, 0);
91
92 sr=GWEN_Sar_new();
93 rv=GWEN_Sar_OpenArchive(sr, aname,
96 if (rv<0) {
97 fprintf(stderr, "ERROR: Error opening archive (%d)\n", rv);
98 return 2;
99 }
100 else {
101 const GWEN_SAR_FILEHEADER_LIST *fhl;
102
103 fhl=GWEN_Sar_GetHeaders(sr);
104 if (fhl) {
105 const GWEN_SAR_FILEHEADER *fh;
106
108 while (fh) {
109 const char *s;
110
112 if (s && *s) {
113 rv=GWEN_Sar_ExtractFile(sr, fh);
114 if (rv<0) {
115 DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
117 GWEN_Sar_free(sr);
118 return rv;
119 }
120 if (verbosity>0) {
121 fprintf(stdout, "extracted \"%s\"\n", s);
122 }
123 }
125 }
126 }
127
128 rv=GWEN_Sar_CloseArchive(sr, 0);
129 if (rv<0) {
130 fprintf(stderr, "ERROR: Error closing archive (%d)\n", rv);
131 return 2;
132 }
133
134 return 0;
135 }
136}
137
138
139
140
141
142
#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
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
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
int extractArchive(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: extract.c:22
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
const char * GWEN_SarFileHeader_GetPath(const GWEN_SAR_FILEHEADER *p_struct)
struct GWEN_SAR_FILEHEADER GWEN_SAR_FILEHEADER
GWEN_SAR_FILEHEADER * GWEN_SarFileHeader_List_First(const GWEN_SAR_FILEHEADER_LIST *l)
GWEN_SAR_FILEHEADER * GWEN_SarFileHeader_List_Next(const GWEN_SAR_FILEHEADER *element)
#define GWEN_LOGDOMAIN
Definition: logger.h:35
int GWEN_Sar_ExtractFile(GWEN_SAR *sr, const GWEN_SAR_FILEHEADER *fh)
Definition: sar.c:1807
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
GWEN_SAR * GWEN_Sar_new(void)
Definition: sar.c:50
const GWEN_SAR_FILEHEADER_LIST * GWEN_Sar_GetHeaders(GWEN_SAR *sr)
Definition: sar.c:1837
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
@ GWEN_SyncIo_File_CreationMode_OpenExisting
Definition: syncio_file.h:38
#define GWEN_SYNCIO_FILE_FLAGS_READ
Definition: syncio_file.h:53