Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Delete all files under managed paths on cleanup
Browse files Browse the repository at this point in the history
Reviewed By: yukonfb

Differential Revision: D36265084

fbshipit-source-id: 0f026c0adab6e250bc25e6acc62e502251c1da75
  • Loading branch information
aandreyeu authored and facebook-github-bot committed May 17, 2022
1 parent cc50c61 commit 256981b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 104 deletions.
23 changes: 12 additions & 11 deletions java/main/com/facebook/profilo/logger/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import android.content.Context;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -113,11 +114,6 @@ public FileManager(Context context, @Nullable File customFolder) {
} else {
// If unable to create the custom folder or not provided, fallback to default
mBaseFolder = new File(context.getFilesDir(), PROFILO_FOLDER);
// Check if we need to migrate the old folder cache/profilo to files/profilo
File oldProfiloFolder = new File(context.getCacheDir(), PROFILO_FOLDER);
if (oldProfiloFolder.exists()) {
oldProfiloFolder.renameTo(mBaseFolder);
}
if (!mBaseFolder.exists() && !mBaseFolder.mkdirs()) {
throw new IllegalStateException("Unable to initialize Profilo folder");
}
Expand Down Expand Up @@ -232,12 +228,10 @@ public boolean deleteAllFiles() {

public Iterable<File> getAllFiles() {
List<File> allFiles = new ArrayList<>();

allFiles.addAll(getFiles(getUploadFolder(), PRIORITY_FILES_FILTER));
allFiles.addAll(getFiles(getUploadFolder(), DEFAULT_FILES_FILTER));
allFiles.addAll(getFiles(getFolder(), PRIORITY_FILES_FILTER));
allFiles.addAll(getFiles(getFolder(), DEFAULT_FILES_FILTER));
allFiles.addAll(getAllFiles(getFolder()));
allFiles.addAll(getAllFiles(getUploadFolder()));
allFiles.addAll(getAllFiles(getCrashDumpFolder()));
allFiles.addAll(getAllFiles(getMmapBufferFolder()));
return allFiles;
}

Expand Down Expand Up @@ -333,7 +327,14 @@ private void trimFolderByAge(
}

private List<File> getAllFiles(File folder) {
File[] files = folder.listFiles();
File[] files =
folder.listFiles(
new FileFilter() {
@Override
public boolean accept(File file) {
return file.isFile();
}
});
if (files == null) {
return Collections.EMPTY_LIST;
}
Expand Down
112 changes: 19 additions & 93 deletions java/test/com/facebook/profilo/logger/FileManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void setup() throws IOException {
mTempFile = File.createTempFile("fake-trace-", FileManager.TMP_SUFFIX, mFolder);
Files.touch(mTempFile);

when(mContext.getCacheDir()).thenReturn(mFolder);
when(mContext.getFilesDir()).thenReturn(mFolder);
mFileManager = new FileManager(mContext, mFolder);
mFileManager.setTrimThreshold(Integer.MAX_VALUE);
Expand Down Expand Up @@ -73,71 +72,8 @@ public void testFileManagerInitWithFakeCustomFolder() {
.isEqualTo(mFolder.getAbsolutePath());
}

@Test
public void testFileManagerInitWithMigration() throws IOException {
File cacheFolder = Files.createTempDir();
File cacheProfiloFolder = new File(cacheFolder, FileManager.PROFILO_FOLDER);
cacheProfiloFolder.mkdirs();
File fakeTraceFile =
File.createTempFile("fake-trace-", FileManager.TMP_SUFFIX, cacheProfiloFolder);

File filesFolder = Files.createTempDir();
File filesProfiloFolder = new File(filesFolder, FileManager.PROFILO_FOLDER);
filesProfiloFolder.mkdirs();

when(mContext.getCacheDir()).thenReturn(cacheFolder);
when(mContext.getFilesDir()).thenReturn(filesFolder);

try {
FileManager fileManager = new FileManager(mContext, null);
assertThat(fileManager.getFolder().getParentFile().getAbsolutePath())
.isEqualTo(filesFolder.getAbsolutePath());
assertThat(cacheProfiloFolder.exists()).isFalse();
assertThat(fileManager.getFolder().list().length).isEqualTo(1);
assertThat(fileManager.getFolder().list()[0]).isEqualTo(fakeTraceFile.getName());
} finally {
// Cleanup
deleteDir(cacheFolder);
deleteDir(filesFolder);
}
}

@Test
public void testFileManagerInitIgnoreMigrationIfNonEmptyTarget() throws IOException {
File cacheFolder = Files.createTempDir();
File cacheProfiloFolder = new File(cacheFolder, FileManager.PROFILO_FOLDER);
cacheProfiloFolder.mkdirs();
File fakeCacheTraceFile =
File.createTempFile("fake-trace-", FileManager.TMP_SUFFIX, cacheProfiloFolder);

File filesFolder = Files.createTempDir();
File filesProfiloFolder = new File(filesFolder, FileManager.PROFILO_FOLDER);
filesProfiloFolder.mkdirs();
File fakeFilesTraceFile =
File.createTempFile("fake-trace-", FileManager.TMP_SUFFIX, filesProfiloFolder);

when(mContext.getCacheDir()).thenReturn(cacheFolder);
when(mContext.getFilesDir()).thenReturn(filesFolder);

try {
FileManager fileManager = new FileManager(mContext, null);
assertThat(fileManager.getFolder().getParentFile().getAbsolutePath())
.isEqualTo(filesFolder.getAbsolutePath());
// Assert that migration was ignored
assertThat(cacheProfiloFolder.exists()).isTrue();
assertThat(fileManager.getFolder().list().length).isEqualTo(1);
assertThat(fileManager.getFolder().list()[0]).isEqualTo(fakeFilesTraceFile.getName());
} finally {
deleteDir(cacheFolder);
deleteDir(filesFolder);
}
}

@Test
public void testNormalFileManagerInitFromFilesFolder() {
File cacheFolder = new File("/dev/null/void");
when(mContext.getCacheDir()).thenReturn(cacheFolder);

FileManager fileManager = new FileManager(mContext, null);
assertThat(fileManager.getFolder().getParentFile().getAbsolutePath())
.isEqualTo(mFolder.getAbsolutePath());
Expand All @@ -159,6 +95,25 @@ public void testDeleteAllRemovesAlsoCrashDumpFiles() throws Exception {
assertNoFilesInFolder(mFolder);
}

@Test
public void testDeleteAllRemovesConfiguration() throws Exception {
File confiFile = new File(mFolder, "ProfiloInitFileConfig.json");
assertThat(confiFile.createNewFile()).isTrue();
mFileManager.deleteAllFiles();
assertNoFilesInFolder(mFolder);
}

@Test
public void testDeleteAllRemovesMmapBuffers() throws Exception {
File mmapFolder = mFileManager.getMmapBufferFolder();
if (!mmapFolder.exists()) {
mmapFolder.mkdirs();
}
File.createTempFile("mmap_buffer", "buff", mFileManager.getMmapBufferFolder());
mFileManager.deleteAllFiles();
assertNoFilesInFolder(mmapFolder);
}

@Test
public void testDeleteAllRemovesTrimmableFilesScheduledForUpload() throws Exception {
mFileManager.addFileToUploads(mTempFile, false);
Expand Down Expand Up @@ -212,26 +167,6 @@ public void testErrorsCreatingUploadDirUpdatesCorrectly() throws Exception {
assertNoFilesInFolder(mFolder);
}

@Test
public void testErrorsDeleteUpdatesCorrectly() throws Exception {
assertThat(mFileManager.mFileManagerStatistics.errorsDelete).isEqualTo(0);
mFileManager.addFileToUploads(mTempFile, false);

// Make files undeletable by converting to a non-empty dir of same name
for (File f : mFileManager.getAllFiles()) {
makeUndeletable(f);
}
mFileManager.deleteAllFiles();
assertThat(mFileManager.mFileManagerStatistics.errorsDelete).isEqualTo(1);

for (File f : mFileManager.getAllFiles()) {
deleteDir(f);
}

mFileManager.deleteAllFiles();
assertNoFilesInFolder(mFolder);
}

@Test
public void testErrorsMoveUpdatesCorrectly() throws Exception {
assertThat(mFileManager.mFileManagerStatistics.errorsMove).isEqualTo(0);
Expand Down Expand Up @@ -361,15 +296,6 @@ public boolean accept(File pathname) {
});
}

// Make a file undeletable by making it a non-empty directory.
private static void makeUndeletable(File f) throws IOException {
if (!f.delete()) {
return; // Nothing to do
}
f.mkdir();
File.createTempFile("foo", null, f);
}

// Deletes a File that we made "undeletable" above
private static void deleteDir(File f) {
// so directories are empty when we get to them
Expand Down

0 comments on commit 256981b

Please sign in to comment.