-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymlink.go
More file actions
85 lines (77 loc) · 2.47 KB
/
Copy pathsymlink.go
File metadata and controls
85 lines (77 loc) · 2.47 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package fs
import "context"
// A SymlinkFS is a file system with the Symlink method.
type SymlinkFS interface {
FS
// Symlink creates newname as a symbolic link to oldname.
Symlink(ctx context.Context, oldname, newname string) error
}
// A ReadLinkFS is a file system with the ReadLink and Lstat methods.
type ReadLinkFS interface {
FS
// ReadLink returns the destination of the named symbolic link.
// If the link destination is relative, ReadLink returns the relative
// path without resolving it to an absolute one.
ReadLink(ctx context.Context, name string) (string, error)
// Lstat returns FileInfo describing the named file.
// If the file is a symbolic link, the returned FileInfo
// describes the symbolic link. Lstat makes no attempt to follow
// the link.
Lstat(ctx context.Context, name string) (FileInfo, error)
}
// Symlink creates newname as a symbolic link to oldname.
// Analogous to: [os.Symlink], ln -s, 9P2000.u Tsymlink.
//
// Requires: [SymlinkFS]
func Symlink(ctx context.Context, fsys FS, oldname, newname string) (err error) {
if oldname, err = localizePath(ctx, fsys, oldname); err != nil {
return err
}
if newname, err = localizePath(ctx, fsys, newname); err != nil {
return err
}
if sfs, ok := fsys.(SymlinkFS); ok {
return sfs.Symlink(ctx, oldname, newname)
}
return &PathError{
Op: "symlink",
Path: newname,
Err: ErrUnsupported,
}
}
// ReadLink returns the destination of the named symbolic link.
// Analogous to: [os.Readlink], readlink, 9P2000.u Treadlink.
// If the link destination is relative, ReadLink returns the relative path
// without resolving it to an absolute one.
//
// Requires: [ReadLinkFS]
func ReadLink(ctx context.Context, fsys FS, name string) (string, error) {
name, err := localizePath(ctx, fsys, name)
if err != nil {
return "", err
}
if rfs, ok := fsys.(ReadLinkFS); ok {
return rfs.ReadLink(ctx, name)
}
return "", &PathError{
Op: "readlink",
Path: name,
Err: ErrUnsupported,
}
}
// Lstat returns FileInfo describing the named file.
// Analogous to: [os.Lstat], stat (without -L).
// If the file is a symbolic link, the returned FileInfo describes the
// symbolic link. Lstat makes no attempt to follow the link.
//
// Requires: [ReadLinkFS] || [StatFS]
func Lstat(ctx context.Context, fsys FS, name string) (FileInfo, error) {
name, err := localizePath(ctx, fsys, name)
if err != nil {
return nil, err
}
if rfs, ok := fsys.(ReadLinkFS); ok {
return rfs.Lstat(ctx, name)
}
return Stat(ctx, fsys, name)
}