You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ar...@apache.org on 2006/03/01 16:10:46 UTC

svn commit: r382047 - in /incubator/harmony/enhanced/jchevm: configure.ac libjc/zip.c libjc/zip.h

Author: archie
Date: Wed Mar  1 07:10:46 2006
New Revision: 382047

URL: http://svn.apache.org/viewcvs?rev=382047&view=rev
Log:
Add support for systems that don't have the pread(2) function. For these systems,
we use a mutex associated with each ZIP file to protect the file pointer while
we read out ZIP file entries.

Modified:
    incubator/harmony/enhanced/jchevm/configure.ac
    incubator/harmony/enhanced/jchevm/libjc/zip.c
    incubator/harmony/enhanced/jchevm/libjc/zip.h

Modified: incubator/harmony/enhanced/jchevm/configure.ac
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/jchevm/configure.ac?rev=382047&r1=382046&r2=382047&view=diff
==============================================================================
--- incubator/harmony/enhanced/jchevm/configure.ac (original)
+++ incubator/harmony/enhanced/jchevm/configure.ac Wed Mar  1 07:10:46 2006
@@ -106,8 +106,9 @@
 AC_CHECK_FUNCS([getcwd gettimeofday memmove memset munmap], , \
     [AC_MSG_ERROR([required function missing])])
 
-# See if pthread_attr_setstack() is available
+# Check for optional library functions
 AC_CHECK_FUNCS([pthread_attr_setstack])
