Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better inform users of permissions errors by tmutil Error (-50), fixes #14 #19

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions pkg/tmutil/tmutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"bufio"
"bytes"
"fmt"
"github.com/tg44/heptapod/pkg/utils"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/tg44/heptapod/pkg/utils"
)

func AddPathsToTM(paths []string, logDir string, bufferSize int, verbose int) int {
Expand Down Expand Up @@ -101,6 +102,19 @@ func checkPath(path string, verbose int) (bool, error) {
return !strings.Contains(s, "[Excluded]"), nil
}

func printTmutilError(path string, out bytes.Buffer, outErr bytes.Buffer) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I manually verified write permissions are the one needed for files and directories for adding or removing exclusions, they are not needed for checking

log.Println(out.String())
var errorMessageToPrint string
errorString := outErr.String()
_, found := strings.CutPrefix(errorString, path+": Error (-50)")
if found {
errorMessageToPrint = path + ": Please make sure you have write permissions to that file or directory."
} else {
errorMessageToPrint = errorString
}
log.Println(errorMessageToPrint)
}

func addPath(path string, logfile *os.File) error {
cmd := exec.Command("tmutil", "addexclusion", path)
var out bytes.Buffer
Expand All @@ -109,8 +123,7 @@ func addPath(path string, logfile *os.File) error {
cmd.Stderr = &outErr
err := cmd.Run()
if err != nil {
log.Println(out.String())
log.Println(outErr.String())
printTmutilError(path, out, outErr)
return err
}
_, _ = logfile.WriteString(path + "\r\n")
Expand All @@ -125,8 +138,7 @@ func removePath(path string) error {
cmd.Stderr = &outErr
err := cmd.Run()
if err != nil {
log.Println(out.String())
log.Println(outErr.String())
printTmutilError(path, out, outErr)
}
return err
}
Expand Down
Loading