File: selfupdate.go

package info (click to toggle)
rclone 1.60.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 34,820 kB
  • sloc: sh: 957; xml: 857; python: 655; javascript: 612; makefile: 264; ansic: 101; php: 74
file content (475 lines) | stat: -rw-r--r-- 14,128 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//go:build !noselfupdate
// +build !noselfupdate

// Package selfupdate provides the selfupdate command.
package selfupdate

import (
	"archive/zip"
	"bufio"
	"bytes"
	"context"
	"crypto/sha256"
	"encoding/hex"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"runtime"
	"strings"

	"github.com/rclone/rclone/cmd"
	"github.com/rclone/rclone/cmd/cmount"
	"github.com/rclone/rclone/fs"
	"github.com/rclone/rclone/fs/config/flags"
	"github.com/rclone/rclone/fs/fshttp"
	"github.com/rclone/rclone/lib/buildinfo"
	"github.com/rclone/rclone/lib/random"
	"github.com/spf13/cobra"

	versionCmd "github.com/rclone/rclone/cmd/version"
)

// Options contains options for the self-update command
type Options struct {
	Check   bool
	Output  string // output path
	Beta    bool   // mutually exclusive with Stable (false means "stable")
	Stable  bool   // mutually exclusive with Beta
	Version string
	Package string // package format: zip, deb, rpm (empty string means "zip")
}

// Opt is options set via command line
var Opt = Options{}

func init() {
	cmd.Root.AddCommand(cmdSelfUpdate)
	cmdFlags := cmdSelfUpdate.Flags()
	flags.BoolVarP(cmdFlags, &Opt.Check, "check", "", Opt.Check, "Check for latest release, do not download")
	flags.StringVarP(cmdFlags, &Opt.Output, "output", "", Opt.Output, "Save the downloaded binary at a given path (default: replace running binary)")
	flags.BoolVarP(cmdFlags, &Opt.Stable, "stable", "", Opt.Stable, "Install stable release (this is the default)")
	flags.BoolVarP(cmdFlags, &Opt.Beta, "beta", "", Opt.Beta, "Install beta release")
	flags.StringVarP(cmdFlags, &Opt.Version, "version", "", Opt.Version, "Install the given rclone version (default: latest)")
	flags.StringVarP(cmdFlags, &Opt.Package, "package", "", Opt.Package, "Package format: zip|deb|rpm (default: zip)")
}

var cmdSelfUpdate = &cobra.Command{
	Use:     "selfupdate",
	Aliases: []string{"self-update"},
	Short:   `Update the rclone binary.`,
	Long:    strings.ReplaceAll(selfUpdateHelp, "|", "`"),
	Run: func(command *cobra.Command, args []string) {
		cmd.CheckArgs(0, 0, command, args)
		if Opt.Package == "" {
			Opt.Package = "zip"
		}
		gotActionFlags := Opt.Stable || Opt.Beta || Opt.Output != "" || Opt.Version != "" || Opt.Package != "zip"
		if Opt.Check && !gotActionFlags {
			versionCmd.CheckVersion()
			return
		}
		if Opt.Package != "zip" {
			if Opt.Package != "deb" && Opt.Package != "rpm" {
				log.Fatalf("--package should be one of zip|deb|rpm")
			}
			if runtime.GOOS != "linux" {
				log.Fatalf(".deb and .rpm packages are supported only on Linux")
			} else if os.Geteuid() != 0 && !Opt.Check {
				log.Fatalf(".deb and .rpm must be installed by root")
			}
			if Opt.Output != "" && !Opt.Check {
				fmt.Println("Warning: --output is ignored with --package deb|rpm")
			}
		}
		if err := InstallUpdate(context.Background(), &Opt); err != nil {
			log.Fatalf("Error: %v", err)
		}
	},
}

