diff options
author | Roel Spilker <r.spilker@gmail.com> | 2009-07-23 23:17:59 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2009-07-23 23:17:59 +0200 |
commit | 5835344277d10d069d792c59d67e41313d589d68 (patch) | |
tree | 73ef2f20d81c485cbde73eacb3c28513398a8943 /src/lombok/installer/WindowsDriveInfo.java | |
parent | f4bcabbe1a8a35028e18726168b56d10dc812787 (diff) | |
download | lombok-5835344277d10d069d792c59d67e41313d589d68.tar.gz lombok-5835344277d10d069d792c59d67e41313d589d68.tar.bz2 lombok-5835344277d10d069d792c59d67e41313d589d68.zip |
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.
Diffstat (limited to 'src/lombok/installer/WindowsDriveInfo.java')
-rw-r--r-- | src/lombok/installer/WindowsDriveInfo.java | 98 |
1 files changed, 93 insertions, 5 deletions
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: + * <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"); + } + } } |