File: io_sendfile.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (172 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* http://delegate.uec.ac.jp:8081/club/mma/~shimiz98/misc/sendfile.html */
#define _FILE_OFFSET_BITS 64
#include "io_internal.h"
#include "havebsdsf.h"
#include "havesendfile.h"
#include <errno.h>

#if defined(HAVE_BSDSENDFILE)
#define SENDFILE 1
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
  off_t sbytes;
  size_t ct=n>0x7fffffffull ? 0x7fffffff : (size_t)n;
  int r=sendfile(fd,s,off,ct,0,&sbytes,0);
  if (r==-1) {
    io_eagain_write(s);
    return (errno==EAGAIN?(sbytes?sbytes:-1):-3);
  }
  if ((uint64)sbytes<(uint64)ct)
    io_eagain_write(s);
  return n;
}

#elif defined(HAVE_SENDFILE)

#ifdef __hpux__

#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/socket.h>

int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  long long r=sendfile64(out,in,off,bytes,0,0);
  if (r==-1 && errno!=EAGAIN) r=-3;
  if (r!=bytes)
    io_eagain_write(out);
  }
  return r;
}

#elif defined (__sun__) && defined(__svr4__)

#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sendfile.h>

int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  off64_t o=off;
  long long r=sendfile64(out,in,&o,bytes);
  if (r==-1 && errno!=EAGAIN) r=-3;
  if (r!=bytes) {
    io_eagain_write(out);
  }
  return r;
}

#elif defined(_AIX)

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>

int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  struct sf_parms p;
  int destfd=out;
  p.header_data=0;
  p.header_length=0;
  p.file_descriptor=in;
  p.file_offset=off;
  p.file_bytes=bytes;
  p.trailer_data=0;
  p.trailer_length=0;
  if (send_file(&destfd,&p,0)>=0) {
    if (p.bytes_sent != bytes) {
      io_eagain_write(out);
    }
    return p.bytes_sent;
  } if (errno==EAGAIN)
    return -1;
  else
    return -3;
}

#elif defined(__linux__)

#if defined(__GLIBC__)
#include <sys/sendfile.h>
#elif defined(__dietlibc__)
#include <sys/sendfile.h>
#else
#include <linux/unistd.h>
_syscall4(int,sendfile,int,out,int,in,long *,offset,unsigned long,count)
#endif

int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n) {
  off_t o=off;
  off_t i;
  uint64 done=0;
  /* What a spectacularly broken design for sendfile64.
   * The offset is 64-bit for sendfile64, but the count is not. */
  while (n) {
    off_t todo=n>0x7fffffff?0x7fffffff:n;
    i=sendfile(s,fd,&o,todo);
    if (i==todo) {
      done+=todo;
      n-=todo;
      if (n==0) return done;
      continue;
    } else {
      io_eagain_write(s);
      if (i==-1)
	return (errno==EAGAIN?-1:-3);
      else
	return done+i;
    }
  }
  return 0;
}
#endif

#elif defined(__MINGW32__)

#include <windows.h>
#include <mswsock.h>

int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  io_entry* e=iarray_get(&io_fds,out);
  if (!e) { errno=EBADF; return -3; }
  if (e->sendfilequeued==1) {
    /* we called TransmitFile, and it returned. */
    e->sendfilequeued=2;
    errno=e->errorcode;
    if (e->bytes_written==-1) return -1;
    if (e->bytes_written!=bytes) {	/* we wrote less than caller wanted to write */
      e->sendfilequeued=1;	/* so queue next request */
      off+=e->bytes_written;
      bytes-=e->bytes_written;
      e->os.Offset=off;
      e->os.OffsetHigh=(off>>32);
      TransmitFile(out,(HANDLE)in,bytes>0xffff?0xffff:bytes,0,&e->os,0,TF_USE_KERNEL_APC);
    }
    return e->bytes_written;
  } else {
    e->sendfilequeued=1;
    e->os.Offset=off;
    e->os.OffsetHigh=(off>>32);
    /* we always write at most 64k, so timeout handling is possible */
    if (!TransmitFile(out,(HANDLE)in,bytes>0xffff?0xffff:bytes,0,&e->os,0,TF_USE_KERNEL_APC))
      return -3;
  }
}

#else

#include <iob.h>
#include <unistd.h>

static int64 writecb(int64 s,const void* buf,uint64 n) {
  return write(s,buf,n);
}

int64 io_sendfile(int64 out,int64 in,uint64 off,uint64 bytes) {
  return io_mmapwritefile(out,in,off,bytes,writecb);
}

#endif