// GetVersion can get the latest release number from the download site
// or massage a stable release number - prepend semantic "v" prefix
// or find the latest micro release for a given major.minor release.
// Note: this will not be applied to beta releases.
func GetVersion(ctx context.Context, beta bool, version string) (newVersion, siteURL string, err error) {
	siteURL = "https://downloads.rclone.org"
	if beta {
		siteURL = "https://beta.rclone.org"
	}

	if version == "" {
		// Request the latest release number from the download site
		_, newVersion, _, err = versionCmd.GetVersion(siteURL + "/version.txt")
		return
	}

	newVersion = version
	if version[0] != 'v' {
		newVersion = "v" + version
	}
	if beta {
		return
	}

	if valid, _ := regexp.MatchString(`^v\d+\.\d+(\.\d+)?$`, newVersion); !valid {
		return "", siteURL, errors.New("invalid semantic version")
	}

	// Find the latest stable micro release
	if strings.Count(newVersion, ".") == 1 {
		html, err := downloadFile(ctx, siteURL)
		if err != nil {
			return "", siteURL, fmt.Errorf("failed to get list of releases: %w", err)
		}
		reSubver := fmt.Sprintf(`href="\./%s\.\d+/"`, regexp.QuoteMeta(newVersion))
		allSubvers := regexp.MustCompile(reSubver).FindAllString(string(html), -1)
		if allSubvers == nil {
			return "", siteURL, errors.New("could not find the minor release")
		}
		// Use the fact that releases in the index are sorted by date
		lastSubver := allSubvers[len(allSubvers)-1]
		newVersion = lastSubver[8 : len(lastSubver)-2]
	}
	return
}

// InstallUpdate performs rclone self-update
func InstallUpdate(ctx context.Context, opt *Options) error {
	// Find the latest release number
	if opt.Stable && opt.Beta {
		return errors.New("--stable and --beta are mutually exclusive")
	}

	// The `cmount` tag is added by cmd/cmount/mount.go only if build is static.
	_, tags := buildinfo.GetLinkingAndTags()
	if strings.Contains(" "+tags+" ", " cmount ") && !cmount.ProvidedBy(runtime.GOOS) {
		return errors.New("updating would discard the mount FUSE capability, aborting")
	}

	newVersion, siteURL, err := GetVersion(ctx, opt.Beta, opt.Version)
	if err != nil {
		return fmt.Errorf("unable to detect new version: %w", err)
	}

	oldVersion := fs.Version
	if newVersion == oldVersion {
		fs.Logf(nil, "rclone is up to date")
		return nil
	}

	// Install .deb/.rpm package if requested by user
	if opt.Package == "deb" || opt.Package == "rpm" {
		if opt.Check {
			fmt.Println("Warning: --package flag is ignored in --check mode")
		} else {
			err := installPackage(ctx, opt.Beta, newVersion, siteURL, opt.Package)
			if err == nil {
				fs.Logf(nil, "Successfully updated rclone package from version %s to version %s", oldVersion, newVersion)
			}
			return err
		}
	}

	// Get the current executable path
	executable, err := os.Executable()
	if err != nil {
		return fmt.Errorf("unable to find executable: %w", err)
	}

	targetFile := opt.Output
	if targetFile == "" {
		targetFile = executable
	}

	if opt.Check {
		fmt.Printf("Without --check this would install rclone version %s at %s\n", newVersion, targetFile)
		return nil
	}

	// Make temporary file names and check for possible access errors in advance
	var newFile string
	if newFile, err = makeRandomExeName(targetFile, "new"); err != nil {
		return err
	}
	savedFile := ""
	if runtime.GOOS == "windows" {
		savedFile = targetFile
		savedFile = strings.TrimSuffix(savedFile, ".exe")
		savedFile += ".old.exe"
	}

	if savedFile == executable || newFile == executable {
		return fmt.Errorf("%s: a temporary file would overwrite the executable, specify a different --output path", targetFile)
	}

	if err := verifyAccess(targetFile); err != nil {
		return err
	}

	// Download the update as a temporary file
	err = downloadUpdate(ctx, opt.Beta, newVersion, siteURL, newFile, "zip")
	if err != nil {
		return fmt.Errorf("failed to update rclone: %w", err)
	}

	err = replaceExecutable(targetFile, newFile, savedFile)
	if err == nil {
		fs.Logf(nil, "Successfully updated rclone from version %s to version %s", oldVersion, newVersion)
	}
	return err
}

