From 5f8a6cea6fd3858fc3a177cb12a138bffe8ee1da Mon Sep 17 00:00:00 2001 From: David Date: Fri, 14 Oct 2016 23:54:54 +0000 Subject: [PATCH] don't print status info if running in the background clean fix OS issues & format code fix issues --- src/cmds/restic/background.go | 9 +++++++++ src/cmds/restic/background_linux.go | 21 +++++++++++++++++++++ src/cmds/restic/cmd_backup.go | 13 +++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/cmds/restic/background.go create mode 100644 src/cmds/restic/background_linux.go diff --git a/src/cmds/restic/background.go b/src/cmds/restic/background.go new file mode 100644 index 000000000..2f115adfd --- /dev/null +++ b/src/cmds/restic/background.go @@ -0,0 +1,9 @@ +// +build !linux + +package main + +// IsProcessBackground should return true if it is running in the background or false if not +func IsProcessBackground() bool { + //TODO: Check if the process are running in the background in other OS than linux + return false +} diff --git a/src/cmds/restic/background_linux.go b/src/cmds/restic/background_linux.go new file mode 100644 index 000000000..285a0e88a --- /dev/null +++ b/src/cmds/restic/background_linux.go @@ -0,0 +1,21 @@ +package main + +import ( + "syscall" + "unsafe" + + "restic/debug" +) + +// IsProcessBackground returns true if it is running in the background or false if not +func IsProcessBackground() bool { + var pid int + _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(syscall.Stdin), syscall.TIOCGPGRP, uintptr(unsafe.Pointer(&pid))) + + if err != 0 { + debug.Log("Can't check if we are in the background. Using default behaviour. Error: %s\n", err.Error()) + return false + } + + return pid != syscall.Getpgrp() +} diff --git a/src/cmds/restic/cmd_backup.go b/src/cmds/restic/cmd_backup.go index 57d09bfab..d3c4d92ff 100644 --- a/src/cmds/restic/cmd_backup.go +++ b/src/cmds/restic/cmd_backup.go @@ -71,8 +71,13 @@ func newScanProgress(gopts GlobalOptions) *restic.Progress { p := restic.NewProgress() p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) { + if IsProcessBackground() { + return + } + PrintProgress("[%s] %d directories, %d files, %s", formatDuration(d), s.Dirs, s.Files, formatBytes(s.Bytes)) } + p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) { PrintProgress("scanned %d directories, %d files in %s\n", s.Dirs, s.Files, formatDuration(d)) } @@ -91,6 +96,10 @@ func newArchiveProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress itemsTodo := todo.Files + todo.Dirs archiveProgress.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) { + if IsProcessBackground() { + return + } + sec := uint64(d / time.Second) if todo.Bytes > 0 && sec > 0 && ticker { bps = s.Bytes / sec @@ -144,6 +153,10 @@ func newArchiveStdinProgress(gopts GlobalOptions) *restic.Progress { var bps uint64 archiveProgress.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) { + if IsProcessBackground() { + return + } + sec := uint64(d / time.Second) if s.Bytes > 0 && sec > 0 && ticker { bps = s.Bytes / sec