2 #include <lib/base/filepush.h>
3 #include <lib/base/eerror.h>
10 eFilePushThread::eFilePushThread(): m_messagepump(eApp, 0)
16 CONNECT(m_messagepump.recv_msg, eFilePushThread::recvEvent);
19 static void signal_handler(int x)
23 void eFilePushThread::thread()
26 size_t bytes_read = 0;
28 off_t current_span_offset;
29 size_t current_span_remaining = 0;
31 int already_empty = 0;
32 eDebug("FILEPUSH THREAD START");
33 // this is a race. FIXME.
35 /* we set the signal to not restart syscalls, so we can detect our signal. */
37 act.sa_handler = signal_handler; // no, SIG_IGN doesn't do it. we want to receive the -EINTR
39 sigaction(SIGUSR1, &act, 0);
41 dest_pos = lseek(m_fd_dest, 0, SEEK_CUR);
42 /* m_stop must be evaluated after each syscall. */
45 /* first try flushing the bufptr */
46 if (m_buf_start != m_buf_end)
48 // TODO: take care of boundaries.
49 int w = write(m_fd_dest, m_buffer + m_buf_start, m_buf_end - m_buf_start);
50 // eDebug("wrote %d bytes", w);
55 eDebug("eFilePushThread *write error* (%m) - not yet handled");
56 // ... we would stop the thread
59 /* this should flush all written pages to disk. */
60 posix_fadvise(m_fd_dest, dest_pos, w, POSIX_FADV_DONTNEED);
63 // printf("FILEPUSH: wrote %d bytes\n", w);
68 /* now fill our buffer. */
70 if (m_sg && !current_span_remaining)
72 m_sg->getNextSourceSpan(bytes_read, current_span_offset, current_span_remaining);
76 size_t maxread = sizeof(m_buffer);
78 /* if we have a source span, don't read past the end */
79 if (m_sg && maxread < current_span_remaining)
80 maxread = current_span_remaining;
86 m_buf_end = read(m_fd_source, m_buffer, maxread);
88 bytes_read += m_buf_end;
95 eDebug("eFilePushThread *read error* - not yet handled");
99 /* on EOF, try COMMITting once. */
100 if (m_send_pvr_commit && !already_empty)
102 eDebug("sending PVR commit");
104 if (::ioctl(m_fd_dest, PVR_COMMIT) == EINTR)
106 eDebug("commit done");
107 /* well check again */
112 eDebug("FILEPUSH: end-of-file! (currently unhandled)");
113 if (!lseek(m_fd_source, 0, SEEK_SET))
122 // printf("FILEPUSH: read %d bytes\n", m_buf_end);
125 eDebug("FILEPUSH THREAD STOP");
128 void eFilePushThread::start(int fd_source, int fd_dest)
130 m_fd_source = fd_source;
135 void eFilePushThread::stop()
137 if (!thread_running()) /* FIXME: races */
144 void eFilePushThread::pause()
149 void eFilePushThread::seek(int whence, off_t where)
151 ::lseek(m_fd_source, where, whence);
154 void eFilePushThread::resume()
160 void eFilePushThread::flush()
162 m_buf_start = m_buf_end = 0;
165 void eFilePushThread::enablePVRCommit(int s)
167 m_send_pvr_commit = s;
170 void eFilePushThread::sendEvent(int evt)
172 m_messagepump.send(evt);
175 void eFilePushThread::recvEvent(const int &evt)