From 5835344277d10d069d792c59d67e41313d589d68 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Thu, 23 Jul 2009 23:17:59 +0200 Subject: [PATCH] Rewrote the "find hard disks on windows" to use a dll instead. The reasons: A) FSUTIL is internationalized, so on non-english installs, we can't figure out if a disk is fixed. B) I trust this better than fsutil for user control access (a.k.a. popups from hell) on vista. C) fsutil.exe is in C:\windows\system32 so pretty sure it would be there, but there's always a chance it's not in the PATH, or otherwise not accessible. --- src/lombok/installer/EclipseFinder.java | 103 ++++++++++----------- src/lombok/installer/WindowsDriveInfo.dll | Bin 0 -> 14472 bytes src/lombok/installer/WindowsDriveInfo.java | 98 +++++++++++++++++++- winsrc/.gitignore | 2 + winsrc/lombok_installer_WindowsDriveInfo.c | 29 ++++++ winsrc/lombok_installer_WindowsDriveInfo.h | 29 ++++++ 6 files changed, 200 insertions(+), 61 deletions(-) create mode 100644 src/lombok/installer/WindowsDriveInfo.dll create mode 100644 winsrc/.gitignore create mode 100644 winsrc/lombok_installer_WindowsDriveInfo.c create mode 100644 winsrc/lombok_installer_WindowsDriveInfo.h diff --git a/src/lombok/installer/EclipseFinder.java b/src/lombok/installer/EclipseFinder.java index ba3c7e14..5356d6fc 100644 --- a/src/lombok/installer/EclipseFinder.java +++ b/src/lombok/installer/EclipseFinder.java @@ -23,17 +23,17 @@ package lombok.installer; import static java.util.Arrays.asList; -import java.io.BufferedReader; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URI; import java.net.URLDecoder; import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -62,70 +62,59 @@ class EclipseFinder { } } + private static final AtomicBoolean windowsDriveInfoLibLoaded = new AtomicBoolean(false); + private static void loadWindowsDriveInfoLib() throws IOException, FileNotFoundException { + if ( !windowsDriveInfoLibLoaded.compareAndSet(false, true) ) return; + + InputStream in = EclipseFinder.class.getResourceAsStream("WindowsDriveInfo.dll"); + File dllFile; + try { + File temp = File.createTempFile("lombok", ".mark"); + dllFile = new File(temp.getParentFile(), "lombok-WindowsDriveInfo.dll"); + temp.delete(); + dllFile.deleteOnExit(); + try { + FileOutputStream out = new FileOutputStream(dllFile); + try { + byte[] b = new byte[32000]; + while ( true ) { + int r = in.read(b); + if ( r == -1 ) break; + out.write(b, 0, r); + } + } finally { + out.close(); + } + } catch ( IOException e ) { + if ( dllFile.exists() && dllFile.canRead() ) { + //Fall through - if there is a file named lombok-WindowsDriveInfo.dll, we'll try it. + } else throw e; + } + } finally { + in.close(); + } + + System.load(dllFile.getAbsolutePath()); + } + /** * Returns all drive letters on windows, regardless of what kind of drive is represented. * - * Relies on the 'fsutil.exe' program. I have no idea if you can call it without triggering a gazillion - * security warnings on Vista. I did a cursory test on an out-of-the-box Windows XP and that seems to work. - * * @return A List of drive letters, such as ["A", "C", "D", "X"]. */ - static List getDrivesOnWindows() throws IOException { - ProcessBuilder builder = new ProcessBuilder("fsutil.exe", "fsinfo", "drives"); - builder.redirectErrorStream(true); - Process process = builder.start(); - InputStream in = process.getInputStream(); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); + static List getDrivesOnWindows() throws Throwable { + loadWindowsDriveInfoLib(); List drives = new ArrayList(); - String line; - while ( (line = br.readLine()) != null ) { - if (line.startsWith("Drives:")) { - line = line.substring(7); - } - line = line.trim(); - if (line.isEmpty()) { - continue; - } - for ( String driveLetter : line.split("\\:\\\\\\s*") ) { - drives.add(driveLetter.trim()); - } - } - - Iterator it = drives.iterator(); - while ( it.hasNext() ) { - if ( !isLocalDriveOnWindows(it.next()) ) it.remove(); + WindowsDriveInfo info = new WindowsDriveInfo(); + for ( String drive : info.getLogicalDrives() ) { + if ( info.isFixedDisk(drive) ) drives.add(drive); } return drives; } - - /** - * @return true if the letter represents a local fixed disk, false if its a disk drive, optical drive, - * USB stick, network drive, or any other kind of drive. Substed (virtual) drives that are an alias to - * a directory on a local disk cause a 'return true', but this is intentional. - */ - static boolean isLocalDriveOnWindows(String driveLetter) { - if ( driveLetter == null || driveLetter.length() == 0 ) return false; - try { - ProcessBuilder builder = new ProcessBuilder("c:\\windows\\system32\\fsutil.exe", "fsinfo", "drivetype", driveLetter + ":"); - builder.redirectErrorStream(true); - Process process = builder.start(); - InputStream in = process.getInputStream(); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); - - String line; - while ( (line = br.readLine()) != null ) { - if ( line.substring(5).equalsIgnoreCase("Fixed Drive") ) return true; - } - - return false; - } catch ( Exception e ) { - return false; - } - } - + /** * Returns a list of paths of Eclipse installations. * Eclipse installations are found by checking for the existence of 'eclipse.exe' in the following locations: @@ -143,7 +132,9 @@ class EclipseFinder { List driveLetters = asList("C"); try { driveLetters = getDrivesOnWindows(); - } catch ( IOException ignore ) {} + } catch ( Throwable ignore ) { + ignore.printStackTrace(); + } for ( String letter : driveLetters ) { File f = new File(letter + ":\\"); diff --git a/src/lombok/installer/WindowsDriveInfo.dll b/src/lombok/installer/WindowsDriveInfo.dll new file mode 100644 index 0000000000000000000000000000000000000000..eb7fa49a2c98dcb44b2e8662ee5f75d33e1aa034 GIT binary patch literal 14472 zcmeHOe{fXCecu!3AO{SxBPAq5$l)ppCf0E}y*pzE+7qV>0?VKvVW=^AI_cg?2lvZ& z?=W~sN49h`x;d-XA580Z$P5?g44o2}bVy7vlU@;xtf}2$oU~!$cxq2VD_2a$c5OnY z)Ys2{`tJT}G_Fvjf2r4nPOQBv~p-}!$A(Y@pg zXO^&2cfPvvw5{XSmE8l8v?G~H^rwO`M_(`=Pw0+b&5_E)9g(=BZEKe!mI!IiyYBjA zqZHj4V64NokUc#)u$e7mY>$nxH`yotV3Dn6Evp4lJ#r0WJ#{E4JCNCk#_PbzZm{r@ zde|T`z+W%7albeN@eu1M~II#djUtscZXn&V5 z8f3nrQxo!dj1Vhs8`)WrxV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Y-WhN8tzl~Y6qNd z-bfJM!n6TZn4ebhk&GVcb1Ib>(R(TZ&gPwIr5PydE03~DK>-qpB~{gOknN*GYjxZ9 zE!~?Q38)yJ}?5&EEuBBp;I8KO2 zt{&z7NXBTSR~$#HLCifm7?c*3kKMU7+(@wvzy*xCnh2BnzP)N+A{L7XE36$U4JYmU zG;UukxF?aqIQFIwq;)N(?$c7~NFpw5$33gMKWchooc@bA@ZN~otEv(ao2{ziIlZ!X z?zjcEv~h)onFCrdS!q6ZQr=x4P+U#2QnW-2#fT}KN~A5`Kxjy2V$@1hdthCMrNcCH$3PrC>${p;R(XNj`H0`cjSfX!+F3ZZs?5Y~Pxu=$~ RelWXD8_K(e5nAE({{lEN%^Ls! literal 0 HcmV?d00001 diff --git a/src/lombok/installer/WindowsDriveInfo.java b/src/lombok/installer/WindowsDriveInfo.java index 4689e535..a0cfcbc1 100644 --- a/src/lombok/installer/WindowsDriveInfo.java +++ b/src/lombok/installer/WindowsDriveInfo.java @@ -1,15 +1,70 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.installer; import java.util.ArrayList; import java.util.List; +/** + * This class uses native calls on windows to figure out all drives, + * and, for each drive, if its a harddisk or something else. + * + * The output is essentially equivalent to running windows executable: + *
fsutil fsinfo drives
+ * and + *
fsutil fsinfo drivetype C:
+ * + * except that (A) fsutil requires privileges, (B) someone might have moved + * it out of the path or some such, and (C) its output is internationalized, + * so unless you want to include a table of how to say "Fixed Disk" in 300 + * languages, this really is a superior solution. + *

+ * To compile it, you'll need windows, as well as MinGW: + * http://sourceforge.net/projects/mingw/files/ + *

+ * Fetch gcc 4.0.4+, you don't need anything extra. Toss /c/mingw/bin in + * your git bash prompt's path (/etc/profile) and then run: + * + * $ gcc -c \ + * -I "/c/Program Files/Java/jdk1.6.0_14/include" \ + * -I "/c/Program Files/Java/jdk1.6.0_14/include/win32" \ + * -D__int64="long long" lombok_installer_WindowsDriveInfo.c + * + * $ dllwrap.exe --add-stdcall-alias \ + * -o WindowsDriveInfo.dll \ + * lombok_installer_WindowsDriveInfo.o + * + * You may get a warning along the lines of "Creating an export definition". + * This is expected behaviour. + * + * The DLL produced has been checked into the git repository so you won't + * need to build this file again unless you make some changes to it. + */ public class WindowsDriveInfo { + /** + * Return a list of all available drive letters, such as ["A", "C", "D"]. + */ public List getLogicalDrives() { int flags = getLogicalDrives0(); - flags = (flags & 0xFF) << 24 | - ((flags >> 8) & 0xFF) << 16 | - ((flags >> 16) & 0xFF) << 8 | - (flags >> 24) & 0xFF; List letters = new ArrayList(); for ( int i = 0 ; i < 26 ; i++ ) { @@ -19,11 +74,44 @@ public class WindowsDriveInfo { return letters; } + /** + * Calls kernel32's GetLogicalDrives, which returns an int containing + * flags; bit 0 corresponds to drive A, bit 25 to drive Z. on = disk exists. + */ private native int getLogicalDrives0(); + /** + * Feed it a drive letter (such as 'A') to see if it is a fixed disk. + */ public boolean isFixedDisk(String letter) { - return getDriveType(letter.toUpperCase() + ":\\") == 3L; + if ( letter.length() != 1 ) throw new IllegalArgumentException("Supply 1 letter, not: " + letter); + char drive = Character.toUpperCase(letter.charAt(0)); + if ( drive < 'A' || drive > 'Z' ) throw new IllegalArgumentException( + "A drive is indicated by a letter, so A-Z inclusive. Not " + drive); + return getDriveType(drive + ":\\") == 3L; } + /** + * Mirror of kernel32's GetDriveTypeA. You must pass in 'A:\\' - + * so including both a colon and a backslash! + * + * 0 = error + * 1 = doesn't exist + * 2 = removable drive + * 3 = fixed disk + * 4 = remote (network) disk + * 5 = cd-rom + * 6 = ram disk + */ private native int getDriveType(String name); + + public static void main(String[] args) { + System.loadLibrary("WindowsDriveInfo"); + WindowsDriveInfo info = new WindowsDriveInfo(); + + for ( String letter : info.getLogicalDrives() ) { + System.out.printf("Drive %s: - %s\n", letter, + info.isFixedDisk(letter) ? "Fixed Disk" : "Not Fixed Disk"); + } + } } diff --git a/winsrc/.gitignore b/winsrc/.gitignore new file mode 100644 index 00000000..7d929cba --- /dev/null +++ b/winsrc/.gitignore @@ -0,0 +1,2 @@ +lombok_installer_WindowsDriveInfo.o +WindowsDriveInfo.dll diff --git a/winsrc/lombok_installer_WindowsDriveInfo.c b/winsrc/lombok_installer_WindowsDriveInfo.c new file mode 100644 index 00000000..5bccd047 --- /dev/null +++ b/winsrc/lombok_installer_WindowsDriveInfo.c @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +#include +#include +#include +#include "lombok_installer_WindowsDriveInfo.h" + +/* + * Class: lombok_installer_WindowsDriveInfo + * Method: getLogicalDrives0 + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_lombok_installer_WindowsDriveInfo_getLogicalDrives0 + (JNIEnv *env, jobject obj) { + return GetLogicalDrives(); +} + +/* + * Class: lombok_installer_WindowsDriveInfo + * Method: getDriveType + * Signature: (Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_lombok_installer_WindowsDriveInfo_getDriveType + (JNIEnv *env, jobject obj, jstring drive) { + const char *str= (*env)->GetStringUTFChars(env, drive, 0); + DWORD val = GetDriveTypeA(str); + (*env)->ReleaseStringUTFChars(env, drive, str); + return val; +} diff --git a/winsrc/lombok_installer_WindowsDriveInfo.h b/winsrc/lombok_installer_WindowsDriveInfo.h new file mode 100644 index 00000000..3f6c28ff --- /dev/null +++ b/winsrc/lombok_installer_WindowsDriveInfo.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class lombok_installer_WindowsDriveInfo */ + +#ifndef _Included_lombok_installer_WindowsDriveInfo +#define _Included_lombok_installer_WindowsDriveInfo +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: lombok_installer_WindowsDriveInfo + * Method: getLogicalDrives0 + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_lombok_installer_WindowsDriveInfo_getLogicalDrives0 + (JNIEnv *, jobject); + +/* + * Class: lombok_installer_WindowsDriveInfo + * Method: getDriveType + * Signature: (Ljava/lang/String;)I + */ +JNIEXPORT jint JNICALL Java_lombok_installer_WindowsDriveInfo_getDriveType + (JNIEnv *, jobject, jstring); + +#ifdef __cplusplus +} +#endif +#endif