-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix duplicated 8+3 filenames for long file name entries #71
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,10 @@ package fat32 | |
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"hash/fnv" | ||
"math/rand" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
@@ -382,7 +385,7 @@ func calculateSlots(s string) int { | |
// returns shortName, extension, isLFN, isTruncated | ||
// isLFN : was there an LFN that had to be converted | ||
// isTruncated : was the shortname longer than 8 chars and had to be converted? | ||
func convertLfnSfn(name string) (string, string, bool, bool) { | ||
func convertLfnSfn(name string, cache map[string]bool) (string, string, bool, bool) { | ||
isLFN, isTruncated := false, false | ||
// get last period in name | ||
lastDot := strings.LastIndex(name, ".") | ||
|
@@ -413,11 +416,47 @@ func convertLfnSfn(name string) (string, string, bool, bool) { | |
isLFN = true | ||
} | ||
|
||
truncate := func(name string, n int) string { | ||
return name[0:6] + "~" + strconv.Itoa(n) | ||
} | ||
|
||
hash := func(shortName string, n int) string { | ||
h := fnv.New32a() | ||
// the input name may have more information than the short name | ||
h.Write([]byte(name)) | ||
return shortName[0:2] + fmt.Sprintf("%04X", (h.Sum32()%0x10000)) + "~" + strconv.Itoa(n) | ||
} | ||
|
||
random := func() string { | ||
return fmt.Sprintf("%06X", (rand.Int63()%0x1000000)) + "~" + strconv.Itoa(1+rand.Intn(8)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// convert shortName to 8 chars | ||
if len(shortName) > 8 { | ||
isLFN = true | ||
isTruncated = true | ||
shortName = shortName[:6] + "~" + "1" | ||
if !cache[truncate(shortName, 9)+"."+extension] { | ||
i := 1 | ||
for i < 9 && cache[truncate(shortName, i)+"."+extension] { | ||
i++ | ||
} | ||
shortName = truncate((shortName), i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This calls Also, why is this: i := 1
for i < 9 && cache[truncate(shortName, i)+"."+extension] {
i++
} and not the simpler and more idiomatic: var found string
for i := i; i++; i < 9 {
found = truncate(shortName, i)+"."+extension
if _, ok := cache[found]; ok {
break
}
}
shortName = found or similar |
||
} else { | ||
i := 0 | ||
for i <= 9 && cache[hash(shortName, i)+"."+extension] { | ||
i++ | ||
} | ||
if i <= 9 { | ||
shortName = hash(shortName, i) | ||
} else { | ||
rnd := random() | ||
for i < 1000 && cache[rnd+"."+extension] { | ||
i++ | ||
rnd = random() | ||
} | ||
shortName = rnd | ||
} | ||
} | ||
} | ||
return shortName, extension, isLFN, isTruncated | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not madly in love with inline anonymous functions here, as they will get declared every time we call
convertLfnSfn
, and they are harder to understand and test.Can we pull these three out into named functions, and give them more representative names?