Skip to content

Commit

Permalink
feat: Preserve original lastModified time during file processing (#52 #…
Browse files Browse the repository at this point in the history
  • Loading branch information
deltazefiro committed Apr 28, 2023
1 parent f9d08f0 commit e63ebc6
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions app/src/main/java/deltazero/amarok/FileHider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import androidx.annotation.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.FileVisitResult;
Expand All @@ -20,6 +21,7 @@

import deltazero.amarok.utils.FileHiderUtil;

@SuppressWarnings("deprecation")
public class FileHider {
private final static String TAG = "FileHider";

Expand Down Expand Up @@ -124,20 +126,34 @@ private static Path processFilename(Path path, ProcessConfig.ProcessMethod proce
private static void processFileHeader(Path path) {
Log.d(TAG, "Processing file header: " + path);

try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "rw")) {
try {

File file = path.toFile();

// Preserve original lastModified time
var lastModified = path.toFile().lastModified();

byte[] bytes = new byte[8];
int numBytesRead = file.read(bytes);
int numBytesToReplace = Math.max(Math.min(numBytesRead, 8), 0);
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) {

for (int i = 0; i < numBytesToReplace; i++) {
bytes[i] = (byte) ~bytes[i];
byte[] bytes = new byte[8];
int numBytesRead = randomAccessFile.read(bytes);
int numBytesToReplace = Math.max(Math.min(numBytesRead, 8), 0);

for (int i = 0; i < numBytesToReplace; i++) {
bytes[i] = (byte) ~bytes[i];
}

randomAccessFile.seek(0);
randomAccessFile.write(bytes, 0, numBytesToReplace);

} catch (IOException e) {
Log.w(TAG, "processFileHeader failed: ", e);
}

file.seek(0);
file.write(bytes, 0, numBytesToReplace);
// noinspection ResultOfMethodCallIgnored
file.setLastModified(lastModified);

} catch (IOException e) {
} catch (SecurityException e) {
Log.w(TAG, "processFileHeader failed: ", e);
}
}
Expand All @@ -149,23 +165,35 @@ private static void processWholeFile(Path path) {
long numReadLoops = 0;
int numBytesRead, numBytesToReplace;

try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "rw")) {
try {

while ((numBytesRead = file.read(buffer)) != -1) {
File file = path.toFile();

numBytesToReplace = Math.max(Math.min(numBytesRead, buffer.length), 0);
// Preserve original lastModified time
var lastModified = path.toFile().lastModified();

for (int i = 0; i < numBytesToReplace; i++) {
buffer[i] = (byte) ~buffer[i];
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) {
while ((numBytesRead = randomAccessFile.read(buffer)) != -1) {

file.seek(numReadLoops * buffer.length);
file.write(buffer, 0, numBytesToReplace);
numBytesToReplace = Math.max(Math.min(numBytesRead, buffer.length), 0);

for (int i = 0; i < numBytesToReplace; i++) {
buffer[i] = (byte) ~buffer[i];
}

numReadLoops++;
randomAccessFile.seek(numReadLoops * buffer.length);
randomAccessFile.write(buffer, 0, numBytesToReplace);

numReadLoops++;
}
} catch (IOException e) {
Log.w(TAG, "processWholeFile failed: ", e);
}

} catch (IOException e) {
// noinspection ResultOfMethodCallIgnored
file.setLastModified(lastModified);

} catch (SecurityException e) {
Log.w(TAG, "processWholeFile failed: ", e);
}
}
Expand Down

0 comments on commit e63ebc6

Please sign in to comment.