Skip to content

path/filepath: can not handle files on windows that end with a . #69801

Not planned
@sweharris

Description

@sweharris

Go version

go version go1.23.2 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\sweh\AppData\Local\go-build
set GOENV=C:\Users\sweh\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\sweh\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\sweh\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.2
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\sweh\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\cygwin64\tmp\go-build1197283358=/tmp/go-build -gno-record-gcc-switches

What did you do?

Windows typically doesn't allow for files or directories ending in a "." and attempts to do so will result in the final "." being removed. But not all subsystems obey this, so we can use cygwin

mkdir TEST TEST/a. TEST/b.
touch TEST/b./c

If we try to do a "walk" on this then errors are passed to the walk function, the info field isn't populated, and the tree isn't fully recursed.

import (
        "fmt"
        "os"
        "path/filepath"
)

func find_walk(path string, info os.FileInfo, err error) error {
        fmt.Printf("Looking at %s\n", path)
        if err != nil {
                fmt.Printf("Error: %s\n", err)
                // return err
        }

        // fmt.Printf("We found %s of size %d\n", path,info.Size())
        return nil
}

func main() {
        filepath.Walk("TEST", find_walk)
}

I suspect this is os.Lstat() failing on Windows by calling the Win32 handler which can't support these files, even though the files can be created!

What did you see happen?

Looking at TEST
Looking at TEST\a.
Error: CreateFile TEST\a.: The system cannot find the file specified.
Looking at TEST\b.
Error: CreateFile TEST\b.: The system cannot find the file specified.

What did you expect to see?

The directory "b." should be recursed into but it's not because the Lstat() is failing and so the function doesn't know it's a directory.

I'm not sure this is easily fixable!

Activity

changed the title [-]path/filepath: can not handle files on windows that end , a .[/-] [+]path/filepath: can not handle files on windows that end with a .[/+] on Oct 7, 2024
ianlancetaylor

ianlancetaylor commented on Oct 7, 2024

@ianlancetaylor
Contributor

CC @golang/windows

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Oct 7, 2024
added this to the Backlog milestone on Oct 7, 2024
qmuntal

qmuntal commented on Oct 8, 2024

@qmuntal
Member

Related to #54040.

qmuntal

qmuntal commented on Oct 8, 2024

@qmuntal
Member

Windows file naming rules says that files and directories should not contain trailing spaces or periods:

Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".

In #54040 we agreed to not special case trailing spaces and periods, as it is not possible to (easily) fully support them using Win32 API calls. If someone really needs to operate with these non-compliant file paths, then Windows already provide an escape hatch: prepend the path with \\?\ so the path is not validated nor mangled in any way.

@sweharris, in your example you can change the main function to do the following:

func main() {
	abs, err := filepath.Abs("TEST")
        if err != nil {
             panic(err)
        }
	filepath.Walk(`\\?\`+abs, find_walk)
}
removed
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ianlancetaylor@qmuntal@sweharris@cherrymui@gabyhelp

        Issue actions

          path/filepath: can not handle files on windows that end with a . · Issue #69801 · golang/go