-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabs.go
More file actions
66 lines (58 loc) · 1.9 KB
/
Copy pathabs.go
File metadata and controls
66 lines (58 loc) · 1.9 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
package fs
import (
"context"
"errors"
"lesiw.io/fs/path"
)
// An AbsFS is a file system with the Abs method.
type AbsFS interface {
FS
// Abs returns an absolute representation of path.
// If the path is not absolute, it will be joined with the filesystem's
// root and any working directory from ctx to produce an absolute path.
//
// The returned path format depends on the filesystem implementation:
// local filesystems return OS paths, remote filesystems may return URLs.
Abs(ctx context.Context, name string) (string, error)
}
// Abs returns an absolute representation of path within the filesystem.
//
// If the path is already absolute, Abs returns it cleaned.
// If the path is relative, Abs attempts to resolve it to an absolute path.
//
// The returned path format depends on the filesystem implementation.
// Local filesystems return OS-specific absolute paths (e.g., /home/user/file
// or C:\Users\file). Remote filesystems may return URLs (e.g.,
// s3://bucket/key or https://server/path).
//
// # Files
//
// Returns an absolute representation of the file path.
//
// Requires: [AbsFS] || (absolute [WorkDir] in ctx)
//
// # Directories
//
// Returns an absolute representation of the directory path.
//
// Requires: [AbsFS] || (absolute [WorkDir] in ctx)
//
// Similar capabilities: [path/filepath.Abs], realpath, pwd.
func Abs(ctx context.Context, fsys FS, name string) (string, error) {
// Try native capability first
if afs, ok := fsys.(AbsFS); ok {
abs, err := afs.Abs(ctx, name)
if !errors.Is(err, ErrUnsupported) {
return abs, err
}
}
// If path is already absolute, return it cleaned
if path.IsAbs(name) {
return path.Clean(name), nil
}
// Fallback: if WorkDir is absolute and path is relative, compute it
if workDir := WorkDir(ctx); workDir != "" && path.IsAbs(workDir) {
return path.Join(workDir, name), nil
}
return "", &PathError{Op: "abs", Path: name, Err: ErrUnsupported}
}