func installPackage(ctx context.Context, beta bool, version, siteURL, packageFormat string) error {
	tempFile, err := ioutil.TempFile("", "rclone.*."+packageFormat)
	if err != nil {
		return fmt.Errorf("unable to write temporary package: %w", err)
	}
	packageFile := tempFile.Name()
	_ = tempFile.Close()
	defer func() {
		if rmErr := os.Remove(packageFile); rmErr != nil {
			fs.Errorf(nil, "%s: could not remove temporary package: %v", packageFile, rmErr)
		}
	}()
	if err := downloadUpdate(ctx, beta, version, siteURL, packageFile, packageFormat); err != nil {
		return err
	}

	packageCommand := "dpkg"
	if packageFormat == "rpm" {
		packageCommand = "rpm"
	}
	cmd := exec.Command(packageCommand, "-i", packageFile)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	if err := cmd.Run(); err != nil {
		return fmt.Errorf("failed to run %s: %v", packageCommand, err)
	}
	return nil
}

func replaceExecutable(targetFile, newFile, savedFile string) error {
	// Copy permission bits from the old executable
	// (it was extracted with mode 0755)
	fileInfo, err := os.Lstat(targetFile)
	if err == nil {
		if err = os.Chmod(newFile, fileInfo.Mode()); err != nil {
			return fmt.Errorf("failed to set permission: %w", err)
		}
	}

	if err = os.Remove(targetFile); os.IsNotExist(err) {
		err = nil
	}

	if err != nil && savedFile != "" {
		// Windows forbids removal of a running executable so we rename it.
		// For starters, rename download as the original file with ".old.exe" appended.
		var saveErr error
		if saveErr = os.Remove(savedFile); os.IsNotExist(saveErr) {
			saveErr = nil
		}
		if saveErr == nil {
			saveErr = os.Rename(targetFile, savedFile)
		}
		if saveErr != nil {
			// The ".old" file cannot be removed or cannot be renamed to.
			// This usually means that the running executable has a name with ".old".
			// This can happen in very rare cases, but we ought to handle it.
			// Try inserting a randomness in the name to mitigate it.
			fs.Debugf(nil, "%s: cannot replace old file, randomizing name", savedFile)

			savedFile, saveErr = makeRandomExeName(targetFile, "old")
			if saveErr == nil {
				if saveErr = os.Remove(savedFile); os.IsNotExist(saveErr) {
					saveErr = nil
				}
			}
			if saveErr == nil {
				saveErr = os.Rename(targetFile, savedFile)
			}
		}
		if saveErr == nil {
			fs.Infof(nil, "The old executable was saved as %s", savedFile)
			err = nil
		}
	}

	if err == nil {
		err = os.Rename(newFile, targetFile)
	}
	if err != nil {
		if rmErr := os.Remove(newFile); rmErr != nil {
			fs.Errorf(nil, "%s: could not remove temporary file: %v", newFile, rmErr)
		}
		return err
	}
	return nil
}

func makeRandomExeName(baseName, extension string) (string, error) {
	const maxAttempts = 5

	if runtime.GOOS == "windows" {
		baseName = strings.TrimSuffix(baseName, ".exe")
		extension += ".exe"
	}

	for attempt := 0; attempt < maxAttempts; attempt++ {
		filename := fmt.Sprintf("%s.%s.%s", baseName, random.String(4), extension)
		if _, err := os.Stat(filename); os.IsNotExist(err) {
			return filename, nil
		}
	}

	return "", fmt.Errorf("cannot find a file name like %s.xxxx.%s", baseName, extension)
}

