summaryrefslogtreecommitdiff
path: root/contrib/findholes/main.go
blob: 17597ae8385a919165f857874064eee26805dd51 (plain)
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
// Find and pretty-print holes & data sections of a file.
package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/rfjakob/gocryptfs/contrib/findholes/holes"
)

func main() {
	flags := struct {
		verify *bool
		create *bool
	}{}
	flags.verify = flag.Bool("verify", false, "Verify results using full file scan")
	flags.create = flag.Bool("create", false, "Create test file with random holes")
	flag.Parse()
	if flag.NArg() != 1 {
		fmt.Printf("Usage: findholes FILE\n")
		os.Exit(1)
	}

	path := flag.Arg(0)

	if *flags.create {
		holes.Create(path)
	}

	f, err := os.Open(path)
	if err != nil {
		// os.Open() gives nicer error messages than syscall.Open()
		fmt.Println(err)
		os.Exit(1)
	}
	defer f.Close()

	segments, err := holes.Find(int(f.Fd()))
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println(holes.PrettyPrint(segments))

	if *flags.verify {
		err = holes.Verify(int(f.Fd()), segments)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		} else {
			fmt.Println("verify ok")
		}
	}

}