Skip to content

Commit a5ebc83

Browse files
adicatanafacebook-github-bot
authored andcommitted
Run prepare() for BackupSoSource to cleanup previous state
Summary: Currently, the BackupSoSource does not clean after itself when a new version of the app is installed. That is because prepare() does not run unless there is a crash. This diff changes that by running prepare() with special flags whenever initialising SoLoader. With the new flag, we make sure that `deleteSoFiles` runs when necessary, e.g. when deps have changed. To differentiate between different states, we simply skip writing sos to disk and the deps state (which is empty). Reviewed By: danjin250 Differential Revision: D60186847 fbshipit-source-id: e029b9c31ac9a677cae5e9c9d80cf8aee79da35d
1 parent fd35038 commit a5ebc83

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

java/com/facebook/soloader/BackupSoSource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public int loadLibrary(String soName, int loadFlags, StrictMode.ThreadPolicy thr
100100

101101
@Override
102102
public void prepare(int flags) throws IOException {
103-
if ((flags & SoSource.PREPARE_FLAG_SKIP_BACKUP_SO_SOURCE) != 0) {
103+
if ((flags & SoSource.PREPARE_FLAG_NO_UNPACKING_BACKUP_SO_SOURCE) != 0) {
104+
super.prepare(flags | SoSource.PREPARE_FLAG_NO_UNPACKING);
104105
return;
105106
}
106107
super.prepare(flags);

java/com/facebook/soloader/SoLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ private static int makePrepareFlags() {
534534
prepareFlags |= SoSource.PREPARE_FLAG_DISABLE_FS_SYNC_JOB;
535535
}
536536
if ((sFlags & SOLOADER_EXPLICITLY_ENABLE_BACKUP_SOSOURCE) == 0) {
537-
prepareFlags |= SoSource.PREPARE_FLAG_SKIP_BACKUP_SO_SOURCE;
537+
prepareFlags |= SoSource.PREPARE_FLAG_NO_UNPACKING_BACKUP_SO_SOURCE;
538538
}
539539
return prepareFlags;
540540
} finally {

java/com/facebook/soloader/SoSource.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public abstract class SoSource {
5959
public static final int PREPARE_FLAG_DISABLE_FS_SYNC_JOB = (1 << 2);
6060

6161
/** Skip preparing backup so source. */
62-
public static final int PREPARE_FLAG_SKIP_BACKUP_SO_SOURCE = (1 << 3);
62+
public static final int PREPARE_FLAG_NO_UNPACKING_BACKUP_SO_SOURCE = (1 << 3);
63+
64+
public static final int PREPARE_FLAG_NO_UNPACKING = (1 << 4);
6365

6466
/** Prepare to install this SoSource in SoLoader. */
6567
protected void prepare(int flags) throws IOException {

java/com/facebook/soloader/UnpackingSoSource.java

+16-11
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,23 @@ private boolean refreshLocked(final FileLocker lock, final int flags) throws IOE
338338
LogUtil.v(TAG, "so store dirty: regenerating");
339339
writeState(stateFileName, STATE_DIRTY, runFsync);
340340
deleteSoFiles();
341-
try (Unpacker u = makeUnpacker()) {
342-
u.unpack(soDirectory);
343-
}
344341

345-
// N.B. We can afford to write the deps file without fsyncs because we've marked the DSO
346-
// store STATE_DIRTY, which will cause us to ignore all intermediate state when regenerating it.
347-
// That is, it's okay for the depsFile blocks to hit the disk before the actual DSO data file
348-
// blocks as long as both hit the disk before we reset STATE_CLEAN.
349-
final File depsFileName = new File(soDirectory, DEPS_FILE_NAME);
350-
try (RandomAccessFile depsFile = new RandomAccessFile(depsFileName, "rw")) {
351-
depsFile.write(recomputedDeps);
352-
depsFile.setLength(depsFile.getFilePointer());
342+
final boolean noUnpacking = (flags & PREPARE_FLAG_NO_UNPACKING) != 0;
343+
if (!noUnpacking) {
344+
try (Unpacker u = makeUnpacker()) {
345+
u.unpack(soDirectory);
346+
}
347+
348+
// N.B. We can afford to write the deps file without fsyncs because we've marked the DSO
349+
// store STATE_DIRTY, which will cause us to ignore all intermediate state when regenerating
350+
// it.
351+
// That is, it's okay for the depsFile blocks to hit the disk before the actual DSO data file
352+
// blocks as long as both hit the disk before we reset STATE_CLEAN.
353+
final File depsFileName = new File(soDirectory, DEPS_FILE_NAME);
354+
try (RandomAccessFile depsFile = new RandomAccessFile(depsFileName, "rw")) {
355+
depsFile.write(recomputedDeps);
356+
depsFile.setLength(depsFile.getFilePointer());
357+
}
353358
}
354359

355360
// Task to dump the buffer cache to disk to guard against battery outages. The default is to run

0 commit comments

Comments
 (0)