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 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
90 copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
91 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
94 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
96 nameSize := make([]byte, 2)
97 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
98 copy(fnwi.NameSize[:], nameSize[:])
100 fnwi.name = []byte(strippedName)
102 b, err := fnwi.MarshalBinary()
106 fields = append(fields, NewField(fieldFileNameWithInfo, b))
112 func CalcTotalSize(filePath string) ([]byte, error) {
114 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
123 totalSize += uint32(info.Size())
131 bs := make([]byte, 4)
132 binary.BigEndian.PutUint32(bs, totalSize)
137 func CalcItemCount(filePath string) ([]byte, error) {
139 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
152 bs := make([]byte, 2)
153 binary.BigEndian.PutUint16(bs, itemcount-1)
158 func EncodeFilePath(filePath string) []byte {
159 pathSections := strings.Split(filePath, "/")
160 pathItemCount := make([]byte, 2)
161 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
163 bytes := pathItemCount
165 for _, section := range pathSections {
166 bytes = append(bytes, []byte{0, 0}...)
168 pathStr := []byte(section)
169 bytes = append(bytes, byte(len(pathStr)))
170 bytes = append(bytes, pathStr...)
176 const incompleteFileSuffix = ".incomplete"
178 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
179 func effectiveFile(filePath string) (*os.File, error) {
180 file, err := os.Open(filePath)
181 if err != nil && !errors.Is(err, fs.ErrNotExist) {
185 if errors.Is(err, fs.ErrNotExist) {
186 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)