-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalize.go
More file actions
69 lines (63 loc) · 1.93 KB
/
Copy pathlocalize.go
File metadata and controls
69 lines (63 loc) · 1.93 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
package fs
import (
"context"
"errors"
"lesiw.io/fs/path"
)
// A LocalizeFS is a filesystem that can convert Unix-style paths
// to native paths.
//
// In most circumstances, this should be a lexical operation.
// For true path canonicalization, see [AbsFS].
//
// Operations like Open, Create, Append, and Truncate automatically call
// Localize internally when the filesystem implements LocalizeFS. The localized
// path is returned via the Path() method on the returned [ReadPathCloser] or
// [WritePathCloser].
type LocalizeFS interface {
FS
// Localize converts a Unix-style path to a native representation.
//
// Localize must be idempotent: calling Localize on an already-localized
// path should return the same path. That is, Localize(Localize(p))
// should equal Localize(p).
Localize(ctx context.Context, path string) (string, error)
}
// Localize converts a path from Unix-style to native.
//
// This is typically a lexical operation.
// For canonical path representation, use [Abs].
//
// Localize may be called with an already-localized path and should return the
// same path unchanged (idempotent behavior).
//
// Requires: [LocalizeFS]
func Localize(ctx context.Context, fsys FS, path string) (string, error) {
lfs, ok := fsys.(LocalizeFS)
if !ok {
return "", &PathError{
Op: "localize",
Path: path,
Err: ErrUnsupported,
}
}
return lfs.Localize(ctx, path)
}
// localizePath is an internal helper that cleans and localizes a path.
// It always returns a valid path: if localization is unsupported or fails
// with ErrUnsupported, it returns the cleaned path. Other errors are returned.
func localizePath(ctx context.Context, fsys FS, name string) (string, error) {
name = path.Clean(name)
lfs, ok := fsys.(LocalizeFS)
if !ok {
return name, nil
}
local, err := lfs.Localize(ctx, name)
if err != nil && !errors.Is(err, ErrUnsupported) {
return "", err
}
if err == nil {
return local, nil
}
return name, nil
}