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