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] #2396

Merged
merged 5 commits into from
Nov 8, 2023

Conversation

saleniuk
Copy link
Contributor

@saleniuk saleniuk commented Nov 7, 2023


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.

Copy link
Contributor

github-actions bot commented Nov 7, 2023

Test Results

599 tests   598 ✔️  7m 13s ⏱️
  85 suites      1 💤
  85 files        0

Results for commit 8b0e94c.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Nov 7, 2023

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

@AndroidBob
Copy link
Collaborator

Build 1675 succeeded.

The build produced the following APK's:

@saleniuk saleniuk requested review from a team, typfel, gongracr, yamilmedina, borichellow and mchenani and removed request for a team and gongracr November 7, 2023 15:01
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

Build 1692 succeeded.

The build produced the following APK's:

@saleniuk saleniuk merged commit 76bd823 into release/candidate Nov 8, 2023
9 checks passed
@saleniuk saleniuk deleted the fix/download_file_crash branch November 8, 2023 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants