- ignore non-int returns on calls
[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                         if (!lseek(m_fd_source, 0, SEEK_SET))
60                         {
61                                 eDebug("(looping)");
62                                 continue;
63                         }
64                         break;
65                 }
66 //              printf("FILEPUSH: read %d bytes\n", m_buf_end);
67         }
68         
69         eDebug("FILEPUSH THREAD STOP");
70 }
71
72 void eFilePushThread::start(int fd_source, int fd_dest)
73 {
74         m_fd_source = fd_source;
75         m_fd_dest = fd_dest;
76         m_stop = 0;
77         run();
78 }
79
80 void eFilePushThread::stop()
81 {
82         m_stop = 1;
83         sendSignal(SIGUSR1);
84         kill();
85 }