Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash when saving file - duplicate names [WPB-5026] #2402

Merged
merged 3 commits into from
Nov 10, 2023

Conversation

AndroidBob
Copy link
Collaborator

@AndroidBob AndroidBob commented Nov 8, 2023

BugWPB-5026 [Android] Crash when downloading a file

Cherry pick from the original PR:


⚠️ Conflicts during cherry-pick:
kalium


PR Submission Checklist for internal contributors

  • The PR Title

    • conforms to the style of semantic commits messages¹ supported in Wire's Github Workflow²
    • contains a reference JIRA issue number like SQPIT-764
    • answers the question: If merged, this PR will: ... ³
  • The PR Description

    • is free of optional paragraphs and you have filled the relevant parts to the best of your ability

What's new in this PR?

Issues

On some devices, the app is randomly crashing when downloading a file.
java.lang.IllegalStateException: Failed to build unique file: /storage/emulated/0/Download/image owner_package_name=com.waz.zclient.dev _display_name=image mime_type=application/octet-stream _data=/storage/emulated/0/Download/image _size=246181 is_download=1 relative_path=Download/

Causes (Optional)

In theory, ContentResolver handles duplicate names itself and we use it on Android 10 and up, but it uses buildUniqueFileWithExtension from FileUtils which looks like this:

while (file.exists()) {
            if (n++ >= 32) {
                throw new FileNotFoundException("Failed to create unique file");
            }
            file = buildFile(parent, name + " (" + n + ")", ext);
        }

so it works but up to 32 times, after that it returns this exact crash.

Solutions

Use our function findFirstUniqueName also for Android versions that use ContentResolver, to also determine unique filename ourselves.

This ContentResolver also changes the file names to remove disallowed characters (like colon or quotation mark) so it's then not possible to compare these names, so it's required to do the same (also to not allow for creating files with invalid names), that's why sanitizeFilename is also added.

Testing

Test Coverage (Optional)

  • I have added automated test to this contribution

How to Test

Try to save the same file more than 32 times.


PR Post Submission Checklist for internal contributors (Optional)

  • Wire's Github Workflow has automatically linked the PR to a JIRA issue

PR Post Merge Checklist for internal contributors

  • If any soft of configuration variable was introduced by this PR, it has been added to the relevant documents and the CI jobs have been updated.

References
  1. https://sparkbox.com/foundry/semantic_commit_messages
  2. https://github.com/wireapp/.github#usage
  3. E.g. feat(conversation-list): Sort conversations by most emojis in the title #SQPIT-764.

@AndroidBob AndroidBob added the cherry-pick PR is cherry-picking changes from another banch label Nov 8, 2023
Copy link
Contributor

github-actions bot commented Nov 8, 2023

Test Results

680 tests  +1   680 ✔️ +1   8m 32s ⏱️ +3s
  98 suites ±0       0 💤 ±0 
  98 files   ±0       0 ±0 

Results for commit ae53cb0. ± Comparison against base commit 23af8cd.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Nov 8, 2023

APKs built during tests are available here. Scroll down to Artifacts!

Copy link
Contributor

github-actions bot commented Nov 8, 2023

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator Author

Build 1699 succeeded.

The build produced the following APK's:

@saleniuk saleniuk requested review from a team, alexandreferris, borichellow, MohamadJaara, vitorhugods and ohassine and removed request for a team November 8, 2023 16:44
Copy link

codecov bot commented Nov 9, 2023

Codecov Report

Merging #2402 (b0d84a5) into develop (23af8cd) will increase coverage by 0.00%.
The diff coverage is n/a.

❗ Current head b0d84a5 differs from pull request most recent head ae53cb0. Consider uploading reports for the commit ae53cb0 to get more accurate results

@@            Coverage Diff             @@
##             develop    #2402   +/-   ##
==========================================
  Coverage      41.46%   41.47%           
+ Complexity      1083     1081    -2     
==========================================
  Files            344      344           
  Lines          12408    12389   -19     
  Branches        1630     1629    -1     
==========================================
- Hits            5145     5138    -7     
+ Misses          6779     6767   -12     
  Partials         484      484           
Files Coverage Δ
.../src/main/kotlin/com/wire/android/util/FileUtil.kt 0.00% <ø> (ø)

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 23af8cd...ae53cb0. Read the comment docs.

Copy link
Contributor

github-actions bot commented Nov 9, 2023

APKs built during tests are available here. Scroll down to Artifacts!

@AndroidBob
Copy link
Collaborator Author

Build 1722 succeeded.

The build produced the following APK's:

@saleniuk saleniuk added this pull request to the merge queue Nov 10, 2023
Merged via the queue into develop with commit 914723f Nov 10, 2023
12 checks passed
@saleniuk saleniuk deleted the fix/download_file_crash-cherry-pick branch November 10, 2023 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-pick PR is cherry-picking changes from another banch size/M
Projects
None yet
3 participants