Ref: configuration class shouldn't create and handle directory, DirectoryManager should do it
This commit is contained in:
@@ -26,12 +26,8 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
import com.sheepit.client.hardware.cpu.CPU;
|
||||
import com.sheepit.client.hardware.gpu.GPUDevice;
|
||||
@@ -187,40 +183,26 @@ import lombok.Data;
|
||||
* @param cache_dir_ File representing cache directory
|
||||
*/
|
||||
public void setCacheDir(File cache_dir_) {
|
||||
removeWorkingDirectory();
|
||||
if (cache_dir_ == null) {
|
||||
this.userHasSpecifiedACacheDir = false;
|
||||
try {
|
||||
this.workingDirectory = File.createTempFile("farm_", "");
|
||||
this.workingDirectory.createNewFile(); // hoho...
|
||||
this.workingDirectory.delete(); // hoho
|
||||
this.workingDirectory.mkdir();
|
||||
this.workingDirectory.deleteOnExit();
|
||||
|
||||
// since there is no working directory and the client will be working in the system temp directory,
|
||||
// we can also set up a 'permanent' directory for immutable files (like renderer binary)
|
||||
|
||||
this.woolCacheDirectory = new File(this.workingDirectory.getParent() + File.separator + WOOL_CACHE_DIRNAME);
|
||||
this.woolCacheDirectory.mkdir();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// since there is no working directory and the client will be working in the system temp directory,
|
||||
// we can also set up a 'permanent' directory for immutable files (like renderer binary)
|
||||
this.woolCacheDirectory = new File(this.workingDirectory.getParent() + File.separator + WOOL_CACHE_DIRNAME);
|
||||
|
||||
}
|
||||
else {
|
||||
this.userHasSpecifiedACacheDir = true;
|
||||
this.workingDirectory = new File(cache_dir_.getAbsolutePath() + File.separator + WORKING_DIRNAME);
|
||||
this.woolCacheDirectory = new File(cache_dir_.getAbsolutePath() + File.separator + WOOL_CACHE_DIRNAME);
|
||||
this.workingDirectory.mkdirs();
|
||||
this.woolCacheDirectory.mkdirs();
|
||||
}
|
||||
|
||||
if (this.sharedDownloadsDirectory != null) {
|
||||
this.sharedDownloadsDirectory.mkdirs();
|
||||
|
||||
if (!this.sharedDownloadsDirectory.exists()) {
|
||||
System.err.println("Configuration::setCacheDir Unable to create common directory " + this.sharedDownloadsDirectory.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,175 +227,6 @@ import lombok.Data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the working and storage directories
|
||||
*/
|
||||
public void cleanWorkingDirectory() {
|
||||
this.cleanDirectory(this.workingDirectory);
|
||||
this.cleanDirectory(this.woolCacheDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public 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 = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase();
|
||||
String name = file.getName().substring(0, file.getName().length() - 1 * extension.length());
|
||||
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 (StringIndexOutOfBoundsException e) { // because the file does not have an . in his path
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans working directory and also deletes it if the user hasn't specified a cache directory
|
||||
*/
|
||||
public void removeWorkingDirectory() {
|
||||
if (this.userHasSpecifiedACacheDir) {
|
||||
this.cleanWorkingDirectory();
|
||||
}
|
||||
else {
|
||||
Utils.delete(this.workingDirectory);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of archives (files with extensions .zip or .wool) in the
|
||||
* 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> files = new LinkedList<File>();
|
||||
if (this.workingDirectory != null) {
|
||||
File[] filesInDirectory = this.workingDirectory.listFiles();
|
||||
if (filesInDirectory != null) {
|
||||
files.addAll(Arrays.asList(filesInDirectory));
|
||||
}
|
||||
}
|
||||
if (this.woolCacheDirectory != null) {
|
||||
File[] filesInDirectory = this.woolCacheDirectory.listFiles();
|
||||
if (filesInDirectory != null) {
|
||||
files.addAll(Arrays.asList(filesInDirectory));
|
||||
}
|
||||
}
|
||||
if (this.sharedDownloadsDirectory != null) {
|
||||
File[] filesInDirectory = this.sharedDownloadsDirectory.listFiles();
|
||||
if (filesInDirectory != null) {
|
||||
files.addAll(Arrays.asList(filesInDirectory));
|
||||
}
|
||||
}
|
||||
|
||||
for (File file : files) {
|
||||
if (file.isFile()) {
|
||||
try {
|
||||
String extension = file.getName().substring(file.getName().lastIndexOf('.')).toLowerCase();
|
||||
String name = file.getName().substring(0, file.getName().length() - 1 * extension.length());
|
||||
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)) {
|
||||
files_local.add(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (StringIndexOutOfBoundsException e) { // because the file does not have an . his path
|
||||
}
|
||||
}
|
||||
}
|
||||
return files_local;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs through all SheepIt related directories and checks if files and folders are all readable, writeable
|
||||
* and in case of directories, checks if the contents can be listed and if usable space is enough.
|
||||
* Only logs instances where something was detected, otherwise is it will only print "FilesystemHealthCheck started"
|
||||
* @return an ArrayList of Strings containing all logs of the FSHealth check
|
||||
*/
|
||||
public List<String> filesystemHealthCheck() {
|
||||
List<String> logs = new ArrayList<>();
|
||||
String f = "FSHealth: ";
|
||||
logs.add(f + "FilesystemHealthCheck started");
|
||||
List<File> dirsToCheck = new ArrayList<>();
|
||||
List<File> dirsChecked = new ArrayList<>();
|
||||
dirsToCheck.add(workingDirectory.getAbsoluteFile());
|
||||
if (sharedDownloadsDirectory != null && dirsToCheck.contains(sharedDownloadsDirectory.getAbsoluteFile()) == false) {
|
||||
dirsToCheck.add(sharedDownloadsDirectory.getAbsoluteFile());
|
||||
}
|
||||
if (woolCacheDirectory != null && dirsToCheck.contains(woolCacheDirectory.getAbsoluteFile()) == false) {
|
||||
dirsToCheck.add(woolCacheDirectory.getAbsoluteFile());
|
||||
}
|
||||
ListIterator<File> dirs = dirsToCheck.listIterator();
|
||||
while (dirs.hasNext()) {
|
||||
File dir = dirs.next();
|
||||
dirs.remove();
|
||||
dirsChecked.add(dir);
|
||||
File[] fileList = dir.listFiles();
|
||||
if (fileList == null) {
|
||||
logs.add(f + "File list of " + dir + " is null");
|
||||
}
|
||||
else {
|
||||
for (File file : fileList) {
|
||||
file = file.getAbsoluteFile();
|
||||
//logs.add(f + file);
|
||||
boolean canRead = file.canRead();
|
||||
boolean canWrite = file.canWrite();
|
||||
boolean isDir = file.isDirectory();
|
||||
if (canRead == false) {
|
||||
logs.add(f + "Can't read from " + file);
|
||||
}
|
||||
if (canWrite == false) {
|
||||
logs.add(f + "Can't write to " + file);
|
||||
}
|
||||
if (canRead && canWrite && isDir) {
|
||||
if (dirsChecked.contains(file)) {
|
||||
logs.add(f + "Dir " + file + " already checked. Loop detected");
|
||||
}
|
||||
else {
|
||||
dirs.add(file);
|
||||
}
|
||||
long usableSpace = file.getUsableSpace();
|
||||
if (usableSpace < 512 * 1024) {
|
||||
logs.add(f + "Usable space is " + usableSpace + " for " + file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return logs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a string representing the version of the jar or
|
||||
* {@code 6.0.0} if on error is encountered or the VERSION file doesn't exist
|
||||
|
||||
Reference in New Issue
Block a user