Skip to content

Commit

Permalink
Merge pull request ElektraInitiative#4470 from hannes99/quickdump_clo…
Browse files Browse the repository at this point in the history
…se_stdout

quickdump: don't close stdout
  • Loading branch information
markus2330 authored Nov 17, 2022
2 parents 23c5bb7 + d766fe0 commit e9f2cab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
6 changes: 2 additions & 4 deletions doc/news/_preparation_next_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ The following text lists news about the [plugins](https://www.libelektra.org/plu
- <<TODO>>
- <<TODO>>

### <<Plugin>>
### quickdump

- <<TODO>>
- <<TODO>>
- <<TODO>>
- elektraQuickdumpSet: don't fclose if stdout _(@hannes99)_

### <<Plugin>>

Expand Down
40 changes: 24 additions & 16 deletions src/plugins/quickdump/quickdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ static void ensureBufferSize (struct stringbuffer * buffer, size_t minSize);

#include "varint.c"

static inline void closeFile (FILE * file)
{
if (file != stdout)
{
fclose (file);
}
}

static inline bool writeData (FILE * file, const char * data, kdb_unsigned_long_long_t size, Key * errorKey)
{
if (!varintWrite (file, size))
Expand Down Expand Up @@ -394,7 +402,7 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
kdb_unsigned_long_long_t magic = htobe64 (MAGIC_NUMBER_V3);
if (fwrite (&magic, sizeof (kdb_unsigned_long_long_t), 1, file) < 1)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

Expand All @@ -420,22 +428,22 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
size_t fullNameSize = keyGetNameSize (cur);
if (fullNameSize < parentOffset)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

kdb_unsigned_long_long_t nameSize = fullNameSize == parentOffset ? 0 : fullNameSize - 1 - parentOffset;
if (!writeData (file, keyName (cur) + parentOffset, nameSize, parentKey))
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

if (keyIsBinary (cur))
{
if (fputc ('b', file) == EOF)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

Expand All @@ -447,15 +455,15 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
value = elektraMalloc (valueSize);
if (keyGetBinary (cur, value, valueSize) == -1)
{
fclose (file);
closeFile (file);
elektraFree (value);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}

if (!writeData (file, value, valueSize, parentKey))
{
fclose (file);
closeFile (file);
elektraFree (value);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
Expand All @@ -465,14 +473,14 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (fputc ('s', file) == EOF)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

kdb_unsigned_long_long_t valueSize = keyGetValueSize (cur) - 1;
if (!writeData (file, keyString (cur), valueSize, parentKey))
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
Expand All @@ -487,22 +495,22 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (fputc ('m', file) == EOF)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

// ignore meta namespace when writing to file
kdb_unsigned_long_long_t metaNameSize = keyGetNameSize (meta) - 1 - (sizeof ("meta:/") - 1);
if (!writeData (file, keyName (meta) + sizeof ("meta:/") - 1, metaNameSize, parentKey))
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

kdb_unsigned_long_long_t metaValueSize = keyGetValueSize (meta) - 1;
if (!writeData (file, keyString (meta), metaValueSize, parentKey))
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

Expand All @@ -512,30 +520,30 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (fputc ('c', file) == EOF)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

kdb_unsigned_long_long_t keyNameSize = metaKeys.array[result]->keyNameSize;
if (!writeData (file, metaKeys.array[result]->keyName, keyNameSize, parentKey))
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}

// ignore meta namespace when writing to file
kdb_unsigned_long_long_t metaNameSize = keyGetNameSize (meta) - 1 - (sizeof ("meta:/") - 1);
if (!writeData (file, keyName (meta) + sizeof ("meta:/") - 1, metaNameSize, parentKey))
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
}

if (fputc (0, file) == EOF)
{
fclose (file);
closeFile (file);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
Expand All @@ -546,7 +554,7 @@ int elektraQuickdumpSet (Plugin * handle, KeySet * returned, Key * parentKey)
}
elektraFree (metaKeys.array);

fclose (file);
closeFile (file);

return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/specload/testmod_specload.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,6 @@ static void test_sendspec (void)

PLUGIN_OPEN ("specload");

// TODO: if elektraSpecloadSendSpec should not fail as expected stdout will be closed with the current quickdump implementation

int tmp = elektraSpecloadSendSpec (plugin, NULL, parentKey);
succeed_if (tmp == ELEKTRA_PLUGIN_STATUS_ERROR, "sendspec should return an error if spec is NULL");

Expand Down

0 comments on commit e9f2cab

Please sign in to comment.