Skip to content

[gap] trailing '/' on a regular file is silently stripped: readers accept it, tee/dd/cp/mv write through it (data loss), some errors report ENOENT or exit 0 #1052

Closed

Description

@caomengxuan666

Summary

A trailing / (or //, /.) on a pathname must force directory resolution: on a regular file GNU returns ENOTDIR; a nonexistent name/ is also ENOTDIR, not a createable file. WinuxCmd normalizes the separator away and proceeds, producing three symptom classes.

Test file: f2.txt = "line1\nline2\n" (12 bytes).

Class 1 — silently READS the file (GNU: Not a directory, rc=1/2)

winuxcmd.exe wc -l f2.txt/      ->  2 f2.txt/          rc=0   (GNU: wc: f2.txt/: Not a directory, rc=1)
winuxcmd.exe sort f2.txt/       ->  prints contents    rc=0   (GNU: Not a directory, rc=2)
winuxcmd.exe head/tail f2.txt/  ->  prints contents    rc=0
winuxcmd.exe grep x f2.txt/     ->  matches            rc=0
winuxcmd.exe od / nl / fold / cut / rev / fmt / pr f2.txt/   -> all read it, rc=0
winuxcmd.exe md5sum/sha1sum/sha256sum/sha512sum/b2sum/cksum/sum f2.txt/  -> hashes printed, rc=0
   (note: sha1sum/sha512sum print the EMPTY-input hash da39a3ee../cf83e135.., md5sum/sha256sum print the real content hash)
winuxcmd.exe cmp f2.txt f2.txt/ ->  rc=0 (identical!)  (GNU: cmp: f2.txt/: Not a directory, rc=2)
winuxcmd.exe comm/join/paste f2.txt/  -> read it, rc=0
winuxcmd.exe split -l1 f2.txt/  ->  creates xaa/xab    rc=0   (GNU: cannot open ... Not a directory)
winuxcmd.exe df f2.txt/         ->  prints D:\ volume info, rc=0   (GNU: df: f2.txt/: Not a directory, rc=1)
winuxcmd.exe realpath f2.txt/   ->  prints D:\...\f2.txt\, rc=0    (GNU: realpath: f2.txt/: Not a directory, rc=1)

Class 2 — silently WRITES / creates files (GNU errors; potential data loss)

echo OVERWRITING | winuxcmd.exe tee f2.txt/     -> rc=0, f2.txt TRUNCATED to "OVERWRITING\n"
echo zz | winuxcmd.exe dd of=f2.txt/ status=none -> rc=0, f2.txt rewritten to "zz\n"
winuxcmd.exe cp f2.txt nodir/                   -> rc=0, creates REGULAR FILE 'nodir' (GNU: cp: failed to access 'nodir/': Not a directory, rc=1)
winuxcmd.exe mv f2.txt nodir/                   -> rc=0, renames f2.txt -> 'nodir' (GNU: ENOTDIR, rc=1)
winuxcmd.exe cp f2.txt f2.txt/                  -> rc=0, copies file onto itself (GNU: ENOTDIR, rc=1)

Class 3 — errors, but wrong errno (ENOENT where GNU reports ENOTDIR)

winuxcmd.exe cp f2.txt/ x    -> "cannot stat 'f2.txt/': No such file or directory"  (GNU: Not a directory)
winuxcmd.exe mv / stat / du / diff / strings / expand / unexpand / chown / sync f2.txt/
                             -> ENOENT wording where GNU reports ENOTDIR
winuxcmd.exe ln f2.txt f2.txt/  -> "File exists"  (GNU: Not a directory)
winuxcmd.exe shred -n0 f2.txt/  -> "shred: cannot open 'f2.txt/'" rc=1  (GNU adds ': Not a directory')
winuxcmd.exe install f2.txt f2.txt/ -> "cannot copy" rc=1  (GNU: ENOTDIR wording)

Commands already correct: rm, rmdir, touch, truncate, cat (ENOTDIR), base64, tac, uniq, shuf, mkdir (EEXIST matches).

Versions

  • WinuxCmd 1.0.6 (build-vs, WINUX_LANG=en)
  • GNU coreutils 8.32 (D:/Git/usr/bin)

Activity

  1. added
    bug
    compat-gap
    on Sep 14, 2026
  2. caomengxuan666 commented on Sep 14, 2026

    @caomengxuan666
    MemberAuthor

    Follow-up: backslash separators behave identically — wc -c f2.txt\ reads the file (rc=0), cp f2.txt nodirz\ creates a regular file nodirz (rc=0). GNU treats trailing \ the same as /: wc: 'f2.txt\': Not a directory, cp: cannot create regular file 'nodirz\': Is a directory (note GNU reports EISDIR here, not ENOTDIR).

  3. caomengxuan666 commented on Sep 14, 2026

    @caomengxuan666
    MemberAuthor

    Fixed in 182b1de.

    Root cause: path normalization silently stripped trailing separators, so tee file/, cp src file/ etc. opened the regular file and could truncate/overwrite it.

    Fix: normalize_api_operand_w() keeps the separator when the stripped target is not a directory, so the OS rejects the operand. Shared helpers operand_target_attributes_w()/operand_file_open_error() classify the real target: ENOTDIR for read opens, EISDIR for write/create opens (matches the Linux O_CREAT quirk — GNU tee f/ reports "Is a directory"), ENOENT for missing targets. file_io helpers now return failbit-set streams; tee/cp surface the real error.

    Verified: file content preserved, mkdir dir/ and cp src dir/ still work, 26 trailing-separator tests + 216 tests across touched commands pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompat-gap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions