gwenhywfar  5.14.1
buildctx.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Feb 08 2021
3  copyright : (C) 2021 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 #define DISABLE_DEBUGLOG
15 
16 
19 #include "gwenbuild/buildctx/buildctx_p.h"
20 
21 #include <gwenhywfar/debug.h>
22 #include <gwenhywfar/text.h>
23 #include <gwenhywfar/directory.h>
24 #include <gwenhywfar/gwentime.h>
25 
26 #include <unistd.h>
27 #include <ctype.h>
28 
29 
30 static int _prepareBaseLogPath(GWB_BUILD_CONTEXT *bctx);
31 
32 
33 
35 {
36  GWB_BUILD_CONTEXT *bctx;
37 
39  bctx->commandList=GWB_BuildCmd_List2_new();
40  bctx->fileList=GWB_File_List2_new();
41 
42  return bctx;
43 }
44 
45 
46 
48 {
49  if (bctx) {
50  GWB_BuildCmd_List2_free(bctx->waitingQueue);
51  GWB_BuildCmd_List2_free(bctx->runningQueue);
52  GWB_BuildCmd_List2_free(bctx->finishedQueue);
53 
54  GWB_BuildCmd_List2_FreeAll(bctx->commandList);
55  GWB_File_List2_FreeAll(bctx->fileList);
56 
57  free(bctx->initialSourceDir);
58 
59  GWEN_FREE_OBJECT(bctx);
60  }
61 }
62 
63 
64 
66 {
67  return bctx->logPath;
68 }
69 
70 
71 
72 void GWB_BuildCtx_SetLogPath(GWB_BUILD_CONTEXT *bctx, const char *s)
73 {
74  free(bctx->logPath);
75  bctx->logPath=s?strdup(s):NULL;
76 }
77 
78 
79 
81 {
82  return bctx->initialSourceDir;
83 }
84 
85 
86 
88 {
89  free(bctx->initialSourceDir);
90  bctx->initialSourceDir=s?strdup(s):NULL;
91 }
92 
93 
94 
95 GWB_BUILD_CMD_LIST2 *GWB_BuildCtx_GetCommandList(const GWB_BUILD_CONTEXT *bctx)
96 {
97  return bctx->commandList;
98 }
99 
100 
101 
103 {
104  GWB_BuildCmd_List2_PushBack(bctx->commandList, cmd);
105 }
106 
107 
108 
109 GWB_FILE_LIST2 *GWB_BuildCtx_GetFileList(const GWB_BUILD_CONTEXT *bctx)
110 {
111  return bctx->fileList;
112 }
113 
114 
115 
117 {
118  GWB_File_SetId(file, ++(bctx->lastFileId));
119  DBG_INFO(NULL, "Adding file%s/%s", GWB_File_GetFolder(file), GWB_File_GetName(file));
120  GWB_File_List2_PushBack(bctx->fileList, file);
121 }
122 
123 
124 
125 GWB_FILE *GWB_BuildCtx_GetFileByPathAndName(const GWB_BUILD_CONTEXT *bctx, const char *folder, const char *fname)
126 {
127  return GWB_File_List2_GetFileByPathAndName(bctx->fileList, folder, fname);
128 }
129 
130 
131 
133 {
134  GWB_FILE *storedFile;
135 
137  if (storedFile)
138  GWB_BuildCmd_AddInFile(bcmd, storedFile);
139  else {
140  GWB_FILE *fileCopy;
141 
142  fileCopy=GWB_File_dup(file);
143  GWB_BuildCtx_AddFile(bctx, fileCopy);
144  GWB_BuildCmd_AddInFile(bcmd, fileCopy);
145  }
146 }
147 
148 
149 
150 void GWB_BuildCtx_AddInFilesToCtxAndCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *bcmd, GWB_FILE_LIST2 *fileList)
151 {
152  GWB_FILE_LIST2_ITERATOR *it;
153 
154  it=GWB_File_List2_First(fileList);
155  if (it) {
156  GWB_FILE *file;
157 
158  file=GWB_File_List2Iterator_Data(it);
159  while(file) {
160  GWB_BuildCtx_AddInFileToCtxAndCmd(bctx, bcmd, file);
161  file=GWB_File_List2Iterator_Next(it);
162  }
163  GWB_File_List2Iterator_free(it);
164  }
165 }
166 
167 
168 
170 {
171  GWB_FILE *storedFile;
172 
174  if (storedFile)
175  GWB_BuildCmd_AddOutFile(bcmd, storedFile);
176  else {
177  GWB_FILE *fileCopy;
178 
179  fileCopy=GWB_File_dup(file);
180  GWB_BuildCtx_AddFile(bctx, fileCopy);
181  GWB_BuildCmd_AddOutFile(bcmd, fileCopy);
182  }
183 }
184 
185 
186 
187 void GWB_BuildCtx_AddOutFilesToCtxAndCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *bcmd, GWB_FILE_LIST2 *fileList)
188 {
189  GWB_FILE_LIST2_ITERATOR *it;
190 
191  it=GWB_File_List2_First(fileList);
192  if (it) {
193  GWB_FILE *file;
194 
195  file=GWB_File_List2Iterator_Data(it);
196  while(file) {
197  GWB_BuildCtx_AddOutFileToCtxAndCmd(bctx, bcmd, file);
198  file=GWB_File_List2Iterator_Next(it);
199  }
200  GWB_File_List2Iterator_free(it);
201  }
202 }
203 
204 
205 
206 void GWB_BuildCtx_Dump(const GWB_BUILD_CONTEXT *bctx, int indent)
207 {
208  int i;
209 
210  for(i=0; i<indent; i++)
211  fprintf(stderr, " ");
212  fprintf(stderr, "BuildCtx:\n");
213 
214  GWBUILD_Debug_PrintBuildCmdList2("commandList", bctx->commandList, indent+2);
215  GWBUILD_Debug_PrintFileList2("fileList", bctx->fileList, indent+2);
216 }
217 
218 
219 
221 {
222  GWEN_BUFFER *dbuf;
223  int rv;
224 
225  if (bctx->logPath==NULL) {
226  rv=_prepareBaseLogPath(bctx);
227  if (rv<0) {
228  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
229  return rv;
230  }
231  }
232 
233  dbuf=GWEN_Buffer_new(0, 256, 0, 1);
234  GWEN_Buffer_AppendString(dbuf, bctx->logPath);
235 
236 #if 0
238  if (rv<0) {
239  DBG_ERROR(GWEN_LOGDOMAIN, "Error opening/creating folder \"%s\": %d", GWEN_Buffer_GetStart(dbuf), rv);
240  GWEN_Buffer_free(dbuf);
241  return rv;
242  }
243 #endif
244  /*GWEN_Buffer_AppendString(dbuf, GWEN_DIR_SEPARATOR_S);*/
245  GWEN_Buffer_AppendString(dbuf, "-");
246  GWEN_Buffer_AppendArgs(dbuf, "%05d", ++(bctx->lastLogfileId));
247  GWEN_Buffer_AppendString(dbuf, ".log");
248 
250  GWEN_Buffer_free(dbuf);
251 
252  return 0;
253 }
254 
255 
256 
258 {
259  GWEN_BUFFER *dbuf;
260  GWEN_TIME *ti;
261  int rv;
262 
263  dbuf=GWEN_Buffer_new(0, 256, 0, 1);
264  ti=GWEN_CurrentTime();
265 
266  GWEN_Buffer_AppendString(dbuf, ".logs");
268  if (rv<0) {
269  DBG_ERROR(GWEN_LOGDOMAIN, "Error getting path \"%s\": %d", GWEN_Buffer_GetStart(dbuf), rv);
270  GWEN_Time_free(ti);
271  GWEN_Buffer_free(dbuf);
272  return rv;
273  }
275 
276  rv=GWEN_Time_toString(ti, "YYYYMMDD-hhmmss", dbuf);
277  if (rv<0) {
278  DBG_ERROR(GWEN_LOGDOMAIN, "Error adding time string to buffer: %d", rv);
279  GWEN_Time_free(ti);
280  GWEN_Buffer_free(dbuf);
281  return rv;
282  }
283 
285  GWEN_Time_free(ti);
286  GWEN_Buffer_free(dbuf);
287 
288  return 0;
289 }
290 
291 
GWB_BUILD_CONTEXT * GWB_BuildCtx_new()
Definition: buildctx.c:34
struct GWEN_TIME GWEN_TIME
Definition: gwentime.h:43
#define DBG_ERROR(dbg_logger, format,...)
Definition: debug.h:97
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
const char * GWB_BuildCtx_GetLogPath(const GWB_BUILD_CONTEXT *bctx)
Definition: buildctx.c:65
static int _prepareBaseLogPath(GWB_BUILD_CONTEXT *bctx)
Definition: buildctx.c:257
void GWB_BuildCtx_SetLogPath(GWB_BUILD_CONTEXT *bctx, const char *s)
Definition: buildctx.c:72
int GWB_BuildCtx_CreateAndSetLogFilenameForSubCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_SUBCMD *cmd)
Definition: buildctx.c:220
#define GWEN_DIR_SEPARATOR_S
struct GWB_FILE GWB_FILE
Definition: file.h:18
struct GWB_BUILD_CONTEXT GWB_BUILD_CONTEXT
Definition: buildctx.h:16
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define NULL
Definition: binreloc.c:300
GWB_FILE * GWB_File_dup(const GWB_FILE *oldFile)
Definition: file.c:50
void GWB_BuildCtx_free(GWB_BUILD_CONTEXT *bctx)
Definition: buildctx.c:47
void GWB_File_SetId(GWB_FILE *f, uint32_t i)
Definition: file.c:92
#define GWEN_LOGDOMAIN
Definition: logger.h:32
void GWBUILD_Debug_PrintBuildCmdList2(const char *sName, const GWB_BUILD_CMD_LIST2 *buildCmdList2, int indent)
Definition: gwenbuild.c:442
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
GWB_FILE * GWB_BuildCtx_GetFileByPathAndName(const GWB_BUILD_CONTEXT *bctx, const char *folder, const char *fname)
Definition: buildctx.c:125
const char * GWB_File_GetFolder(const GWB_FILE *f)
Definition: file.c:127
const char * GWB_BuildCtx_GetInitialSourceDir(const GWB_BUILD_CONTEXT *bctx)
Definition: buildctx.c:80
const char * GWB_File_GetName(const GWB_FILE *f)
Definition: file.c:146
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
GWENHYWFAR_API int GWEN_Time_toString(const GWEN_TIME *t, const char *tmpl, GWEN_BUFFER *buf)
Definition: gwentime_all.c:830
GWB_FILE_LIST2 * GWB_BuildCtx_GetFileList(const GWB_BUILD_CONTEXT *bctx)
Definition: buildctx.c:109
void GWB_BuildCmd_AddInFile(GWB_BUILD_CMD *bcmd, GWB_FILE *file)
Definition: buildcmd.c:262
int GWEN_Buffer_AppendArgs(GWEN_BUFFER *bf, const char *fmt,...)
Definition: buffer.c:1087
void GWB_BuildCtx_SetInitialSourceDir(GWB_BUILD_CONTEXT *bctx, const char *s)
Definition: buildctx.c:87
void GWBUILD_Debug_PrintFileList2(const char *sName, const GWB_FILE_LIST2 *fileList2, int indent)
Definition: gwenbuild.c:340
void GWB_BuildCtx_AddInFilesToCtxAndCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *bcmd, GWB_FILE_LIST2 *fileList)
Definition: buildctx.c:150
void GWB_File_List2_FreeAll(GWB_FILE_LIST2 *fileList2)
Definition: file.c:284
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWB_BUILD_SUBCMD GWB_BUILD_SUBCMD
Definition: buildsubcmd.h:16
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
GWENHYWFAR_API void GWEN_Time_free(GWEN_TIME *t)
Definition: gwentime_all.c:462
void GWB_BuildCtx_AddCommand(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *cmd)
Definition: buildctx.c:102
void GWB_BuildCtx_AddOutFilesToCtxAndCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *bcmd, GWB_FILE_LIST2 *fileList)
Definition: buildctx.c:187
void GWB_BuildCmd_List2_FreeAll(GWB_BUILD_CMD_LIST2 *cmdList)
Definition: buildcmd.c:548
GWB_FILE * GWB_File_List2_GetFileByPathAndName(const GWB_FILE_LIST2 *fileList, const char *folder, const char *fname)
Definition: file.c:353
void GWB_BuildCtx_AddFile(GWB_BUILD_CONTEXT *bctx, GWB_FILE *file)
Definition: buildctx.c:116
struct GWB_BUILD_CMD GWB_BUILD_CMD
Definition: buildcmd.h:20
GWB_BUILD_CMD_LIST2 * GWB_BuildCtx_GetCommandList(const GWB_BUILD_CONTEXT *bctx)
Definition: buildctx.c:95
void GWB_BuildCmd_AddOutFile(GWB_BUILD_CMD *bcmd, GWB_FILE *file)
Definition: buildcmd.c:277
void GWB_BuildCtx_AddInFileToCtxAndCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *bcmd, GWB_FILE *file)
Definition: buildctx.c:132
GWENHYWFAR_API int GWEN_Directory_GetPath(const char *path, unsigned int flags)
#define DBG_INFO(dbg_logger, format,...)
Definition: debug.h:181
void GWB_BuildCtx_Dump(const GWB_BUILD_CONTEXT *bctx, int indent)
Definition: buildctx.c:206
GWENHYWFAR_API GWEN_TIME * GWEN_CurrentTime(void)
Definition: gwentime_all.c:62
void GWB_BuildCtx_AddOutFileToCtxAndCmd(GWB_BUILD_CONTEXT *bctx, GWB_BUILD_CMD *bcmd, GWB_FILE *file)
Definition: buildctx.c:169
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:992
void GWB_BuildSubCmd_SetLogFilename(GWB_BUILD_SUBCMD *cmd, const char *s)
Definition: buildsubcmd.c:80