func downloadUpdate(ctx context.Context, beta bool, version, siteURL, newFile, packageFormat string) error {
	osName := runtime.GOOS
	arch := runtime.GOARCH
	if osName == "darwin" {
		osName = "osx"
	}

	archiveFilename := fmt.Sprintf("rclone-%s-%s-%s.%s", version, osName, arch, packageFormat)
	archiveURL := fmt.Sprintf("%s/%s/%s", siteURL, version, archiveFilename)
	archiveBuf, err := downloadFile(ctx, archiveURL)
	if err != nil {
		return err
	}
	gotHash := sha256.Sum256(archiveBuf)
	strHash := hex.EncodeToString(gotHash[:])
	fs.Debugf(nil, "downloaded release archive with hashsum %s from %s", strHash, archiveURL)

	// CI/CD does not provide hashsums for beta releases
	if !beta {
		if err := verifyHashsum(ctx, siteURL, version, archiveFilename, gotHash[:]); err != nil {
			return err
		}
	}

	if packageFormat == "deb" || packageFormat == "rpm" {
		if err := ioutil.WriteFile(newFile, archiveBuf, 0644); err != nil {
			return fmt.Errorf("cannot write temporary .%s: %w", packageFormat, err)
		}
		return nil
	}

	entryName := fmt.Sprintf("rclone-%s-%s-%s/rclone", version, osName, arch)
	if runtime.GOOS == "windows" {
		entryName += ".exe"
	}

	// Extract executable to a temporary file, then replace it by an instant rename
	err = extractZipToFile(archiveBuf, entryName, newFile)
	if err != nil {
		return err
	}
	fs.Debugf(nil, "extracted %s to %s", entryName, newFile)
	return nil
}

func verifyAccess(file string) error {
	admin := "root"
	if runtime.GOOS == "windows" {
		admin = "Administrator"
	}

	fileInfo, fileErr := os.Lstat(file)

	if fileErr != nil {
		dir := filepath.Dir(file)
		dirInfo, dirErr := os.Lstat(dir)
		if dirErr != nil {
			return dirErr
		}
		if !dirInfo.Mode().IsDir() {
			return fmt.Errorf("%s: parent path is not a directory, specify a different path using --output", dir)
		}
		if !writable(dir) {
			return fmt.Errorf("%s: directory is not writable, please run self-update as %s", dir, admin)
		}
	}

	if fileErr == nil && !fileInfo.Mode().IsRegular() {
		return fmt.Errorf("%s: path is not a normal file, specify a different path using --output", file)
	}

	if fileErr == nil && !writable(file) {
		return fmt.Errorf("%s: file is not writable, run self-update as %s", file, admin)
	}

	return nil
}

func findFileHash(buf []byte, filename string) (hash []byte, err error) {
	lines := bufio.NewScanner(bytes.NewReader(buf))
	for lines.Scan() {
		tokens := strings.Split(lines.Text(), "  ")
		if len(tokens) == 2 && tokens[1] == filename {
			if hash, err := hex.DecodeString(tokens[0]); err == nil {
				return hash, nil
			}
		}
	}
	return nil, fmt.Errorf("%s: unable to find hash", filename)
}

func extractZipToFile(buf []byte, entryName, newFile string) error {
	zipReader, err := zip.NewReader(bytes.NewReader(buf), int64(len(buf)))
	if err != nil {
		return err
	}

	var reader io.ReadCloser
	for _, entry := range zipReader.File {
		if entry.Name == entryName {
			reader, err = entry.Open()
			break
		}
	}
	if reader == nil || err != nil {
		return fmt.Errorf("%s: file not found in archive", entryName)
	}
	defer func() {
		_ = reader.Close()
	}()

	err = os.Remove(newFile)
	if err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("%s: unable to create new file: %v", newFile, err)
	}
	writer, err := os.OpenFile(newFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.FileMode(0755))
	if err != nil {
		return err
	}

	_, err = io.Copy(writer, reader)
	_ = writer.Close()
	if err != nil {
		if rmErr := os.Remove(newFile); rmErr != nil {
			fs.Errorf(nil, "%s: could not remove temporary file: %v", newFile, rmErr)
		}
	}
	return err
}

func downloadFile(ctx context.Context, url string) ([]byte, error) {
	resp, err := fshttp.NewClient(ctx).Get(url)
	if err != nil {
		return nil, err
	}
	defer fs.CheckClose(resp.Body, &err)
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("failed with %s downloading %s", resp.Status, url)
	}
	return ioutil.ReadAll(resp.Body)
}