{"record":{"id":"076505e90eb3888b","repo":"apache/hadoop","slug":"enotsup","errorCode":"ENOTSUP","errorMessage":"ERROR: cannot open an hdfs file in O_RDWR mode\n","messagePattern":"ERROR: cannot open an hdfs file in O_RDWR mode\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/hdfs.c","lineNumber":1138,"sourceCode":"    jint jBufferSize = bufferSize;\n    jshort jReplication = replication;\n\n    /* The hadoop java api/signature */\n    const char *method = NULL;\n    const char *signature = NULL;\n\n    /* Get the JNIEnv* corresponding to current thread */\n    JNIEnv* env = getJNIEnv();\n    if (env == NULL) {\n      errno = EINTERNAL;\n      return NULL;\n    }\n\n\n    if (accmode == O_RDONLY || accmode == O_WRONLY) {\n\t/* yay */\n    } else if (accmode == O_RDWR) {\n      fprintf(stderr, \"ERROR: cannot open an hdfs file in O_RDWR mode\\n\");\n      errno = ENOTSUP;\n      return NULL;\n    } else {\n      fprintf(stderr, \"ERROR: cannot open an hdfs file in mode 0x%x\\n\",\n              accmode);\n      errno = EINVAL;\n      return NULL;\n    }\n\n    if ((flags & O_CREAT) && (flags & O_EXCL)) {\n      fprintf(stderr,\n              \"WARN: hdfs does not truly support O_CREATE && O_EXCL\\n\");\n    }\n\n    if (accmode == O_RDONLY) {\n        method = \"open\";\n        signature = JMETHOD2(JPARAM(HADOOP_PATH), \"I\", JPARAM(HADOOP_FSDISTRM));\n    } else if (flags & O_APPEND) {","sourceCodeStart":1120,"sourceCodeEnd":1156,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/hdfs.c#L1120-L1156","documentation":"hdfsOpenFile rejects accmode O_RDWR with ENOTSUP before any JNI call: HDFS files are write-once - a stream is either an FSDataInputStream (O_RDONLY) or an FSDataOutputStream (O_WRONLY/O_APPEND) - so read-write random-access opens are impossible in the storage model, not merely unimplemented by the client.","triggerScenarios":"hdfsOpenFile(fs, path, O_RDWR, ...), including combined flags like O_RDWR|O_CREAT; POSIX code ported verbatim that uses open(path, O_RDWR); code that opens a file for update-in-place (seek + write + read on one handle).","commonSituations":"Porting file-oriented C programs (config writers, state files) or POSIX-style libraries to libhdfs; database-style workloads (SQLite, dbm) that fundamentally need r/w - these cannot run on HDFS files at all.","solutions":["Split into phases: write the file with O_WRONLY (optionally O_CREAT/O_APPEND), hdfsCloseFile, then reopen with O_RDONLY to read back.","For update-in-place semantics, rewrite to write-a-new-file + atomic rename (hdfsRename), or use HBase/Kudu instead of raw HDFS files.","Audit ported code for O_RDWR early - it will never work, so fail fast with your own error before calling libhdfs."],"exampleFix":"// before - ENOTSUP: HDFS files are either input or output, never both\nhdfsFile f = hdfsOpenFile(fs, \"/x\", O_RDWR | O_CREAT, 0, 0, 0);\n\n// after - write, close, reopen read-only\nhdfsFile out = hdfsOpenFile(fs, \"/x\", O_WRONLY | O_CREAT, 0, 0, 0);\nhdfsWrite(fs, out, buf, n);\nhdfsCloseFile(fs, out);\nhdfsFile in = hdfsOpenFile(fs, \"/x\", O_RDONLY, 0, 0, 0);","handlingStrategy":"validation","validationCode":"int flags = O_WRONLY | O_CREAT;      /* or O_RDONLY - never O_RDWR */\nif ((flags & O_ACCMODE) == O_RDWR) {\n    fail_fast(\"HDFS files cannot be opened read-write\");\n}\nhdfsFile f = hdfsOpenFile(fs, path, flags, 0, 0, 0);","typeGuard":"static int hdfs_open_mode_supported(int flags) {\n    int m = flags & O_ACCMODE;\n    return m == O_RDONLY || m == O_WRONLY;   /* O_RDWR is ENOTSUP in libhdfs */\n}","tryCatchPattern":"hdfsFile f = hdfsOpenFile(fs, path, flags, 0, 0, 0);\nif (!f && errno == ENOTSUP && (flags & O_ACCMODE) == O_RDWR) {\n    /* structural limitation: switch to write-then-reopen, do not retry */\n    f = open_write_then_read_phases(fs, path);\n}","preventionTips":["Fail fast on O_RDWR in your own wrappers - retrying can never succeed.","Design for write-once semantics: write file, close, reopen read-only; use write-temp + hdfsRename for atomic replace.","Do not port POSIX open() flag words blindly; audit for O_RDWR during HDFS migrations.","For true random-access read/write data, choose HBase/Kudu rather than HDFS files."],"tags":["c","libhdfs","hdfs","file-open","enotsup","posix-portability"],"backgroundTag":"unsupported-open-mode","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}