aboutsummaryrefslogtreecommitdiff
path: root/lib/memfs
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-07-16 23:32:22 +0700
committerShulhan <ms@kilabit.info>2022-07-16 23:32:22 +0700
commitca2ded2b40eeb69ba7dda3facb2b7742f13b36a8 (patch)
tree315c7dce820fd454e99502f397e82925ccce2ab2 /lib/memfs
parent6e43f4b1af113c6cf70539e2a92ae13d9e523cb9 (diff)
downloadpakakeh.go-ca2ded2b40eeb69ba7dda3facb2b7742f13b36a8.tar.xz
lib/memfs: set the Root SysPath to the first MemFS instance on Merge
Previously, calling Merge(...), set the merged MemFS Root.SysPath to "..". Since we allow the TryDirect to access the file directly (if its set to true), this may cause the file system leaks if returned MemFS set this flag to true. To prevent that, we set the SysPath to the first MemFS SysPath.
Diffstat (limited to 'lib/memfs')
-rw-r--r--lib/memfs/memfs.go14
-rw-r--r--lib/memfs/memfs_test.go4
2 files changed, 15 insertions, 3 deletions
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index c55311bd..c1fccea9 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -42,6 +42,9 @@ type MemFS struct {
// Merge one or more instances of MemFS into single hierarchy.
//
+// The returned MemFS instance will have SysPath set to the first
+// MemFS.SysPath in parameter.
+//
// If there are two instance of Node that have the same path, the last
// instance will be ignored.
func Merge(params ...*MemFS) (merged *MemFS) {
@@ -57,7 +60,16 @@ func Merge(params ...*MemFS) (merged *MemFS) {
merged.PathNodes.Set("/", merged.Root)
- for _, mfs := range params {
+ var (
+ x int
+ mfs *MemFS
+ )
+
+ for x, mfs = range params {
+ if x == 0 {
+ merged.Root.SysPath = mfs.Root.SysPath
+ }
+
for _, child := range mfs.Root.Childs {
gotNode := merged.PathNodes.Get(child.Path)
if gotNode != nil {
diff --git a/lib/memfs/memfs_test.go b/lib/memfs/memfs_test.go
index 22c71bf5..f61d2930 100644
--- a/lib/memfs/memfs_test.go
+++ b/lib/memfs/memfs_test.go
@@ -576,7 +576,7 @@ func TestMerge(t *testing.T) {
PathNodes: &PathNode{
v: map[string]*Node{
"/": &Node{
- SysPath: "..",
+ SysPath: mfsDirect.Root.SysPath,
Path: "/",
Childs: []*Node{
mfsDirect.MustGet("/add"),
@@ -596,7 +596,7 @@ func TestMerge(t *testing.T) {
PathNodes: &PathNode{
v: map[string]*Node{
"/": &Node{
- SysPath: "..",
+ SysPath: mfsDirect.Root.SysPath,
Path: "/",
Childs: []*Node{
mfsDirect.MustGet("/add"),