Parcourir la source

create temporary file in the same folder with the export file

K8s doesn't allow to rename files from one volume to another. Keeping them in the same folder should avoid such issues.

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 il y a 3 ans
Parent
commit
d62a1792da
1 fichiers modifiés avec 9 ajouts et 2 suppressions
  1. 9 2
      pkg/filemanager/filemanager.go

+ 9 - 2
pkg/filemanager/filemanager.go

@@ -7,7 +7,9 @@ import (
 	"io"
 	"net/url"
 	"os"
+	"path/filepath"
 	"strings"
+	"time"
 
 	"cloud.google.com/go/storage"
 	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
@@ -202,11 +204,17 @@ func (s *SystemFile) Download(ctx context.Context, f *os.File) error {
 }
 
 func (s *SystemFile) Upload(ctx context.Context, f *os.File) error {
+	// we want to avoid truncating the file if the upload fails
+	// so want to write to a temp file and then rename it
+	// to the final destination
+	// temp file should be in the same directory as the final destination
+	// to avoid "invalid cross-device link" errors when attempting to rename the file
 	_, err := f.Seek(0, io.SeekStart)
 	if err != nil {
 		return err
 	}
-	tmpF, err := os.CreateTemp("", "opencost-upload-*")
+	tmpFilePath := filepath.Join(filepath.Dir(s.path), fmt.Sprintf(".tmp-%d", time.Now().UnixNano()))
+	tmpF, err := os.Create(tmpFilePath)
 	if err != nil {
 		return err
 	}
@@ -216,7 +224,6 @@ func (s *SystemFile) Upload(ctx context.Context, f *os.File) error {
 	if err != nil {
 		return err
 	}
-	// avoid file corruption on interrupt
 	err = os.Rename(tmpF.Name(), s.path)
 	if err != nil {
 		return err