Move the read from file descriptor into buffer into generic routine as I intend to reuse this for external epggrab input.

This commit is contained in:
Adam Sutton 2012-06-06 20:31:01 +01:00 committed by Adam Sutton
parent ee30f1573b
commit fc4fc7e753
4 changed files with 110 additions and 56 deletions

View file

@ -42,6 +42,7 @@ SRCS = src/main.c \
src/tcp.c \
src/http.c \
src/notify.c \
src/file.c \
src/cron.c \
src/epg.c \
src/epggrab.c\

80
src/file.c Normal file
View file

@ -0,0 +1,80 @@
/*
* Process file functions
* Copyright (C) 2008 Andreas Öman
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_RDBUF_SIZE 8192
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "queue.h"
#include "file.h"
typedef struct file_read_buf {
TAILQ_ENTRY(file_read_buf) link;
int size;
char buf[MAX_RDBUF_SIZE];
} file_read_buf_t;
TAILQ_HEAD(file_read_buf_queue, file_read_buf);
size_t file_readall ( int fd, char **outp )
{
int r, totalsize = 0;
struct file_read_buf_queue bufs;
file_read_buf_t *b = NULL;
char *outbuf;
TAILQ_INIT(&bufs);
while(1) {
if(b == NULL) {
b = malloc(sizeof(file_read_buf_t));
b->size = 0;
TAILQ_INSERT_TAIL(&bufs, b, link);
}
r = read(fd, b->buf + b->size, MAX_RDBUF_SIZE - b->size);
if(r < 1)
break;
b->size += r;
totalsize += r;
if(b->size == MAX_RDBUF_SIZE)
b = NULL;
}
close(fd);
if(totalsize == 0) {
free(b);
*outp = NULL;
return 0;
}
outbuf = malloc(totalsize + 1);
r = 0;
while((b = TAILQ_FIRST(&bufs)) != NULL) {
memcpy(outbuf + r, b->buf, b->size);
r+= b->size;
TAILQ_REMOVE(&bufs, b, link);
free(b);
}
assert(r == totalsize);
*outp = outbuf;
outbuf[totalsize] = 0;
return totalsize;
}

26
src/file.h Normal file
View file

@ -0,0 +1,26 @@
/*
* Process file operations
* Copyright (C) 2012 Adam Sutton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILE_H
#define FILE_H
#include <sys/types.h>
size_t file_readall ( int fd, char **outbuf );
#endif /* FILE_H */

View file

@ -28,6 +28,7 @@
#include <fcntl.h>
#include "tvheadend.h"
#include "file.h"
#include "spawn.h"
extern char **environ;
@ -43,21 +44,6 @@ typedef struct spawn {
} spawn_t;
/**
* Structs for reading back output from a spawn via a pipe
*/
TAILQ_HEAD(spawn_output_buf_queue, spawn_output_buf);
#define MAX_SOB_SIZE 4000
typedef struct spawn_output_buf {
TAILQ_ENTRY(spawn_output_buf) sob_link;
int sob_size;
char sob_buf[MAX_SOB_SIZE];
} spawn_output_buf_t;
/**
* The reaper is called once a second to finish of any pending spawns
*/
@ -135,10 +121,7 @@ int
spawn_and_store_stdout(const char *prog, char *const argv[], char **outp)
{
pid_t p;
int fd[2], r, totalsize = 0, f;
char *outbuf;
struct spawn_output_buf_queue bufs;
spawn_output_buf_t *b = NULL;
int fd[2], f;
const char *local_argv[2];
if(argv == NULL) {
@ -194,43 +177,7 @@ spawn_and_store_stdout(const char *prog, char *const argv[], char **outp)
close(fd[1]);
TAILQ_INIT(&bufs);
while(1) {
if(b == NULL) {
b = malloc(sizeof(spawn_output_buf_t));
b->sob_size = 0;
TAILQ_INSERT_TAIL(&bufs, b, sob_link);
}
r = read(fd[0], b->sob_buf + b->sob_size, MAX_SOB_SIZE - b->sob_size);
if(r < 1)
break;
b->sob_size += r;
totalsize += r;
if(b->sob_size == MAX_SOB_SIZE)
b = NULL;
}
close(fd[0]);
if(totalsize == 0) {
free(b);
*outp = NULL;
return 0;
}
outbuf = malloc(totalsize + 1);
r = 0;
while((b = TAILQ_FIRST(&bufs)) != NULL) {
memcpy(outbuf + r, b->sob_buf, b->sob_size);
r+= b->sob_size;
TAILQ_REMOVE(&bufs, b, sob_link);
free(b);
}
assert(r == totalsize);
*outp = outbuf;
outbuf[totalsize] = 0;
return totalsize;
return file_readall(fd[0], outp);
}