00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035
00036 #ifdef HAVE_UNISTD_H
00037 #include <unistd.h>
00038 #else
00039 #error need unistd.h
00040 #endif
00041
00042 #ifdef HAVE_STDLIB_H
00043 #include <stdlib.h>
00044 #else
00045 #error need stdlib.h
00046 #endif
00047
00048 #ifdef HAVE_SYS_TYPES_H
00049 #include <sys/types.h>
00050 #else
00051 #error need sys/types.h
00052 #endif
00053
00054 #ifdef HAVE_ERRNO_H
00055 #include <errno.h>
00056 #else
00057 #error need errno.h
00058 #endif
00059
00060 #ifdef HAVE_SYS_STAT_H
00061 #include <sys/stat.h>
00062 #else
00063 #error need sys/stat.h
00064 #endif
00065
00066 #ifdef HAVE_FCNTL_H
00067 #include <fcntl.h>
00068 #else
00069 #error need fcntl.h
00070 #endif
00071
00072 #ifdef HAVE_SYS_TIME_H
00073 #include <sys/time.h>
00074 #else
00075 #error need sys/time.h
00076 #endif
00077
00078 #ifdef HAVE_STRING_H
00079 #include <string.h>
00080 #else
00081 #error need string.h
00082 #endif
00083
00084
00085 #include "Util.h"
00086 #include "Exception.h"
00087 #include "FileSink.h"
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 static const char fileid[] = "$Id: FileSink.cpp,v 1.8 2002/03/28 16:41:49 darkeye Exp $";
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 void
00110 FileSink :: init ( const char * name ) throw ( Exception )
00111 {
00112 fileName = Util::strDup( name);
00113 fileDescriptor = 0;
00114 }
00115
00116
00117
00118
00119
00120 void
00121 FileSink :: strip ( void) throw ( Exception )
00122 {
00123 if ( isOpen() ) {
00124 close();
00125 }
00126
00127 delete[] fileName;
00128 }
00129
00130
00131
00132
00133
00134 FileSink :: FileSink ( const FileSink & fs ) throw ( Exception )
00135 : Sink( fs )
00136 {
00137 int fd;
00138
00139 init( fs.fileName);
00140
00141 if ( (fd = fs.fileDescriptor ? dup( fs.fileDescriptor) : 0) == -1 ) {
00142 strip();
00143 throw Exception( __FILE__, __LINE__, "dup failure");
00144 }
00145
00146 fileDescriptor = fd;
00147 }
00148
00149
00150
00151
00152
00153 FileSink &
00154 FileSink :: operator= ( const FileSink & fs ) throw ( Exception )
00155 {
00156 if ( this != &fs ) {
00157 int fd;
00158
00159
00160 strip();
00161
00162
00163
00164 Sink::operator=( fs );
00165
00166 init( fs.fileName);
00167
00168 if ( (fd = fs.fileDescriptor ? dup( fs.fileDescriptor) : 0) == -1 ) {
00169 strip();
00170 throw Exception( __FILE__, __LINE__, "dup failure");
00171 }
00172
00173 fileDescriptor = fd;
00174 }
00175
00176 return *this;
00177 }
00178
00179
00180
00181
00182
00183 bool
00184 FileSink :: exists ( void ) const throw ()
00185 {
00186 struct stat st;
00187
00188 if ( stat( (const char*)fileName, &st) == -1 ) {
00189 return false;
00190 }
00191
00192 return S_ISREG( st.st_mode);
00193 }
00194
00195
00196
00197
00198
00199 bool
00200 FileSink :: create ( void ) throw ( Exception )
00201 {
00202 int fd;
00203
00204 if ( isOpen() ) {
00205 return false;
00206 }
00207
00208 if ( (fd = ::creat( fileName, S_IRUSR | S_IWUSR)) == -1 ) {
00209 reportEvent( 3, "can't create file", fileName, errno);
00210 return false;
00211 }
00212
00213 ::close( fd);
00214 return true;
00215 }
00216
00217
00218
00219
00220
00221 bool
00222 FileSink :: open ( void ) throw ( Exception )
00223 {
00224 if ( isOpen() ) {
00225 return false;
00226 }
00227
00228 if ( (fileDescriptor = ::open( fileName, O_WRONLY | O_TRUNC, 0)) == -1 ) {
00229 fileDescriptor = 0;
00230 return false;
00231 }
00232
00233 return true;
00234 }
00235
00236
00237
00238
00239
00240 bool
00241 FileSink :: canWrite ( unsigned int sec,
00242 unsigned int usec ) throw ( Exception )
00243 {
00244 fd_set fdset;
00245 struct timeval tv;
00246 int ret;
00247
00248 if ( !isOpen() ) {
00249 return false;
00250 }
00251
00252 FD_ZERO( &fdset);
00253 FD_SET( fileDescriptor, &fdset);
00254 tv.tv_sec = sec;
00255 tv.tv_usec = usec;
00256
00257 ret = select( fileDescriptor + 1, NULL, &fdset, NULL, &tv);
00258
00259 if ( ret == -1 ) {
00260 throw Exception( __FILE__, __LINE__, "select error");
00261 }
00262
00263 return ret > 0;
00264 }
00265
00266
00267
00268
00269
00270 unsigned int
00271 FileSink :: write ( const void * buf,
00272 unsigned int len ) throw ( Exception )
00273 {
00274 ssize_t ret;
00275
00276 if ( !isOpen() ) {
00277 return 0;
00278 }
00279
00280 ret = ::write( fileDescriptor, buf, len);
00281
00282 if ( ret == -1 ) {
00283 if ( errno == EAGAIN ) {
00284 ret = 0;
00285 } else {
00286 throw Exception( __FILE__, __LINE__, "write error", errno);
00287 }
00288 }
00289
00290 return ret;
00291 }
00292
00293
00294
00295
00296
00297 void
00298 FileSink :: close ( void ) throw ( Exception )
00299 {
00300 if ( !isOpen() ) {
00301 return;
00302 }
00303
00304 flush();
00305 ::close( fileDescriptor);
00306 fileDescriptor = 0;
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341