You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/09 08:23:47 UTC

svn commit: r823419 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/ main/native/include/arch/windows/ main/native/os/unix/ main/native/os/win32/ test/org/apache/commons/runtime/

Author: mturk
Date: Fri Oct  9 06:23:46 2009
New Revision: 823419

URL: http://svn.apache.org/viewvc?rev=823419&view=rev
Log:
Implement Java mktemp API

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java Fri Oct  9 06:23:46 2009
@@ -201,6 +201,44 @@
     }
 
     /**
+     * Create new temporary file inide {@code path}.
+     *
+     * @param prefix temporary file prefix.
+     *
+     * @return new temporary {@code FileInstance}.
+     */
+    public static FileInstance createTemp(File path, String prefix,
+                                          String sufix, boolean preserve)
+        throws FileNotFoundException, IOException, IllegalArgumentException,
+               SecurityException
+    {
+        if (prefix == null) {
+            throw new IllegalArgumentException();
+        }
+        Descriptor fd = FileWrapper.mktemp(path, prefix, sufix, preserve);
+        return new FileInstance(fd);
+    }
+
+    /**
+     * Create new temporary file inside current directory.
+     *
+     * @param prefix file prefix.
+     *
+     * @return new temporary {@code FileInstance}.
+     */
+    public static FileInstance createTemp(String prefix, String sufix,
+                                          boolean preserve)
+        throws FileNotFoundException, IOException, IllegalArgumentException,
+               SecurityException
+    {
+        if (prefix == null) {
+            throw new IllegalArgumentException();
+        }
+        Descriptor fd = FileWrapper.mktemp(prefix, sufix, preserve);
+        return new FileInstance(fd);
+    }
+
+    /**
      * Clear the file errors.
      * <p>
      * Method clears all pending file errors and resets the

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java Fri Oct  9 06:23:46 2009
@@ -211,6 +211,42 @@
         return Descriptor.NULL;
     }
 
+    private static native Descriptor mktemp0(String path, String prefix,
+                                             String sufix, boolean preserve)
+        throws FileNotFoundException, IOException, SecurityException;
+    /**
+     * Create temp file Descriptor.
+     *
+     * @return create temp file descriptor.
+     * @throws IOException on error.
+     */
+    public static Descriptor mktemp(File path, String prefix, String sufix,
+                                    boolean preserve)
+        throws FileNotFoundException, IOException, IllegalArgumentException,
+               SecurityException
+    {
+        if (path == null) {
+            throw new IllegalArgumentException();
+        }
+        // Call the native method.
+        return mktemp0(path.getPath(), prefix, sufix, preserve);
+    }
+
+    /**
+     * Create temp file Descriptor.
+     *
+     * @return create temp file descriptor.
+     * @throws IOException on error.
+     */
+    public static Descriptor mktemp(String prefix, String sufix,
+                                    boolean preserve)
+        throws FileNotFoundException, IOException, IllegalArgumentException,
+               SecurityException
+    {
+        // Call the native method.
+        return mktemp0(null, prefix, sufix, preserve);
+    }
+
     private static native int lock0(int fd, int type)
         throws IOException;
     /**

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Fri Oct  9 06:23:46 2009
@@ -209,6 +209,7 @@
 char    *x_strdup_utf8(const wchar_t *);
 LPWSTR   MbsToWcs(DWORD, LPCSTR, LPWSTR, DWORD);
 LPSTR    WcsToMbs(DWORD, LPCWSTR, LPSTR, DWORD);
+wchar_t *acr_GetAbsolutePath(JNIEnv *, HANDLE, const wchar_t *);
 
 /**
  * Functions from posix.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c Fri Oct  9 06:23:46 2009
@@ -376,6 +376,49 @@
     return fdo;
 }
 
+ACR_IO_EXPORT_DECLARE(jobject, FileWrapper, mktemp0)(ACR_JNISTDARGS,
+                                                     jstring path,
+                                                     jstring prefix,
+                                                     jstring sufix,
+                                                     jboolean preserve)
+{
+    int rc =  0;
+    int fo;
+    jobject fdo = NULL;
+    int ip = preserve == JNI_TRUE ? 1 : 0;
+
+    WITH_CSTR(prefix) {
+        WITH_ZCSTR(path) {
+            WITH_ZCSTR(sufix) {
+                fprintf(stdout, "Opening %s - %s - %s %d\n",
+                        J2S(path), J2S(prefix),
+                                      J2S(sufix), ip);
+                fflush(stdout);
+                fo = ACR_TempFileMake(_E, J2S(path), J2S(prefix),
+                                      J2S(sufix), ip);
+                if (fo > 0) {
+                    /* Create File Descriptor Object */
+                    fdo = ACR_DescriptorCreate(_E, ACR_DT_FILE, fo, NULL,
+                                               descriptor_cleanup);
+                    if (fdo) {
+                        acr_file_t *fp = (acr_file_t *)ACR_IOH_FDATA(fo);
+                        fp->descriptor = (*_E)->NewWeakGlobalRef(_E, fdo);
+                    }
+                    else
+                        rc = ACR_GET_OS_ERROR();
+                }
+                else
+                    rc = ACR_GET_OS_ERROR();
+            } END_WITH_CSTR(sufix);
+        } END_WITH_CSTR(path);
+    } END_WITH_CSTR(prefix);
+
+    if (rc) {
+        ACR_THROW_IO_IF_ERR(rc);
+    }
+    return fdo;
+}
+
 static int wait_for_io_or_timeout(acr_file_t *f, int for_read)
 {
     int rc;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/temps.c Fri Oct  9 06:23:46 2009
@@ -124,7 +124,7 @@
     }
     fp->fd       = fd;
     fp->type     = ACR_FT_REG;
