Skip to content

Commit

Permalink
Workaround cleanupIntervalS timer not firing: Force cleanup on startup (
Browse files Browse the repository at this point in the history
fixes #990) (#1001)

.
  • Loading branch information
Catfriend1 authored Jul 22, 2023
1 parent f02d7db commit 56630b5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.preference.PreferenceManager;

import com.annimon.stream.Stream;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
Expand Down Expand Up @@ -152,6 +155,8 @@ public interface OnResultListener1<T> {
private int mLastOnlineDeviceCount = 0;
private int mLastTotalSyncCompletion = -1;

private Boolean hasShutdown = false;

private Gson mGson;

@Inject NotificationHandler mNotificationHandler;
Expand Down Expand Up @@ -221,6 +226,21 @@ private void checkReadConfigFromRestApiCompleted() {
LogV("Reading config from REST completed. Syncthing version is " + mVersion);
// Tell SyncthingService it can transition to State.ACTIVE.
mOnApiAvailableListener.onApiAvailable();

// Temporarily lower cleanupIntervalS for every folder to force cleanup after startup.
// https://github.com/Catfriend1/syncthing-android/issues/990
setVersioningCleanupIntervalS(2);
final Handler resetCleanupIntervalHandler = new Handler(Looper.getMainLooper());
resetCleanupIntervalHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (hasShutdown) {
LogV("Skipping setVersioningCleanupIntervalS(3600) due to hasShutdown == true");
return;
}
setVersioningCleanupIntervalS(3600);
}
}, 10000);
}
}

Expand Down Expand Up @@ -557,6 +577,7 @@ public void sendConfig() {
* This will cause SyncthingNative to exit and not restart.
*/
public void shutdown() {
hasShutdown = true;
new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_SHUTDOWN, mApiKey,
null, null, null);
}
Expand Down Expand Up @@ -1297,6 +1318,17 @@ public void applyCustomRunConditions(RunConditionMonitor runConditionMonitor) {
}
}

private void setVersioningCleanupIntervalS (Integer cleanupIntervalS) {
synchronized (mConfigLock) {
for (Folder folder : mConfig.folders) {
folder.versioning.cleanupIntervalS = cleanupIntervalS;
}
LogV("Set VersioningCleanupIntervalS to " + cleanupIntervalS);
sendConfig();
}
return;
}

private void onTotalSyncCompletionChange() {
// LogV("onTotalSyncCompletionChange fired.");
if (mNotificationHandler == null) {
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ public void generateConfig() throws OpenConfigException, SyncthingRunnable.Execu
changed = changeLocalDeviceName(localDeviceID) || changed;
}

// Change default folder section.
Element elementDefaults = (Element) mConfig.getDocumentElement()
.getElementsByTagName("defaults").item(0);
if (elementDefaults != null) {
Element elementDefaultFolder = (Element) elementDefaults
.getElementsByTagName("folder").item(0);
if (elementDefaultFolder != null) {
Element elementVersioning = (Element) elementDefaultFolder.getElementsByTagName("versioning").item(0);
if (elementVersioning != null) {
elementVersioning.setAttribute("type", "trashcan");
Node nodeParam = mConfig.createElement("param");
elementVersioning.appendChild(nodeParam);
Element elementParam = (Element) nodeParam;
elementParam.setAttribute("key", "cleanoutDays");
elementParam.setAttribute("val", "14");
changed = true;
}
}
}

// Set default folder to the "camera" folder: path and name
changed = changeDefaultFolder() || changed;

Expand Down Expand Up @@ -1155,6 +1175,12 @@ private boolean changeDefaultFolder() {
folder.setAttribute("type", Constants.FOLDER_TYPE_SEND_RECEIVE);
folder.setAttribute("fsWatcherEnabled", Boolean.toString(defaultFolder.fsWatcherEnabled));
folder.setAttribute("fsWatcherDelayS", Integer.toString(defaultFolder.fsWatcherDelayS));

Element elementVersioning = (Element) folder.getElementsByTagName("versioning").item(0);
if (elementVersioning != null) {
elementVersioning.setAttribute("type", "trashcan");
}

return true;
}

Expand Down
20 changes: 20 additions & 0 deletions debug_scripts/adb_pull_config_xml_cleanupDays.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@echo off
cls
SET SCRIPT_PATH=%~dps0
cd /d "%SCRIPT_PATH%"
REM
REM Script Consts.
REM
REM SET PACKAGE_NAME=com.github.catfriend1.syncthingandroid
SET PACKAGE_NAME=com.github.catfriend1.syncthingandroid.debug
REM
:loopMe
cls
adb shell "su root cat /data/data/%PACKAGE_NAME%/files/config.xml" > "%SCRIPT_PATH%config.xml"
IF EXIST "%SCRIPT_PATH%config.xml" TYPE "%SCRIPT_PATH%config.xml" | findstr /c:"<folder id=" /c:"<cleanupIntervalS>" | findstr /v /c:"<folder id=""""" | ..\psreplace ".*label=" "" | ..\psreplace "path=.*" ""
echo.
pause
echo.
echo ==========================================================
echo.
goto :loopMe

0 comments on commit 56630b5

Please sign in to comment.