Skip to content

Commit

Permalink
Remove log files in Realm.deleteRealm (#2839)
Browse files Browse the repository at this point in the history
Close #2834
  • Loading branch information
beeender committed May 19, 2016
1 parent 1d569a0 commit 165915e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* The annotation processor now correctly reports an error if trying to reference interfaces in model classes (#2808).
* 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();
// A core upgrade might change the location of the 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
15 changes: 13 additions & 2 deletions realm/realm-library/src/main/java/io/realm/BaseRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ 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"),
Expand Down Expand Up @@ -628,9 +629,19 @@ public void onResult(int count) {
File realmFolder = configuration.getRealmFolder();
String realmFileName = configuration.getRealmFileName();
File managementFolder = new File(realmFolder, realmFileName + management);
realmDeleted.set(deletes(realmFolder.getPath()+ "/" + realmFileName + management, managementFolder, realmFileName));
realmDeleted.set(realmDeleted.get() && deletes(canonicalPath, realmFolder, realmFileName));

// delete files in management folder and the folder
// there is no subfolders in the management folder
File[] files = managementFolder.listFiles();
if (files != null) {
for (File file : files) {
realmDeleted.set(realmDeleted.get() && file.delete());
}
}
realmDeleted.set(realmDeleted.get() && managementFolder.delete());

// delete specific files in root folder
realmDeleted.set(realmDeleted.get() && deletes(canonicalPath, realmFolder, realmFileName));
}
});

Expand Down

0 comments on commit 165915e

Please sign in to comment.