{"record":{"id":"af29b55152c70569","repo":"MuntashirAkon/AppManager","slug":"path-cannot-be-opened-for-both-reading-and-writing","errorCode":null,"errorMessage":"+ path + cannot be opened for both reading and writing.","messagePattern":"\\+ path \\+ cannot be opened for both reading and writing\\.","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/io/fs/VirtualFileSystem.java","lineNumber":1007,"sourceCode":"    }\n\n    @NonNull\n    public FileChannel openChannel(String path, int mode) throws IOException {\n        boolean read = false;\n        boolean write = false;\n        if ((mode & MODE_READ_WRITE) != 0) {\n            read = true;\n            write = true;\n        } else if ((mode & MODE_READ_ONLY) != 0) {\n            read = true;\n        } else if ((mode & MODE_WRITE_ONLY) != 0) {\n            write = true;\n        } else {\n            throw new IllegalArgumentException(\"Bad mode: \" + mode);\n        }\n\n        if (read && write && !checkAccess(path, OsConstants.R_OK | OsConstants.W_OK)) {\n            throw new IOException(path + \" cannot be opened for both reading and writing.\");\n        } else if (read && !checkAccess(path, OsConstants.R_OK)) {\n            throw new IOException(path + \" cannot be opened for both reading.\");\n        } else if (write && !checkAccess(path, OsConstants.W_OK)) {\n            throw new IOException(path + \" cannot be opened for both writing.\");\n        }\n        Node<?> targetNode = getNode(path);\n        if (targetNode == null) {\n            throw new FileNotFoundException(path + \" does not exist.\");\n        }\n        if (!targetNode.isFile()) {\n            throw new IOException(path + \" is not a file.\");\n        }\n        return FileSystemManager.getLocal().openChannel(getCachedFile(targetNode, write), mode);\n    }\n\n    @NonNull\n    public ParcelFileDescriptor openFileDescriptor(String path, int mode) throws IOException {\n        boolean read = false;","sourceCodeStart":989,"sourceCodeEnd":1025,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/io/fs/VirtualFileSystem.java#L989-L1025","documentation":"VirtualFileSystem.openChannel(path, mode) throws this IOException when mode requires both read and write but checkAccess(path, R_OK|W_OK) fails — the VFS reports the path cannot be opened for read+write. (The message text is misleading if only write was intended; it is thrown strictly in the read&&write branch.)","triggerScenarios":"Calling openChannel with MODE_READ_WRITE on a read-only mount or a file the caller lacks write permission for; opening a file on a provider that grants read but not write; a random-access edit on a file in a read-only location.","commonSituations":"In-place editing of files on a read-only mount (system partition, SAF read-only grant); missing storage permissions; file owned by another app/UID.","solutions":["Open with MODE_READ_ONLY if writing is not actually required","Remount/choose a writable provider or copy the file to a writable location before read-write access","Verify host-OS storage permissions are granted","Check writability via checkAccess(path, R_OK|W_OK) before opening"],"exampleFix":"// before\nFileChannel ch = vfs.openChannel(path, MODE_READ_WRITE); // read-only mount\n// after\nif (vfs.checkAccess(path, OsConstants.R_OK | OsConstants.W_OK)) {\n    FileChannel ch = vfs.openChannel(path, MODE_READ_WRITE);\n} else {\n    FileChannel ch = vfs.openChannel(path, MODE_READ_ONLY);\n}","handlingStrategy":"try-catch","validationCode":"boolean canReadWrite = vfs.checkAccess(path, OsConstants.R_OK | OsConstants.W_OK);","typeGuard":null,"tryCatchPattern":"try {\n    FileChannel ch = vfs.openChannel(path, MODE_READ_WRITE);\n} catch (IOException e) {\n    // degrade to MODE_READ_ONLY or copy to a writable location\n}","preventionTips":["Pre-flight R_OK|W_OK before MODE_READ_WRITE opens","Only request read-write when in-place writes are truly needed","Confirm the mount/provider supports writes"],"tags":["filesystem","virtual-fs","access-mode"],"backgroundTag":"permission-denied","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"}