mirror of
https://github.com/containerd/containerd.git
synced 2026-11-06 05:17:02 +00:00
Creating symlinks on Windows requires administrator privileges or Developer Mode, so NewBundle fails with "A required privilege is not held by the client" when containerd creates a task without them. Replace the direct os.Symlink and os.Readlink calls with platform specific helpers. On non-Windows platforms they keep the existing symlink behaviour. On Windows, createWorkLink creates a directory junction using go-winio's reparse point encoding, which does not need elevated privileges. os.Readlink resolves both symlinks and junctions, so resolveWorkLink uses it on both platforms. createWorkLink closes the directory handle before removing the half-created junction directory on failure. The handle is opened without sharing, so removing the directory while it is still open would fail. Signed-off-by: Craig Gumbley <craiggumbley@gmail.com> Signed-off-by: Eric Curtin <eric.curtin@docker.com> Assisted-by: OpenCode (anthropic/claude-sonnet-5-5)
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
/*
|
|
Copyright The containerd Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package v2
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
winio "github.com/Microsoft/go-winio"
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// createWorkLink creates a directory junction from bundlePath/work -> work.
|
|
// Junctions do not require administrator privileges or Developer Mode,
|
|
// unlike symlinks on Windows.
|
|
func createWorkLink(work, bundlePath string) error {
|
|
target, err := filepath.Abs(work)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
link := filepath.Join(bundlePath, "work")
|
|
if err := os.Mkdir(link, 0711); err != nil {
|
|
return err
|
|
}
|
|
|
|
linkPtr, err := windows.UTF16PtrFromString(link)
|
|
if err != nil {
|
|
os.Remove(link)
|
|
return err
|
|
}
|
|
|
|
handle, err := windows.CreateFile(
|
|
linkPtr,
|
|
windows.GENERIC_WRITE,
|
|
0,
|
|
nil,
|
|
windows.OPEN_EXISTING,
|
|
windows.FILE_FLAG_BACKUP_SEMANTICS,
|
|
0,
|
|
)
|
|
if err != nil {
|
|
os.Remove(link)
|
|
return err
|
|
}
|
|
|
|
rp := winio.ReparsePoint{
|
|
Target: target,
|
|
IsMountPoint: true,
|
|
}
|
|
data := winio.EncodeReparsePoint(&rp)
|
|
|
|
var bytesReturned uint32
|
|
err = windows.DeviceIoControl(
|
|
handle,
|
|
windows.FSCTL_SET_REPARSE_POINT,
|
|
&data[0],
|
|
uint32(len(data)),
|
|
nil,
|
|
0,
|
|
&bytesReturned,
|
|
nil,
|
|
)
|
|
// The handle is opened without sharing, so it must be closed before the
|
|
// directory can be removed on failure.
|
|
windows.CloseHandle(handle)
|
|
if err != nil {
|
|
os.Remove(link)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// resolveWorkLink reads the junction/symlink at bundlePath/work to recover
|
|
// the working directory path. os.Readlink works for both symlinks and
|
|
// junctions on Windows.
|
|
func resolveWorkLink(bundlePath string) (string, error) {
|
|
return os.Readlink(filepath.Join(bundlePath, "work"))
|
|
}
|