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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.os.Bundle;
import androidx.documentfile.provider.DocumentFile;
import android.os.Environment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
Expand Down Expand Up @@ -537,6 +538,10 @@ private void checkWriteAndUpdateUI() {
* Access level readwrite: folder can be configured "sendonly" or "sendreceive".
*/
mCanWriteToPath = Util.nativeBinaryCanWriteToPath(FolderActivity.this, mFolder.path);
if (!mCanWriteToPath){
final File externalStorageDirectory = Environment.getExternalStorageDirectory();
mCanWriteToPath = Util.nativeBinaryCanWriteToPath2(externalStorageDirectory, mFolder.path);
}
if (mCanWriteToPath) {
binding.accessExplanationView.setText(R.string.folder_path_readwrite);
binding.folderType.setEnabled(true);
Expand Down
33 changes: 32 additions & 1 deletion app/src/main/java/com/nutomic/syncthingandroid/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.preference.PreferenceManager;
import androidx.appcompat.app.AlertDialog;
import android.util.Log;
Expand Down Expand Up @@ -172,6 +171,38 @@ public static boolean nativeBinaryCanWriteToPath(Context context, String absolut
return true;
}

/**
* 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.
*/
public static Boolean nativeBinaryCanWriteToPath2(File externalStorageDir, String absoluteFolderPath) {
final String TOUCH_FILE_NAME = ".stwritetest";

// 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());
Comment on lines +183 to +185
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.


// Write permission test file.
File touchFile = new File(normalizedPath, TOUCH_FILE_NAME);
final String touchFilePath = touchFile.getAbsolutePath();
try {
Boolean fileCreated = touchFile.createNewFile();
Log.d(TAG, String.format("%s createNewFile result %b", touchFilePath, fileCreated));
Boolean fileDeleted = touchFile.delete();
Log.d(TAG, String.format("%s delete result %b", touchFilePath, fileDeleted));
} catch (IOException e) {
Log.e(TAG, String.format("Failed to write test file '%s'", touchFilePath), e);
return false;
}

// Detected we have write permission.
Log.i(TAG, String.format("Successfully wrote test file '%s'", touchFile));

return true;
}

/**
* Run command in a shell and return the exit code.
*/
Expand Down
Loading