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

Add easier error handling #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 22 additions & 23 deletions cmd/extract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,34 @@ const (
typeFullBackup = 2
)

var debug = os.Getenv("DEBUG") == "1"

func main() {
if len(os.Args) != 3 {
_, _ = fmt.Fprintf(os.Stderr, "usage: %s path-to-backup mnemonic\n", filepath.Base(os.Args[0]))
os.Exit(1)
}

backupPath := os.Args[1]
err := extractAppBackup(backupPath)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}

func extractAppBackup(backupPath string) error {
metadataPath := filepath.Join(backupPath, ".backup.metadata")
_, err := os.Stat(metadataPath)
if errors.Is(err, os.ErrNotExist) {
_, _ = fmt.Fprintln(os.Stderr, "error: not a backup (missing .backup.metadata)")
os.Exit(1)
return fmt.Errorf("not a backup (missing .backup.metadata)")
} else if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to stat %q: %s\n", metadataPath, err)
os.Exit(1)
return fmt.Errorf("failed to stat %q: %s\n", metadataPath, err)
Copy link
Owner

@jackwilsdon jackwilsdon May 19, 2023

Choose a reason for hiding this comment

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

Can we remove the newlines from any returned errors? We're ending up with an extra one for some errors now as we're also adding a newline when writing the error to stderr.

}

metadataFile, err := os.Open(metadataPath)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to open %q: %s\n", metadataPath, err)
os.Exit(1)
return fmt.Errorf("failed to open %q: %s\n", metadataPath, err)
}
defer func() {
_ = metadataFile.Close()
Expand All @@ -57,24 +64,20 @@ func main() {
metadataReader := bufio.NewReader(metadataFile)
version, err := metadataReader.ReadByte()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to read version from %q: %s\n", metadataPath, err)
os.Exit(1)
return fmt.Errorf("failed to read version from %q: %s\n", metadataPath, err)
}
if version != 1 {
_, _ = fmt.Fprintf(os.Stderr, "error: unsupported version %d (only version 1 is supported)\n", version)
os.Exit(1)
return fmt.Errorf("unsupported version %d (only version 1 is supported)\n", version)
}

debug := os.Getenv("DEBUG") == "1"
if debug {
fmt.Printf("version: %d\n", version)
}

backupName := filepath.Base(backupPath)
token, err := strconv.ParseUint(backupName, 10, 64)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to parse backup name %q: %s\n", backupName, err)
os.Exit(1)
return fmt.Errorf("failed to parse backup name %q: %s\n", backupName, err)
}

if debug {
Expand All @@ -100,35 +103,30 @@ func main() {
binary.BigEndian.PutUint64(associatedData[2:], token)
metadataBytes, err := decrypt(metadataReader, key, associatedData)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to decrypt metadata: %s\n", err)
os.Exit(1)
return fmt.Errorf("failed to decrypt metadata: %s\n", err)
}
if debug {
fmt.Printf("metadata: %s\n", string(metadataBytes))
}

var metadataMap map[string]json.RawMessage
if err := json.Unmarshal(metadataBytes, &metadataMap); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to unmarshal metadata: %s\n", err)
os.Exit(1)
return fmt.Errorf("failed to unmarshal metadata: %s\n", err)
}

metadataMetaBytes, ok := metadataMap["@meta@"]
if !ok {
_, _ = fmt.Fprintf(os.Stderr, "error: missing @meta@ key\n")
os.Exit(1)
return fmt.Errorf("missing @meta@ key\n")
}
var metadataMeta struct {
Version byte `json:"version"`
Salt string `json:"salt"`
}
if err := json.Unmarshal(metadataMetaBytes, &metadataMeta); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: failed to unmarshal @meta@: %s\n", err)
os.Exit(1)
return fmt.Errorf("failed to unmarshal @meta@: %s\n", err)
}
if metadataMeta.Version != version {
_, _ = fmt.Fprintf(os.Stderr, "error: @meta@ version %d does not match metadata file version %d\n", metadataMeta.Version, version)
os.Exit(1)
return fmt.Errorf("@meta@ version %d does not match metadata file version %d\n", metadataMeta.Version, version)
}

for packageName, packageMetaBytes := range metadataMap {
Expand Down Expand Up @@ -212,6 +210,7 @@ func main() {
_, _ = fmt.Fprintf(os.Stderr, "warning: failed to extract %q: %s\n", packageName, err)
}
}
return nil
}

func mnemonicToSeed(mnemonic string) ([]byte, error) {
Expand Down