Skip to content

Commit

Permalink
Redmine#5454: use CSV parser in package*matching() and CSV writer in …
Browse files Browse the repository at this point in the history
…ReportSoftware
  • Loading branch information
tzz committed Apr 23, 2014
1 parent d37bcd0 commit aaf6dae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
27 changes: 21 additions & 6 deletions cf-agent/verify_packages.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <eval_context.h>
#include <retcode.h>
#include <known_dirs.h>
#include <csv_writer.h>
#include <cf-agent-enterprise-stubs.h>
#include <cf-windows-functions.h>

Expand Down Expand Up @@ -687,17 +688,31 @@ static void ReportSoftware(PackageManager *list)
return;
}

for (mp = list; mp != NULL; mp = mp->next)
Writer *writer_installed = FileWriter(fout);

CsvWriter *c = CsvWriterOpen(writer_installed);
if (c)
{
for (pi = mp->pack_list; pi != NULL; pi = pi->next)
for (mp = list; mp != NULL; mp = mp->next)
{
fprintf(fout, "%s,", CanonifyChar(pi->name, ','));
fprintf(fout, "%s,", CanonifyChar(pi->version, ','));
fprintf(fout, "%s,%s\n", pi->arch, ReadLastNode(RealPackageManager(mp->manager)));
for (pi = mp->pack_list; pi != NULL; pi = pi->next)
{
CsvWriterField(c, pi->name);
CsvWriterField(c, pi->version);
CsvWriterField(c, pi->arch);
CsvWriterField(c, ReadLastNode(RealPackageManager(mp->manager)));
CsvWriterNewRecord(c);
}
}

CsvWriterClose(c);
}
else
{
Log(LOG_LEVEL_ERR, "Cannot write CSV to file '%s'", name);
}

fclose(fout);
WriterClose(writer_installed);
}

/**
Expand Down
30 changes: 16 additions & 14 deletions libpromises/evalfunction.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include <files_lib.h>
#include <connection_info.h>
#include <printsize.h>
#include <csv_parser.h>

#include <math_eval.h>

Expand Down Expand Up @@ -1328,6 +1329,8 @@ static FnCallResult FnCallPackagesMatching(ARG_UNUSED EvalContext *ctx, ARG_UNUS
GetSoftwarePatchesFilename(filename);
}

Log(LOG_LEVEL_DEBUG, "%s: reading inventory from '%s'", fp->name, filename);

if ((fin = fopen(filename, "r")) == NULL)
{
Log(LOG_LEVEL_VERBOSE, "%s cannot open the %s packages inventory '%s' - "
Expand All @@ -1340,13 +1343,10 @@ static FnCallResult FnCallPackagesMatching(ARG_UNUSED EvalContext *ctx, ARG_UNUS

int linenumber = 0;

size_t line_size = CF_BUFSIZE;
char *line = xmalloc(line_size);

for(;;)
{
ssize_t res = CfReadLine(&line, &line_size, fin);
if (res == -1)
char *line = GetCsvLineNext(fin);
if (NULL == line)
{
if (!feof(fin))
{
Expand Down Expand Up @@ -1374,29 +1374,31 @@ static FnCallResult FnCallPackagesMatching(ARG_UNUSED EvalContext *ctx, ARG_UNUS

if (StringMatchFull(regex, line))
{
char name[CF_MAXVARSIZE], version[CF_MAXVARSIZE], arch[CF_MAXVARSIZE], method[CF_MAXVARSIZE];
Seq *list = SeqParseCsvString(line);
JsonElement *line_obj = JsonObjectCreate(4);
int scancount = sscanf(line, "%250[^,],%250[^,],%250[^,],%250[^\n]", name, version, arch, method);
if (scancount != 4)
if (SeqLength(list) != 4)
{
Log(LOG_LEVEL_ERR, "Line %d from package inventory '%s' did not yield 4 elements", linenumber, filename);
Log(LOG_LEVEL_ERR, "Line %d from package inventory '%s' did not yield 4 elements: %s", linenumber, filename, line);
JsonDestroy(line_obj);
++linenumber;
SeqDestroy(list);
continue;
}

JsonObjectAppendString(line_obj, "name", name);
JsonObjectAppendString(line_obj, "version", version);
JsonObjectAppendString(line_obj, "arch", arch);
JsonObjectAppendString(line_obj, "method", method);
JsonObjectAppendString(line_obj, "name", SeqAt(list, 0));
JsonObjectAppendString(line_obj, "version", SeqAt(list, 1));
JsonObjectAppendString(line_obj, "arch", SeqAt(list, 2));
JsonObjectAppendString(line_obj, "method", SeqAt(list, 3));
SeqDestroy(list);

JsonArrayAppendObject(json, line_obj);
}

++linenumber;
free(line);
}

fclose(fin);
free(line);

return (FnCallResult) { FNCALL_SUCCESS, (Rval) { json, RVAL_TYPE_CONTAINER } };

Expand Down

0 comments on commit aaf6dae

Please sign in to comment.