Improve MD5 and startup speed
This commit is contained in:
@@ -37,6 +37,7 @@ import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class DirectoryManager {
|
||||
@@ -62,6 +63,8 @@ public class DirectoryManager {
|
||||
return copyFileFromSharedToCache(getSharedPathFor(chunk), getCachePathFor(chunk));
|
||||
}
|
||||
|
||||
private static final Log log = Log.getInstance();
|
||||
|
||||
private boolean copyFileFromSharedToCache(String source, String target) {
|
||||
Path existingArchivePath = Paths.get(source);
|
||||
Path targetArchivePath = Paths.get(target);
|
||||
@@ -82,12 +85,12 @@ public class DirectoryManager {
|
||||
| SecurityException // user is not allowed to create hard-links
|
||||
ignore) {
|
||||
// Creating hardlinks might not be supported on some filesystems
|
||||
Log.getInstance().debug("Failed to create hardlink, falling back to copying file to " + targetArchivePath);
|
||||
log.debug("Failed to create hardlink, falling back to copying file to " + targetArchivePath);
|
||||
Files.copy(existingArchivePath, targetArchivePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
Log.getInstance().error("Error while copying " + source + " from shared downloads directory to working dir");
|
||||
log.error("Error while copying " + source + " from shared downloads directory to working dir");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -107,10 +110,14 @@ public class DirectoryManager {
|
||||
this.configuration.getSharedDownloadsDirectory().mkdirs();
|
||||
|
||||
if (this.configuration.getSharedDownloadsDirectory().exists() == false) {
|
||||
System.err.println("DirectoryManager::createCacheDir Unable to create common directory " + this.configuration.getSharedDownloadsDirectory().getAbsolutePath());
|
||||
log.error("DirectoryManager::createCacheDir Unable to create common directory " + this.configuration.getSharedDownloadsDirectory().getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> createCacheDirAsync() {
|
||||
return CompletableFuture.runAsync(this::createCacheDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans working directory and also deletes it if the user hasn't specified a cache directory
|
||||
@@ -128,51 +135,8 @@ public class DirectoryManager {
|
||||
* Deletes the working and storage directories
|
||||
*/
|
||||
public void cleanWorkingDirectory() {
|
||||
this.cleanDirectory(this.configuration.getWorkingDirectory());
|
||||
this.cleanDirectory(this.configuration.getWoolCacheDirectory());
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans a directory and removes files in it from the md5 cache
|
||||
* @param dir representing the directory to be cleaned
|
||||
* @return false if the dir null, true otherwise
|
||||
*/
|
||||
private boolean cleanDirectory(File dir) {
|
||||
if (dir == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
File[] files = dir.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory()) {
|
||||
Utils.delete(file);
|
||||
}
|
||||
else {
|
||||
try {
|
||||
String extension = FilenameUtils.getExtension(file.getName()).toLowerCase();
|
||||
String name = FilenameUtils.removeExtension(file.getName());
|
||||
if ("wool".equals(extension)) {
|
||||
// check if the md5 of the file is ok
|
||||
String md5_local = Utils.md5(file.getAbsolutePath());
|
||||
|
||||
if (md5_local.equals(name) == false) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
// TODO: remove old one
|
||||
}
|
||||
else {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException e) { // because the file does not have an . in his path
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
new DirectoryCleaner(this.configuration.getWorkingDirectory()).run();
|
||||
new DirectoryCleaner(this.configuration.getWoolCacheDirectory()).run();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,7 +144,7 @@ public class DirectoryManager {
|
||||
* working, storage, and shared downloads directories as long as they are not null
|
||||
*/
|
||||
public List<File> getLocalCacheFiles() {
|
||||
List<File> files_local = new LinkedList<File>();
|
||||
List<File> filesLocal = new LinkedList<File>();
|
||||
List<File> files = new LinkedList<File>();
|
||||
if (this.configuration.getWorkingDirectory() != null) {
|
||||
File[] filesInDirectory = this.configuration.getWorkingDirectory().listFiles();
|
||||
@@ -208,10 +172,10 @@ public class DirectoryManager {
|
||||
String name = FilenameUtils.removeExtension(file.getName());
|
||||
if ("wool".equals(extension)) {
|
||||
// check if the md5 of the file is ok
|
||||
String md5_local = Utils.md5(file.getAbsolutePath());
|
||||
String md5Local = Utils.md5(file.getAbsolutePath());
|
||||
|
||||
if (md5_local.equals(name)) {
|
||||
files_local.add(file);
|
||||
if (md5Local.equals(name)) {
|
||||
filesLocal.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -220,7 +184,7 @@ public class DirectoryManager {
|
||||
}
|
||||
}
|
||||
|
||||
return files_local;
|
||||
return filesLocal;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user