Skip to content

Commit

Permalink
Remove log files in Realm.deleteRealm
Browse files Browse the repository at this point in the history
Close #2834
  • Loading branch information
beeender committed May 19, 2016
1 parent 4e0e867 commit c9c8213
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Calling RealmResults.deleteAllFromRealm() might lead to native crash (#2759).
* Added null check to `addChangeListener` and `removeChangeListener` in `Realm` and `DynamicRealm` (#2772).
* Calling `RealmObjectSchema.addPrimaryKey()` adds an index to the primary key field, and calling `RealmObjectSchema.removePrimaryKey()` removes the index from the field (#2832).
* Log files are not deleted when calling `Realm.deleteRealm()` (#2834).

### Enhancements

Expand Down
26 changes: 8 additions & 18 deletions realm/realm-library/src/androidTest/java/io/realm/RealmTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1884,20 +1884,9 @@ public void setter_updateField() throws Exception {

@Test
public void deleteRealm() throws InterruptedException {
File tempDir = new File(context.getFilesDir(), "delete_test_dir");
if (!tempDir.exists()) {
assertTrue(tempDir.mkdir());
}

assertTrue(tempDir.isDirectory());

// Delete all files in the directory
File[] files = tempDir.listFiles();
if (files != null) {
for (File file : files) {
assertTrue(file.delete());
}
}
File tempDir = new File(configFactory.getRoot(), "delete_test_dir");
File tempDirRenamed = new File(configFactory.getRoot(), "delete_test_dir_2");
assertTrue(tempDir.mkdir());

final RealmConfiguration configuration = new RealmConfiguration.Builder(tempDir).build();

Expand All @@ -1922,14 +1911,15 @@ public void run() {
realm.beginTransaction();
realm.createObject(AllTypes.class);
realm.commitTransaction();
// Move the directory so core won't be able to delete log files
assertTrue(tempDir.renameTo(tempDirRenamed));
readyToCloseLatch.countDown();

realm.close();
closedLatch.await();
// Now we get log files back!
assertTrue(tempDirRenamed.renameTo(tempDir));

// ATTENTION: log, log_a, log_b will be deleted when the other thread close the Realm peacefully. And we force
// user to close all Realm instances before deleting. It would be difficult to simulate a case that log files
// exist before deletion. Let's keep the case like this for now, we might allow user to delete Realm even there
// are instances opened in the future.
assertTrue(Realm.deleteRealm(configuration));

// Directory should be empty now
Expand Down
5 changes: 5 additions & 0 deletions realm/realm-library/src/main/java/io/realm/BaseRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,14 @@ static private boolean deletes(String canonicalPath, File rootFolder, String rea
List<File> filesToDelete = Arrays.asList(
new File(rootFolder, realmFileName),
new File(rootFolder, realmFileName + ".lock"),
// Old core log file naming styles
new File(rootFolder, realmFileName + ".log_a"),
new File(rootFolder, realmFileName + ".log_b"),
new File(rootFolder, realmFileName + ".log"),
// New styles
new File(rootFolder, "log_a"),
new File(rootFolder, "log_b"),
new File(rootFolder, "log_access"),
new File(canonicalPath));
for (File fileToDelete : filesToDelete) {
if (fileToDelete.exists()) {
Expand Down

0 comments on commit c9c8213

Please sign in to comment.