Diffstat (limited to 'noncore/apps/opie-console/filetransfer.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/opie-console/filetransfer.cpp | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/noncore/apps/opie-console/filetransfer.cpp b/noncore/apps/opie-console/filetransfer.cpp new file mode 100644 index 0000000..78982bd --- a/dev/null +++ b/noncore/apps/opie-console/filetransfer.cpp | |||
@@ -0,0 +1,142 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | #include <errno.h> | ||
4 | #include <fcntl.h> | ||
5 | #include <unistd.h> | ||
6 | |||
7 | #include <qcstring.h> | ||
8 | #include <qsocketnotifier.h> | ||
9 | |||
10 | #include <opie/oprocess.h> | ||
11 | |||
12 | #include "filetransfer.h" | ||
13 | |||
14 | bool FileTransfer::terminate = false; | ||
15 | pid_t FileTransfer::m_pid; | ||
16 | |||
17 | FileTransfer::FileTransfer( Type t, IOLayer* lay ) | ||
18 | : FileTransferLayer( lay ), m_type( t ) { | ||
19 | signal( SIGCHLD, signal_handler ); | ||
20 | } | ||
21 | FileTransfer::~FileTransfer() { | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * now we will send the file. | ||
26 | * | ||
27 | * we request an fd. The IOLayer should be closed | ||
28 | * then we will setup a pipe for progress communication | ||
29 | * then we will dup2 the m_fd in the forked process | ||
30 | * to do direct IO from and to the fd | ||
31 | */ | ||
32 | void FileTransfer::sendFile( const QString& file ) { | ||
33 | m_fd = layer()->rawIO(); | ||
34 | // | ||
35 | // m_fd = ::open("/dev/ttyS0", O_RDWR); | ||
36 | |||
37 | |||
38 | if ( pipe( m_comm ) < 0 ) | ||
39 | m_comm[0] = m_comm[1] = 0; | ||
40 | if ( pipe( m_info ) < 0 ) | ||
41 | m_info[0] = m_info[1] = 0; | ||
42 | |||
43 | qWarning("output:"+file ); | ||
44 | m_pid = fork(); | ||
45 | switch( m_pid ) { | ||
46 | case 0:{ | ||
47 | setupChild(); | ||
48 | /* exec */ | ||
49 | char* verbose = "-vv"; | ||
50 | char* binray = "-b"; | ||
51 | |||
52 | |||
53 | /* we should never return from here */ | ||
54 | execlp("sz", "sz", verbose, binray, file.latin1(), NULL ); | ||
55 | |||
56 | /* communication for error!*/ | ||
57 | char resultByte =1; | ||
58 | if (m_info[1] ) | ||
59 | write(m_info[1], &resultByte, 1 ); | ||
60 | _exit( -1 ); | ||
61 | break; | ||
62 | } | ||
63 | default:{ | ||
64 | if ( m_info[1] ) | ||
65 | close( m_info[1] ); | ||
66 | if ( m_info[0] ) for (;;) { | ||
67 | char resultByte; int len; | ||
68 | len = read(m_info[0], &resultByte, 1 ); | ||
69 | /* len == 1 start up failed */ | ||
70 | if ( len == 1 ) | ||
71 | return; | ||
72 | if ( len == -1 ) | ||
73 | if ( (errno == ECHILD ) || (errno == EINTR ) ) | ||
74 | continue; | ||
75 | |||
76 | // len == 0 or something like this | ||
77 | break; | ||
78 | } | ||
79 | if ( m_info[0] ) | ||
80 | close( m_info[0] ); | ||
81 | |||
82 | terminate = false; | ||
83 | fd_set fds; | ||
84 | struct timeval timeout; | ||
85 | int sel; | ||
86 | |||
87 | /* replace by QSocketNotifier!!! */ | ||
88 | while (!terminate) { | ||
89 | FD_ZERO( &fds ); | ||
90 | FD_SET( m_comm[0], &fds ); | ||
91 | sel = select( m_comm[0]+1, &fds, NULL, NULL, &timeout ); | ||
92 | if (sel ) { | ||
93 | if ( FD_ISSET(m_comm[0], &fds ) ) { | ||
94 | printf("out:"); | ||
95 | QByteArray ar(4096); | ||
96 | int len = read(m_comm[0], ar.data(), 4096 ); | ||
97 | for (int i = 0; i < len; i++ ) { | ||
98 | // printf("%c", ar[i] ); | ||
99 | } | ||
100 | printf("\n"); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | break; | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | void FileTransfer::sendFile( const QFile& file ) { | ||
109 | sendFile( file.name() ); | ||
110 | } | ||
111 | void FileTransfer::signal_handler(int ) { | ||
112 | int status; | ||
113 | signal( SIGCHLD, signal_handler ); | ||
114 | waitpid( m_pid, &status, WNOHANG ); | ||
115 | terminate = true; | ||
116 | } | ||
117 | void FileTransfer::setupChild() { | ||
118 | /* | ||
119 | * we do not want to read from our | ||
120 | * information channel | ||
121 | */ | ||
122 | if (m_info[0] ) | ||
123 | close(m_info[0] ); | ||
124 | /* | ||
125 | * FD_CLOEXEC will close the | ||
126 | * fd on successfull exec | ||
127 | */ | ||
128 | if (m_info[1] ) | ||
129 | fcntl(m_info[1], F_SETFD, FD_CLOEXEC ); | ||
130 | |||
131 | if (m_comm[0] ) | ||
132 | close( m_comm[0] ); | ||
133 | /* | ||
134 | * now set the communication | ||
135 | * m_fd STDIN_FILENO | ||
136 | * STDOUT_FILENO | ||
137 | * STDERR_FILENO | ||
138 | */ | ||
139 | dup2( m_fd, STDIN_FILENO ); | ||
140 | dup2( m_fd, STDOUT_FILENO ); | ||
141 | dup2( m_comm[1], STDERR_FILENO ); | ||
142 | } | ||