Improve MD5 and startup speed
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.sheepit.client.config;
|
||||
|
||||
import com.sheepit.client.logger.Log;
|
||||
import com.sheepit.client.utils.Utils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class DirectoryCleaner implements Runnable {
|
||||
|
||||
public static final String FILE_SEPARATOR = " file: ";
|
||||
static Log log = Log.getInstance();
|
||||
private File directory;
|
||||
|
||||
|
||||
@Override public void run() {
|
||||
log.debug("DirectoryCleaner initialized. Cleaning " + directory.getName());
|
||||
cleanDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans a directory and removes files in it from the md5 cache
|
||||
*/
|
||||
private void cleanDirectory() {
|
||||
if (directory == null) {
|
||||
return;
|
||||
}
|
||||
final String LOG_PREFIX = "DirectoryCleaner::cleanDirectory:"+ directory.getName();
|
||||
File[] files = directory.listFiles();
|
||||
int removedFilesCount = 0;
|
||||
if (files != null) {
|
||||
log.debug(LOG_PREFIX + " found files in directory " + files.length);
|
||||
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)) {
|
||||
String md5Local = Utils.md5(file.getAbsolutePath());
|
||||
// Check if .wool file is corrupt
|
||||
if (md5Local.equals(name) == false) {
|
||||
if (file.delete()) {
|
||||
removedFilesCount++;
|
||||
}
|
||||
else {
|
||||
String baseMessage = " failed to delete .wool file that is corrupt";
|
||||
String message = LOG_PREFIX + baseMessage + " file: " + file.getName();
|
||||
log.debug(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (file.delete()) {
|
||||
removedFilesCount++;
|
||||
}
|
||||
else {
|
||||
String baseMessage = LOG_PREFIX + " failed to delete non-wool file that is not present in cache";
|
||||
String message = baseMessage + FILE_SEPARATOR + file.getName();
|
||||
log.debug(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IllegalArgumentException e) { // because the file does not have a . in its path
|
||||
if (file.delete()) {
|
||||
removedFilesCount++;
|
||||
}
|
||||
else {
|
||||
String baseMessage = LOG_PREFIX + " failed to delete file with no extension";
|
||||
String message = baseMessage + FILE_SEPARATOR + file.getName();
|
||||
log.debug(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
log.debug(LOG_PREFIX + " successfully removed " + removedFilesCount + " from " + directory.getName());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -523,7 +523,6 @@ public class SettingsLoader {
|
||||
|
||||
if (config.isUserHasSpecifiedACacheDir() == false && cacheDir != null) {
|
||||
config.setCacheDir(new File(cacheDir.getValue()));
|
||||
(new DirectoryManager(config)).createCacheDir();
|
||||
}
|
||||
|
||||
if (config.getUIType() == null && ui != null) {
|
||||
|
||||
Reference in New Issue
Block a user