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)
64 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
68 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
69 copy(fnwi.Type[:], []byte("fldr")[:])
70 copy(fnwi.Creator[:], fileCreator[:])
72 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
73 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:])
74 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:])
77 } else if file.IsDir() {
78 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
82 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
83 copy(fnwi.Type[:], []byte("fldr")[:])
84 copy(fnwi.Creator[:], fileCreator[:])
86 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
87 copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
88 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
91 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
93 nameSize := make([]byte, 2)
94 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
95 copy(fnwi.NameSize[:], nameSize[:])
97 fnwi.name = []byte(strippedName)
99 b, err := fnwi.MarshalBinary()
103 fields = append(fields, NewField(fieldFileNameWithInfo, b))
109 func CalcTotalSize(filePath string) ([]byte, error) {
111 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
120 totalSize += uint32(info.Size())
128 bs := make([]byte, 4)
129 binary.BigEndian.PutUint32(bs, totalSize)
134 func CalcItemCount(filePath string) ([]byte, error) {
136 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
149 bs := make([]byte, 2)
150 binary.BigEndian.PutUint16(bs, itemcount-1)
155 func EncodeFilePath(filePath string) []byte {
156 pathSections := strings.Split(filePath, "/")
157 pathItemCount := make([]byte, 2)
158 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
160 bytes := pathItemCount
162 for _, section := range pathSections {
163 bytes = append(bytes, []byte{0, 0}...)
165 pathStr := []byte(section)
166 bytes = append(bytes, byte(len(pathStr)))
167 bytes = append(bytes, pathStr...)
173 const incompleteFileSuffix = ".incomplete"
175 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
176 func effectiveFile(filePath string) (*os.File, error) {
177 file, err := os.Open(filePath)
178 if err != nil && !errors.Is(err, fs.ErrNotExist) {
182 if errors.Is(err, fs.ErrNotExist) {
183 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)