-    fp->name     = ACR_StrdupA(_E, THROW_NMARK, name);
+    fp->name     = acr_GetAbsolutePath(_E, name);
     fp->blocking = BLK_ON;
     fp->timeout  = -1;
     fp->flags    = ACR_FOPEN_READ | ACR_FOPEN_WRITE | ACR_FOPEN_CREATE;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/fsysio.c Fri Oct  9 06:23:46 2009
@@ -239,6 +239,11 @@
     fp->fd    = fh;
     fp->flags = flags;
     fp->type  = ACR_FT_REG; /* Presume it's a regular file */
+    fp->name  = acr_GetAbsolutePath(_E, fh, fname);
+    if (fp->name) {
+        rc = ACR_GET_OS_ERROR();
+        goto finally;
+    }
     if (flags & ACR_FOPEN_NONBLOCK) {
         fp->blocking = BLK_OFF;
         fp->timeout  = 0;
@@ -255,35 +260,6 @@
         rc = ACR_GET_OS_ERROR();
         goto finally;
     }
-    if (ACR_HAVE_LATE_DLL_FUNC(GetFinalPathNameByHandleW)) {
-        DWORD nl;
-        nl = GetFinalPathNameByHandleW(fh, NULL, 0, FILE_NAME_NORMALIZED);
-        if (nl == 0) {
-            rc = ACR_GET_OS_ERROR();
-            goto finally;
-        }
-        fp->name = ACR_MALLOC(wchar_t, nl + 1);
-        if (!fp->name) {
-            rc = ACR_ENOMEM;
-            goto finally;
-        }
-        GetFinalPathNameByHandleW(fh, fp->name, nl, FILE_NAME_NORMALIZED);
-    }
-    else {
-        DWORD nl;
-
-        nl = GetFullPathNameW(fname, 0, NULL, NULL);
-        if (nl == 0) {
-            rc = ACR_GET_OS_ERROR();
-            goto finally;
-        }
-        fp->name = ACR_MALLOC(wchar_t, nl + 1);
-        if (!fp->name) {
-            rc = ACR_ENOMEM;
-            goto finally;
-        }
-        GetFullPathNameW(fname, nl, fp->name, NULL);
-    }
     if (flags & ACR_FOPEN_APPEND) {
         __try {
             /* On Vista+ exception is no longer thrown
@@ -437,6 +413,45 @@
     return fdo;
 }
 
+ACR_IO_EXPORT_DECLARE(jobject, FileWrapper, mktemp0)(ACR_JNISTDARGS,
+                                                     jstring path,
+                                                     jstring prefix,
+                                                     jstring sufix,
+                                                     jboolean preserve)
+{
+    int rc =  0;
+    int fo;
+    jobject fdo = NULL;
+    int ip = preserve == JNI_TRUE ? 1 : 0;
+
+    WITH_WSTR(prefix) {
+        WITH_ZWSTR(path) {
+            WITH_ZWSTR(sufix) {
+                fo = ACR_TempFileMake(_E, J2W(path), J2W(prefix),
+                                      J2W(sufix), ip);
+                if (fo > 0) {
+                    /* Create File Descriptor Object */
+                    fdo = ACR_DescriptorCreate(_E, ACR_DT_FILE, fo, NULL,
+                                               descriptor_cleanup);
+                    if (fdo) {
+                        acr_file_t *fp = (acr_file_t *)ACR_IOH_FDATA(fo);
+                        fp->descriptor = (*_E)->NewWeakGlobalRef(_E, fdo);
+                    }
+                    else
+                        rc = ACR_GET_OS_ERROR();
+                }
+                else
+                    rc = ACR_GET_OS_ERROR();
+            } END_WITH_WSTR(sufix);
+        } END_WITH_WSTR(path);
+    } END_WITH_WSTR(prefix);
+
+    if (rc) {
+        ACR_THROW_IO_IF_ERR(rc);
+    }
+    return fdo;
+}
+
 static int do_lock(acr_file_t *f, DWORD flags)
 {
     if (f->blocking == BLK_OFF) {

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/temps.c Fri Oct  9 06:23:46 2009
@@ -16,8 +16,6 @@
 
 #include "acr.h"
 #include "acr_private.h"
-#include "acr_arch.h"
-#include "acr_port.h"
 #include "acr_error.h"
 #include "acr_string.h"
 #include "acr_memory.h"
@@ -25,6 +23,10 @@
 #include "acr_env.h"
 #include "acr_file.h"
 
+#define ACR_WANT_LATE_DLL
+#include "acr_arch.h"
+#include "acr_port.h"
+
 #define TMP_PATH_MAX    (ACR_MBUFF_SIZ - 12)
 
 static const wchar_t *_try_envs[] = {
@@ -306,7 +308,7 @@
     }
     fp->fd       = fd;
     fp->type     = ACR_FT_REG;
-    fp->name     = ACR_StrdupW(_E, THROW_NMARK, name);
+    fp->name     = acr_GetAbsolutePath(_E, fd, name);
     fp->blocking = BLK_ON;
     fp->timeout  = INFINITE;
     fp->flags    = ACR_FOPEN_READ | ACR_FOPEN_WRITE | ACR_FOPEN_CREATE;

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/wutil.c Fri Oct  9 06:23:46 2009
@@ -16,7 +16,6 @@
 
 #include "acr.h"
 #include "acr_private.h"
-#include "acr_arch.h"
 #include "acr_clazz.h"
 #include "acr_error.h"
 #include "acr_memory.h"
@@ -24,6 +23,9 @@
 #include "acr_descriptor.h"
 #include "acr_crypto.h"
 
+#define ACR_WANT_LATE_DLL
+#include "acr_arch.h"
+
 #include <direct.h>
 #define NON_UNC_PATH_LENGTH 248
 
@@ -389,3 +391,39 @@
 
 }
 
+wchar_t *acr_GetAbsolutePath(JNIEnv *_E, HANDLE fh, const wchar_t *path)
+{
+    wchar_t *fname = NULL;
+    if (ACR_HAVE_LATE_DLL_FUNC(GetFinalPathNameByHandleW)) {
+        DWORD nl;
+        nl = GetFinalPathNameByHandleW(fh, NULL, 0, FILE_NAME_NORMALIZED);
+        if (nl == 0) {
+            rc = ACR_GET_OS_ERROR();
+            goto finally;
+        }
+        fname = ACR_MALLOC(wchar_t, nl + 1);
+        if (!fname) {
+            rc = ACR_ENOMEM;
+            goto finally;
+        }
+        GetFinalPathNameByHandleW(f, fname, nl, FILE_NAME_NORMALIZED);
+    }
+    else {
+        DWORD nl;
+
+        nl = GetFullPathNameW(path, 0, NULL, NULL);
+        if (nl == 0) {
+            rc = ACR_GET_OS_ERROR();
+            goto finally;
+        }
+        fname = ACR_MALLOC(wchar_t, nl + 1);
+        if (!fname) {
+            rc = ACR_ENOMEM;
+            goto finally;
+        }
+        GetFullPathNameW(path, nl, fname, NULL);
+    }
+
+finally:
+    return fname;
+}

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java?rev=823419&r1=823418&r2=823419&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java Fri Oct  9 06:23:46 2009
@@ -131,6 +131,16 @@
         assertEquals("Written", 3, wr);
     }
 
+    public void testFileSysMktemp()
+        throws Exception
+    {
+        FileInstance f = FileInstance.createTemp("tmp.", null, false);
+        assertFalse("FileInstance", f == null);
+        System.out.println();
+        System.out.println("Temporary file " + f.getPath());
+        f.close();
+    }
+
 
 }