diff --git a/main.go b/main.go index ba131e9..65a1f22 100644 --- a/main.go +++ b/main.go @@ -181,6 +181,9 @@ func allowedFragcount(file *storage.FileInformation, minBpf int) int { // Note that when we will do the deduplication more clever (comparing all blocks of all files), we may also need to do // the defragmentation in a more clever way. func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, minBpf int, noact bool) (copy []*storage.FileInformation) { + if len(files) == 0 { + return files + } copy = make([]*storage.FileInformation, len(files), len(files)) for idx, file := range files { copy[idx] = file @@ -198,6 +201,7 @@ func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, min } fragcount := len(copy[0].Fragments) allowedFragcount := allowedFragcount(copy[0], minBpf) + allowedFragcount = 1 if fragcount <= allowedFragcount { return } @@ -247,9 +251,10 @@ func reorderAndDefragIfNeeded(ctx context, files []*storage.FileInformation, min if newFile, err := readFileMeta(file.Path, path); err != nil { log.Printf("Error while reading the fragmentation table again: %v", err) + return reorderAndDefragIfNeeded(ctx, copy[1:], minBpf, noact) } else if newFile == nil { log.Printf("File can not be deduplicated after defragmentation") - copy = copy[1:] + return reorderAndDefragIfNeeded(ctx, copy[1:], minBpf, noact) } else { copy[0] = newFile log.Printf("Number of fragments was %d and is now %d for file %s", fragcount, len(newFile.Fragments), path) diff --git a/sys/frags.go b/sys/frags.go index 1e1159a..bbc0db3 100644 --- a/sys/frags.go +++ b/sys/frags.go @@ -55,7 +55,7 @@ func Fragments(file *os.File) ([]Fragment, error) { return nil, err } if data.fm_mapped_extents == 0 { - return nil, errors.New("No (more) extends found") + return nil, errors.New("No (more) extends found, file may be sparse") } for _, extend := range data.fm_extents[0:data.fm_mapped_extents] { last = last || extend.fe_flags&FIEMAP_EXTENT_LAST != 0