From e7c193af58c77ba144df1eb5c001778a75d45c5d Mon Sep 17 00:00:00 2001 From: Tony Lenz Date: Fri, 11 Oct 2024 15:42:34 -0700 Subject: [PATCH] Truncate export bookmarks file on overwrite (#4371) Task/Issue URL: https://github.com/duckduckgo/Android/issues/3069 ### Description In Android 10/11, the "w" write mode no longer truncates the file during an overwrite. More details about this workaround can be found at https://issuetracker.google.com/issues/180526528 ### Steps to test this PR 1. Add some bookmarks to DDG and export them. 2. Delete half of the bookmarks and export them again to the same file, confirming overwriting. 3. Observe the size and contents of the file.
Example non-truncated output pre-fix ``` Bookmarks

Bookmarks

DuckDuckGo Bookmarks

DuckDuckGo - Wikipedia

/en.m.wikipedia.org/wiki/DuckDuckGo" ADD_DATE="1618844074" LAST_MODIFIED="1618844074">DuckDuckGo - Wikipedia

About DuckDuckGo

```

Example truncated output post-fix ``` Bookmarks

Bookmarks

DuckDuckGo Bookmarks

DuckDuckGo - Wikipedia

```

--- .../duckduckgo/savedsites/impl/service/SavedSitesExporter.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/saved-sites/saved-sites-impl/src/main/java/com/duckduckgo/savedsites/impl/service/SavedSitesExporter.kt b/saved-sites/saved-sites-impl/src/main/java/com/duckduckgo/savedsites/impl/service/SavedSitesExporter.kt index 8c35bd2fa1ce..28665ee44a94 100644 --- a/saved-sites/saved-sites-impl/src/main/java/com/duckduckgo/savedsites/impl/service/SavedSitesExporter.kt +++ b/saved-sites/saved-sites-impl/src/main/java/com/duckduckgo/savedsites/impl/service/SavedSitesExporter.kt @@ -58,7 +58,8 @@ class RealSavedSitesExporter( if (content.isEmpty()) { return ExportSavedSitesResult.NoSavedSitesExported } - val file = contentResolver.openFileDescriptor(uri, "w") + // "w" mode does not truncate on Android 10/11, use "rwt" mode workaround - https://issuetracker.google.com/issues/180526528 + val file = contentResolver.openFileDescriptor(uri, "rwt") if (file != null) { val fileOutputStream = FileOutputStream(file.fileDescriptor) fileOutputStream.write(content.toByteArray())