diff options
Diffstat (limited to 'libs/IO/src')
-rw-r--r-- | libs/IO/src/CommandReader.cc | 120 | ||||
-rw-r--r-- | libs/IO/src/File.cc | 12 | ||||
-rw-r--r-- | libs/IO/src/FileReader.cc | 18 | ||||
-rw-r--r-- | libs/IO/src/FileRepresentation.cc | 12 | ||||
-rw-r--r-- | libs/IO/src/LineReader.cc | 4 | ||||
-rw-r--r-- | libs/IO/src/Reader.cc | 44 |
6 files changed, 186 insertions, 24 deletions
diff --git a/libs/IO/src/CommandReader.cc b/libs/IO/src/CommandReader.cc new file mode 100644 index 0000000..6b67511 --- /dev/null +++ b/libs/IO/src/CommandReader.cc @@ -0,0 +1,120 @@ +/** + * ======================== legal notice ====================== + * + * File: CommandReader.cc + * Created: 30. Juli 2012, 06:42 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libIO: classes for files, filesystem and input/output + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <CommandReader.h> +#include <Logging.h> +#include <stdarg.h> +#include <errno.h> +#include <wait.h> + +cCommandReader::cCommandReader(const char *cmd) + : pid(-1) +{ + args.push_back(cmd); +} + +cCommandReader::~cCommandReader() +{ + Close(); +} + +void cCommandReader::AddCommandParameter(const char* Param) +{ + args.push_back(Param); +} + +void cCommandReader::Close(void) +{ + int status; + + if (pid != waitpid(pid, &status, 0)) { + esyslog("ERROR: failed to wait for child #%d - error #%d", pid, errno); + } + else { + isyslog("child exit status: %d", WEXITSTATUS(status)); + } + pid = fd = -1; +} + +bool cCommandReader::Open(void) +{ + enum { FDRead, FDWrite }; + int parent2Child[2]; + int child2Parent[2]; + + if (pipe(parent2Child)) { + esyslog("ERROR: failed to create parent2Child-pipe #%d", errno); + return false; + } + if (pipe(child2Parent)) { + esyslog("ERROR: failed to create child2Parent-pipe #%d", errno); + return false; + } + switch ((pid = fork())) { + case -1: + esyslog("fork failed"); + return false; + + case 0: { /* child */ + const char *cmdArgs[args.size() + 1]; + + for (size_t i=0; i < args.size(); ++i) { + cmdArgs[i] = args[i].c_str(); + } + cmdArgs[args.size()] = 0; + if (dup2(parent2Child[FDRead], STDIN_FILENO) < 0) { + esyslog("ERROR: failed to create parents stdin #%d", errno); + exit(-1); + } + if (dup2(child2Parent[FDWrite], STDOUT_FILENO) < 0) { + esyslog("ERROR: failed to create childs stdout #%d", errno); + exit(-1); + } + if (dup2(child2Parent[FDWrite], STDERR_FILENO) < 0) { + esyslog("ERROR: failed to create childs stderr #%d", errno); + exit(-1); + } + if (close(parent2Child[FDWrite])) { + esyslog("ERROR: failed to close parents write-end of the pipe #%d", errno); + exit(-1); + } + if (close(child2Parent[FDRead])) { + esyslog("ERROR: failed to close childs read-end of the pipe #%d", errno); + exit(-1); + } + execv(args[0].c_str(), (char *const*) cmdArgs); + + esyslog("should never be reached!"); + exit(-1); + } break; + + default: { /* parent */ + isyslog("child process #%d started ...", pid); + fd = child2Parent[FDRead]; + + return true; + } + } + return false; +} diff --git a/libs/IO/src/File.cc b/libs/IO/src/File.cc index 9b92b55..910d24d 100644 --- a/libs/IO/src/File.cc +++ b/libs/IO/src/File.cc @@ -1,25 +1,25 @@ /** * ======================== legal notice ====================== - * + * * File: File.cc * Created: 21. Juli 2012, 12 * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> * Project: libIO: classes for files, filesystem and input/output - * + * * CMP - compound media player - * + * * is a client/server mediaplayer intended to play any media from any workstation * without the need to export or mount shares. cmps is an easy to use backend * with a (ready to use) HTML-interface. Additionally the backend supports * authentication via HTTP-digest authorization. * cmpc is a client with vdr-like osd-menues. - * + * * Copyright (c) 2012 Reinhard Mantey, some rights reserved! * published under Creative Commons by-sa * For details see http://creativecommons.org/licenses/by-sa/3.0/ - * + * * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp - * + * * -------------------------------------------------------------- */ #include <File.h> diff --git a/libs/IO/src/FileReader.cc b/libs/IO/src/FileReader.cc index 519c749..fd692ca 100644 --- a/libs/IO/src/FileReader.cc +++ b/libs/IO/src/FileReader.cc @@ -29,13 +29,18 @@ #include <unistd.h> cFileReader::cFileReader(cFile *Input) - : fd(-1) - , file(Input) + : file(Input) +{ + Open(); +} + +bool cFileReader::Open() { if (file && file->Exists() && file->CanRead()) { fd = open(file->AbsolutePath(), O_RDONLY); if (fd < 0) esyslog("ERROR: could not open %s", file->AbsolutePath()); } + return fd > 0; } cFileReader::~cFileReader() @@ -45,7 +50,7 @@ cFileReader::~cFileReader() void cFileReader::Close() { - if (fd >= 0) { + if (fd > 0) { close(fd); fd = -1; } @@ -54,10 +59,3 @@ void cFileReader::Close() file = NULL; } } - -int cFileReader::Read(char* buf, int bufSize) -{ - if (fd >= 0) return read(fd, buf, bufSize); - esyslog("ERROR: not an open file! %s", file->AbsolutePath()); - return 0; -} diff --git a/libs/IO/src/FileRepresentation.cc b/libs/IO/src/FileRepresentation.cc index 3366157..f74d2c8 100644 --- a/libs/IO/src/FileRepresentation.cc +++ b/libs/IO/src/FileRepresentation.cc @@ -1,25 +1,25 @@ /** * ======================== legal notice ====================== - * + * * File: FileRepresentation.cc * Created: 21. Juli 2012, 12 * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> * Project: libIO: classes for files, filesystem and input/output - * + * * CMP - compound media player - * + * * is a client/server mediaplayer intended to play any media from any workstation * without the need to export or mount shares. cmps is an easy to use backend * with a (ready to use) HTML-interface. Additionally the backend supports * authentication via HTTP-digest authorization. * cmpc is a client with vdr-like osd-menues. - * + * * Copyright (c) 2012 Reinhard Mantey, some rights reserved! * published under Creative Commons by-sa * For details see http://creativecommons.org/licenses/by-sa/3.0/ - * + * * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp - * + * * -------------------------------------------------------------- */ #include <FileRepresentation.h> diff --git a/libs/IO/src/LineReader.cc b/libs/IO/src/LineReader.cc index bf4bcc2..0d06628 100644 --- a/libs/IO/src/LineReader.cc +++ b/libs/IO/src/LineReader.cc @@ -23,14 +23,14 @@ * -------------------------------------------------------------- */ #include <LineReader.h> -#include <FileReader.h> +#include <Reader.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> #include <iostream> -cLineReader::cLineReader(cFileReader *FileReader) +cLineReader::cLineReader(cReader *FileReader) : reader(FileReader) , bytesRead(0) , off(0) diff --git a/libs/IO/src/Reader.cc b/libs/IO/src/Reader.cc new file mode 100644 index 0000000..cbead31 --- /dev/null +++ b/libs/IO/src/Reader.cc @@ -0,0 +1,44 @@ +/** + * ======================== legal notice ====================== + * + * File: Reader.cc + * Created: 30. Juli 2012, 08:11 + * Author: <a href="mailto:geronimo013@gmx.de">Geronimo</a> + * Project: libIO: classes for files, filesystem and input/output + * + * CMP - compound media player + * + * is a client/server mediaplayer intended to play any media from any workstation + * without the need to export or mount shares. cmps is an easy to use backend + * with a (ready to use) HTML-interface. Additionally the backend supports + * authentication via HTTP-digest authorization. + * cmpc is a client with vdr-like osd-menues. + * + * Copyright (c) 2012 Reinhard Mantey, some rights reserved! + * published under Creative Commons by-sa + * For details see http://creativecommons.org/licenses/by-sa/3.0/ + * + * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp + * + * -------------------------------------------------------------- + */ +#include <Reader.h> +#include <Logging.h> +#include <unistd.h> + +cReader::cReader() + : fd(-1) +{ +} + +cReader::~cReader() +{ +} + +int cReader::Read(char* buf, int bufSize) +{ + if (fd < 0) Open(); + if (fd > 0) return read(fd, buf, bufSize); + esyslog("ERROR: no valid filehandle to read from!"); + return 0; +} |