aboutsummaryrefslogtreecommitdiff
path: root/contrib/findholes/holes/holes.go
blob: a2c16de1e92aacf0fcd953ab07b70b6c6fa51043 (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
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
// Package holes finds and pretty-prints holes & data sections of a file.
// Used by TestFileHoleCopy in the gocryptfs test suite.
package holes

import (
	"fmt"
	"syscall"
)

const (
	SEEK_DATA = 3
	SEEK_HOLE = 4

	SegmentHole = SegmentType(100)
	SegmentData = SegmentType(101)
	SegmentEOF  = SegmentType(102)
)

type Segment struct {
	Offset int64
	Type   SegmentType
}

func (s Segment) String() string {
	return fmt.Sprintf("%10d %v", s.Offset, s.Type)
}

type SegmentType int

func (s SegmentType) String() string {
	switch s {
	case SegmentHole:
		return "hole"
	case SegmentData:
		return "data"
	case SegmentEOF:
		return "eof"
	default:
		return "???"
	}
}

// PrettyPrint pretty-prints the Segments.
func PrettyPrint(segments []Segment) (out string) {
	for _, s := range segments {
		out += "\n" + s.String()
	}
	return out
}

// Find examines the file passed via file descriptor and returns the found holes
// and data sections.
func Find(fd int) (segments []Segment, err error) {
	var st syscall.Stat_t
	err = syscall.Fstat(fd, &st)
	if err != nil {
		return nil, err
	}
	totalSize := st.Size

	var cursor int64

	// find out if file starts with data or hole
	off, err := syscall.Seek(fd, 0, SEEK_DATA)
	if err == syscall.ENXIO {
		segments = append(segments,
			Segment{0, SegmentHole},
			Segment{totalSize, SegmentEOF})
		return segments, nil
	}
	if err != nil {
		return nil, err
	}
	if off == cursor {
		segments = append(segments, Segment{0, SegmentData})
	} else {
		segments = append(segments,
			Segment{0, SegmentHole},
			Segment{totalSize, SegmentData})
		cursor = off
	}

	// now we are at the start of data.
	// find next hole, then next data, then next hole, then next data...
	for {
		cursor, err = syscall.Seek(fd, cursor, SEEK_HOLE)
		if err != nil {
			return nil, err
		}
		if cursor == totalSize {
			segments = append(segments, Segment{cursor, SegmentEOF})
			break
		}
		segments = append(segments, Segment{cursor, SegmentHole})
		cursor, err = syscall.Seek(fd, cursor, SEEK_DATA)
		if err != nil {
			return nil, err
		}
		if cursor == totalSize {
			segments = append(segments, Segment{cursor, SegmentEOF})
			break
		}
		segments = append(segments, Segment{cursor, SegmentData})
	}
	return segments, nil
}