Deprecate FLAG_REDIRECT_STDERR

This commit is contained in:
topjohnwu 2024-06-24 18:06:49 -07:00
parent 6c9618b878
commit c084ef5d32
5 changed files with 36 additions and 39 deletions

View File

@ -96,22 +96,7 @@ public abstract class Shell implements Closeable {
public static final int FLAG_MOUNT_MASTER = (1 << 1);
/* Preserve (1 << 2) due to historical reasons */
/**
* If set, STDERR outputs will be redirected to STDOUT outputs.
* <p>
* Note: This flag only affects the following methods:
* <ul>
* <li>{@link #cmd(String...)}</li>
* <li>{@link #cmd(InputStream)}</li>
* <li>{@link Job#to(List)}</li>
* </ul>
* Check the descriptions of each method above for more details.
* <p>
* Constant value {@value}.
*/
public static final int FLAG_REDIRECT_STDERR = (1 << 3);
/* Preserve (1 << 3) due to historical reasons */
/* Preserve (1 << 4) due to historical reasons */
@Retention(SOURCE)
@ -132,6 +117,21 @@ public abstract class Shell implements Closeable {
*/
public static boolean enableVerboseLogging = false;
/**
* This flag exists for compatibility reasons. DO NOT use unless necessary.
* <p>
* If enabled, STDERR outputs will be redirected to the STDOUT output list
* when a {@link Job} is configured with {@link Job#to(List)}.
* Since the {@code Shell.cmd(...)} methods are functionally equivalent to
* {@code Shell.getShell().newJob().add(...).to(new ArrayList<>())}, this variable
* also affects the behavior of those methods.
* <p>
* Note: The recommended way to redirect STDERR output to STDOUT is to assign the
* same list to both STDOUT and STDERR with {@link Job#to(List, List)}.
* The behavior of this flag is unintuitive and error prone.
*/
public static boolean enableLegacyStderrRedirection = false;
/**
* Override the default {@link Builder}.
* <p>
@ -387,12 +387,10 @@ public abstract class Shell implements Closeable {
}
/**
* Set flags that controls how {@code Shell} works and how a new {@code Shell} will be
* constructed.
* Set flags to control how a new {@code Shell} will be constructed.
* @param flags the desired flags.
* Value is either 0 or bitwise-or'd value of
* {@link #FLAG_NON_ROOT_SHELL}, {@link #FLAG_MOUNT_MASTER}, or
* {@link #FLAG_REDIRECT_STDERR}
* {@link #FLAG_NON_ROOT_SHELL} or {@link #FLAG_MOUNT_MASTER}
* @return this Builder object for chaining of calls.
*/
@NonNull
@ -547,16 +545,12 @@ public abstract class Shell implements Closeable {
public abstract static class Job {
/**
* Store output to a specific list.
* <p>
* Output of STDERR will be also be stored in the same {@link List} if the flag
* {@link #FLAG_REDIRECT_STDERR} is set; {@link Result#getErr()}
* will always return an empty list.
* @param output the list to store outputs. Pass {@code null} to omit all outputs.
* Store output of STDOUT to a specific list.
* @param stdout the list to store STDOUT. Pass {@code null} to omit all outputs.
* @return this Job object for chaining of calls.
*/
@NonNull
public abstract Job to(@Nullable List<String> output);
public abstract Job to(@Nullable List<String> stdout);
/**
* Store output of STDOUT and STDERR to specific lists.
@ -710,6 +704,14 @@ public abstract class Shell implements Closeable {
@Deprecated
public static final int ROOT_MOUNT_MASTER = 2;
/**
* For compatibility, setting this flag will set {@link #enableLegacyStderrRedirection}
* @deprecated not used anymore
* @see #enableLegacyStderrRedirection
*/
@Deprecated
public static final int FLAG_REDIRECT_STDERR = (1 << 3);
/**
* Whether the application has access to root.
* <p>

View File

@ -18,6 +18,7 @@ package com.topjohnwu.superuser.internal;
import static com.topjohnwu.superuser.Shell.FLAG_MOUNT_MASTER;
import static com.topjohnwu.superuser.Shell.FLAG_NON_ROOT_SHELL;
import static com.topjohnwu.superuser.Shell.FLAG_REDIRECT_STDERR;
import android.content.Context;
import android.text.TextUtils;
@ -132,6 +133,9 @@ public final class BuilderImpl extends Shell.Builder {
Utils.ex(e);
throw new NoShellException("Unable to create a shell!", e);
}
if (hasFlags(FLAG_REDIRECT_STDERR)) {
Shell.enableLegacyStderrRedirection = true;
}
MainShell.setCached(shell);
if (initializers != null) {
Context ctx = Utils.getContext();

View File

@ -47,8 +47,6 @@ abstract class JobTask extends Shell.Job implements Shell.Task {
private final List<ShellInputSource> sources = new ArrayList<>();
boolean redirect;
@Nullable protected List<String> out = null;
@Nullable protected List<String> err = UNSET_LIST;
@Nullable protected Executor callbackExecutor;
@ -75,7 +73,7 @@ abstract class JobTask extends Shell.Job implements Shell.Task {
boolean noErr = err == UNSET_LIST;
List<String> outList = out;
List<String> errList = noErr ? (redirect ? out : null) : err;
List<String> errList = noErr ? (Shell.enableLegacyStderrRedirection ? out : null) : err;
if (outList != null && outList == errList && !Utils.isSynchronized(outList)) {
// Synchronize the list internally only if both lists are the same and are not
@ -119,8 +117,8 @@ abstract class JobTask extends Shell.Job implements Shell.Task {
@NonNull
@Override
public Shell.Job to(List<String> output) {
out = output;
public Shell.Job to(List<String> stdout) {
out = stdout;
return this;
}

View File

@ -42,7 +42,6 @@ import java.util.concurrent.TimeoutException;
class ShellImpl extends Shell {
private volatile int status;
private final boolean redirect;
private final Process proc;
private final NoCloseOutputStream STDIN;
private final NoCloseInputStream STDOUT;
@ -87,7 +86,6 @@ class ShellImpl extends Shell {
ShellImpl(BuilderImpl builder, Process process) throws IOException {
status = UNKNOWN;
redirect = builder.hasFlags(FLAG_REDIRECT_STDERR);
proc = process;
STDIN = new NoCloseOutputStream(process.getOutputStream());
STDOUT = new NoCloseInputStream(process.getInputStream());
@ -226,10 +224,6 @@ class ShellImpl extends Shell {
return;
}
if (task instanceof JobTask) {
((JobTask) task).redirect = redirect;
}
task.run(STDIN, STDOUT, STDERR);
}

View File

@ -50,7 +50,6 @@ public class MainActivity extends Activity implements Handler.Callback {
static {
Shell.enableVerboseLogging = BuildConfig.DEBUG;
Shell.setDefaultBuilder(Shell.Builder.create()
.setFlags(Shell.FLAG_REDIRECT_STDERR)
.setInitializers(ExampleInitializer.class)
);
}