(binDir, binary, plugin string)
| 144 | } |
| 145 | |
| 146 | func createOrUpdateLink(binDir, binary, plugin string) error { |
| 147 | var op errors.Op = "plugins.createOrUpdateLink" |
| 148 | dst := filepath.Join(binDir, PluginNameToBin(plugin, IsWindows())) |
| 149 | |
| 150 | if err := removeLink(dst); err != nil { |
| 151 | return errors.E(op, fmt.Errorf("failed to remove old symlink: %w", err)) |
| 152 | } |
| 153 | if _, err := os.Stat(binary); stderrors.Is(err, fs.ErrNotExist) { |
| 154 | return errors.E(op, fmt.Errorf("can't create symbolic link, source binary (%q) cannot be found in extracted archive: %w", binary, err)) |
| 155 | } |
| 156 | |
| 157 | // Create new |
| 158 | if err := os.Symlink(binary, dst); err != nil { |
| 159 | if IsWindows() { |
| 160 | // If cloning the symlink fails on Windows because the user |
| 161 | // does not have the required privileges, ignore the error and |
| 162 | // fall back to copying the file contents. |
| 163 | // |
| 164 | // ERROR_PRIVILEGE_NOT_HELD is 1314 (0x522): |
| 165 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms681385(v=vs.85).aspx |
| 166 | var lerr *os.LinkError |
| 167 | if stderrors.As(err, &lerr); lerr.Err != syscall.Errno(1314) { |
| 168 | return errors.E(op, err) |
| 169 | } |
| 170 | if err := copyFile(binary, dst, 0755); err != nil { |
| 171 | return errors.E(op, err) |
| 172 | } |
| 173 | } else { |
| 174 | return errors.E(op, fmt.Errorf("failed to create a symlink from %q to %q: %w", binary, dst, err)) |
| 175 | } |
| 176 | } |
| 177 | return nil |
| 178 | } |
| 179 | |
| 180 | // removeLink removes a symlink reference if exists. |
| 181 | func removeLink(path string) error { |
no test coverage detected