libzypp 17.37.10

ExternalDataSource.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
11
12
13#define _GNU_SOURCE 1 // for ::getline
14
15#include <errno.h>
16#include <unistd.h>
17#include <sys/wait.h>
18#include <fcntl.h>
19#include <iostream>
20#include <stdlib.h>
21#include <string.h>
22#include <sstream>
23#include <string>
24
25#include <zypp-core/base/Logger.h>
26#include <zypp-core/base/ExternalDataSource.h>
27#include <zypp-core/AutoDispose.h>
28#include <zypp-core/zyppng/base/private/linuxhelpers_p.h>
29
30 using std::endl;
31
32 namespace zypp {
33 namespace externalprogram {
34
35 ExternalDataSource::ExternalDataSource( FILE *ifile, FILE *ofile )
36 : inputfile( ifile ),
37 outputfile( ofile ),
38 linebuffer( 0 ),
39 linebuffer_size( 0 )
40 {
41 }
42
43
44 ExternalDataSource::~ExternalDataSource()
45 {
46 if (linebuffer)
47 free( linebuffer );
48 close ();
49 }
50
51
52 bool
53 ExternalDataSource::send( const char *buffer, size_t length )
54 {
55 if ( outputfile ) {
56 bool success = fwrite( buffer, length, 1, outputfile ) != 0;
57 fflush( outputfile );
58 return success;
59 }
60 else
61 return false;
62 }
63
64
65 bool
66 ExternalDataSource::send( std::string s )
67 {
68 DBG << "send (" << s << ")";
69 return send( s.data(), s.length() );
70 }
71
72
73 std::string
74 ExternalDataSource::receiveUpto( char c )
75 {
76 if ( inputfile && !feof( inputfile ) )
77 {
78 std::ostringstream datas;
79 while ( true )
80 {
81 int readc = fgetc( inputfile );
82 if ( readc == EOF ) break;
83 datas << (char)readc;
84 if ( (char)readc == c ) break;
85 }
86 return datas.str();
87 }
88 return std::string();
89 }
90
91 std::string ExternalDataSource::receiveUpto( char c, io::timeout_type timeout )
92 {
93 const auto &received = io::receiveUpto( inputFile(), c, timeout );
94
95 if ( received.first == io::Timeout )
96 ZYPP_THROW( io::TimeoutException() );
97
98 return received.second;
99 }
100
101 size_t
102 ExternalDataSource::receive( char *buffer, size_t length )
103 {
104 if ( inputfile )
105 return fread( buffer, 1, length, inputfile );
106 else
107 return 0;
108 }
109
110 void ExternalDataSource::setBlocking( bool mode )
111 {
112 io::setFILEBlocking( inputfile, mode );
113 }
114
115 std::string ExternalDataSource::receiveLine()
116 {
117 if ( inputfile ) {
118 ssize_t nread = zyppng::eintrSafeCallEx( getline, [&](){clearerr ( inputfile );}, &linebuffer, &linebuffer_size, inputfile );
119 if ( nread == -1 )
120 return "";
121 else
122 return std::string( linebuffer, nread );
123 }
124 else
125 return "";
126 }
127
128 std::string ExternalDataSource::receiveLine( io::timeout_type timeout )
129 {
130 return receiveUpto( '\n', timeout );
131 }
132
133 int
134 ExternalDataSource::close()
135 {
136 if ( inputfile && inputfile != outputfile )
137 fclose( inputfile );
138 if ( outputfile )
139 fclose( outputfile );
140 inputfile = 0;
141 outputfile = 0;
142 return 0;
143 }
144
145 } // namespace externalprogram
146} // namespace zypp
147
zypp::externalprogram::ExternalDataSource::receive
size_t receive(char *buffer, size_t length)
Read some data from the input stream.
Definition ExternalDataSource.cc:102
zypp::externalprogram::ExternalDataSource::linebuffer_size
size_t linebuffer_size
Definition ExternalDataSource.h:35
zypp::externalprogram::ExternalDataSource::send
bool send(const char *buffer, size_t length)
Send some data to the output stream.
Definition ExternalDataSource.cc:53
zypp::externalprogram::ExternalDataSource::ExternalDataSource
ExternalDataSource(FILE *inputfile=0, FILE *outputfile=0)
Create a new instance.
Definition ExternalDataSource.cc:35
zypp::externalprogram::ExternalDataSource::inputfile
FILE * inputfile
Definition ExternalDataSource.h:30
zypp::externalprogram::ExternalDataSource::close
virtual int close()
Close the input and output streams.
Definition ExternalDataSource.cc:134
zypp::externalprogram::ExternalDataSource::setBlocking
void setBlocking(bool mode)
Set the blocking mode of the input stream.
Definition ExternalDataSource.cc:110
zypp::externalprogram::ExternalDataSource::outputfile
FILE * outputfile
Definition ExternalDataSource.h:31
zypp::externalprogram::ExternalDataSource::inputFile
FILE * inputFile() const
Return the input stream.
Definition ExternalDataSource.h:118
zypp::externalprogram::ExternalDataSource::receiveUpto
std::string receiveUpto(char c)
Read characters into a string until delimiter c or EOF is read.
Definition ExternalDataSource.cc:74
zypp::externalprogram::ExternalDataSource::receiveLine
std::string receiveLine()
Read one line from the input stream.
Definition ExternalDataSource.cc:115
zypp::externalprogram::ExternalDataSource::~ExternalDataSource
virtual ~ExternalDataSource()
Implicitly close the connection.
Definition ExternalDataSource.cc:44
zypp::externalprogram::ExternalDataSource::linebuffer
char * linebuffer
Definition ExternalDataSource.h:34
zypp::io::TimeoutException
Definition IOTools.h:52
linuxhelpers_p.h
zypp::externalprogram
Definition ExternalDataSource.cc:33
zypp::io::setFILEBlocking
BlockingMode setFILEBlocking(FILE *file, bool mode)
Enables or disabled non blocking mode on a file descriptor.
Definition IOTools.cc:25
zypp::io::receiveUpto
std::pair< ReceiveUpToResult, std::string > receiveUpto(FILE *file, char c, timeout_type timeout, bool failOnUnblockError)
Definition IOTools.cc:85
zypp::io::Timeout
@ Timeout
Definition IOTools.h:72
zypp::io::timeout_type
size_t timeout_type
Definition IOTools.h:77
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zyppng::eintrSafeCallEx
auto eintrSafeCallEx(const Fun &function, const RestartCb &restartCb, Args &&... args)
Definition linuxhelpers_p.h:35
string.h
AutoDispose.h
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
ExternalDataSource.h
Logger.h
DBG
#define DBG
Definition Logger.h:99