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

Add another attempt to check write permissions on a folder (fixes #1971) #2057

Closed
wants to merge 4 commits into from

Conversation

wrobbins
Copy link

@wrobbins wrobbins commented Feb 12, 2024

Description

Fixes issue #1971

Changes

  • Add another attempt to check write permissions on a folder translating ~ to externalStorageDirectory.
    • ignores Constants.PREF_USE_ROOT
    • uses java.io.File to check access instead of shelling out.

Notes

This an unobtrusive change, leaving the existing write-access check in place.

I tested this on a Pixel 6 Pro with Android 14.

@tomasz1986 tomasz1986 changed the title fix: Can't set a share on the internal storage to "Send & Receive" #1971 Add another attempt to check write permissions on a folder (fixes #1971) Feb 16, 2024
public static Boolean nativeBinaryCanWriteToPath2(File externalStorageDir, String absoluteFolderPath) {
final String TOUCH_FILE_NAME = ".stwritetest";

// normalize path replacing ~ with external storage directory
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// normalize path replacing ~ with external storage directory
// Normalize path replacing ~ with external storage directory.

final String TOUCH_FILE_NAME = ".stwritetest";

// normalize path replacing ~ with external storage directory
// this is consistent with what $HOME is set to in SyncthingRunnable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// this is consistent with what $HOME is set to in SyncthingRunnable
// This is consistent with what $HOME is set to in SyncthingRunnable.

// this is consistent with what $HOME is set to in SyncthingRunnable
final String normalizedPath = absoluteFolderPath.replaceFirst("^~", externalStorageDir.getAbsolutePath());

// Write permission test file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Write permission test file
// Write permission test file.

* Returns if the syncthing binary would be able to write a file into
* the given folder given the configured access level.
*
* This uses the Android-native File API
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This uses the Android-native File API
* This uses the Android-native File API.

@@ -9,6 +9,8 @@
import android.os.Build;
import android.os.Bundle;
import androidx.documentfile.provider.DocumentFile;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the new line.

@@ -537,6 +539,10 @@ private void checkWriteAndUpdateUI() {
* Access level readwrite: folder can be configured "sendonly" or "sendreceive".
*/
mCanWriteToPath = Util.nativeBinaryCanWriteToPath(FolderActivity.this, mFolder.path);
if(!mCanWriteToPath){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(!mCanWriteToPath){
if (!mCanWriteToPath){

// Write permission test file
File touchFile = new File(normalizedPath, TOUCH_FILE_NAME);
final String touchFilePath = touchFile.getAbsolutePath();
try{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try{
try {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double-check whether the code follows the same convention as the current one (spacing, new lines, etc.).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomasz1986 thank you for the review. I addressed the formatting issues you highlighted.

Copy link
Member

@imsodin imsodin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! And sorry for the long wait on the review.
This looks good. One question inline and a concern below, just in case you have any insight on it.

I am a somewhat worried that Android handles IO differently when executing shell command vs using the java file API. However I think it's better to potentially have some false positives than the current state, where the detection always fails and users need to know they can workaround it by creating a folder in the web UI.

Also FYI it might be a while (at worst never) until this reaches users due to google, see #2064

Comment on lines +183 to +185
// Normalize path replacing ~ with external storage directory.
// This is consistent with what $HOME is set to in SyncthingRunnable.
final String normalizedPath = absoluteFolderPath.replaceFirst("^~", externalStorageDir.getAbsolutePath());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test explicitly if this part alone helped?

Seems like we should use the same environment, including setting HOME, when executing the shell command as we do in SyncthingRunnable.

@wrobbins wrobbins closed this Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants