Skip to content

Commit

Permalink
Merge branch 'keep_blacklisted' into server_build
Browse files Browse the repository at this point in the history
  • Loading branch information
brrritssocold committed Nov 19, 2023
2 parents 0a6e58e + 324523f commit ab0ad73
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
24 changes: 22 additions & 2 deletions hath-base/src/main/java/hath/base/CacheHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@

package hath.base;

import java.lang.Thread;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.List;
import java.util.Hashtable;

import hath.base.util.FileTools;

public class CacheHandler {
private static final int MEMORY_TABLE_ELEMENTS = 1048576;
private Hashtable<String, Long> staticRangeOldest = null;
Expand Down Expand Up @@ -650,6 +651,7 @@ public synchronized void processBlacklist(long deltatime) {
File file = hvFile.getLocalFileRef();

if(file.exists()) {
copyFileFromCacheDir(hvFile);
deleteFileFromCache(hvFile);
Out.debug("CacheHandler: Removed blacklisted file " + fileid);
++counter;
Expand Down Expand Up @@ -803,4 +805,22 @@ public void markRecentlyAccessed(HVFile hvFile, boolean skipMetaUpdate) {
}
}
}

public void copyFileFromCacheDir(HVFile hvFile) {
String blacklistDirectory = "data/blacklist";
File copySource = hvFile.getLocalFileRef();

try {
FileTools.checkAndCreateDir(new File(blacklistDirectory));

String filename = copySource.getName();

File copyTarget = new File(blacklistDirectory + "/" + filename);

FileTools.copy(copySource, copyTarget);
Out.info("Successfully copied blacklisted file " + filename + " to blacklist directory");
} catch (IOException e) {
Out.error("Cachehandler: Failed to copy blacklist file " + copySource + " due to error " + e);
}
}
}
51 changes: 51 additions & 0 deletions hath-base/src/test/java/hath/base/CacheHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package hath.base;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;


public class CacheHandlerTest {
private HentaiAtHomeClient client;
private CacheHandler cut;

private Path sourceFile;
private HVFile mockHVFile;
private Settings settings;

@BeforeEach
public void createCacheHandler() throws Exception {
client = mock(HentaiAtHomeClient.class);
mockHVFile = mock(HVFile.class);
settings = new Settings();

settings.updateSetting("skip_free_space_check", "true");
settings.updateSetting("temp_dir", Files.createTempDirectory("CacheHandlerTestTempDir").toString());
settings.initializeDirectories();

cut = new CacheHandler(client);
sourceFile = Files.createTempFile("hath-client-CacheHandlerTest", null);
}

@AfterEach
public void tearDown() {
cut.terminateCache();
}

@Test
public void testCopyFileFromCacheDir() throws Exception {
when(mockHVFile.getLocalFileRef()).thenReturn(sourceFile.toFile());
cut.copyFileFromCacheDir(mockHVFile);

assertThat(new File("data/blacklist/" + sourceFile.getFileName().toString()).exists(), is(true));
}
}

0 comments on commit ab0ad73

Please sign in to comment.