MuntashirAkon/AppManager · error

Bad mode

Error message

Bad mode: ${mode}

What it means

FileUtils.modeToPosix(mode) maps FileSystemManager mode bits (MODE_READ_WRITE/MODE_WRITE_ONLY/MODE_READ_ONLY etc.) to OsConstants open() flags. If the mode bits contain no recognized access-mode combination, it throws IllegalArgumentException("Bad mode: " + mode).

Solutions

  1. Use the FileSystemManager.MODE_* constants: at least one of MODE_READ_ONLY, MODE_WRITE_ONLY, or MODE_READ_WRITE must be set.
  2. Build the mode with bitwise OR of constants (e.g. MODE_CREATE | MODE_TRUNCATE | MODE_WRITE_ONLY).
  3. Validate the mode before opening: (mode & (MODE_READ_ONLY|MODE_WRITE_ONLY|MODE_READ_WRITE)) != 0.

Example fix

// before
fs.openFile(f, 0);
// after
fs.openFile(f, FileSystemManager.MODE_WRITE_ONLY | FileSystemManager.MODE_CREATE);
Defensive patterns

Strategy: validation

Validate before calling

static boolean validAccessMode(int mode) {
    int acc = FileSystemManager.MODE_READ_ONLY | FileSystemManager.MODE_WRITE_ONLY | FileSystemManager.MODE_READ_WRITE;
    return (mode & acc) != 0;
}

Try / catch

try {
    fs.openFile(f, mode);
} catch (IllegalArgumentException e) {
    if (e.getMessage().startsWith("Bad mode")) mode |= FileSystemManager.MODE_READ_ONLY;
}

Prevention

When it happens

Trigger: Passing a mode int whose access bits are 0 or composed of flags not defined by FileSystemManager (e.g. passing 0, or raw O_* constants instead of the MODE_* constants).

Common situations: Passing Java NIO/POSIX constants instead of FileSystemManager.MODE_* constants; accidentally clearing access bits when combining flags; passing mode 0.

Understand the failure class

Background: "unknown output mode", "invalid value for flag", "expects true/false": fixing invalid flag value errors in CLI tools — this error's family across 24 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/a36b804c13b236e1. Report an issue: GitHub.

Appendix: source

Thrown at libcore/io/src/main/java/io/github/muntashirakon/io/FileUtils.java:66

    static class Flag {
        boolean read;
        boolean write;
        boolean create;
        boolean truncate;
        boolean append;
    }

    static int modeToPosix(int mode) {
        int res;
        if ((mode & MODE_READ_WRITE) == MODE_READ_WRITE) {
            res = O_RDWR;
        } else if ((mode & MODE_WRITE_ONLY) == MODE_WRITE_ONLY) {
            res = O_WRONLY;
        } else if ((mode & MODE_READ_ONLY) == MODE_READ_ONLY) {
            res = O_RDONLY;
        } else {
            throw new IllegalArgumentException("Bad mode: " + mode);
        }
        if ((mode & MODE_CREATE) == MODE_CREATE) {
            res |= O_CREAT;
        }
        if ((mode & MODE_TRUNCATE) == MODE_TRUNCATE) {
            res |= O_TRUNC;
        }
        if ((mode & MODE_APPEND) == MODE_APPEND) {
            res |= O_APPEND;
        }
        return res;
    }

    @RequiresApi(api = 26)
    static Set<OpenOption> modeToOptions(int mode) {
        Set<OpenOption> set = new ArraySet<>();
        if ((mode & MODE_READ_WRITE) == MODE_READ_WRITE) {
            set.add(StandardOpenOption.READ);

View on GitHub (pinned to 0152f468fc)