-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os: Windows: Stat fails on special files like 'con' #34900
Comments
Not quite sure what to do here if |
#include <stdio.h>
#include <sys/stat.h>
int
main(int argc, char* argv[]) {
FILE* fp = fopen("CON", "r");
if (fp == NULL) {
perror("fopen");
return 1;
}
fclose(fp);
struct stat st;
int r = stat("CON", &st);
printf("result of stat: %d\n", r);
printf("st_dev = %d\n", st.st_dev);
printf("st_ino = %d\n", st.st_ino);
printf("st_mode = %x\n", st.st_mode);
printf("st_nlink = %d\n", st.st_nlink);
printf("st_uid = %d\n", st.st_uid);
printf("st_gid = %d\n", st.st_gid);
printf("st_rdev = %d\n", st.st_rdev);
printf("st_size = %lu\n", st.st_size);
return 0;
}
In this C program, |
Hopefully this will work. diff --git a/src/os/stat_windows.go b/src/os/stat_windows.go
index 3e0e0a59ed..a6830c0f6e 100644
--- a/src/os/stat_windows.go
+++ b/src/os/stat_windows.go
@@ -74,6 +74,18 @@ func stat(funcname, name string, createFileAttrs uint32) (FileInfo, error) {
}
return fs, nil
}
+ if err == syscall.ERROR_FILE_NOT_FOUND {
+ if attr, err := syscall.GetFileAttributes(namep); err == nil && attr == syscall.FILE_ATTRIBUTE_ARCHIVE {
+ return &fileStat{
+ name: name,
+ // hopefully this will work for CON, PRN, AUX, CONOUT$ or etc.
+ vol: 0,
+ idxhi: 0,
+ idxlo: 0,
+ }, nil
+ }
+ }
+
// GetFileAttributesEx fails with ERROR_SHARING_VIOLATION error for
// files, like c:\pagefile.sys. Use FindFirstFile for such files.
if err == windows.ERROR_SHARING_VIOLATION { |
Change https://golang.org/cl/201157 mentions this issue: |
Thanks, you guys are really quick~
|
@mattn Lines 24 to 26 in 902d5aa
Lines 24 to 26 in 902d5aa
and maybe isWindowsNulName can be cleaned:Lines 565 to 581 in a38a917
WinAPI CreateDirectory should have covered these reserved names:go/src/syscall/syscall_windows.go Lines 443 to 449 in a38a917
confiremed by package main
import (
"os"
)
func main() {
err := os.Mkdir("con", os.ModePerm)
if err != nil {
panic(err)
}
} |
append: |
Change https://golang.org/cl/373355 mentions this issue: |
Change https://go.dev/cl/560755 mentions this issue: |
\\.\con and CON need to be opened with GENERIC_READ access, else CreateFile will fail with ERROR_INVALID_PARAMETER. Special-case ERROR_INVALID_PARAMETER in os.[L]Stat so it retries with GENERIC_READ access. Fixes golang#34900. Change-Id: I5010e736d0189c8ada4fc0eca98d71a438c41426 Reviewed-on: https://go-review.googlesource.com/c/go/+/560755 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: David Chase <[email protected]>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
try
test_Stat-con.go
to reproduce the issueI have debugged and located that
GetFileAttributesEx
fails on special files like 'con':go/src/os/stat_windows.go
Lines 61 to 76 in 902d5aa
and only 'nul':
go/src/os/stat_windows.go
Lines 24 to 26 in 902d5aa
go/src/os/stat_windows.go
Lines 24 to 26 in 902d5aa
reserved names
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9
:https://docs.microsoft.com/zh-cn/windows/win32/fileio/naming-a-file#naming-conventions
CONIN$
andCONOUT$
:https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles
ref:
https://github.com/gcc-mirror/gcc/blob/bbcdad768752f5cab6727242515bcd15706acbea/gcc/ada/adaint.c#L1606-L1618
What did you expect to see?
We can access these special Windows files, especially
con
for printing to stdout.What did you see instead?
I found sometimes we need to use stdout as a file:
https://github.com/DNSCrypt/dnscrypt-proxy/blob/858957ce91015057a1f6c5a9a24f3ea2beb48537/dnscrypt-proxy/example-dnscrypt-proxy.toml#L339-L341
But
con
doesn't work.By doing some research, I located the code here as described.
GetFileAttributesEx
andCreateFile
ofStat
failed on the reserved file namecon
, and only 'nul' is considered as the special case.The text was updated successfully, but these errors were encountered: