/usr/share/gocode/src/github.com/mitchellh/go-fs/fat/short_name.go is in golang-github-mitchellh-go-fs-dev 0.0~git20161108.7bae45d-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | package fat
import (
"bytes"
"fmt"
"strings"
)
// checksumShortName returns the checksum for the shortname that is used
// for the long name entries.
func checksumShortName(name string) uint8 {
var sum uint8 = name[0]
for i := uint8(1); i < 11; i++ {
sum = name[i] + (((sum & 1) << 7) + ((sum & 0xFE) >> 1))
}
return sum
}
// generateShortName takes a list of existing short names and a long
// name and generates the next valid short name. This process is done
// according to the MS specification.
func generateShortName(longName string, used []string) (string, error) {
longName = strings.ToUpper(longName)
// Split the string at the final "."
dotIdx := strings.LastIndex(longName, ".")
var ext string
if dotIdx == -1 {
dotIdx = len(longName)
} else {
ext = longName[dotIdx+1 : len(longName)]
}
ext = cleanShortString(ext)
ext = ext[0:len(ext)]
rawName := longName[0:dotIdx]
name := cleanShortString(rawName)
simpleName := fmt.Sprintf("%s.%s", name, ext)
if ext == "" {
simpleName = simpleName[0 : len(simpleName)-1]
}
doSuffix := name != rawName || len(name) > 8
if !doSuffix {
for _, usedSingle := range used {
if strings.ToUpper(usedSingle) == simpleName {
doSuffix = true
break
}
}
}
if doSuffix {
found := false
for i := 1; i < 99999; i++ {
serial := fmt.Sprintf("~%d", i)
nameOffset := 8 - len(serial)
if len(name) < nameOffset {
nameOffset = len(name)
}
serialName := fmt.Sprintf("%s%s", name[0:nameOffset], serial)
simpleName = fmt.Sprintf("%s.%s", serialName, ext)
exists := false
for _, usedSingle := range used {
if strings.ToUpper(usedSingle) == simpleName {
exists = true
break
}
}
if !exists {
found = true
break
}
}
if !found {
return "", fmt.Errorf("could not generate short name for %s", longName)
}
}
return simpleName, nil
}
// shortNameEntryValue returns the proper formatted short name value
// for the directory cluster entry.
func shortNameEntryValue(name string) string {
var shortParts []string
if name == "." || name == ".." {
shortParts = []string{name, ""}
} else {
shortParts = strings.Split(name, ".")
}
if len(shortParts) == 1 {
shortParts = append(shortParts, "")
}
if len(shortParts[0]) < 8 {
var temp bytes.Buffer
temp.WriteString(shortParts[0])
for i := 0; i < 8-len(shortParts[0]); i++ {
temp.WriteRune(' ')
}
shortParts[0] = temp.String()
}
if len(shortParts[1]) < 3 {
var temp bytes.Buffer
temp.WriteString(shortParts[1])
for i := 0; i < 3-len(shortParts[1]); i++ {
temp.WriteRune(' ')
}
shortParts[1] = temp.String()
}
return fmt.Sprintf("%s%s", shortParts[0], shortParts[1])
}
func cleanShortString(v string) string {
var result bytes.Buffer
for _, char := range v {
// We skip these chars
if char == '.' || char == ' ' {
continue
}
if !validShortChar(char) {
char = '_'
}
result.WriteRune(char)
}
return result.String()
}
func validShortChar(char rune) bool {
if char >= 'A' && char <= 'Z' {
return true
}
if char >= '0' && char <= '9' {
return true
}
validShortSymbols := []rune{
'_', '^', '$', '~', '!', '#', '%', '&', '-', '{', '}', '(',
')', '@', '\'', '`',
}
for _, valid := range validShortSymbols {
if char == valid {
return true
}
}
return false
}
|