filepush: do not loop anymore on eof, but signal an event
[vuplus_dvbapp] / lib / base / filepush.cpp
1 #include <config.h>
2 #include <lib/base/filepush.h>
3 #include <lib/base/eerror.h>
4 #include <errno.h>
5 #include <fcntl.h>
6
7 eFilePushThread::eFilePushThread(): m_messagepump(eApp, 0)
8 {
9         m_stop = 0;
10         flush();
11         CONNECT(m_messagepump.recv_msg, eFilePushThread::recvEvent);
12 }
13
14 static void signal_handler(int x)
15 {
16 }
17
18 void eFilePushThread::thread()
19 {
20         off_t dest_pos = 0;
21         eDebug("FILEPUSH THREAD START");
22                 // this is a race. FIXME.
23         
24                 /* we set the signal to not restart syscalls, so we can detect our signal. */
25         struct sigaction act;
26         act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
27         act.sa_flags = 0;
28         sigaction(SIGUSR1, &act, 0);
29         
30         dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
31                 /* m_stop must be evaluated after each syscall. */
32         while (!m_stop)
33         {
34                         /* first try flushing the bufptr */
35                 if (m_buf_start != m_buf_end)
36                 {
37                                 // TODO: take care of boundaries.
38                         int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
39 //                      eDebug("wrote %d bytes", w);
40                         if (w <= 0)
41                         {
42                                 if (errno == -EINTR)
43                                         continue;
44                                 eDebug("eFilePushThread *write error* (%m) - not yet handled");
45                                 // ... we would stop the thread
46                         }
47
48                                 /* this should flush all written pages to disk. */
49                         posix_fadvise(m_fd_dest, dest_pos, w, POSIX_FADV_DONTNEED);
50
51                         dest_pos += w;
52 //                      printf("FILEPUSH: wrote %d bytes\n", w);
53                         m_buf_start += w;
54                         continue;
55                 }
56                         
57                         /* now fill our buffer. */
58                 m_buf_start = 0;
59                 m_buf_end = read(m_fd_source, m_buffer, sizeof(m_buffer));
60                 if (m_buf_end < 0)
61                 {
62                         m_buf_end = 0;
63                         if (errno == EINTR)
64                                 continue;
65                         eDebug("eFilePushThread *read error* - not yet handled");
66                 }
67                 if (m_buf_end == 0)
68                 {
69                         sendEvent(evtEOF);
70
71 #if 0
72                         eDebug("FILEPUSH: end-of-file! (currently unhandled)");
73                         if (!lseek(m_fd_source, 0, SEEK_SET))
74                         {
75                                 eDebug("(looping)");
76                                 continue;
77                         }
78 #endif
79                         break;
80                 }
81 //              printf("FILEPUSH: read %d bytes\n", m_buf_end);
82         }
83         
84         eDebug("FILEPUSH THREAD STOP");
85 }
86
87 void eFilePushThread::start(int fd_source, int fd_dest)
88 {
89         m_fd_source = fd_source;
90         m_fd_dest = fd_dest;
91         resume();
92 }
93
94 void eFilePushThread::stop()
95 {
96         m_stop = 1;
97         sendSignal(SIGUSR1);
98         kill();
99 }
100
101 void eFilePushThread::pause()
102 {
103         stop();
104 }
105
106 void eFilePushThread::seek(int whence, off_t where)
107 {
108         ::lseek(m_fd_source, where, whence);
109 }
110
111 void eFilePushThread::resume()
112 {
113         m_stop = 0;
114         run();
115 }
116
117 void eFilePushThread::flush()
118 {
119         m_buf_start = m_buf_end = 0;
120 }
121
122
123 void eFilePushThread::sendEvent(int evt)
124 {
125         m_messagepump.send(evt);
126 }
127
128 void eFilePushThread::recvEvent(const int &evt)
129 {
130         m_event(evt);
131 }