libzypp: zypp/base/ExternalDataSource.cc Source File

libzypp 17.25.10

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