github.com/Cloud-Foundations/Dominator@v0.3.4/lib/objectcache/filename.go (about) 1 package objectcache 2 3 import ( 4 "fmt" 5 6 "github.com/Cloud-Foundations/Dominator/lib/hash" 7 ) 8 9 func filenameToHash(fileName string) (hash.Hash, error) { 10 var hashVal hash.Hash 11 var prev_nibble byte = 16 12 index := 0 13 for _, char := range fileName { 14 var nibble byte 15 if char >= '0' && char <= '9' { 16 nibble = byte(char) - '0' 17 } else if char >= 'a' && char <= 'f' { 18 nibble = byte(char) - 'a' + 10 19 } else { 20 continue // Ignore everything else. Treat them as separators. 21 } 22 if prev_nibble < 16 { 23 if index >= len(hashVal) { 24 return hashVal, fmt.Errorf("filename too long: %s", fileName) 25 } 26 hashVal[index] = nibble | prev_nibble<<4 27 index++ 28 prev_nibble = 16 29 } else { 30 prev_nibble = nibble 31 } 32 } 33 return hashVal, nil 34 } 35 36 func hashToFilename(hashVal hash.Hash) string { 37 return fmt.Sprintf("%02x/%02x/%0x", hashVal[0], hashVal[1], hashVal[2:]) 38 }