From ad4dc5ae122eb03a75b1b0d72bc2e182bde98b17 Mon Sep 17 00:00:00 2001 From: Adrian Catana Date: Fri, 6 Oct 2023 06:12:39 -0700 Subject: [PATCH] Delete outdated libs as part of unpack() Summary: We might want different logic for different so sources. Reviewed By: danjin250 Differential Revision: D49539327 fbshipit-source-id: fc868e95d0c0063efc1964f4c381c5483c3f9560 --- java/com/facebook/soloader/ExoSoSource.java | 1 + java/com/facebook/soloader/ExtractFromZipSoSource.java | 1 + java/com/facebook/soloader/UnpackingSoSource.java | 10 ++-------- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/java/com/facebook/soloader/ExoSoSource.java b/java/com/facebook/soloader/ExoSoSource.java index c1a07f2..f6ca94e 100644 --- a/java/com/facebook/soloader/ExoSoSource.java +++ b/java/com/facebook/soloader/ExoSoSource.java @@ -126,6 +126,7 @@ public Dso[] getDsos() throws IOException { @Override public void unpack(File soDirectory) throws IOException { byte[] ioBuffer = new byte[32 * 1024]; + deleteUnmentionedFiles(mDsos); for (FileDso fileDso : mDsos) { FileInputStream is = new FileInputStream(fileDso.backingFile); try (InputDso inputDso = new InputDso(fileDso, is)) { diff --git a/java/com/facebook/soloader/ExtractFromZipSoSource.java b/java/com/facebook/soloader/ExtractFromZipSoSource.java index 432125d..c69f86a 100644 --- a/java/com/facebook/soloader/ExtractFromZipSoSource.java +++ b/java/com/facebook/soloader/ExtractFromZipSoSource.java @@ -136,6 +136,7 @@ public final Dso[] getDsos() throws IOException { public void unpack(File soDirectory) throws IOException { byte[] ioBuffer = new byte[32 * 1024]; ZipDso[] dsos = getExtractableDsosFromZip(); + deleteUnmentionedFiles(dsos); for (ZipDso zipDso : dsos) { InputStream is = mZipFile.getInputStream(zipDso.backingEntry); try (InputDso inputDso = new InputDso(zipDso, is)) { diff --git a/java/com/facebook/soloader/UnpackingSoSource.java b/java/com/facebook/soloader/UnpackingSoSource.java index 6bd1a00..dce597e 100644 --- a/java/com/facebook/soloader/UnpackingSoSource.java +++ b/java/com/facebook/soloader/UnpackingSoSource.java @@ -136,7 +136,7 @@ private static void writeState(File stateFileName, byte state, boolean runFsync) } /** Delete files not mentioned in the given DSO list. */ - private void deleteUnmentionedFiles(Dso[] dsos) throws IOException { + protected void deleteUnmentionedFiles(Dso[] dsos) throws IOException { String[] existingFiles = soDirectory.list(); if (existingFiles == null) { throw new IOException("unable to list directory " + soDirectory); @@ -169,6 +169,7 @@ private void deleteUnmentionedFiles(Dso[] dsos) throws IOException { protected abstract static class Unpacker implements Closeable { public abstract Dso[] getDsos() throws IOException; + /* Unpacks to soDirectory and removes existing outdated libs */ public abstract void unpack(File soDirectory) throws IOException; @Override @@ -358,17 +359,10 @@ private boolean refreshLocked(final FileLocker lock, final int flags) throws IOE final boolean runFsync = ((flags & PREPARE_FLAG_DISABLE_FS_SYNC_JOB) == 0); LogUtil.v(TAG, "so store dirty: regenerating"); writeState(stateFileName, STATE_DIRTY, runFsync); - Dso[] desiredDsos = null; try (Unpacker u = makeUnpacker(forceUnpacking)) { - desiredDsos = u.getDsos(); - deleteUnmentionedFiles(desiredDsos); u.unpack(soDirectory); } - if (desiredDsos == null) { - return false; // No sync needed - } - // N.B. We can afford to write the deps file without fsyncs because we've marked the DSO // store STATE_DIRTY, which will cause us to ignore all intermediate state when regenerating it. // That is, it's okay for the depsFile blocks to hit the disk before the actual DSO data file