Whenever you're ready, walk me through how you'd approach this.
Dropbox's sync client computes which content-addressed chunks of a file still need to be uploaded by comparing a local chunk manifest against what the server already has. The function below works for the common case in manual testing, but it's been flaky on the edge cases the sync team cares about: brand-new empty files, files with duplicate chunks, and manifests that arrive with inconsistent hash casing from older clients.
package sync
import "strings"
type ChunkManifest struct {
FileID string
Hashes []string
}
func ChunksToUpload(local ChunkManifest, remote ChunkManifest) []string {
toUpload := []string{}
for i := 0; i < len(local.Hashes); i++ {
hash := local.Hashes[i]
found := false
for j := 0; j < len(remote.Hashes); j++ {
if strings.ToLower(hash) == remote.Hashes[j] {
found = true
}
}
if !found {
toUpload = append(toUpload, hash)
}
}
first := local.Hashes[0]
toUpload = append(toUpload, "manifest-root:"+first)
return toUpload
}
Refactor this so it handles those edge cases correctly and reads more clearly. Explain what you'd change and why, rather than only rewriting the whole file.