diff --git a/swarm/api/api.go b/swarm/api/api.go index 57b6300cf7..07c29a29d2 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -379,7 +379,7 @@ func (self *Api) Modify(key storage.Key, path, contentHash, contentType string) apiModifyFail.Inc(1) return nil, err } - return trie.hash, nil + return trie.ref, nil } func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver bool) (storage.Key, string, error) { diff --git a/swarm/api/filesystem.go b/swarm/api/filesystem.go index e816a17c92..cd682e507d 100644 --- a/swarm/api/filesystem.go +++ b/swarm/api/filesystem.go @@ -164,7 +164,7 @@ func (self *FileSystem) Upload(lpath, index string, toEncrypt bool) (string, err err2 := trie.recalcAndStore() var hs string if err2 == nil { - hs = trie.hash.Hex() + hs = trie.ref.Hex() } awg.Wait() return hs, err2 diff --git a/swarm/api/http/server.go b/swarm/api/http/server.go index 6b7e44437e..73dd0ca13f 100644 --- a/swarm/api/http/server.go +++ b/swarm/api/http/server.go @@ -177,8 +177,13 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *Request) { return } + toEncrypt := false + if r.uri.Addr == "encrypt" { + toEncrypt = true + } + var key storage.Key - if r.uri.Addr != "" { + if r.uri.Addr != "" && r.uri.Addr != "encrypt" { key, err = s.api.Resolve(r.uri) if err != nil { postFilesFail.Inc(1) @@ -187,7 +192,7 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *Request) { } log.Debug("resolved key", "ruid", r.ruid, "key", key) } else { - key, err = s.api.NewManifest(false) + key, err = s.api.NewManifest(toEncrypt) if err != nil { postFilesFail.Inc(1) Respond(w, r, err.Error(), http.StatusInternalServerError) @@ -376,7 +381,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *Request) { Respond(w, r, err2.Error(), code) return } - m, err := s.api.NewResourceManifest(r.uri.Addr, false) + m, err := s.api.NewResourceManifest(r.uri.Addr) if err != nil { Respond(w, r, fmt.Sprintf("failed to create resource manifest: %v", err), http.StatusInternalServerError) return diff --git a/swarm/api/http/server_test.go b/swarm/api/http/server_test.go index b94847f82d..5f87ab6537 100644 --- a/swarm/api/http/server_test.go +++ b/swarm/api/http/server_test.go @@ -218,8 +218,8 @@ func TestBzzResource(t *testing.T) { } func TestBzzGetPath(t *testing.T) { - // testBzzGetPath(false, t) - testBzzGetPath(true, t) + testBzzGetPath(false, t) + // testBzzGetPath(true, t) } func testBzzGetPath(encrypted bool, t *testing.T) { diff --git a/swarm/api/manifest.go b/swarm/api/manifest.go index 6047066a64..4b3d72909c 100644 --- a/swarm/api/manifest.go +++ b/swarm/api/manifest.go @@ -72,7 +72,7 @@ func (a *Api) NewManifest(toEncrypt bool) (storage.Key, error) { // Manifest hack for supporting Mutable Resource Updates from the bzz: scheme // see swarm/api/api.go:Api.Get() for more information -func (a *Api) NewResourceManifest(resourceKey string, toEncrypt bool) (storage.Key, error) { +func (a *Api) NewResourceManifest(resourceKey string) (storage.Key, error) { var manifest Manifest entry := ManifestEntry{ Hash: resourceKey, @@ -83,7 +83,7 @@ func (a *Api) NewResourceManifest(resourceKey string, toEncrypt bool) (storage.K if err != nil { return nil, err } - key, _, err := a.Store(bytes.NewReader(data), int64(len(data)), toEncrypt) + key, _, err := a.Store(bytes.NewReader(data), int64(len(data)), false) return key, err } @@ -105,9 +105,7 @@ func (a *Api) NewManifestWriter(key storage.Key, quitC chan bool) (*ManifestWrit // AddEntry stores the given data and adds the resulting key to the manifest func (m *ManifestWriter) AddEntry(data io.Reader, e *ManifestEntry) (storage.Key, error) { - toEncrypt := (len(m.trie.hash) > m.trie.dpa.HashSize()) - - key, _, err := m.api.Store(data, e.Size, toEncrypt) + key, _, err := m.api.Store(data, e.Size, m.trie.encrypted) if err != nil { return nil, err } @@ -125,7 +123,7 @@ func (m *ManifestWriter) RemoveEntry(path string) error { // Store stores the manifest, returning the resulting storage key func (m *ManifestWriter) Store() (storage.Key, error) { - return m.trie.hash, m.trie.recalcAndStore() + return m.trie.ref, m.trie.recalcAndStore() } // ManifestWalker is used to recursively walk the entries in the manifest and @@ -185,9 +183,10 @@ func (m *ManifestWalker) walk(trie *manifestTrie, prefix string, walkFn WalkFn) } type manifestTrie struct { - dpa *storage.DPA - entries [257]*manifestTrieEntry // indexed by first character of basePath, entries[256] is the empty basePath entry - hash storage.Key // if hash != nil, it is stored + dpa *storage.DPA + entries [257]*manifestTrieEntry // indexed by first character of basePath, entries[256] is the empty basePath entry + ref storage.Key // if ref != nil, it is stored + encrypted bool } func newManifestTrieEntry(entry *ManifestEntry, subtrie *manifestTrie) *manifestTrieEntry { @@ -231,7 +230,7 @@ func readManifest(manifestReader storage.LazySectionReader, hash storage.Key, dp return } - log.Trace("manifest retrieved", "key", hash) + log.Debug("manifest retrieved", "key", hash) var man struct { Entries []*manifestTrieEntry `json:"entries"` } @@ -245,7 +244,8 @@ func readManifest(manifestReader storage.LazySectionReader, hash storage.Key, dp log.Trace("manifest entries", "key", hash, "len", len(man.Entries)) trie = &manifestTrie{ - dpa: dpa, + dpa: dpa, + encrypted: (len(hash) > dpa.HashSize()), } for _, entry := range man.Entries { trie.addEntry(entry, quitC) @@ -254,7 +254,7 @@ func readManifest(manifestReader storage.LazySectionReader, hash storage.Key, dp } func (self *manifestTrie) addEntry(entry *manifestTrieEntry, quitC chan bool) { - self.hash = nil // trie modified, hash needs to be re-calculated on demand + self.ref = nil // trie modified, hash needs to be re-calculated on demand if len(entry.Path) == 0 { self.entries[256] = entry @@ -286,7 +286,8 @@ func (self *manifestTrie) addEntry(entry *manifestTrieEntry, quitC chan bool) { commonPrefix := entry.Path[:cpl] subtrie := &manifestTrie{ - dpa: self.dpa, + dpa: self.dpa, + encrypted: self.encrypted, } entry.Path = entry.Path[cpl:] oldentry.Path = oldentry.Path[cpl:] @@ -310,7 +311,7 @@ func (self *manifestTrie) getCountLast() (cnt int, entry *manifestTrieEntry) { } func (self *manifestTrie) deleteEntry(path string, quitC chan bool) { - self.hash = nil // trie modified, hash needs to be re-calculated on demand + self.ref = nil // trie modified, hash needs to be re-calculated on demand if len(path) == 0 { self.entries[256] = nil @@ -346,7 +347,7 @@ func (self *manifestTrie) deleteEntry(path string, quitC chan bool) { } func (self *manifestTrie) recalcAndStore() error { - if self.hash != nil { + if self.ref != nil { return nil } @@ -361,7 +362,7 @@ func (self *manifestTrie) recalcAndStore() error { if err != nil { return err } - entry.Hash = entry.subtrie.hash.Hex() + entry.Hash = entry.subtrie.ref.Hex() } list.Entries = append(list.Entries, entry.ManifestEntry) } @@ -374,9 +375,9 @@ func (self *manifestTrie) recalcAndStore() error { } sr := bytes.NewReader(manifest) - key, wait, err2 := self.dpa.Store(sr, int64(len(manifest)), false) + key, wait, err2 := self.dpa.Store(sr, int64(len(manifest)), self.encrypted) wait() - self.hash = key + self.ref = key return err2 } diff --git a/swarm/api/manifest_test.go b/swarm/api/manifest_test.go index 7098ca16fd..27bf32007a 100644 --- a/swarm/api/manifest_test.go +++ b/swarm/api/manifest_test.go @@ -42,7 +42,9 @@ func manifest(paths ...string) (manifestReader storage.LazySectionReader) { func testGetEntry(t *testing.T, path, match string, multiple bool, paths ...string) *manifestTrie { quitC := make(chan bool) - trie, err := readManifest(manifest(paths...), nil, nil, quitC) + dpa := storage.NewDPA(nil, storage.NewDPAParams()) + ref := make([]byte, dpa.HashSize()) + trie, err := readManifest(manifest(paths...), ref, dpa, quitC) if err != nil { t.Errorf("unexpected error making manifest: %v", err) } @@ -97,7 +99,9 @@ func TestGetEntry(t *testing.T) { func TestExactMatch(t *testing.T) { quitC := make(chan bool) mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map") - trie, err := readManifest(mf, nil, nil, quitC) + dpa := storage.NewDPA(nil, storage.NewDPAParams()) + ref := make([]byte, dpa.HashSize()) + trie, err := readManifest(mf, ref, dpa, quitC) if err != nil { t.Errorf("unexpected error making manifest: %v", err) } @@ -128,7 +132,9 @@ func TestAddFileWithManifestPath(t *testing.T) { reader := &storage.LazyTestSectionReader{ SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))), } - trie, err := readManifest(reader, nil, nil, nil) + dpa := storage.NewDPA(nil, storage.NewDPAParams()) + ref := make([]byte, dpa.HashSize()) + trie, err := readManifest(reader, ref, dpa, nil) if err != nil { t.Fatal(err) }