Skip to content

Commit 6dcb80f

Browse files
committed
Retry defragmentation for next file if previous one became sparse
1 parent 2166b4e commit 6dcb80f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ func allowedFragcount(file *storage.FileInformation, minBpf int) int {
181181
// Note that when we will do the deduplication more clever (comparing all blocks of all files), we may also need to do
182182
// the defragmentation in a more clever way.
183183
func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, minBpf int, noact bool) (copy []*storage.FileInformation) {
184+
if len(files) == 0 {
185+
return files
186+
}
184187
copy = make([]*storage.FileInformation, len(files), len(files))
185188
for idx, file := range files {
186189
copy[idx] = file
@@ -198,6 +201,7 @@ func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, min
198201
}
199202
fragcount := len(copy[0].Fragments)
200203
allowedFragcount := allowedFragcount(copy[0], minBpf)
204+
allowedFragcount = 1
201205
if fragcount <= allowedFragcount {
202206
return
203207
}
@@ -247,9 +251,10 @@ func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, min
247251

248252
if newFile, err := readFileMeta(file.Path, path); err != nil {
249253
log.Printf("Error while reading the fragmentation table again: %v", err)
254+
return reorderAndDefragIfNeeded(ctx, copy[1:], minBpf, noact)
250255
} else if newFile == nil {
251256
log.Printf("File can not be deduplicated after defragmentation")
252-
copy = copy[1:]
257+
return reorderAndDefragIfNeeded(ctx, copy[1:], minBpf, noact)
253258
} else {
254259
copy[0] = newFile
255260
log.Printf("Number of fragments was %d and is now %d for file %s", fragcount, len(newFile.Fragments), path)

sys/frags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func Fragments(file *os.File) ([]Fragment, error) {
5555
return nil, err
5656
}
5757
if data.fm_mapped_extents == 0 {
58-
return nil, errors.New("No (more) extends found")
58+
return nil, errors.New("No (more) extends found, file may be sparse")
5959
}
6060
for _, extend := range data.fm_extents[0:data.fm_mapped_extents] {
6161
last = last || extend.fe_flags&FIEMAP_EXTENT_LAST != 0

0 commit comments

Comments
 (0)