Author: mturk
Date: Wed Aug 10 13:55:06 2011
New Revision: 1156186
URL: http://svn.apache.org/viewvc?rev=1156186&view=rev
Log:
Add common unix sendfile code
Added:
commons/sandbox/runtime/trunk/src/main/native/os/unix/sendfile.c (with
props)
Modified:
commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c
commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h
Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1156186&r1=1156185&r2=1156186&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Wed Aug 10
13:55:06 2011
@@ -74,6 +74,7 @@ UNIX_SOURCES=\
$(TOPDIR)/os/unix/shmem.c \
$(TOPDIR)/os/unix/selectset.c \
$(TOPDIR)/os/unix/semaphore.c \
+ $(TOPDIR)/os/unix/sendfile.c \
$(TOPDIR)/os/unix/sockopts.c \
$(TOPDIR)/os/unix/sockstream.c \
$(TOPDIR)/os/unix/time.c \
Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h?rev=1156186&r1=1156185&r2=1156186&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h Wed
Aug 10 13:55:06 2011
@@ -82,13 +82,27 @@ struct acr_sd_t {
#endif
#if defined(WINDOWS)
WCHAR *socketfname;
- LPOVERLAPPED pob; /**< For TransmitFile */
+#endif
+};
+
+typedef struct acr_sf_t acr_sf_t;
+struct acr_sf_t {
+#if !defined(WINDOWS)
+ int fd;
#else
- off_t off;
- size_t len;
+ HANDLE fh;
#endif
+ acr_sd_t *sd;
+ off_t off;
+#if !defined(WINDOWS)
+ struct_stat_t info;
+#else
+ LPOVERLAPPED pob; /**< For TransmitFile */
+#endif
+ size_t count;
};
+
#ifdef __cplusplus
extern "C" {
#endif
Modified: commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c?rev=1156186&r1=1156185&r2=1156186&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c Wed Aug
10 13:55:06 2011
@@ -26,3 +26,45 @@
#include "arch_opts.h"
#include "arch_sync.h"
+#include <poll.h>
+#include <sys/sendfile.h>
+
+ACR_NET_EXPORT(jint, SendFile, send0)(JNI_STDARGS, jlong fp)
+{
+ int rc = 0;
+ ssize_t wr = 0;
+ size_t cnt;
+ acr_sf_t *sf = J2P(fp, acr_sf_t *);
+
+ if ((sf->info.st_size - sf->off) > INT_MAX)
+ cnt = INT_MAX;
+ else
+ cnt = sf->info.st_size - sf->off;
+ if (ACR_HASFLAG(sf->sd, ACR_SO_WPART)) {
+ ACR_CLRFLAG(sf->sd, ACR_SO_WPART);
+ rc = AcrWaitIO(sf->sd->s, sf->sd->timeout, POLLOUT);
+ }
+ while (rc == 0) {
+ if (cnt == 0)
+ break;
+ wr = sendfile(sf->sd->s, /* socket */
+ sf->fd, /* open file descriptor of the file to be
sent */
+ &sf->off, /* where in the file to start */
+ cnt); /* number of bytes to send */
+ if (wr == -1) {
+ if ((errno == EAGAIN || errno == EWOULDBLOCK) && (sf->sd->timeout
> 0))
+ rc = AcrWaitIO(sf->sd->s, sf->sd->timeout, POLLOUT);
+ else if (errno != EINTR)
+ rc = errno;
+ }
+ else
+ break;
+ }
+ if (rc != 0) {
+ wr = -1;
+ ACR_THROW_NET_ERROR(rc);
+ }
+ else if (sf->sd->timeout > 0 && wr < cnt)
+ sf->sd->flags |= ACR_SO_WPART;
+ return (jint)wr;
+}
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h?rev=1156186&r1=1156185&r2=1156186&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/arch_defs.h Wed Aug
10 13:55:06 2011
@@ -156,5 +156,4 @@ typedef struct stat struct_stat_
#else
#endif
-
#endif /* _ACR_ARCH_DEFS_H_ */
Added: commons/sandbox/runtime/trunk/src/main/native/os/unix/sendfile.c
URL:
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/sendfile.c?rev=1156186&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/sendfile.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/sendfile.c Wed Aug 10
13:55:06 2011
@@ -0,0 +1,133 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/jnitypes.h"
+#include "acr/error.h"
+#include "acr/debug.h"
+#include "acr/iofd.h"
+#include "acr/memory.h"
+#include "acr/netapi.h"
+#include "acr/string.h"
+#include "acr/unsafe.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+#include "arch_sync.h"
+#include <poll.h>
+
+ACR_NET_EXPORT(jlong, SendFile, open0)(JNI_STDARGS, jlong sp, jstring fname)
+{
+ int rc = 0;
+ acr_sf_t *sf;
+ acr_sd_t *sd = J2P(sp, acr_sd_t *);
+
+ if (sd == 0 || sd->s == -1) {
+ ACR_THROW_NET_ERROR(ACR_EBADF);
+ return 0;
+ }
+
+ if ((sf = ACR_TALLOC(acr_sf_t)) == 0)
+ return 0;
+ sf->sd = sd;
+ sf->fd = -1;
+ WITH_CSTR(fname) {
+ int flags = O_RDONLY;
+#ifdef O_LARGEFILE
+ flags |= O_LARGEFILE;
+#endif
+#ifdef O_CLOEXEC
+ flags |= O_CLOEXEC;
+#endif
+ sf->fd = open(J2S(fname), flags);
+ if (sf->fd != -1) {
+#ifndef O_CLOEXEC
+ if ((rc = AcrCloseOnExec(sf->fd, 1)) == 0)
+#endif
+ {
+ if (fstat(sf->fd, &sf->info) != 0)
+ rc = errno;
+ }
+ }
+ } DONE_WITH_STR(fname);
+ if (rc != 0) {
+ s_close(sf->fd);
+ AcrFree(sf);
+ ACR_THROW_NET_ERROR(rc);
+ return 0;
+ }
+ AcrSdRetain(sd);
+ return P2J(sf);
+}
+
+ACR_NET_EXPORT(jlong, SendFile, open1)(JNI_STDARGS, jlong sp, jobject fd)
+{
+ int rc = 0;
+ int nd;
+ acr_sf_t *sf;
+ acr_sd_t *sd = J2P(sp, acr_sd_t *);
+
+ if (sd == 0 || fd == 0 || sd->s == -1) {
+ ACR_THROW_NET_ERROR(ACR_EBADF);
+ return 0;
+ }
+ nd = AcrGetFileDescriptorFd(env, fd);
+ if (nd == -1) {
+ ACR_THROW_NET_ERROR(ACR_EIO);
+ return 0;
+ }
+ if ((sf = ACR_TALLOC(acr_sf_t)) == 0)
+ return 0;
+ sf->sd = sd;
+ /* Duplicate FileDescriptor so
+ * we don't have to reference it
+ */
+#if HAVE_DUP3
+ sf->fd = dup3(nd, -1, O_CLOEXEC);
+#else
+ sf->fd = dup2(nd, -1);
+ if (sf->fd != -1) {
+ rc = AcrCloseOnExec(sf->fd, 1);
+ if (rc != 0) {
+ r_close(sf->fd);
+ errno = rc;
+ sf->fd = -1;
+ }
+ }
+#endif
+ if (sf->fd == -1) {
+ ACR_THROW_NET_ERRNO();
+ AcrFree(sf);
+ return 0;
+ }
+ if (fstat(sf->fd, &sf->info) != 0) {
+ rc = ACR_GET_OS_ERROR();
+ AcrFree(sf);
+ ACR_THROW_NET_ERROR(rc);
+ return 0;
+ }
+ AcrSdRetain(sd);
+ return P2J(sf);
+}
+
+ACR_NET_EXPORT(jint, SendFile, close0)(JNI_STDARGS, jlong fp)
+{
+ int rc = 0;
+ acr_sf_t *sf = J2P(fp, acr_sf_t *);
+
+ rc = s_close(sf->fd);
+ AcrSdRelease(sf->sd);
+ AcrFree(sf);
+ return rc;
+}
Propchange: commons/sandbox/runtime/trunk/src/main/native/os/unix/sendfile.c
------------------------------------------------------------------------------
svn:eol-style = native