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.
This commit is contained in:
Roel Spilker committed 2009-07-23 23:17:59 +02:00
1 parent f4bcabbe1a
commit 5835344277
6 files changed
+200 -61

No files matched your search

+47 -56
View File
@@ -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<String> 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<String> getDrivesOnWindows() throws Throwable {
loadWindowsDriveInfoLib();
List<String> drives = new ArrayList<String>();
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<String> 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<String> driveLetters = asList("C");
try {
driveLetters = getDrivesOnWindows();
} catch ( IOException ignore ) {}
} catch ( Throwable ignore ) {
ignore.printStackTrace();
}
for ( String letter : driveLetters ) {
File f = new File(letter + ":\\");
Binary file not shown.
+93 -5
View File
@@ -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:
* <pre>fsutil fsinfo drives</pre>
* and
* <pre>fsutil fsinfo drivetype C:</pre>
*
* 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.
* <p>
* To compile it, you'll need windows, as well as MinGW:
* http://sourceforge.net/projects/mingw/files/
* <p>
* 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<String> getLogicalDrives() {
int flags = getLogicalDrives0();
flags = (flags & 0xFF) << 24 |
((flags >> 8) & 0xFF) << 16 |
((flags >> 16) & 0xFF) << 8 |
(flags >> 24) & 0xFF;
List<String> letters = new ArrayList<String>();
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");
}
}
}
+2
View File
@@ -0,0 +1,2 @@
lombok_installer_WindowsDriveInfo.o
WindowsDriveInfo.dll
@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <windows.h>
#include <stdio.h>
#include <winbase.h>
#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;
}
@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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