11#include "eager_lib.h"
22
3+ #ifdef __FreeBSD__
4+ #define BUF_SIZE 8192
5+ #define min (a ,b ) (((a)<(b))?(a):(b))
6+ void fatal
7+ (const char * err )
8+ {
9+ perror (err );
10+ abort ();
11+ }
12+
13+ int std_copy (int in_fd , int out_fd , int unused , int size )
14+ {
15+ char buf [4096 ];
16+ int err = -1 ;
17+ int bytes ;
18+ while ( (bytes = read (in_fd , buf , size )) > 0 )
19+ {
20+ if (write (out_fd , buf , bytes ) != bytes )
21+ {
22+ perror ("write:" );
23+ goto out ;
24+ }
25+ }
26+ err = 0 ;
27+ out :
28+ return err ;
29+ }
30+
31+
32+ ssize_t
33+ send_file (int out_fd , int in_fd , off_t * offset , size_t count )
34+ {
35+ off_t orig = 0 ;
36+
37+ if (offset != NULL ) {
38+ /* Save current file offset and set offset to value in '*offset' */
39+ orig = lseek (in_fd , 0 , SEEK_CUR );
40+ if (orig == -1 )
41+ return -1 ;
42+ if (lseek (in_fd , * offset , SEEK_SET ) == -1 )
43+ return -1 ;
44+ }
45+
46+ size_t totSent = 0 ;
47+
48+ while (count > 0 ) {
49+ size_t toRead = min (BUF_SIZE , count );
50+
51+ char buf [BUF_SIZE ];
52+ ssize_t numRead = read (in_fd , buf , toRead );
53+ if (numRead == -1 )
54+ return -1 ;
55+ if (numRead == 0 )
56+ break ; /* EOF */
57+
58+ ssize_t numSent = write (out_fd , buf , numRead );
59+ if (numSent == -1 )
60+ return -1 ;
61+ if (numSent == 0 ) /* Should never happen */
62+ fatal ("send_file: write() transferred 0 bytes" );
63+
64+ count -= numSent ;
65+ totSent += numSent ;
66+ }
67+
68+ if (offset != NULL ) {
69+
70+ /* Return updated file offset in '*offset', and reset the file offset
71+ to the value it had when we were called. */
72+
73+ * offset = lseek (in_fd , 0 , SEEK_CUR );
74+ if (* offset == -1 )
75+ return -1 ;
76+ if (lseek (in_fd , orig , SEEK_SET ) == -1 )
77+ return -1 ;
78+ }
79+ return totSent ;
80+ }
81+ #endif
82+
383int safeOpen3 (const char * pathname , int flags , mode_t mode ) {
484 int fd = open (pathname , flags , mode );
585 if (fd < 0 ) {
@@ -77,10 +157,16 @@ int blockOpenOutput(const char *pathname) {
77157
78158// Returns the number of bytes read, or 0 if the input was done.
79159int readInputWriteToFile (int inputFd , int intermediateWriter , int bufferSize ) {
80-
81- ssize_t res = splice (inputFd , 0 , intermediateWriter , 0 , bufferSize , 0 );
160+ ssize_t res =
161+ #ifdef __linux__
162+ splice (inputFd , 0 , intermediateWriter , 0 , bufferSize , 0 );
163+ #else
164+ // https://man.openbsd.org/sosplice.9
165+ // naive implementation to match our needs, maybe we could we use sosplice, somove
166+ std_copy (inputFd , intermediateWriter , 0 , bufferSize );
167+ #endif
82168 if (res < 0 ) {
83- printf ("Error: Couldn't read from input !\n" );
169+ printf ("Error: Couldn't read from inputaa !\n" );
84170 exit (1 );
85171 }
86172 return res ;
@@ -99,7 +185,7 @@ int bufferedReadInputWriteToFile(int inputFd, int intermediateWriter, int buffer
99185
100186 inputBytesRead = read (inputFd , inputBuf , sizeof (inputBuf ));
101187 if (inputBytesRead < 0 ) {
102- printf ("Error: Couldn't read from input!\n" );
188+ printf ("Error: Couldn't read from input@@ !\n" );
103189 exit (1 );
104190 }
105191 if (inputBytesRead == 0 ) {
@@ -185,7 +271,14 @@ void bufferedOutputRestIntermediateFile(int outputFd, int intermediateWriter, in
185271ssize_t safeWriteOutput (int outputFd , int intermediateReader ,
186272 off_t intermediateFileDiff , int * doneWriting ) {
187273 ssize_t res ;
188- res = sendfile (outputFd , intermediateReader , 0 , intermediateFileDiff );
274+ res =
275+ #ifdef __linux__
276+ sendfile
277+ #else
278+ send_file
279+ #endif
280+ (outputFd , intermediateReader , 0 , intermediateFileDiff );
281+
189282 if (res < 0 && errno != EAGAIN ) {
190283 printf ("ERROR: %s, when outputing %ld bytes!\n" , strerror (errno ), intermediateFileDiff );
191284 exit (1 );
0 commit comments