+AC_CHECK_FUNCS([pread])
 
 # Check for declarations
 AC_CHECK_DECL(environ, [AC_DEFINE([HAVE_DECL_ENVIRON], [1],

Modified: incubator/harmony/enhanced/jchevm/libjc/zip.c
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/jchevm/libjc/zip.c?rev=382047&r1=382046&r2=382047&view=diff
==============================================================================
--- incubator/harmony/enhanced/jchevm/libjc/zip.c (original)
+++ incubator/harmony/enhanced/jchevm/libjc/zip.c Wed Mar  1 07:10:46 2006
@@ -47,8 +47,14 @@
 
 	/* Create new zip structure */
 	if ((zip = _jc_vm_zalloc(env, sizeof(*zip))) == NULL)
-		goto fail;
+		return NULL;
 	zip->fd = -1;
+#if !HAVE_PREAD
+	if (_jc_mutex_init(env, &zip->mutex) != JNI_OK) {
+		_jc_vm_free(&zip);
+		return NULL;
+	}
+#endif
 	if ((zip->path = _jc_vm_strdup(env, path)) == NULL)
 		goto fail;
 
@@ -222,6 +228,9 @@
 	_jc_vm_free(&zip->path);
 	if (zip->fd != -1)
 		close(zip->fd);
+#if !HAVE_PREAD
+	_jc_mutex_destroy(&zip->mutex);
+#endif
 	_jc_vm_free(&zip);
 }
 
@@ -295,6 +304,7 @@
 static jint
 _jc_zip_unstore(_jc_env *env, _jc_zip *zip, _jc_zip_entry *zent, void *data)
 {
+	int status = JNI_ERR;
 	int i;
 	int r;
 
@@ -306,25 +316,50 @@
 		return JNI_ERR;
 	}
 
+#if !HAVE_PREAD
+	/* Lock file pointer, then change it */
+	_JC_MUTEX_LOCK(env, zip->mutex);
+	if (lseek(zip->fd, zent->offset, SEEK_SET) == (off_t)-1) {
+		_JC_EX_STORE(env, IOException, "can't seek to entry `%s'"
+		    " in ZIP file `%s': %s", zent->name, zip->path,
+		    strerror(errno));
+		goto fail;
+	}
+#endif
+
 	/* Read data */
 	for (i = 0; i < zent->comp_len; i += r) {
-		if ((r = pread(zip->fd, (char *)data + i,
-		    zent->comp_len - i, zent->offset + i)) == -1) {
+#if !HAVE_PREAD
+		r = read(zip->fd, (char *)data + i, zent->comp_len - i);
+#else
+		r = pread(zip->fd, (char *)data + i,
+		    zent->comp_len - i, zent->offset + i);
+#endif
+		if (r == -1) {
 			_JC_EX_STORE(env, IOException, "can't read entry `%s'"
 			    " in ZIP file `%s': %s", zent->name, zip->path,
 			    strerror(errno));
-			return JNI_ERR;
+			goto fail;
 		}
 		if (r == 0) {
 			_JC_EX_STORE(env, IOException, "premature EOF reading"
 			    " entry `%s' in ZIP file `%s'", zent->name,
 			    zip->path);
-			return JNI_ERR;
+			goto fail;
 		}
 	}
 
+	/* Success */
+	status = JNI_OK;
+
+fail:
+#if !HAVE_PREAD
+	/* Unlock file pointer */
+	_JC_MUTEX_UNLOCK(env, zip->mutex);
+#endif
+
 	/* Done */
-	return JNI_OK;
+	return status;
 }
 
 /*
@@ -360,6 +395,17 @@
 		_JC_ASSERT(JNI_FALSE);
 	}
 
+#if !HAVE_PREAD
+	/* Lock file pointer, then change it */
+	_JC_MUTEX_LOCK(env, zip->mutex);
+	if (lseek(zip->fd, zent->offset, SEEK_SET) == (off_t)-1) {
+		_JC_EX_STORE(env, IOException, "can't seek to entry `%s'"
+		    " in ZIP file `%s': %s", zent->name, zip->path,
+		    strerror(errno));
+		goto fail;
+	}
+#endif
+
 	/* Read and inflate data */
 	for (i = 0; i < zent->comp_len; i += r) {
 		char buf[512];
@@ -370,8 +416,12 @@
 		to_read = zent->comp_len - i;
 		if (to_read > sizeof(buf))
 			to_read = sizeof(buf);
-		if ((r = pread(zip->fd, buf,
-		    to_read, zent->offset + i)) == -1) {
+#if !HAVE_PREAD
+		r = read(zip->fd, buf, to_read);
+#else
+		r = pread(zip->fd, buf, to_read, zent->offset + i);
+#endif
+		if (r == -1) {
 			_JC_EX_STORE(env, IOException, "error reading entry"
 			    " `%s' in ZIP file `%s': %s", zent->name,
 			    zip->path, strerror(errno));
@@ -389,6 +439,9 @@
 		 * A bug in zlib somewhere causes this to happen sometimes.
 		 */
 		if (zs.avail_out == 0) {
+#if !HAVE_PREAD
+			_JC_MUTEX_UNLOCK(env, zip->mutex);
+#endif
 			r = inflateEnd(&zs);
 			_JC_ASSERT(r == Z_OK);
 			return JNI_OK;
@@ -406,6 +459,9 @@
 		case Z_STREAM_END:
 			if (zs.avail_out != 0 || i + r != zent->comp_len)
 				goto bad_length;
+#if !HAVE_PREAD
+			_JC_MUTEX_UNLOCK(env, zip->mutex);
+#endif
 			r = inflateEnd(&zs);
 			_JC_ASSERT(r == Z_OK);
 			return JNI_OK;
@@ -433,6 +489,9 @@
 
 fail:
 	/* Clean up after failure */
+#if !HAVE_PREAD
+	_JC_MUTEX_UNLOCK(env, zip->mutex);
+#endif
 	inflateEnd(&zs);
 	return JNI_ERR;
 }

Modified: incubator/harmony/enhanced/jchevm/libjc/zip.h
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/jchevm/libjc/zip.h?rev=382047&r1=382046&r2=382047&view=diff
==============================================================================
--- incubator/harmony/enhanced/jchevm/libjc/zip.h (original)
+++ incubator/harmony/enhanced/jchevm/libjc/zip.h Wed Mar  1 07:10:46 2006
@@ -15,7 +15,7 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  *
- * $Id: zip.h,v 1.1 2005/05/24 01:09:38 archiecobbs Exp $
+ * $Id$
  */
 
 #ifndef _ZIP_H_
@@ -53,6 +53,12 @@
 	int		fd;
 	char		*path;
 	int		num_entries;
+#if !HAVE_PREAD
+	pthread_mutex_t	mutex;		/* protects seek pointer */
+#ifndef NDEBUG
+	_jc_env		*mutex_owner;
+#endif
+#endif
 	_jc_zip_entry	*entries;
 };