🐛 Account for file ids > 9
This commit is contained in:
parent
a495a2ef1a
commit
495d50ba10
7 changed files with 40 additions and 37 deletions
17
blocks.go
17
blocks.go
|
|
@ -1,30 +1,31 @@
|
|||
package defrag
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Blocks(diskMap []byte) ([]byte, error) {
|
||||
func Blocks(diskMap []byte) ([]int, error) {
|
||||
current := 0
|
||||
onFile := false
|
||||
buff := bytes.Buffer{}
|
||||
out := []int{}
|
||||
for _, by := range diskMap {
|
||||
count, err := strconv.Atoi(string(by))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
onFile = !onFile
|
||||
|
||||
if count == 0 {
|
||||
continue
|
||||
}
|
||||
append := "."
|
||||
val := -1
|
||||
if onFile {
|
||||
append = strconv.Itoa(current)
|
||||
val = current
|
||||
current++
|
||||
}
|
||||
buff.WriteString(strings.Repeat(append, count))
|
||||
for idx := 0; idx < count; idx++ {
|
||||
out = append(out, val)
|
||||
}
|
||||
}
|
||||
return buff.Bytes(), nil
|
||||
return out, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue