Created
July 5, 2014 13:28
-
-
Save 166MMX/7c0de110f56431ff701d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.initvoid.jlocate | |
import java.nio.file.FileStore | |
import java.nio.file.Files | |
import java.nio.file.LinkOption | |
import java.nio.file.Path | |
import java.util.regex.Pattern | |
class WindowsVolume | |
{ | |
String deviceId | |
UUID uuid | |
Set<String> mountPoints = new HashSet<>() | |
Properties fingerprint = new Properties() | |
private static Pattern deviceIdPattern = Pattern.compile('\\\\\\\\\\?\\\\(Volume(\\{((?:[0-9a-fA-F]{2}){4}-(?:[0-9a-fA-F]{2}){2}-(?:[0-9a-fA-F]{2}){2}-(?:[0-9a-fA-F]{2}){2}-(?:[0-9a-fA-F]{2}){6})\\}))\\\\') | |
static Set<WindowsVolume> systemVolumes = new HashSet<>() | |
static Map<String, WindowsVolume> systemMountPoints = new HashMap<>() | |
static querySystem () | |
{ | |
def lineIterator = 'mountvol'.execute().text.readLines().listIterator() | |
while (lineIterator.hasNext()) | |
{ | |
def line = lineIterator.next().trim() | |
def deviceIdMatcher = line =~ deviceIdPattern | |
if (!deviceIdMatcher.matches()) | |
{ | |
continue | |
} | |
def volume = new WindowsVolume() | |
volume.deviceId = deviceIdMatcher.group(0) | |
systemVolumes << volume | |
if (!lineIterator.hasNext()) | |
{ | |
throw new IllegalStateException() | |
} | |
def mountPoint = lineIterator.next().trim() | |
if (!mountPoint.contains(File.separator)) | |
{ | |
continue | |
} | |
volume.mountPoints << mountPoint | |
systemMountPoints[mountPoint] = volume | |
} | |
for (systemVolume in systemVolumes) | |
{ | |
if (systemVolume.mountPoints.empty) | |
{ | |
continue | |
} | |
if (!systemVolume.hasFingerprint()) | |
{ | |
systemVolume.createFingerprint() | |
systemVolume.writeFingerprint() | |
} | |
else | |
{ | |
systemVolume.readFingerprint() | |
} | |
} | |
} | |
void setDeviceId(String deviceId) | |
{ | |
this.deviceId = deviceId | |
fingerprint.setProperty('DeviceId', deviceId) | |
} | |
boolean hasFingerprint() | |
{ | |
if (mountPoints.empty) | |
{ | |
throw new UnsupportedOperationException() | |
} | |
def mountPoint = new File(mountPoints[0]) | |
def file = new File(mountPoint, '.jLocate.properties') | |
file.exists() && file.size() != 0 | |
} | |
void createFingerprint() | |
{ | |
uuid = UUID.randomUUID() | |
fingerprint.setProperty('UUID', uuid.toString()) | |
} | |
void writeFingerprint() | |
{ | |
if (mountPoints.empty) | |
{ | |
throw new UnsupportedOperationException() | |
} | |
def mountPoint = new File(mountPoints[0]) | |
def mountPointPath = mountPoint.toPath() | |
FileStore fileStore | |
try | |
{ | |
fileStore = Files.getFileStore(mountPointPath) | |
} | |
catch (IOException ex) | |
{ | |
if (!ex.message.contains('The device is not ready')) | |
{ | |
throw ex | |
} | |
return | |
} | |
switch (fileStore.type()) | |
{ | |
case 'CDFS': | |
return | |
} | |
def file = new File(mountPoint, '.jLocate.properties') | |
def os = file.newOutputStream() | |
fingerprint.store(os, 'Volume fingerprint to survive mount point changes - modified volume clones must have a unique fingerprint') | |
Path path = file.toPath() | |
Boolean hidden = Files.getAttribute(path, 'dos:hidden', LinkOption.NOFOLLOW_LINKS) | |
if (hidden != null && !hidden) | |
{ | |
Files.setAttribute(path, 'dos:hidden', Boolean.TRUE, LinkOption.NOFOLLOW_LINKS) | |
} | |
file.setReadOnly() | |
} | |
void readFingerprint() | |
{ | |
if (mountPoints.empty) | |
{ | |
throw new UnsupportedOperationException() | |
} | |
def mountPoint = new File(mountPoints[0]) | |
def file = new File(mountPoint, '.jLocate.properties') | |
def is = file.newInputStream() | |
fingerprint.load(is) | |
} | |
static void main(String[] argv) | |
{ | |
querySystem() | |
for (systemVolume in systemVolumes) | |
{ | |
for (mountPoint in systemVolume.mountPoints) | |
{ | |
println "$mountPoint\t$systemVolume.deviceId" | |
} | |
if (systemVolume.mountPoints.size() == 0) | |
{ | |
println "null\t$systemVolume.deviceId" | |
} | |
} | |
'blub' | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment