Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6359bc44 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

add method to remove oldestCrashlogs. add CrashlogsFileFilter with unitTest class.

parent 3c5f9953
Loading
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
package foundation.e.drive.fileFilters;

import java.io.File;
import java.io.FileFilter;

import foundation.e.drive.utils.ServiceExceptionHandler;

public class CrashlogsFileFilter implements FileFilter {
    private final static long max_timestamp_delta = 864000000; //10 days in ms (240*3600*1000)

    @Override
    public boolean accept(File pathname) {
        String fileTimestamp = extractTimestamp(pathname.getName(),
                ServiceExceptionHandler.LOG_FILE_NAME_PREFIX,
                ServiceExceptionHandler.LOG_FILE_EXTENSION);

        long timestamp;
        try {
            timestamp = Long.parseLong(fileTimestamp);
        }catch (NumberFormatException e){
            //Can't parse the extracted timestamp
            //This file has not the expected name. It must be removed
            return true;
        }

        //if current Date - file date >= max deta allowed
        return ((System.currentTimeMillis() - timestamp ) >= max_timestamp_delta);
    }

    /**
     * Extract the timestamp from the name of the file
     * UnitTested!
     * @param fileName Filename
     * @param prefix prefix to ignore
     * @param extension extension to ignore
     * @return the timestamp extracted from the name
     */
    private String extractTimestamp(String fileName, String prefix, String extension){
        return fileName.substring(prefix.length(), (fileName.length() - extension.length()));
    }

}
+28 −0
Original line number Diff line number Diff line
@@ -30,13 +30,17 @@ import com.owncloud.android.lib.resources.files.model.RemoteFile;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.stream.Stream;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.fileFilters.CrashlogsFileFilter;
import foundation.e.drive.fileFilters.FileFilterFactory;
import foundation.e.drive.fileFilters.OnlyFileFilter;
import foundation.e.drive.models.SyncedFolder;
@@ -151,12 +155,36 @@ public class ObserverService extends Service implements OnRemoteOperationListene
        Log.i(TAG, "begin()");
        this.isWorking = true;
        clearCachedFile();
        deleteOldestCrashlogs();
        startScan(true);
    }

    /**
     * This method remove all the crash-logs file
     * in external dir that are 10 days or more old.
     */
    private void deleteOldestCrashlogs(){
        Log.i(TAG, "deleteOldestCrashLogs()");
        File[] fileToRemove = getExternalFilesDir(ServiceExceptionHandler.CRASH_LOG_FOLDER)
                .listFiles(new CrashlogsFileFilter());

        int counter = 0;
        for (File file : fileToRemove) {
            try {
                file.delete();
                ++counter;
            }catch (SecurityException e){
                e.printStackTrace();
            }
        }
        Log.d(TAG, counter+" old crashlogs file.s deleted");
    }


    /**
     * Clear cached file unused:
     * remove each cached file which isn't in OperationManagerService.lockedSyncedFileState();
     * @TODO rewrite this method!
     */
    private void clearCachedFile(){
        Log.i(TAG, "clearCachedFile()");
+5 −2
Original line number Diff line number Diff line
@@ -26,6 +26,9 @@ import foundation.e.drive.services.OperationManagerService;
 */
public class ServiceExceptionHandler implements UncaughtExceptionHandler{
    private final static String TAG = ServiceExceptionHandler.class.getSimpleName();
    public final static String CRASH_LOG_FOLDER = "crash-logs";
    public final static String LOG_FILE_NAME_PREFIX = "eDrive-crash-";
    public final static String LOG_FILE_EXTENSION = ".log";
    private UncaughtExceptionHandler defaultUEH;
    private Service service;

@@ -58,9 +61,9 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
            Long timestamp = System.currentTimeMillis();

            //Create a new file that user can sent to us
            String fileName = "eDrive-crash-"+timestamp+".log";
            String fileName = LOG_FILE_NAME_PREFIX+timestamp+LOG_FILE_EXTENSION;

            File downloadDir = service.getApplication().getExternalFilesDir("Logs");
            File downloadDir = service.getApplication().getExternalFilesDir(CRASH_LOG_FOLDER);
            File logFile = new File(downloadDir, fileName);
            try {

+48 −0
Original line number Diff line number Diff line
package foundation.e.drive.Test.FileFilterTest;

import org.junit.Assert;
import org.junit.Test;

public class CrashlogFileFilterTest {

    private String mockFileName(String target, String prefix, String extension){
        return prefix+target+extension;
    }

    private String extractTimestamp(String fileName, String prefix, String extension){
        return fileName.substring(prefix.length(), (fileName.length() - extension.length()));
    }

    @Test
    public void extractTimeStampFromFileNameTest(){
        String prefix = "edrive-";
        String extension = ".log";
        String target = "";
        //Case 1 Empty Target
        String base = mockFileName(target, prefix, extension);
        Assert.assertEquals("Base length is incorrect", prefix.length()+extension.length(), base.length());

        String fileTimestamp = extractTimestamp(base, prefix, extension);
        Assert.assertEquals("result is not empty String", "", fileTimestamp);

        //Case 2: Prefix is empty
        prefix = "";
        target = "1234";
        base = mockFileName(target, prefix, extension);
        Assert.assertEquals("Base length is incorrect", prefix.length()+target.length()+extension.length(), base.length());

        fileTimestamp = extractTimestamp(base, prefix, extension);
        Assert.assertEquals("result is not empty String", target, fileTimestamp);

        //Case 3: extension is empty

        prefix = "edrive-";
        extension = "";

        base = mockFileName(target, prefix, extension);
        Assert.assertEquals("Base length is incorrect", prefix.length()+target.length(), base.length());

        fileTimestamp = extractTimestamp(base, prefix, extension);
        Assert.assertEquals("result is not empty String", target, fileTimestamp);
    }
}