Encountered std.posix.UnexpectedError reading certain symlinks on Windows #30164
Labels
No labels
abi/f32
abi/ilp32
abi/n32
abi/sf
abi/x32
accepted
arch/1750a
arch/21k
arch/6502
arch/a29k
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/avr32
arch/bfin
arch/bpf
arch/clipper
arch/colossus
arch/cr16
arch/cris
arch/csky
arch/dlx
arch/dsp16xx
arch/elxsi
arch/epiphany
arch/fr30
arch/frv
arch/h8300
arch/h8500
arch/hexagon
arch/hppa
arch/hppa64
arch/i370
arch/i860
arch/i960
arch/ia64
arch/ip2k
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/maxq
arch/mcore
arch/metag
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/mn10200
arch/mn10300
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/nios2
arch/ns32k
arch/nvptx
arch/or1k
arch/pdp10
arch/pdp11
arch/pj
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390
arch/s390x
arch/sh
arch/sh64
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/st200
arch/starcore
arch/tilegx
arch/tilepro
arch/tricore
arch/ts
arch/ubicom8
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/we32k
arch/x86
arch/x86_64
arch/xcore
arch/xgate
arch/xstormy16
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/aix
os/android
os/bridgeos
os/contiki
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hermit
os/hurd
os/illumos
os/ios
os/kfreebsd
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/solaris
os/tvos
os/uefi
os/visionos
os/wali
os/wasi
os/watchos
os/windows
os/zos
proposal
release notes
testing
tier system
zig cc
zig fmt
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ziglang/zig#30164
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Zig Version
0.16.0-dev.1484+d0ba6642b
Steps to Reproduce and Observed Output
I'm not sure if this fits the "improve error message" template or if it's a bug report.
While iterating a directory on Windows, I've noticed that certain symlinks of directories don't show up as symlinks but as directories itself when reading
entry.kind.An example:
Turns out zig does not always behave this way. In fact, symlinks created by zig itself seem to be reported as symlinks correctly. Symlinks created by Powershell seem to be "different" somehow. 😃
So what's the best way to check for such symlinks? I thought maybe I should just read the symlink and eat the error
NotLink.Iterating a directory in such a way produces errors when encountering a (real) directory which is not a symlink, as expected. In theory, one can just ignore the error and move on.
Instead of throwing
NotLinktrying toreadLinkone of these directories producesstd.posix.UnexpectedErrorand as the stdlib documentation says "When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function." - I'm here to report. 😃I've included a reproduction zip with this issue. It contains a zig test and a PowerShell script. The zig tests executed the script which temporarily creates a directory and a symlink, which points to that directory. Please try to run it on WIndows using
zig test .\symlinks.zigIt should yield something similar to:
Expected Output
In the reproduction output above, I would expect
error reading link: error.Unexpectedto sayerror reading link: error.NotLink.Encountered std.posix.UnexpectedError reading certain symlinks on Windowsto Encounteredstd.posix.UnexpectedErrorreading certain symlinks on WindowsFor context,
NTSTATUS=0xc0000275isNOT_A_REPARSE_POINTFor the
entry.kindproblem:.directorybecause attrs.DIRECTORY is taking precedence over attrs.REPARSE_POINT. This seems like a mistake and is different to howstatreportskind.Dir.symLinktakes aflagsparameter with anis_directoryfield that needs to be set totrueto create a directory symlink on Windows. If you set that flag, the test behaves the same as the PowerShell-created symlink test:Great feedback, thanks!
Oh, what an oversight on my part. Thanks for pointing that out.
Also interesting that
statreports symlinks when it follows them. I'm learning a lot here. 😃If I may ask another question..
Is this part of a test reading a directory symlink as a file then, considering
O_DIRECTORYis not set?.linux => linux_symlink: {const sub_path_c = try posix.toPosixPath("symlink");// the O_NOFOLLOW | O_PATH combination can obtain a fd to a symlink// note that if O_DIRECTORY is set, then this will error with ENOTDIRconst flags: posix.O = .{.NOFOLLOW = true,.PATH = true,.ACCMODE = .RDONLY,.CLOEXEC = true,};const fd = try posix.openatZ(ctx.dir.fd, &sub_path_c, flags, 0);break :linux_symlink Dir{ .fd = fd };},That's not what I meant, are you seeing that behavior locally?
I was referring to the fact that
File.statcan return.sym_linkif theFile.handleitself pertains to a symlink, which is something you have to pretty intentionally do and isn't possible on all systems. The relevant test is here:test "File.stat on a File that is a symlink returns Kind.sym_link" {// This test requires getting a file descriptor of a symlink which// is not possible on all targetsswitch (builtin.target.os.tag) {.windows, .linux => {},else => return error.SkipZigTest,Well, I wrote that and only then did a deeper dive into zig's fs tests.
No, I don't. Sorry if that caused some confusion.
I guess I'm seeing how to do that in the above comment which I edited before I saw your answer. 😃
Fixed by #30186 and #30232