{"record":{"id":"4fc4d9555546c24f","repo":"MuntashirAkon/AppManager","slug":"append-truncate-not-allowed","errorCode":null,"errorMessage":"APPEND + TRUNCATE not allowed","messagePattern":"APPEND \\+ TRUNCATE not allowed","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"libcore/io/src/main/java/io/github/muntashirakon/io/FileUtils.java","lineNumber":132,"sourceCode":"        } else {\n            throw new IllegalArgumentException(\"Bad mode: \" + mode);\n        }\n        if ((mode & MODE_CREATE) == MODE_CREATE) {\n            f.create = true;\n        }\n        if ((mode & MODE_TRUNCATE) == MODE_TRUNCATE) {\n            f.truncate = true;\n        }\n        if ((mode & MODE_APPEND) == MODE_APPEND) {\n            f.append = true;\n        }\n\n        // Validate flags\n        if (f.append && f.read) {\n            throw new IllegalArgumentException(\"READ + APPEND not allowed\");\n        }\n        if (f.append && f.truncate) {\n            throw new IllegalArgumentException(\"APPEND + TRUNCATE not allowed\");\n        }\n\n        return f;\n    }\n\n    @RequiresApi(api = 28)\n    static long splice(\n            FileDescriptor fdIn, Int64Ref offIn,\n            FileDescriptor fdOut, Int64Ref offOut,\n            long len, int flags) throws ErrnoException {\n        try {\n            if (splice == null) {\n                splice = Os.class.getMethod(\"splice\",\n                        FileDescriptor.class, Int64Ref.class,\n                        FileDescriptor.class, Int64Ref.class,\n                        long.class, int.class);\n            }\n            return (long) splice.invoke(null, fdIn, offIn, fdOut, offOut, len, flags);","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/libcore/io/src/main/java/io/github/muntashirakon/io/FileUtils.java#L114-L150","documentation":"This library's FileUtils.modeToFlag() converts a POSIX-style mode string (e.g. \"rw\", \"rwt\") into a Flag object and validates flag combinations. APPEND combined with TRUNCATE is rejected because truncating the file on open contradicts appending to its end — the kernel's O_APPEND|O_TRUNC combination would erase data the caller apparently intended to keep. The library fails fast with IllegalArgumentException rather than producing surprising file contents.","triggerScenarios":"Calling FileUtils.modeToFlag() (directly or via NIOFactory.openChannel / FileSystemManager openers) with a mode string that parses to both append and truncate, e.g. \"ra\", \"wa\", or a mode containing both 'a' and 't' semantics.","commonSituations":"Constructing mode strings programmatically by concatenating flags; porting code that used \"wa\" assuming truncate-then-append; users specifying custom open modes in remote file-system calls.","solutions":["Remove the truncate flag from the mode string (use \"a\" alone to append, or \"wt\"/\"w\" alone to truncate then write)","If truncate-then-append is truly wanted, open with \"wt\", write, then reopen with \"a\"","Validate mode strings before passing them, rejecting any mode that implies both append and truncate"],"exampleFix":"// before\nFileUtils.modeToFlag(\"wat\"); // IllegalArgumentException: APPEND + TRUNCATE not allowed\n// after\nFileUtils.modeToFlag(\"wa\"); // append-only, or \"wt\" to truncate then write","handlingStrategy":"validation","validationCode":"FileUtils.Flag f = FileUtils.modeToFlag(mode); // or pre-check the mode string\nif (mode.contains(\"a\") && mode.contains(\"t\")) throw new IllegalArgumentException(\"mode must not combine append and truncate\");","typeGuard":"boolean isConflictingMode(String mode) { return mode != null && mode.contains(\"a\") && mode.contains(\"t\"); }","tryCatchPattern":"try { FileUtils.modeToFlag(mode); } catch (IllegalArgumentException e) { /* fix mode string or prompt user */ }","preventionTips":["Never combine 'a' with 't' in mode strings","Centralize mode-string construction in one helper that rejects conflicting flags","Unit-test mode parsing for all supported flag combinations"],"tags":["filesystem","io","illegal-argument","open-flags"],"backgroundTag":"conflicting-config-options","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}