diff --git a/paths.go b/paths.go index a734d17..c19b1f7 100644 --- a/paths.go +++ b/paths.go @@ -561,5 +561,8 @@ func (p *Path) Canonical() *Path { if absPath, err := canonical.Abs(); err == nil { canonical = absPath } + if c, err := canonicalize(canonical.path); c != "" && err == nil { + canonical.path = c + } return canonical } diff --git a/paths_others.go b/paths_others.go new file mode 100644 index 0000000..953489f --- /dev/null +++ b/paths_others.go @@ -0,0 +1,37 @@ +// +// This file is part of PathsHelper library. +// +// Copyright 2025 Arduino AG (http://www.arduino.cc/) +// +// PathsHelper library is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +// +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. +// + +//go:build !windows + +package paths + +func canonicalize(string) (string, error) { + // On non-Windows platforms, the path is already in a canonical form. + return "", nil +} diff --git a/paths_windows.go b/paths_windows.go new file mode 100644 index 0000000..93618ce --- /dev/null +++ b/paths_windows.go @@ -0,0 +1,51 @@ +// +// This file is part of PathsHelper library. +// +// Copyright 2025 Arduino AG (http://www.arduino.cc/) +// +// PathsHelper library is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +// +// As a special exception, you may use this file as part of a free software +// library without restriction. Specifically, if other files instantiate +// templates or use macros or inline functions from this file, or you compile +// this file and link it with other files to produce an executable, this +// file does not by itself cause the resulting executable to be covered by +// the GNU General Public License. This exception does not however +// invalidate any other reasons why the executable file might be covered by +// the GNU General Public License. +// + +package paths + +import ( + "fmt" + + "golang.org/x/sys/windows" +) + +func canonicalize(inPath string) (string, error) { + var buf [4096]uint16 + data := &buf[0] + n, err := windows.GetLongPathName(windows.StringToUTF16Ptr(inPath), &buf[0], uint32(len(buf))) + if n >= uint32(len(buf)) { + buf2 := make([]uint16, n+1) + data = &buf2[0] + _, err = windows.GetLongPathName(windows.StringToUTF16Ptr(inPath), &buf2[0], uint32(len(buf2))) + } + if err != nil { + return "", fmt.Errorf("getting long path name: %w", err) + } + return windows.UTF16PtrToString(data), err +}