Skip to content

Instantly share code, notes, and snippets.

@mdPlusPlus
Created May 31, 2016 21:42
Show Gist options
  • Save mdPlusPlus/a157f5b38226f12069d1b99d7541f743 to your computer and use it in GitHub Desktop.
Save mdPlusPlus/a157f5b38226f12069d1b99d7541f743 to your computer and use it in GitHub Desktop.
check for new drives periodically and copy content to given location
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class CopyNewDrivesToDisk {
private ArrayList<FileStore> prev;
private ArrayList<FileStore> curr;
private Path destination;
public CopyNewDrivesToDisk(Path destination) {
prev = new ArrayList<FileStore>();
curr = new ArrayList<FileStore>();
Iterator<FileStore> drives = FileSystems.getDefault().getFileStores().iterator();
while (drives.hasNext()) {
FileStore temp = drives.next();
if (!temp.isReadOnly()) {
prev.add(temp);
curr.add(temp);
}
}
this.destination = destination;
System.out.println("copying to " + this.destination);
while (true) {
check();
synchronized (this) {
try {
this.wait(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
if (args.length != 1) {
new CopyNewDrivesToDisk(Paths.get(System.getProperty("user.dir")));
} else {
new CopyNewDrivesToDisk(Paths.get(args[0]));
}
}
private FileStore[][] check() {
ArrayList<FileStore> newDevices = new ArrayList<FileStore>();
FileStore[] rNew;
ArrayList<FileStore> removedDevices = new ArrayList<FileStore>();
FileStore[] rRem;
prev.clear();
while (!curr.isEmpty()) {
prev.add(curr.remove(0));
}
Iterator<FileStore> drives = FileSystems.getDefault().getFileStores().iterator();
while (drives.hasNext()) {
FileStore temp = drives.next();
if (!temp.isReadOnly()) {
curr.add(temp);
}
}
for (int i = 0; i < prev.size(); i++) {
if (!curr.contains(prev.get(i))) {
//System.out.println(prev.get(i) + " was removed");
removedDevices.add(prev.get(i));
}
}
for (int i = 0; i < curr.size(); i++) {
if (!prev.contains(curr.get(i))) {
//System.out.println(curr.get(i) + " was attached");
newDevices.add(curr.get(i));
}
}
rNew = new FileStore[newDevices.size()];
rNew = newDevices.toArray(rNew);
rRem = new FileStore[removedDevices.size()];
rRem = removedDevices.toArray(rRem);
System.out.print("New devices: ");
System.out.println(Arrays.deepToString(rNew));
System.out.print("Removed devices: ");
System.out.println(Arrays.deepToString(rRem));
System.out.println();
for (FileStore fs : rNew) {
new CopyFileStoreToLocation(fs, destination).convertFileStoreToPath();
}
return new FileStore[][]{rNew, rRem};
}
public class CopyFileStoreToLocation {
private FileStore fileStore;
private Path location;
public CopyFileStoreToLocation(FileStore fileStore, Path location) {
this.fileStore = fileStore;
this.location = location;
if (fileStore != null && location != null) {
Path source = convertFileStoreToPath();
Path target = this.location;
try {
System.out.println("Copying " + source + " to " + target + " ...");
BigInteger size = FileUtils.sizeOfDirectoryAsBigInteger(source.toFile());
double sizeInKiB = size.doubleValue() / 1024;
double sizeInMiB = sizeInKiB / 1024;
double sizeInGiB = sizeInMiB / 1024;
long startTime = System.currentTimeMillis();
FileUtils.copyDirectoryToDirectory(source.toFile(), target.toFile());
long endTime = System.currentTimeMillis();
long tookInSeconds = (endTime - startTime) / 1000;
System.out.println("Finished copying " + source + " to " + target + ", " + sizeInMiB + " MiB , took " + tookInSeconds + " seconds");
} catch (IOException e) {
//e.printStackTrace();
}
}
}
public CopyFileStoreToLocation(FileStore fileStore, File location) {
this(fileStore, location.toPath());
}
public CopyFileStoreToLocation(FileStore fileStore, String location) {
this(fileStore, Paths.get(location));
}
public CopyFileStoreToLocation(FileStore fileStore, URL location) throws URISyntaxException {
this(fileStore, Paths.get(location.toURI()));
}
public CopyFileStoreToLocation(FileStore fileStore, URI location) {
this(fileStore, Paths.get(location));
}
private Path convertFileStoreToPath() {
// String fileStoreName = fileStore.name();
String fileStoreString = fileStore.toString();
String firstPart = fileStoreString.substring(0, fileStoreString.indexOf(" ("));
String secondPart = fileStoreString.substring(fileStoreString.indexOf(" (") + 2, fileStoreString.length() - 1);
// System.out.println("fileStoreName " + fileStoreName);
// System.out.println("fileStoreString " + fileStoreString);
// System.out.println("firstPart " + firstPart);
// System.out.println("secondPart " + secondPart);
// System.out.println();
if (getOS().equals("win")) {
return Paths.get(secondPart);
} else {
return Paths.get(firstPart);
}
}
private String getOS() {
String os;
String osName = System.getProperty("os.name").toLowerCase();
if (osName.indexOf("win") >= 0) {
os = "win";
} else if (osName.indexOf("mac") >= 0) {
os = "osx";
} else if (osName.indexOf("nix") >= 0) {
os = "lin";
} else if (osName.indexOf("nux") >= 0) {
os = "lin";
} else if (osName.indexOf("aix") >= 0) {
os = "lin";
} else if (osName.indexOf("sunos") >= 0) {
os = "solaris";
} else {
os = "UNKNOWN";
}
return os;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment