Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upos: Windows: Stat fails on special files like 'con' #34900
Comments
This comment has been minimized.
This comment has been minimized.
Not quite sure what to do here if |
This comment has been minimized.
This comment has been minimized.
#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, |
This comment has been minimized.
This comment has been minimized.
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 { |
This comment has been minimized.
This comment has been minimized.
Change https://golang.org/cl/201157 mentions this issue: |
This comment has been minimized.
This comment has been minimized.
Thanks, you guys are really quick~
|
This comment has been minimized.
This comment has been minimized.
@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)
}
} |
This comment has been minimized.
This comment has been minimized.
append: |
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.