-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymlink.go
More file actions
48 lines (41 loc) · 1.19 KB
/
Copy pathsymlink.go
File metadata and controls
48 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"os"
"github.com/limero/offlinerss/domain"
"github.com/limero/offlinerss/log"
"github.com/limero/offlinerss/util"
)
func symlinkClientPaths(clients domain.Clients) error {
for _, client := range clients {
for _, file := range client.GetFiles() {
dataPath := client.GetDataPath()
filePath := dataPath.GetFile(file.FileName)
for _, targetPath := range file.TargetPaths {
dest, _ := os.Readlink(targetPath)
if dest != "" {
if dest == filePath {
log.Debug("Symlink from %q to %q is already correct", filePath, targetPath)
continue
}
log.Warn("Removing incorrect symlink at %q", targetPath)
if err := os.Remove(targetPath); err != nil {
return err
}
} else if util.FileExists(targetPath) {
log.Warn("Non-symlink found at target %q, renaming to .bak", targetPath)
if err := os.Rename(targetPath, targetPath+".bak"); err != nil {
return err
}
}
if err := util.CreateParentDirs(targetPath); err != nil {
return err
}
log.Info("Symlinking %q to %q", filePath, targetPath)
if err := os.Symlink(filePath, targetPath); err != nil {
return err
}
}
}
}
return nil
}