zdev: Prepare for firmware configuration file support

Apply some changes to existing functions and data structures to simplify
the firmware configuration file support implementation.

 - Make qeth and dasd subtype objects non-static
 - Change the existing helper functions for reading file contents into
   memory to also support binary functions
 - Move some configuration file import functions to make them available
   for use in other source files

Signed-off-by: Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Peter Oberparleiter
2017-09-01 11:05:51 +02:00
committed by Jan Höppner
parent e14363373b
commit ab4445c261
10 changed files with 62 additions and 31 deletions

View File

@@ -98,26 +98,47 @@ static void dryrun_end_data(void)
#define READ_CHUNK_SIZE 4096
/* Read text from @fd and return resulting NULL-terminated text buffer.
* If @chomp is non-zero, remove trailing newline character. Return %NULL
* on error or when unprintable characters are read. */
static char *read_fd(FILE *fd, int chomp)
/* Read all data from @fd and return address of resulting buffer in
* @buffer_ptr. If @size_ptr is non-zero, use it to store the size of the
* resulting buffer. Return %EXIT_OK on success. */
exit_code_t misc_read_fd(FILE *fd, void **buffer_ptr, size_t *size_ptr)
{
char *buffer = NULL;
size_t done, i;
size_t done = 0;
done = 0;
while (!feof(fd)) {
buffer = realloc(buffer, done + READ_CHUNK_SIZE + 1);
buffer = realloc(buffer, done + READ_CHUNK_SIZE);
if (!buffer)
oom();
done += fread(&buffer[done], 1, READ_CHUNK_SIZE, fd);
if (ferror(fd)) {
free(buffer);
return NULL;
return EXIT_RUNTIME_ERROR;
}
}
buffer = realloc(buffer, done);
if (!buffer && done > 0)
oom();
*buffer_ptr = buffer;
if (size_ptr)
*size_ptr = done;
return EXIT_OK;
}
/* Read text from @fd and return resulting NULL-terminated text buffer.
* If @chomp is non-zero, remove trailing newline character. Return %NULL
* on error or when unprintable characters are read. */
static char *read_fd(FILE *fd, int chomp)
{
char *buffer;
size_t done, i;
if (misc_read_fd(fd, (void **) &buffer, &done))
return NULL;
/* Check if this is a text file at all (required to filter out
* binary sysfs attributes). */
for (i = 0; i < done; i++) {
@@ -131,12 +152,13 @@ static char *read_fd(FILE *fd, int chomp)
if (chomp && done > 0 && buffer[done - 1] == '\n')
done--;
if (buffer) {
/* NULL-terminate. */
buffer[done++] = 0;
}
/* NULL-terminate. */
buffer = realloc(buffer, done + 1);
if (!buffer)
oom();
buffer[done] = 0;
return realloc(buffer, done);
return buffer;
}
static int count_newline(const char *str)