]> git.r.bdr.sh - rbdr/mobius/commitdiff
Fix overwrite of file during upload
authorJeff Halter <redacted>
Mon, 6 Jun 2022 23:45:27 +0000 (16:45 -0700)
committerJeff Halter <redacted>
Mon, 6 Jun 2022 23:45:27 +0000 (16:45 -0700)
hotline/files.go
hotline/server.go

index 04334e951410cb06d62a9ee4c92349afa94dfbd1..42e25db2aaec634fc3b905b081600f8c3f3f6504 100644 (file)
@@ -180,7 +180,7 @@ func effectiveFile(filePath string) (*os.File, error) {
        }
 
        if errors.Is(err, fs.ErrNotExist) {
-               file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
+               file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
                if err != nil {
                        return nil, err
                }
index cfac442c089822854b09fd2c4b816a6f5d2b2d4a..f164df1c8ff9e2b760f390f3242e8d45baa19246 100644 (file)
@@ -742,11 +742,27 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                }
        case FileUpload:
                destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName)
-               tmpFile := destinationFile + ".incomplete"
 
-               file, err := effectiveFile(destinationFile)
+               var file *os.File
+
+               // A file upload has three possible cases:
+               // 1) Upload a new file
+               // 2) Resume a partially transferred file
+               // 3) Replace a fully uploaded file
+               // Unfortunately we have to infer which case applies by inspecting what is already on the file system
+
+               // 1) Check for existing file:
+               _, err := os.Stat(destinationFile)
+               if err == nil {
+                       // If found, that means this upload is intended to replace the file
+                       if err = os.Remove(destinationFile); err != nil {
+                               return err
+                       }
+                       file, err = os.Create(destinationFile + incompleteFileSuffix)
+               }
                if errors.Is(err, fs.ErrNotExist) {
-                       file, err = FS.Create(tmpFile)
+                       // If not found, open or create a new incomplete file
+                       file, err = os.OpenFile(destinationFile+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
                        if err != nil {
                                return err
                        }