Permanent fallback to copy on hardlink error

This commit is contained in:
2025-01-07 03:39:05 +03:00
parent df4f2ebd99
commit 62daa023f5
3 changed files with 28 additions and 6 deletions

View File

@@ -1,20 +1,36 @@
package modes
import (
"fmt"
"log"
"os"
"sync/atomic"
)
var hardLinkNotSupported = atomic.Bool{}
type HardLink struct {
}
func (h HardLink) PlaceIt(sourcePath, targetPath string, mode os.FileMode) error {
if hardLinkNotSupported.Load() {
if copyErr := (Copy{}).PlaceIt(sourcePath, targetPath, mode); copyErr != nil {
return copyErr
}
}
if err := os.Link(sourcePath, targetPath); err != nil {
if os.IsExist(err) {
return nil
}
return fmt.Errorf("create hard link: %w", err)
log.Println("Create hardlink failed:", err.Error())
hardLinkNotSupported.Store(true)
if copyErr := (Copy{}).PlaceIt(sourcePath, targetPath, mode); copyErr != nil {
return copyErr
}
return nil
}
return nil