sfile.c (2823B)
1 /* 2 * Copyright (c) 2003 Gunnar Ritter 3 * 4 * This software is provided 'as-is', without any express or implied 5 * warranty. In no event will the authors be held liable for any damages 6 * arising from the use of this software. 7 * 8 * Permission is granted to anyone to use this software for any purpose, 9 * including commercial applications, and to alter it and redistribute 10 * it freely, subject to the following restrictions: 11 * 12 * 1. The origin of this software must not be misrepresented; you must not 13 * claim that you wrote the original software. If you use this software 14 * in a product, an acknowledgment in the product documentation would be 15 * appreciated but is not required. 16 * 17 * 2. Altered source versions must be plainly marked as such, and must not be 18 * misrepresented as being the original software. 19 * 20 * 3. This notice may not be removed or altered from any source distribution. 21 */ 22 /* Sccsid @(#)sfile.c 1.9 (gritter) 6/7/04 */ 23 24 #ifdef __linux__ 25 #undef _FILE_OFFSET_BITS 26 27 #include <sys/types.h> 28 #include <sys/sendfile.h> 29 #include <sys/stat.h> 30 #include <unistd.h> 31 #include <limits.h> 32 #include <errno.h> 33 #include "sfile.h" 34 35 long long 36 sfile(int dfd, int sfd, mode_t mode, long long count) 37 { 38 static int enosys, einval, success; 39 off_t offset; 40 ssize_t sent, total; 41 extern void writerr(void *, int, int); 42 /* 43 * A process is not interruptible while executing a sendfile() 44 * system call. So it is not advisable to to send an entire 45 * file with one call; it is sent in parts so signals can 46 * be delivered in between. 47 */ 48 const ssize_t chunk = 196608; 49 50 /* 51 * If a previous call returned ENOSYS, the operating system does 52 * not support sendfile() at all and it makes no sense to try it 53 * again. 54 * 55 * If a previous call returned EINVAL and there was no successful 56 * call yet, it is very likely that this is a permanent error 57 * condition (on Linux 2.6.0-test4, sendfile() may be used for 58 * socket targets only; older versions don't support tmpfs as 59 * source file system etc.). 60 */ 61 if (enosys || !success && einval || 62 (mode&S_IFMT) != S_IFREG || count > SSIZE_MAX) 63 return 0; 64 offset = lseek(sfd, 0, SEEK_CUR); 65 sent = 0, total = 0; 66 while (count > 0 && (sent = sendfile(dfd, sfd, &offset, 67 count > chunk ? chunk : count)) > 0) { 68 count -= sent, total += sent; 69 } 70 if (total && lseek(sfd, offset, SEEK_SET) == (off_t)-1) 71 return -1; 72 if (count == 0 || sent == 0) { 73 success = 1; 74 return total; 75 } 76 switch (errno) { 77 case ENOSYS: 78 enosys = 1; 79 return 0; 80 case EINVAL: 81 einval = 1; 82 return 0; 83 case ENOMEM: 84 return 0; 85 default: 86 writerr(NULL, count > chunk ? chunk : count, 0); 87 return -1; 88 } 89 } 90 #else /* !__linux__ */ 91 #include <sys/types.h> 92 93 /*ARGSUSED*/ 94 long long 95 sfile(int dfd, int sfd, mode_t mode, long long count) 96 { 97 return 0; 98 } 99 #endif /* __linux__ */