13 func downcaseFileExtension(filename string) string {
14 splitStr := strings.Split(filename, ".")
15 ext := strings.ToLower(
16 splitStr[len(splitStr)-1],
22 func fileTypeFromFilename(fn string) fileType {
23 ft, ok := fileTypes[downcaseFileExtension(fn)]
27 return defaultFileType
30 func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
32 ft.CreatorCode = "n/a "
35 ft = fileTypeFromFilename(info.Name())
41 func getFileNameList(filePath string) (fields []Field, err error) {
42 files, err := ioutil.ReadDir(filePath)
47 for _, file := range files {
48 var fnwi FileNameWithInfo
50 fileCreator := make([]byte, 4)
52 if file.Mode()&os.ModeSymlink != 0 {
53 resolvedPath, err := os.Readlink(filePath + "/" + file.Name())
58 rFile, err := os.Stat(filePath + "/" + resolvedPath)
59 if errors.Is(err, os.ErrNotExist) {
67 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
71 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
72 copy(fnwi.Type[:], []byte("fldr")[:])
73 copy(fnwi.Creator[:], fileCreator[:])
75 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
76 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:])
77 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:])
80 } else if file.IsDir() {
81 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
85 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
86 copy(fnwi.Type[:], []byte("fldr")[:])
87 copy(fnwi.Creator[:], fileCreator[:])
89 // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them
90 if file.Size() > 4294967296 {
93 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
94 copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
95 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
98 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
100 nameSize := make([]byte, 2)
101 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
102 copy(fnwi.NameSize[:], nameSize[:])
104 fnwi.name = []byte(strippedName)
106 b, err := fnwi.MarshalBinary()
110 fields = append(fields, NewField(fieldFileNameWithInfo, b))
116 func CalcTotalSize(filePath string) ([]byte, error) {
118 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
127 totalSize += uint32(info.Size())
135 bs := make([]byte, 4)
136 binary.BigEndian.PutUint32(bs, totalSize)
141 func CalcItemCount(filePath string) ([]byte, error) {
143 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
156 bs := make([]byte, 2)
157 binary.BigEndian.PutUint16(bs, itemcount-1)
162 func EncodeFilePath(filePath string) []byte {
163 pathSections := strings.Split(filePath, "/")
164 pathItemCount := make([]byte, 2)
165 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
167 bytes := pathItemCount
169 for _, section := range pathSections {
170 bytes = append(bytes, []byte{0, 0}...)
172 pathStr := []byte(section)
173 bytes = append(bytes, byte(len(pathStr)))
174 bytes = append(bytes, pathStr...)
180 const incompleteFileSuffix = ".incomplete"
182 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
183 func effectiveFile(filePath string) (*os.File, error) {
184 file, err := os.Open(filePath)
185 if err != nil && !errors.Is(err, fs.ErrNotExist) {
189 if errors.Is(err, fs.ErrNotExist) {
190 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)