Skip to content

Commit 30cccb3

Browse files
committed
feat: add file copy skip with verification
Signed-off-by: ashergaga <1214443299@qq.com>
1 parent ca3624d commit 30cccb3

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

internal/server/server.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import (
4040
"k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
4141
"io"
4242
"path/filepath"
43+
"crypto/sha256"
44+
"encoding/hex"
4345
)
4446

4547
const (
@@ -91,6 +93,20 @@ func NewPluginServer(mgr *manager.AscendManager, nodeName string, checkIdleVNPUI
9193
}, nil
9294
}
9395

96+
// fileSHA256 calculates the SHA256 checksum of the specified file
97+
func fileSHA256(path string) (string, error) {
98+
f, err := os.Open(path)
99+
if err != nil {
100+
return "", err
101+
}
102+
defer f.Close()
103+
104+
h := sha256.New()
105+
if _, err := io.Copy(h, f); err != nil {
106+
return "", err
107+
}
108+
return hex.EncodeToString(h.Sum(nil)), nil
109+
}
94110

95111
// Automatically creates directories, sets permissions, and copies core files on the host
96112
func prepareHostResources() error {
@@ -129,10 +145,26 @@ func prepareHostResources() error {
129145

130146
for srcName, destPath := range filesToCopy {
131147
srcPath := filepath.Join(assetsDir, srcName)
148+
149+
// File already exists, skip if content is consistent
150+
if _, err := os.Stat(destPath); err == nil {
151+
srcSum, err1 := fileSHA256(srcPath)
152+
dstSum, err2 := fileSHA256(destPath)
153+
154+
if err1 == nil && err2 == nil && srcSum == dstSum {
155+
klog.Infof("✓ %s already up-to-date, skipping", destPath)
156+
continue
157+
}
158+
}
159+
132160
if err := copyFile(srcPath, destPath); err != nil {
133-
return fmt.Errorf("failed to copy %s to %s: %v", srcPath, destPath, err)
161+
if strings.Contains(err.Error(), "text file busy") {
162+
klog.Warningf("⚠ %s is in use by running process, keeping existing version (safe)", destPath)
163+
continue
164+
}
165+
return fmt.Errorf("failed to copy %s: %v", destPath, err)
134166
}
135-
klog.Infof("Copied %s -> %s", srcPath, destPath)
167+
klog.Infof("Copied %s -> %s", srcPath, destPath)
136168
}
137169

138170
klog.Info("Host resource preparation completed successfully.")

0 commit comments

Comments
 (0)