X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/11843c8da176b40093972e9037df3e04b077890b..d8e28ebc452ef2ebab2846451ce5589e028c5783:/hotline/files.go diff --git a/hotline/files.go b/hotline/files.go index 19375e9..478041b 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -2,6 +2,8 @@ package hotline import ( "encoding/binary" + "errors" + "io/fs" "io/ioutil" "os" "path/filepath" @@ -54,11 +56,13 @@ func getFileNameList(filePath string) (fields []Field, err error) { copy(fnwi.Creator[:], fileCreator[:]) } + strippedName := strings.Replace(file.Name(), ".incomplete", "", -1) + nameSize := make([]byte, 2) - binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name()))) + binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) copy(fnwi.NameSize[:], nameSize[:]) - fnwi.name = []byte(file.Name()) + fnwi.name = []byte(strippedName) b, err := fnwi.MarshalBinary() if err != nil { @@ -133,3 +137,21 @@ func EncodeFilePath(filePath string) []byte { return bytes } + +const incompleteFileSuffix = ".incomplete" + +// effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback +func effectiveFile(filePath string) (*os.File, error) { + file, err := os.Open(filePath) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return nil, err + } + + if errors.Is(err, fs.ErrNotExist) { + file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return nil, err + } + } + return file, nil +}