Skip to content

Commit 2a41e76

Browse files
lmarsobscuren
authored andcommitted
swarm/api: Fix adding paths which exist as manifests (#14482)
Signed-off-by: Lewis Marshall <[email protected]>
1 parent 4a2c17b commit 2a41e76

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

swarm/api/manifest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ func (self *manifestTrie) addEntry(entry *manifestTrieEntry, quitC chan bool) {
237237
}
238238

239239
b := byte(entry.Path[0])
240-
if (self.entries[b] == nil) || (self.entries[b].Path == entry.Path) {
240+
oldentry := self.entries[b]
241+
if (oldentry == nil) || (oldentry.Path == entry.Path && oldentry.ContentType != ManifestType) {
241242
self.entries[b] = entry
242243
return
243244
}
244245

245-
oldentry := self.entries[b]
246246
cpl := 0
247247
for (len(entry.Path) > cpl) && (len(oldentry.Path) > cpl) && (entry.Path[cpl] == oldentry.Path[cpl]) {
248248
cpl++

swarm/api/manifest_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package api
1818

1919
import (
2020
// "encoding/json"
21+
"bytes"
22+
"encoding/json"
2123
"fmt"
2224
"io"
2325
"strings"
@@ -78,3 +80,34 @@ func TestGetEntry(t *testing.T) {
7880
func TestDeleteEntry(t *testing.T) {
7981

8082
}
83+
84+
// TestAddFileWithManifestPath tests that adding an entry at a path which
85+
// already exists as a manifest just adds the entry to the manifest rather
86+
// than replacing the manifest with the entry
87+
func TestAddFileWithManifestPath(t *testing.T) {
88+
// create a manifest containing "ab" and "ac"
89+
manifest, _ := json.Marshal(&Manifest{
90+
Entries: []ManifestEntry{
91+
{Path: "ab", Hash: "ab"},
92+
{Path: "ac", Hash: "ac"},
93+
},
94+
})
95+
reader := &storage.LazyTestSectionReader{
96+
SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
97+
}
98+
trie, err := readManifest(reader, nil, nil, nil)
99+
if err != nil {
100+
t.Fatal(err)
101+
}
102+
checkEntry(t, "ab", "ab", trie)
103+
checkEntry(t, "ac", "ac", trie)
104+
105+
// now add path "a" and check we can still get "ab" and "ac"
106+
entry := &manifestTrieEntry{}
107+
entry.Path = "a"
108+
entry.Hash = "a"
109+
trie.addEntry(entry, nil)
110+
checkEntry(t, "ab", "ab", trie)
111+
checkEntry(t, "ac", "ac", trie)
112+
checkEntry(t, "a", "a", trie)
113+
}

0 commit comments

Comments
 (0)