13 const incompleteFileSuffix = ".incomplete"
15 func downcaseFileExtension(filename string) string {
16 splitStr := strings.Split(filename, ".")
17 ext := strings.ToLower(
18 splitStr[len(splitStr)-1],
24 func fileTypeFromFilename(fn string) fileType {
25 ft, ok := fileTypes[downcaseFileExtension(fn)]
29 return defaultFileType
32 func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
34 ft.CreatorCode = "n/a "
37 ft = fileTypeFromFilename(info.Name())
43 func getFileNameList(filePath string) (fields []Field, err error) {
44 files, err := ioutil.ReadDir(filePath)
49 for _, file := range files {
50 var fnwi FileNameWithInfo
52 fileCreator := make([]byte, 4)
54 if file.Mode()&os.ModeSymlink != 0 {
55 resolvedPath, err := os.Readlink(filePath + "/" + file.Name())
60 rFile, err := os.Stat(filePath + "/" + resolvedPath)
61 if errors.Is(err, os.ErrNotExist) {
69 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
73 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
74 copy(fnwi.Type[:], []byte("fldr")[:])
75 copy(fnwi.Creator[:], fileCreator[:])
77 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
78 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:])
79 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:])
82 } else if file.IsDir() {
83 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
87 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
88 copy(fnwi.Type[:], []byte("fldr")[:])
89 copy(fnwi.Creator[:], fileCreator[:])
91 // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them
92 if file.Size() > 4294967296 {
95 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
96 copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
97 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
100 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
102 nameSize := make([]byte, 2)
103 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
104 copy(fnwi.NameSize[:], nameSize[:])
106 fnwi.name = []byte(strippedName)
108 b, err := fnwi.MarshalBinary()
112 fields = append(fields, NewField(fieldFileNameWithInfo, b))
118 func CalcTotalSize(filePath string) ([]byte, error) {
120 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
129 totalSize += uint32(info.Size())
137 bs := make([]byte, 4)
138 binary.BigEndian.PutUint32(bs, totalSize)
143 func CalcItemCount(filePath string) ([]byte, error) {
145 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
158 bs := make([]byte, 2)
159 binary.BigEndian.PutUint16(bs, itemcount-1)
164 func EncodeFilePath(filePath string) []byte {
165 pathSections := strings.Split(filePath, "/")
166 pathItemCount := make([]byte, 2)
167 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
169 bytes := pathItemCount
171 for _, section := range pathSections {
172 bytes = append(bytes, []byte{0, 0}...)
174 pathStr := []byte(section)
175 bytes = append(bytes, byte(len(pathStr)))
176 bytes = append(bytes, pathStr...)
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)