void Java_com_mathieu_alloclient_Pipe_open(JNIEnv *env, jobject obj) { int fd[2]; if(pipe(fd) != 0) { const char *message; switch(errno) { case EMFILE : message = "Too many file descriptors are in use by the process"; break; case EFAULT : message = "File descriptor array is not valid"; break; case ENFILE : message = "The system limit on the total number of open files has been reached"; break; default: message = "An unknown exception occurred executing 'pipe(int fd[])'"; } throwException(env, "java/io/IOException", message); return; } FileDescriptor in = newFileDescriptor(env, fd[0]); if(in == NULL) return; FileDescriptor out = newFileDescriptor(env, fd[1]); if(out == NULL) return; if(!setObjectField(env, obj, "input", "Ljava/io/FileDescriptor;", in)) return; if(!setObjectField(env, obj, "output", "Ljava/io/FileDescriptor;", out)) return; }
/* * Class: com.sun.honeycomb.archivers.LibArchive * Sig: void getStream(long handle, Object parent) */ JNIEXPORT jboolean JNICALL Java_com_sun_honeycomb_archivers_LibArchive_getStream (JNIEnv* env, jclass cls, jlong handle, jobject parent) { /* This can only be called for a yet-unopened archive object */ arc_handle_t ar = jlong2handle(handle); if (arxive_is_open(ar)) return 0; /* * Set up the pipe, and return the two ends; the read end as an fd * for use by libarchive, and the write end as a FileDescriptor * for the source of the archive. Return the two value to caller * by calling parent->setCookie(long cookie, FileDescriptor fd) */ int fds[2]; if (pipe(fds) < 0) return 0; jobject fd_obj = newFileDescriptor(env, cls, fds[1]); jlong cookie = fds[0]; jclass parent_class = (*env)->GetObjectClass(env, parent); if (parent_class == 0) return 0; jmethodID callback = (*env)->GetMethodID(env, parent_class, "setCookie", "(JLjava/io/FileDescriptor;)V"); if (callback == 0) return 0; (*env)->CallVoidMethod(env, parent, callback, cookie, fd_obj); return 1; }
// // Intercept open - note that this appears NOT to use the standard ABI! // static VMIOS_INTERCEPT_FN(openInt) { Uns32 pathnameAddr; Int32 flags; Int32 mode; Uns32 sp; // obtain function arguments getArg(processor, object, 0, &pathnameAddr); vmiosRegRead(processor, object->sp, &sp); // get file name from data domain memDomainP domain = vmirtGetProcessorDataDomain(processor); memEndian endian = vmirtGetProcessorDataEndian(processor); const char *pathname = vmirtGetString(domain, pathnameAddr); // get flags & mode from data domain vmirtReadNByteDomain(domain, sp, &flags, sizeof(flags), 0, True); vmirtReadNByteDomain(domain, sp+4, &mode, sizeof(mode), 0, True); // swap endianness if required if(endian != ENDIAN_NATIVE) { flags = swap4(flags); mode = swap4(mode); } // implement open Int32 result = vmiosOpen(processor, pathname, flags, mode); // save file descriptor in simulated descriptor table if successful if(result>=0) { Int32 fdMap = newFileDescriptor(object, context); object->fileDescriptors[fdMap] = result; result = fdMap; } // return result setErrnoAndResult(processor, object, result, context); }