Skip to content

Commit

Permalink
Move ReplaceFile function to util package from driver package (#…
Browse files Browse the repository at this point in the history
…335)

Splitted out of
#328.

---

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.

Signed-off-by: Burak Varlı <[email protected]>
  • Loading branch information
unexge authored Jan 14, 2025
1 parent 27c32c5 commit 5c67b96
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
4 changes: 2 additions & 2 deletions cmd/install-mp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"os"
"path/filepath"

"github.com/awslabs/aws-s3-csi-driver/pkg/driver"
"github.com/awslabs/aws-s3-csi-driver/pkg/util"
)

const (
Expand Down Expand Up @@ -49,7 +49,7 @@ func installFiles(binDir string, installDir string) error {
destFile := filepath.Join(installDir, name)

// First copy to a temporary location then rename to handle replacing running binaries
err = driver.ReplaceFile(destFile, filepath.Join(binDir, name), 0755)
err = util.ReplaceFile(destFile, filepath.Join(binDir, name), 0755)
if err != nil {
return fmt.Errorf("Failed to copy file %s: %w", name, err)
}
Expand Down
36 changes: 2 additions & 34 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ package driver
import (
"context"
"fmt"
"io"
"io/fs"
"net"
"os"
"time"

"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node"
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/node/mounter"
"github.com/awslabs/aws-s3-csi-driver/pkg/driver/version"
"github.com/awslabs/aws-s3-csi-driver/pkg/util"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -154,7 +153,7 @@ func (d *Driver) Stop() {
func tokenFileTender(ctx context.Context, sourcePath string, destPath string) {
for {
timer := time.After(10 * time.Second)
err := ReplaceFile(destPath, sourcePath, 0600)
err := util.ReplaceFile(destPath, sourcePath, 0600)
if err != nil {
klog.Infof("Failed to sync AWS web token file: %v", err)
}
Expand All @@ -167,37 +166,6 @@ func tokenFileTender(ctx context.Context, sourcePath string, destPath string) {
}
}

// replaceFile safely replaces a file with a new file by copying to a temporary location first
// then renaming.
func ReplaceFile(destPath string, sourcePath string, perm fs.FileMode) error {
tmpDest := destPath + ".tmp"

sourceFile, err := os.Open(sourcePath)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.OpenFile(tmpDest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
defer destFile.Close()

buf := make([]byte, 64*1024)
_, err = io.CopyBuffer(destFile, sourceFile, buf)
if err != nil {
return err
}

err = os.Rename(tmpDest, destPath)
if err != nil {
return fmt.Errorf("Failed to rename file %s: %w", destPath, err)
}

return nil
}

func kubernetesVersion(clientset *kubernetes.Clientset) (string, error) {
version, err := clientset.ServerVersion()
if err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/util/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package util

import (
"fmt"
"io"
"io/fs"
"os"
)

// ReplaceFile safely replaces a file with a new file by copying to a temporary location first
// then renaming.
func ReplaceFile(destPath string, sourcePath string, perm fs.FileMode) error {
tmpDest := destPath + ".tmp"

sourceFile, err := os.Open(sourcePath)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.OpenFile(tmpDest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
defer destFile.Close()

buf := make([]byte, 64*1024)
_, err = io.CopyBuffer(destFile, sourceFile, buf)
if err != nil {
return err
}

err = os.Rename(tmpDest, destPath)
if err != nil {
return fmt.Errorf("Failed to rename file %s: %w", destPath, err)
}

return nil
}

0 comments on commit 5c67b96

Please sign in to comment.