Skip to content

Use short path for LIMA_HOME on windows #955

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/store/dirnames/dirnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/lima-vm/lima/pkg/store/filenames"
)
Expand All @@ -17,18 +18,33 @@ const DotLima = ".lima"
// NOTE: We do not use `~/Library/Application Support/Lima` on macOS.
// We use `~/.lima` so that we can have enough space for the length of the socket path,
// which can be only 104 characters on macOS.
//
// NOTE: There are some issues when using "long names" on Windows.
// We use "short names" here, so that it works with user names containing unicode etc.
// They normally have 8+3 characters, with suffix.
func LimaDir() (string, error) {
dir := os.Getenv("LIMA_HOME")
if dir == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
// on windows, 8.3 paths are needed by some tools like QEMU
if runtime.GOOS == "windows" {
homeDir, err = ShortPathName(homeDir)
if err != nil {
return "", err
}
}
dir = filepath.Join(homeDir, DotLima)
}
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
return dir, nil
}
// on windows, EvalSymlinks translates short paths back to long again
if runtime.GOOS == "windows" {
return dir, nil
}
realdir, err := filepath.EvalSymlinks(dir)
if err != nil {
return "", fmt.Errorf("cannot evaluate symlinks in %q: %w", dir, err)
Expand Down
9 changes: 9 additions & 0 deletions pkg/store/dirnames/shortname_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !windows
// +build !windows

package dirnames

// ShortPathName just returns the provided path.
func ShortPathName(path string) (string, error) {
return path, nil
}
37 changes: 37 additions & 0 deletions pkg/store/dirnames/shortname_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dirnames

import (
"os"
"path/filepath"
"testing"

"gotest.tools/v3/assert"
)

// Note: can't use t.TempDir(), because it is _always_ long... <sigh>
// instead use os.TempDir(), something like `C:\users\anders\Temp`

func TestShortPathNameShort(t *testing.T) {
d := os.TempDir()
l := filepath.Join(d, "foo")
err := os.Mkdir(l, 0755)
assert.NilError(t, err)
s, err := ShortPathName(l)
assert.NilError(t, err)
t.Logf("%s => %s", l, s)
os.RemoveAll(l)
}

func TestShortPathNameLong(t *testing.T) {
d := os.TempDir()
l := filepath.Join(d, "baaaaaaaaaar")
err := os.Mkdir(l, 0755)
assert.NilError(t, err)
s, err := ShortPathName(l)
assert.NilError(t, err)
t.Logf("%s => %s", l, s)
fi, err := os.Stat(s)
assert.NilError(t, err)
assert.Assert(t, fi.Mode().IsDir())
os.RemoveAll(l)
}
23 changes: 23 additions & 0 deletions pkg/store/dirnames/shortname_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dirnames

import (
"syscall"
)

// ShortPathName return the short path name, when given a long path.
func ShortPathName(path string) (string, error) {
p := syscall.StringToUTF16(path)
b := p // GetShortPathName says we can reuse buffer
n, err := syscall.GetShortPathName(&p[0], &b[0], uint32(len(b)))
if err != nil {
return "", err
}
if n > uint32(len(b)) {
b = make([]uint16, n)
n, err = syscall.GetShortPathName(&p[0], &b[0], uint32(len(b)))
if err != nil {
return "", err
}
}
return syscall.UTF16ToString(b), nil
}