From ef954712eb0a028a9ff8eef8f6888034a2c628cd Mon Sep 17 00:00:00 2001 From: k3b <1374583+k3b@users.noreply.github.com> Date: Sun, 28 Mar 2021 19:19:57 +0200 Subject: [PATCH] #169: Fixed caching fo copy/move/delete --- .../k3b/android/io/AndroidFileCommands.java | 4 +- .../de/k3b/android/io/AndroidFileFacade.java | 45 +++++++------------ .../java/de/k3b/io/FileCommandLogger.java | 5 ++- .../src/main/java/de/k3b/io/FileCommands.java | 7 ++- .../main/java/de/k3b/media/ExifInterface.java | 4 -- .../java/de/k3b/io/filefacade/FileFacade.java | 15 +------ .../de/k3b/io/filefacade/FileWrapper.java | 5 --- .../main/java/de/k3b/io/filefacade/IFile.java | 4 +- .../k3b/io/filefacade/StringFileFacade.java | 5 --- 9 files changed, 33 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/de/k3b/android/io/AndroidFileCommands.java b/app/src/main/java/de/k3b/android/io/AndroidFileCommands.java index e46efb6c..2e27cc46 100644 --- a/app/src/main/java/de/k3b/android/io/AndroidFileCommands.java +++ b/app/src/main/java/de/k3b/android/io/AndroidFileCommands.java @@ -255,7 +255,9 @@ public int execRename(File srcDirFile, String newFolderName) { public void onMoveOrCopyDirectoryPick(boolean move, SelectedFiles selectedFiles, IDirectory destFolder) { if (destFolder != null) { String copyToPath = destFolder.getAbsolute(); - IFile destDirFolder = FileFacade.convert("AndroidFileCommands.onMoveOrCopyDirectoryPick", copyToPath); + IFile destDirFolder = FileFacade + .convert("AndroidFileCommands.onMoveOrCopyDirectoryPick", copyToPath) + .cacheStrategy(IFile.STRATEGY_OUTPUT); setLastCopyToPath(copyToPath); diff --git a/app/src/main/java/de/k3b/android/io/AndroidFileFacade.java b/app/src/main/java/de/k3b/android/io/AndroidFileFacade.java index dddc90a2..dc7bf339 100644 --- a/app/src/main/java/de/k3b/android/io/AndroidFileFacade.java +++ b/app/src/main/java/de/k3b/android/io/AndroidFileFacade.java @@ -85,13 +85,14 @@ public static void initFactory(Context context) { } } - private AndroidFileFacade(@Nullable DocumentFile parentFile, @NonNull File parentFile1) { + private AndroidFileFacade(@Nullable DocumentFile parentFile, @NonNull File parentFile1, int strategyID) { super(parentFile1); androidFile = parentFile; + this.strategyID = strategyID; } public AndroidFileFacade(@NonNull File file) { - this(null, file); + this(null, file, IFile.STRATEGY_INPUT); } @Override @@ -111,22 +112,6 @@ private DocumentFile getDocumentFileOrDirOrNull(@NonNull File file) { return documentFileTranslator.getDocumentFileOrDirOrNull(file, null, this.strategyID); } - @Override - public boolean renameTo(@NonNull IFile newName) { - if (exists() && !newName.exists()) { - if (getParentFile().equals(newName.getParentFile())) { - // same directory - return renameTo(newName.getName()); - } - - if (copyImpl((AndroidFileFacade) newName, true)) { - return true; - } - } - Log.e(LOG_TAG, "renameTo " + this + " -> " + newName + " failed"); - return false; - } - private boolean copyImpl(@NonNull AndroidFileFacade targetFullPath, boolean deleteSourceWhenSuccess) { final String dbgContext = "AndroidFileFacade.copyImpl " + this + " -> " + targetFullPath; try { @@ -144,6 +129,7 @@ private boolean copyImpl(@NonNull AndroidFileFacade targetFullPath, boolean dele @Override public boolean renameTo(@NonNull String newName) { if (exists() && getAndroidFile(false).renameTo(newName)) { + invalidateParentDirCache(); return true; } @@ -156,6 +142,7 @@ public boolean delete() { boolean result = exists() && getAndroidFile(false).delete(); if (result) { + invalidateParentDirCache(); // File (and reference to it) does not exist any more androidFileMayExist = false; androidFile = null; @@ -212,7 +199,7 @@ public IFile getCanonicalFile() { public IFile getParentFile() { final DocumentFile androidFile = getAndroidFile(false); if (androidFile != null) { - return new AndroidFileFacade(androidFile.getParentFile(), getFile().getParentFile()); + return new AndroidFileFacade(androidFile.getParentFile(), getFile().getParentFile(), strategyID); } else { return super.getParentFile(); } @@ -264,7 +251,7 @@ public boolean mkdirs() { public IFile[] listFiles() { final DocumentFile androidFile = getAndroidFile(false); if (androidFile != null) { - return get(androidFile.listFiles()); + return get(androidFile.listFiles(), strategyID); } return new IFile[0]; } @@ -278,7 +265,7 @@ public IFile[] listDirs() { if (file != null && (file.isDirectory() || accept(file.getName().toLowerCase()))) { found.add(new AndroidFileFacade( - file, new File(parent, file.getName()))); + file, new File(parent, file.getName()), strategyID)); } } } @@ -302,11 +289,12 @@ public boolean copy(@NonNull IFile targetFullPath, boolean deleteSourceWhenSucce @Override public OutputStream openOutputStream() throws FileNotFoundException { DocumentFile androidFile = getAndroidFile(false); - String context = "openOutputStream overwrite existing "; + String context = strategyID + "openOutputStream overwrite existing "; if (androidFile == null) { final DocumentFile documentFileParent = documentFileTranslator.getOrCreateDirectory(getFile().getParentFile(), strategyID); androidFile = this.androidFile = documentFileParent.createFile(null, getFile().getName()); - context = "openOutputStream create new "; + context = strategyID + "openOutputStream create new "; + invalidateParentDirCache(); } if (FileFacade.debugLogFacade) { Log.i(LOG_TAG, context + this); @@ -316,22 +304,23 @@ public OutputStream openOutputStream() throws FileNotFoundException { @Override public InputStream openInputStream() throws FileNotFoundException { + String context = strategyID + "openInputStream "; if ((readUri != null)) { if (debugLogFacade) { - Log.i(LOG_TAG, "openInputStream " + this + " for uri " + readUri); + Log.i(LOG_TAG, context + this + " for uri " + readUri); } return documentFileTranslator.openInputStream(readUri); } final DocumentFile androidFile = getAndroidFile(true); final InputStream resultInputStream = documentFileTranslator.openInputStream(androidFile); if (resultInputStream == null) { - final String msg = "openInputStream " + this + " for uri " + final String msg = context + this + " for uri " + ((androidFile != null) ? androidFile.getUri() : "null") + " returns null"; Log.w(LOG_TAG, msg); getAndroidFile(true); // allow debugger to step in throw new FileNotFoundException(msg); } else if (debugLogFacade) { - Log.i(LOG_TAG, "openInputStream " + this + " for uri " + androidFile.getUri()); + Log.i(LOG_TAG, context + this + " for uri " + androidFile.getUri()); } return resultInputStream; } @@ -344,12 +333,12 @@ public void invalidateParentDirCache() { } - private IFile[] get(DocumentFile[] docs) { + private IFile[] get(DocumentFile[] docs, int strategyID) { AndroidFileFacade[] f = new AndroidFileFacade[docs.length]; final File parent = getFile(); for (int i = 0; i < docs.length; i++) { final DocumentFile doc = docs[i]; - f[i] = new AndroidFileFacade(doc, new File(parent, doc.getName())); + f[i] = new AndroidFileFacade(doc, new File(parent, doc.getName()), strategyID); } return f; diff --git a/fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java b/fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java index e9aa9d56..c7f26ec3 100644 --- a/fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java +++ b/fotolib2/src/main/java/de/k3b/io/FileCommandLogger.java @@ -45,7 +45,10 @@ public void openLogfile() { if (mLogFilePath != null) { OutputStream stream = null; try { - IFile logFile = FileFacade.convert("FileCommandLogger.openLogfile", mLogFilePath); + IFile logFile = FileFacade + .convert("FileCommandLogger.openLogfile", mLogFilePath) + .cacheStrategy(IFile.STRATEGY_OUTPUT); + if (logFile.exists()) { // open existing in append mode long ageInHours = (new Date().getTime() - logFile.lastModified()) / (1000 * 60 * 60); diff --git a/fotolib2/src/main/java/de/k3b/io/FileCommands.java b/fotolib2/src/main/java/de/k3b/io/FileCommands.java index a28d27c3..8832ddc6 100644 --- a/fotolib2/src/main/java/de/k3b/io/FileCommands.java +++ b/fotolib2/src/main/java/de/k3b/io/FileCommands.java @@ -216,6 +216,7 @@ int moveOrCopyFilesTo(boolean move, IFile destDirFolder, IProgessListener progessListener) { int result = 0; if (canProcessFile(move ? OP_MOVE : OP_COPY)) { + if (destDirFolder != null) destDirFolder.cacheStrategy(IFile.STRATEGY_OUTPUT); if (osCreateDirIfNeccessary(destDirFolder)) { IFile[] destFiles = createDestFiles(renameProcessor, destDirFolder, selectedFiles.getDatesPhotoTaken(), selectedFiles.getIFiles()); @@ -297,6 +298,7 @@ private IFile[] createDestFiles(IFileNameProcessor renameProcessor, IFile destDi } else { destFile = destDirFolder.create(srcFile.getName()); } + destFile.cacheStrategy(IFile.STRATEGY_OUTPUT); result[pos++] = destFile; } @@ -418,6 +420,9 @@ protected int moveOrCopyFiles(final boolean move, String what, PhotoPropertiesDi // destFile might have renamed it-s extension for private images destFile = FileFacade.convert(mDebugPrefix + "moveOrCopyFiles dest", modifiedOutPath); sameFile = (sourceFile != null) && sourceFile.equals(destFile); + if (!sameFile && destFile != null) { + destFile.cacheStrategy(IFile.STRATEGY_OUTPUT); + } } addProcessedFiles(move, destFile, sourceFile); @@ -532,7 +537,7 @@ protected boolean osRenameTo(File dest, File source) { } protected boolean osRenameTo(IFile dest, IFile source) { - return source.renameTo(dest); + return source.renameTo(dest.getName()); } /** diff --git a/fotolib2/src/main/java/de/k3b/media/ExifInterface.java b/fotolib2/src/main/java/de/k3b/media/ExifInterface.java index 7dc2c240..490782a1 100644 --- a/fotolib2/src/main/java/de/k3b/media/ExifInterface.java +++ b/fotolib2/src/main/java/de/k3b/media/ExifInterface.java @@ -2877,10 +2877,6 @@ protected OutputStream createOutputStream(IFile outFile) throws FileNotFoundExce return outFile.openOutputStream(); } - protected boolean renameTo(IFile originalInFile, IFile renamedInFile) { - return originalInFile.renameTo(renamedInFile.getName()); - } - protected String getAbsolutePath(IFile inFile) { return inFile.getAbsolutePath(); } diff --git a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileFacade.java b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileFacade.java index c0010287..b34858ba 100644 --- a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileFacade.java +++ b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileFacade.java @@ -135,24 +135,13 @@ public static void setFileFacade(Converter fileFacade) { FileFacade.fileFacade = fileFacade; } - @Deprecated - @Override - public boolean renameTo(IFile newName) { - return renameImpl(newName.getFile()); - } - @Override public boolean renameTo(String newName) { File newFile = new File(this.file.getParentFile(), newName); - final boolean result = renameImpl(newFile); + final boolean result = this.file.renameTo(newFile); return result; } - private boolean renameImpl(File newFile) { - final boolean success = this.file.renameTo(newFile); - return success; - } - @Override public boolean delete() { return file.delete(); @@ -326,7 +315,7 @@ public long length() { @Override public String toString() { - return String.format("%s: %s", this.getClass().getSimpleName(), file.getAbsoluteFile()); + return String.format("%s: %i-%s", this.getClass().getSimpleName(), strategyID, file.getAbsoluteFile()); } @Override diff --git a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileWrapper.java b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileWrapper.java index 1acf9d4c..eb0f9b4a 100644 --- a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileWrapper.java +++ b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/FileWrapper.java @@ -37,11 +37,6 @@ public void set(IFile src) { child.set(src); } - @Override - public boolean renameTo(IFile newName) { - return child.renameTo(newName); - } - @Override public boolean renameTo(String newName) { return child.renameTo(newName); diff --git a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/IFile.java b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/IFile.java index 6674dc2f..0f767401 100644 --- a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/IFile.java +++ b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/IFile.java @@ -40,9 +40,6 @@ public interface IFile { void set(IFile src); - @Deprecated - boolean renameTo(IFile newName); - boolean renameTo(String newName); boolean delete(); @@ -104,5 +101,6 @@ public interface IFile { //------- file cache support IFile cacheStrategy(int strategyID); + // may be called after delete, renameTo, openOutputStream, mkdirs void invalidateParentDirCache(); } diff --git a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/StringFileFacade.java b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/StringFileFacade.java index ff5ceb52..208e46b2 100644 --- a/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/StringFileFacade.java +++ b/libK3bFilefacade/src/main/java/de/k3b/io/filefacade/StringFileFacade.java @@ -84,11 +84,6 @@ public void set(IFile src) { } - @Override - public boolean renameTo(IFile newName) { - return false; - } - @Override public boolean renameTo(String newName) { return false;