dedupe: Make it obey the --size-only flag for duplicate detection #4321
This commit is contained in:
+25
-13
@@ -101,22 +101,30 @@ func dedupeDeleteIdentical(ctx context.Context, ht hash.Type, remote string, obj
|
||||
objs = newObjs
|
||||
|
||||
// See how many of these duplicates are identical
|
||||
byHash := make(map[string][]fs.Object, len(objs))
|
||||
dupesByID := make(map[string][]fs.Object, len(objs))
|
||||
for _, o := range objs {
|
||||
md5sum, err := o.Hash(ctx, ht)
|
||||
if err != nil || md5sum == "" {
|
||||
ID := ""
|
||||
if fs.Config.SizeOnly && o.Size() >= 0 {
|
||||
ID = fmt.Sprintf("size %d", o.Size())
|
||||
} else if ht != hash.None {
|
||||
hashValue, err := o.Hash(ctx, ht)
|
||||
if err == nil && hashValue != "" {
|
||||
ID = fmt.Sprintf("%v %s", ht, hashValue)
|
||||
}
|
||||
}
|
||||
if ID == "" {
|
||||
remainingObjs = append(remainingObjs, o)
|
||||
} else {
|
||||
byHash[md5sum] = append(byHash[md5sum], o)
|
||||
dupesByID[ID] = append(dupesByID[ID], o)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete identical duplicates, filling remainingObjs with the ones remaining
|
||||
for md5sum, hashObjs := range byHash {
|
||||
remainingObjs = append(remainingObjs, hashObjs[0])
|
||||
if len(hashObjs) > 1 {
|
||||
fs.Logf(remote, "Deleting %d/%d identical duplicates (%v %q)", len(hashObjs)-1, len(hashObjs), ht, md5sum)
|
||||
for _, o := range hashObjs[1:] {
|
||||
for ID, dupes := range dupesByID {
|
||||
remainingObjs = append(remainingObjs, dupes[0])
|
||||
if len(dupes) > 1 {
|
||||
fs.Logf(remote, "Deleting %d/%d identical duplicates (%s)", len(dupes)-1, len(dupes), ID)
|
||||
for _, o := range dupes[1:] {
|
||||
err := DeleteFile(ctx, o)
|
||||
if err != nil {
|
||||
remainingObjs = append(remainingObjs, o)
|
||||
@@ -132,11 +140,15 @@ func dedupeDeleteIdentical(ctx context.Context, ht hash.Type, remote string, obj
|
||||
func dedupeInteractive(ctx context.Context, f fs.Fs, ht hash.Type, remote string, objs []fs.Object) {
|
||||
fmt.Printf("%s: %d duplicates remain\n", remote, len(objs))
|
||||
for i, o := range objs {
|
||||
md5sum, err := o.Hash(ctx, ht)
|
||||
if err != nil {
|
||||
md5sum = err.Error()
|
||||
hashValue := ""
|
||||
if ht != hash.None {
|
||||
var err error
|
||||
hashValue, err = o.Hash(ctx, ht)
|
||||
if err != nil {
|
||||
hashValue = err.Error()
|
||||
}
|
||||
}
|
||||
fmt.Printf(" %d: %12d bytes, %s, %v %32s\n", i+1, o.Size(), o.ModTime(ctx).Local().Format("2006-01-02 15:04:05.000000000"), ht, md5sum)
|
||||
fmt.Printf(" %d: %12d bytes, %s, %v %32s\n", i+1, o.Size(), o.ModTime(ctx).Local().Format("2006-01-02 15:04:05.000000000"), ht, hashValue)
|
||||
}
|
||||
switch config.Command([]string{"sSkip and do nothing", "kKeep just one (choose which in next step)", "rRename all to be different (by changing file.jpg to file-1.jpg)"}) {
|
||||
case 's':
|
||||
|
||||
@@ -75,6 +75,27 @@ func TestDeduplicateSkip(t *testing.T) {
|
||||
r.CheckWithDuplicates(t, file1, file3)
|
||||
}
|
||||
|
||||
func TestDeduplicateSizeOnly(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
skipIfCantDedupe(t, r.Fremote)
|
||||
|
||||
file1 := r.WriteUncheckedObject(context.Background(), "one", "This is one", t1)
|
||||
file2 := r.WriteUncheckedObject(context.Background(), "one", "THIS IS ONE", t1)
|
||||
file3 := r.WriteUncheckedObject(context.Background(), "one", "This is another one", t1)
|
||||
r.CheckWithDuplicates(t, file1, file2, file3)
|
||||
|
||||
fs.Config.SizeOnly = true
|
||||
defer func() {
|
||||
fs.Config.SizeOnly = false
|
||||
}()
|
||||
|
||||
err := operations.Deduplicate(context.Background(), r.Fremote, operations.DeduplicateSkip)
|
||||
require.NoError(t, err)
|
||||
|
||||
r.CheckWithDuplicates(t, file1, file3)
|
||||
}
|
||||
|
||||
func TestDeduplicateFirst(t *testing.T) {
|
||||
r := fstest.NewRun(t)
|
||||
defer r.Finalise()
|
||||
|
||||
Reference in New Issue
Block a user