MCPcopy
hub / github.com/hasura/graphql-engine / createOrUpdateLink

Function createOrUpdateLink

cli/plugins/util.go:146–178  ·  view source on GitHub ↗
(binDir, binary, plugin string)

Source from the content-addressed store, hash-verified

144}
145
146func 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.
181func removeLink(path string) error {

Callers 1

installPluginMethod · 0.85

Calls 4

PluginNameToBinFunction · 0.85
IsWindowsFunction · 0.85
removeLinkFunction · 0.85
copyFileFunction · 0.85

Tested by

no test coverage detected