Skip to content

Commit 8755972

Browse files
smoserrchamarthy
authored andcommitted
Change 'End' to 'Last' in Partition, and clarify bytes in doc.
The use of 'End' can be confusing. If a partition has Start: 512 End: 1023 It is not obvious whether or not the Partition includes byte 1023. It is more obvious if we use 'Last'.
1 parent 00ce0f4 commit 8755972

File tree

6 files changed

+41
-41
lines changed

6 files changed

+41
-41
lines changed

Diff for: disk.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (d *Disk) FreeSpacesWithMin(minSize uint64) []FreeSpace {
110110
used := uRanges{{0, 1*Mebibyte - 1}, {end, d.Size}}
111111

112112
for _, p := range d.Partitions {
113-
used = append(used, uRange{p.Start, p.End})
113+
used = append(used, uRange{p.Start, p.Last})
114114
}
115115

116116
avail := []FreeSpace{}
@@ -172,20 +172,20 @@ func (d Disk) Details() string {
172172
mbo := func(n uint64) string { return mbsize(n, 0) }
173173
mbe := func(n uint64) string { return mbsize(n, 1) }
174174
lfmt := "[%2s %10s %10s %10s %-16s]\n"
175-
buf := fmt.Sprintf(lfmt, "#", "Start", "End", "Size", "Name")
175+
buf := fmt.Sprintf(lfmt, "#", "Start", "Last", "Size", "Name")
176176

177177
for _, p := range d.Partitions {
178178
if fsn < len(fss) && fss[fsn].Start < p.Start {
179-
buf += fmt.Sprintf(lfmt, "-", mbo(fss[fsn].Start), mbe(fss[fsn].End), mbo(fss[fsn].Size()), "<free>")
179+
buf += fmt.Sprintf(lfmt, "-", mbo(fss[fsn].Start), mbe(fss[fsn].Last), mbo(fss[fsn].Size()), "<free>")
180180
fsn++
181181
}
182182

183183
buf += fmt.Sprintf(lfmt,
184-
fmt.Sprintf("%d", p.Number), mbo(p.Start), mbe(p.End), mbo(p.Size()), p.Name)
184+
fmt.Sprintf("%d", p.Number), mbo(p.Start), mbe(p.Last), mbo(p.Size()), p.Name)
185185
}
186186

187187
if fsn < len(fss) {
188-
buf += fmt.Sprintf(lfmt, "-", mbo(fss[fsn].Start), mbe(fss[fsn].End), mbo(fss[fsn].Size()), "<free>")
188+
buf += fmt.Sprintf(lfmt, "-", mbo(fss[fsn].Start), mbe(fss[fsn].Last), mbo(fss[fsn].Size()), "<free>")
189189
}
190190

191191
return buf
@@ -211,11 +211,11 @@ type PartitionSet map[uint]Partition
211211

212212
// Partition wraps the disk partition information.
213213
type Partition struct {
214-
// Start is the start offset of the disk partition.
214+
// Start is the offset in bytes of the start of this partition.
215215
Start uint64 `json:"start"`
216216

217-
// End is the end offset of the disk partition.
218-
End uint64 `json:"end"`
217+
// Last is the last byte that is part of this partition.
218+
Last uint64 `json:"last"`
219219

220220
// ID is the partition id.
221221
ID GUID `json:"id"`
@@ -232,13 +232,13 @@ type Partition struct {
232232

233233
// Size returns the size of the partition in bytes.
234234
func (p *Partition) Size() uint64 {
235-
return p.End - p.Start + 1
235+
return p.Last - p.Start + 1
236236
}
237237

238238
// jPartition - Partition, but for json (ids are strings)
239239
type jPartition struct {
240240
Start uint64 `json:"start"`
241-
End uint64 `json:"end"`
241+
Last uint64 `json:"last"`
242242
ID string `json:"id"`
243243
Type string `json:"type"`
244244
Name string `json:"name"`
@@ -265,7 +265,7 @@ func (p *Partition) UnmarshalJSON(b []byte) error {
265265
}
266266

267267
p.Start = j.Start
268-
p.End = j.End
268+
p.Last = j.Last
269269
p.ID = id
270270
p.Type = PartType(ptype)
271271
p.Name = j.Name
@@ -278,7 +278,7 @@ func (p *Partition) UnmarshalJSON(b []byte) error {
278278
func (p *Partition) MarshalJSON() ([]byte, error) {
279279
return json.Marshal(&jPartition{
280280
Start: p.Start,
281-
End: p.End,
281+
Last: p.Last,
282282
ID: p.ID.String(),
283283
Type: p.Type.String(),
284284
Name: p.Name,
@@ -290,14 +290,14 @@ func (p PartType) String() string {
290290
return GUIDToString(GUID(p))
291291
}
292292

293-
// FreeSpace indicates a free slot on the disk with a Start and End offset,
294-
// where a partition can be craeted.
293+
// FreeSpace indicates a free slot on the disk with a Start and Last offset,
294+
// where a partition can be created.
295295
type FreeSpace struct {
296296
Start uint64 `json:"start"`
297-
End uint64 `json:"end"`
297+
Last uint64 `json:"last"`
298298
}
299299

300300
// Size returns the size of the free space, which is End - Start.
301301
func (f *FreeSpace) Size() uint64 {
302-
return f.End - f.Start + 1
302+
return f.Last - f.Start + 1
303303
}

Diff for: disk_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ import (
1212
)
1313

1414
func TestFreeSpaceSize(t *testing.T) {
15-
values := []struct{ start, end, expected uint64 }{
15+
values := []struct{ start, last, expected uint64 }{
1616
{0, 9, 10},
1717
{0, 199, 200},
1818
{100, 200, 101},
1919
}
2020

2121
for _, v := range values {
22-
f := disko.FreeSpace{v.start, v.end}
22+
f := disko.FreeSpace{v.start, v.last}
2323
found := f.Size()
2424

2525
if v.expected != found {
@@ -30,13 +30,13 @@ func TestFreeSpaceSize(t *testing.T) {
3030
}
3131

3232
func TestPartitionSize(t *testing.T) {
33-
tables := []struct{ start, end, expected uint64 }{
33+
tables := []struct{ start, last, expected uint64 }{
3434
{0, 99, 100},
3535
{3 * disko.Mebibyte, (5000+3)*disko.Mebibyte - 1, 5000 * disko.Mebibyte},
3636
}
3737

3838
for _, table := range tables {
39-
p := disko.Partition{Start: table.start, End: table.end}
39+
p := disko.Partition{Start: table.start, Last: table.last}
4040
found := p.Size()
4141

4242
if table.expected != found {
@@ -57,8 +57,8 @@ func TestDiskString(t *testing.T) {
5757
Type: disko.HDD,
5858
Attachment: disko.ATA,
5959
Partitions: disko.PartitionSet{
60-
1: {Start: 3 * mib, End: 253*mib - 1, Number: 1}, //nolint: gomnd
61-
3: {Start: 500 * mib, End: 600*mib - 1, Number: 3}, //nolint: gomnd
60+
1: {Start: 3 * mib, Last: 253*mib - 1, Number: 1}, //nolint: gomnd
61+
3: {Start: 500 * mib, Last: 600*mib - 1, Number: 3}, //nolint: gomnd
6262
},
6363
UdevInfo: disko.UdevInfo{},
6464
}
@@ -87,12 +87,12 @@ func TestDiskDetails(t *testing.T) {
8787
Type: disko.HDD,
8888
Attachment: disko.ATA,
8989
Partitions: disko.PartitionSet{
90-
1: {Start: 3 * mib, End: 253*mib - 1, Number: 1}, //nolint: gomnd
90+
1: {Start: 3 * mib, Last: 253*mib - 1, Number: 1}, //nolint: gomnd
9191
},
9292
UdevInfo: disko.UdevInfo{},
9393
}
9494
expected := `
95-
[ # Start End Size Name ]
95+
[ # Start Last Size Name ]
9696
[ 1 3 MiB 253 MiB 250 MiB ]
9797
[ - 253 MiB 1048575 MiB 1048322 MiB <free> ]`
9898

@@ -151,7 +151,7 @@ func TestPartitionSerializeJson(t *testing.T) {
151151
myID, _ := disko.StringToGUID(myIDStr)
152152
p := disko.Partition{
153153
Start: 3 * disko.Mebibyte, //nolint:gomnd
154-
End: 253*disko.Mebibyte - 1,
154+
Last: 253*disko.Mebibyte - 1,
155155
ID: myID,
156156
Type: partid.EFI,
157157
Name: "my system part",
@@ -181,7 +181,7 @@ func TestPartitionUnserializeJson(t *testing.T) {
181181
myID, _ := disko.StringToGUID(myIDStr)
182182
jbytes := []byte(`{
183183
"start": 3145728,
184-
"end": 265289727,
184+
"last": 265289727,
185185
"id": "01234567-89AB-CDEF-0123-456789ABCDEF",
186186
"type": "C12A7328-F81F-11D2-BA4B-00A0C93EC93B",
187187
"name": "my system part",
@@ -197,7 +197,7 @@ func TestPartitionUnserializeJson(t *testing.T) {
197197

198198
expected := disko.Partition{
199199
Start: 3 * disko.Mebibyte, // nolint:gomnd
200-
End: 253*disko.Mebibyte - 1, // nolint:gomnd
200+
Last: 253*disko.Mebibyte - 1, // nolint:gomnd
201201
ID: myID,
202202
Type: partid.EFI,
203203
Name: "my system part",

Diff for: linux/disk.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func findPartitions(fp io.ReadSeeker) (disko.PartitionSet, uint, error) {
136136

137137
part := disko.Partition{
138138
Start: p.FirstLBA * ssize,
139-
End: p.LastLBA*ssize + ssize - 1,
139+
Last: p.LastLBA*ssize + ssize - 1,
140140
ID: disko.GUID(p.Id),
141141
Type: disko.PartType(p.Type),
142142
Name: p.Name(),

Diff for: mockos/lvm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestVG(t *testing.T) {
5454
Name: name,
5555
Number: 1,
5656
Start: 0,
57-
End: d.Size,
57+
Last: d.Size,
5858
Type: partid.LinuxFS,
5959
})
6060

Diff for: mockos/system_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestSystem(t *testing.T) {
6767
}
6868
partition := disko.Partition{
6969
Start: 0,
70-
End: 10000,
70+
Last: 10000,
7171
ID: myID,
7272
Type: partid.LinuxFS,
7373
Name: "sda1",
@@ -115,7 +115,7 @@ func TestSystem(t *testing.T) {
115115
}
116116
partition := disko.Partition{
117117
Start: 0,
118-
End: 10000,
118+
Last: 10000,
119119
ID: myID,
120120
Type: partid.LinuxFS,
121121
Name: "partition1",

Diff for: util.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66
)
77

88
type uRange struct {
9-
Start, End uint64
9+
Start, Last uint64
1010
}
1111

1212
func (r *uRange) Size() uint64 {
13-
return r.End - r.Start
13+
return r.Last - r.Start + 1
1414
}
1515

1616
type uRanges []uRange
@@ -33,25 +33,25 @@ func findRangeGaps(ranges uRanges, min, max uint64) uRanges {
3333
for _, i := range ranges {
3434
for r := 0; r < len(ret); r++ {
3535
// 5 cases:
36-
if i.Start > ret[r].End || i.End < ret[r].Start {
36+
if i.Start > ret[r].Last || i.Last < ret[r].Start {
3737
// a. i has no overlap
38-
} else if i.Start <= ret[r].Start && i.End >= ret[r].End {
38+
} else if i.Start <= ret[r].Start && i.Last >= ret[r].Last {
3939
// b.) i is complete superset, so remove ret[r]
4040
ret = append(ret[:r], ret[r+1:]...)
4141
r--
42-
} else if i.Start > ret[r].Start && i.End < ret[r].End {
42+
} else if i.Start > ret[r].Start && i.Last < ret[r].Last {
4343
// c.) i is strict subset: split ret[r]
4444
ret = append(
45-
append(ret[:r+1], uRange{i.End + 1, ret[r].End}),
45+
append(ret[:r+1], uRange{i.Last + 1, ret[r].Last}),
4646
ret[r+1:]...)
47-
ret[r].End = i.Start - 1
47+
ret[r].Last = i.Start - 1
4848
r++ // added entry is guaranteed to be 'a', so skip it.
4949
} else if i.Start <= ret[r].Start {
5050
// d.) overlap left edge to middle
51-
ret[r].Start = i.End + 1
52-
} else if i.Start <= ret[r].End {
51+
ret[r].Start = i.Last + 1
52+
} else if i.Start <= ret[r].Last {
5353
// e.) middle to right edge (possibly past).
54-
ret[r].End = i.Start - 1
54+
ret[r].Last = i.Start - 1
5555
} else {
5656
panic(fmt.Sprintf("Error in findRangeGaps: %v, r=%d, ret=%v",
5757
i, r, ret))

0 commit comments

Comments
 (0)