Skip to content

Commit 7b06224

Browse files
committed
initial commit
1 parent 8afcef3 commit 7b06224

18 files changed

Lines changed: 1657 additions & 0 deletions

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Universal Path Handling
2+
=======================
3+
4+
This library provides functions to handle either Windows or Unix style paths,
5+
regardless of the operating system the code is running on.
6+
7+
The code for this is copied from the excellent Golang Standard Library's `path/filepath` package (which, unfortunately,
8+
is restricted via build tags to the native OS path style). I do not claim any ownership of this code. The `patches`
9+
directory contains the modifications made to the original code to make it work here.
10+
11+
There are two subpackages:
12+
13+
- `windows` handles Windows style paths (e.g. `C:\Program Files\app\file.txt`)
14+
- `unix` handles Unix style paths (e.g. `/usr/local/bin/app/file.txt`)
15+
16+
The main package provides a `Style` type that can be set to either `Windows` or `Unix` and uses the appropriate subpackage
17+
to perform path operations.

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/secDre4mer/universalpath
2+
3+
go 1.20

patches/unix.diff

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
diff --git a/unix/path_lite.go b/unix/path_lite.go
2+
index 4a37298..c15d1ed 100644
3+
--- a/unix/path_lite.go
4+
+++ b/unix/path_lite.go
5+
@@ -2,17 +2,13 @@
6+
// Use of this source code is governed by a BSD-style
7+
// license that can be found in the LICENSE file.
8+
9+
-// Package filepathlite implements a subset of path/filepath,
10+
-// only using packages which may be imported by "os".
11+
-//
12+
-// Tests for these functions are in path/filepath.
13+
-package filepathlite
14+
+package unix
15+
16+
import (
17+
"errors"
18+
- "internal/stringslite"
19+
"io/fs"
20+
"slices"
21+
+ "strings"
22+
)
23+
24+
var errInvalidPath = errors.New("invalid path")
25+
@@ -149,7 +145,7 @@ func unixIsLocal(path string) bool {
26+
hasDots := false
27+
for p := path; p != ""; {
28+
var part string
29+
- part, p, _ = stringslite.Cut(p, "/")
30+
+ part, p, _ = strings.Cut(p, "/")
31+
if part == "." || part == ".." {
32+
hasDots = true
33+
break
34+
@@ -158,7 +154,7 @@ func unixIsLocal(path string) bool {
35+
if hasDots {
36+
path = Clean(path)
37+
}
38+
- if path == ".." || stringslite.HasPrefix(path, "../") {
39+
+ if path == ".." || strings.HasPrefix(path, "../") {
40+
return false
41+
}
42+
return true
43+
@@ -189,7 +185,7 @@ func FromSlash(path string) string {
44+
}
45+
46+
func replaceStringByte(s string, old, new byte) string {
47+
- if stringslite.IndexByte(s, old) == -1 {
48+
+ if strings.IndexByte(s, old) == -1 {
49+
return s
50+
}
51+
n := []byte(s)
52+
diff --git a/unix/path_lite_nonwindows.go b/unix/path_lite_nonwindows.go
53+
index c9c4c02..497acf3 100644
54+
--- a/unix/path_lite_nonwindows.go
55+
+++ b/unix/path_lite_nonwindows.go
56+
@@ -2,8 +2,6 @@
57+
// Use of this source code is governed by a BSD-style
58+
// license that can be found in the LICENSE file.
59+
60+
-//go:build !windows
61+
-
62+
-package filepathlite
63+
+package unix
64+
65+
func postClean(out *lazybuf) {}
66+
diff --git a/unix/path_lite_unix.go b/unix/path_lite_unix.go
67+
index e31f1ae..21244f3 100644
68+
--- a/unix/path_lite_unix.go
69+
+++ b/unix/path_lite_unix.go
70+
@@ -2,13 +2,10 @@
71+
// Use of this source code is governed by a BSD-style
72+
// license that can be found in the LICENSE file.
73+
74+
-//go:build unix || (js && wasm) || wasip1
75+
-
76+
-package filepathlite
77+
+package unix
78+
79+
import (
80+
- "internal/bytealg"
81+
- "internal/stringslite"
82+
+ "strings"
83+
)
84+
85+
const (
86+
@@ -25,7 +22,7 @@ func isLocal(path string) bool {
87+
}
88+
89+
func localize(path string) (string, error) {
90+
- if bytealg.IndexByteString(path, 0) >= 0 {
91+
+ if strings.IndexByte(path, 0) >= 0 {
92+
return "", errInvalidPath
93+
}
94+
return path, nil
95+
@@ -33,7 +30,7 @@ func localize(path string) (string, error) {
96+
97+
// IsAbs reports whether the path is absolute.
98+
func IsAbs(path string) bool {
99+
- return stringslite.HasPrefix(path, "/")
100+
+ return strings.HasPrefix(path, "/")
101+
}
102+
103+
// volumeNameLen returns length of the leading volume name on Windows.
104+
diff --git a/unix/path_unix.go b/unix/path_unix.go
105+
index 6bc974d..032945b 100644
106+
--- a/unix/path_unix.go
107+
+++ b/unix/path_unix.go
108+
@@ -2,34 +2,13 @@
109+
// Use of this source code is governed by a BSD-style
110+
// license that can be found in the LICENSE file.
111+
112+
-//go:build unix || (js && wasm) || wasip1
113+
-
114+
-package filepath
115+
+package unix
116+
117+
import (
118+
"strings"
119+
)
120+
121+
-// HasPrefix exists for historical compatibility and should not be used.
122+
-//
123+
-// Deprecated: HasPrefix does not respect path boundaries and
124+
-// does not ignore case when required.
125+
-func HasPrefix(p, prefix string) bool {
126+
- return strings.HasPrefix(p, prefix)
127+
-}
128+
-
129+
-func splitList(path string) []string {
130+
- if path == "" {
131+
- return []string{}
132+
- }
133+
- return strings.Split(path, string(ListSeparator))
134+
-}
135+
-
136+
-func abs(path string) (string, error) {
137+
- return unixAbs(path)
138+
-}
139+
-
140+
-func join(elem []string) string {
141+
+func Join(elem ...string) string {
142+
// If there's a bug here, fix the logic in ./path_plan9.go too.
143+
for i, e := range elem {
144+
if e != "" {
145+
@@ -38,7 +17,3 @@ func join(elem []string) string {
146+
}
147+
return ""
148+
}
149+
-
150+
-func sameWord(a, b string) bool {
151+
- return a == b
152+
-}

0 commit comments

Comments
 (0)