d873d2b3fe5191b4c61f5f9e909467b4fc25e54f
[enigma2.git] / lib / base / filepush.cpp
1 #include <lib/base/filepush.h>
2 #include <lib/base/eerror.h>
3 #include <errno.h>
4
5 eFilePushThread::eFilePushThread()
6 {
7         m_stop = 0;
8         m_buf_start = m_buf_end = 0;
9 }
10
11 static void signal_handler(int x)
12 {
13 }
14
15 void eFilePushThread::thread()
16 {
17         eDebug("FILEPUSH THREAD START");
18                 // this is a race. FIXME.
19         
20                 /* we set the signal to not restart syscalls, so we can detect our signal. */
21         struct sigaction act;
22         act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
23         act.sa_flags = 0;
24         sigaction(SIGUSR1, &act, 0);
25         
26                 /* m_stop must be evaluated after each syscall. */
27         while (!m_stop)
28         {
29                         /* first try flushing the bufptr */
30                 if (m_buf_start != m_buf_end)
31                 {
32                                 // TODO: take care of boundaries.
33                         int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
34                         if (w <= 0)
35                         {
36                                 if (errno == -EINTR)
37                                         continue;
38                                 eDebug("eFilePushThread *write error* - not yet handled");
39                                 // ... we would stop the thread
40                         }
41 //                      printf("FILEPUSH: wrote %d bytes\n", w);
42                         m_buf_start += w;
43                         continue;
44                 }
45                         
46                         /* now fill our buffer. */
47                 m_buf_start = 0;
48                 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
49                 if (m_buf_end < 0)
50                 {
51                         m_buf_end = 0;
52                         if (errno == EINTR)
53                                 continue;
54                         eDebug("eFilePushThread *read error* - not yet handled");
55                 }
56                 if (m_buf_end == 0)
57                 {
58                         eDebug("FILEPUSH: end-of-file! (currently unhandled)");
59                         break;
60                 }
61 //              printf("FILEPUSH: read %d bytes\n", m_buf_end);
62         }
63         
64         eDebug("FILEPUSH THREAD STOP");
65 }
66
67 void eFilePushThread::start(int fd_source, int fd_dest)
68 {
69         m_fd_source = fd_source;
70         m_fd_dest = fd_dest;
71         m_stop = 0;
72         run();
73 }
74
75 void eFilePushThread::stop()
76 {
77         m_stop = 1;
78         sendSignal(SIGUSR1);
79         kill();
80 }