]> git.ipfire.org Git - thirdparty/tvheadend.git/commitdiff
Move the reading from file descriptor into generic routine as I intend to reuse this... 100/head
authorAdam Sutton <dev@adamsutton.me.uk>
Wed, 6 Jun 2012 19:31:01 +0000 (20:31 +0100)
committerAdam Sutton <dev@adamsutton.me.uk>
Tue, 19 Jun 2012 10:56:39 +0000 (11:56 +0100)
Makefile
src/file.c [new file with mode: 0644]
src/file.h [new file with mode: 0644]
src/spawn.c

index 526945827a19b265b32cc7a4ee9a2fc1ffaab951..b27448a668f7bc14b94a9990b676b3d313456ec7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,7 @@ SRCS =  src/main.c \
        src/tcp.c \
        src/http.c \
        src/notify.c \
+       src/file.c \
        src/epg.c \
        src/xmltv.c \
        src/spawn.c \
diff --git a/src/file.c b/src/file.c
new file mode 100644 (file)
index 0000000..7731687
--- /dev/null
@@ -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;
+}
diff --git a/src/file.h b/src/file.h
new file mode 100644 (file)
index 0000000..9523a90
--- /dev/null
@@ -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 */
index 9d59c5977a658f7860385e05599ad47c0ee8a5ca..953a2c7979e91f349c374b6e5e189acb91135230 100644 (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);
 }