| 
									
										
										
										
											2017-04-14 10:29:00 +02:00
										 |  |  | // Copyright 2017 The go-ethereum Authors | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | // This file is part of go-ethereum. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // go-ethereum is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // go-ethereum is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Command  MANIFEST update | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	"mime" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 	"github.com/ethereum/go-ethereum/cmd/utils" | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/swarm/api" | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	swarm "github.com/ethereum/go-ethereum/swarm/api/client" | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	"gopkg.in/urfave/cli.v1" | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 10:22:26 +00:00
										 |  |  | const bzzManifestJSON = "application/bzz-manifest+json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | func add(ctx *cli.Context) { | 
					
						
							|  |  |  | 	args := ctx.Args() | 
					
						
							|  |  |  | 	if len(args) < 3 { | 
					
						
							| 
									
										
										
										
											2018-03-02 17:59:26 +08:00
										 |  |  | 		utils.Fatalf("Need at least three arguments <MHASH> <path> <HASH> [<content-type>]") | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		mhash = args[0] | 
					
						
							|  |  |  | 		path  = args[1] | 
					
						
							|  |  |  | 		hash  = args[2] | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		ctype        string | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		mroot        api.Manifest | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(args) > 3 { | 
					
						
							|  |  |  | 		ctype = args[3] | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ctype = mime.TypeByExtension(filepath.Ext(path)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	newManifest := addEntryToManifest(ctx, mhash, path, hash, ctype) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	fmt.Println(newManifest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !wantManifest { | 
					
						
							|  |  |  | 		// Print the manifest. This is the only output to stdout. | 
					
						
							|  |  |  | 		mrootJSON, _ := json.MarshalIndent(mroot, "", "  ") | 
					
						
							|  |  |  | 		fmt.Println(string(mrootJSON)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func update(ctx *cli.Context) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	args := ctx.Args() | 
					
						
							|  |  |  | 	if len(args) < 3 { | 
					
						
							| 
									
										
										
										
											2018-03-02 17:59:26 +08:00
										 |  |  | 		utils.Fatalf("Need at least three arguments <MHASH> <path> <HASH>") | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		mhash = args[0] | 
					
						
							|  |  |  | 		path  = args[1] | 
					
						
							|  |  |  | 		hash  = args[2] | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		ctype        string | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		mroot        api.Manifest | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	) | 
					
						
							|  |  |  | 	if len(args) > 3 { | 
					
						
							|  |  |  | 		ctype = args[3] | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ctype = mime.TypeByExtension(filepath.Ext(path)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	newManifest := updateEntryInManifest(ctx, mhash, path, hash, ctype) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	fmt.Println(newManifest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !wantManifest { | 
					
						
							|  |  |  | 		// Print the manifest. This is the only output to stdout. | 
					
						
							|  |  |  | 		mrootJSON, _ := json.MarshalIndent(mroot, "", "  ") | 
					
						
							|  |  |  | 		fmt.Println(string(mrootJSON)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func remove(ctx *cli.Context) { | 
					
						
							|  |  |  | 	args := ctx.Args() | 
					
						
							|  |  |  | 	if len(args) < 2 { | 
					
						
							| 
									
										
										
										
											2018-03-02 17:59:26 +08:00
										 |  |  | 		utils.Fatalf("Need at least two arguments <MHASH> <path>") | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		mhash = args[0] | 
					
						
							|  |  |  | 		path  = args[1] | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 		wantManifest = ctx.GlobalBoolT(SwarmWantManifestFlag.Name) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		mroot        api.Manifest | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 	newManifest := removeEntryFromManifest(ctx, mhash, path) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	fmt.Println(newManifest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !wantManifest { | 
					
						
							|  |  |  | 		// Print the manifest. This is the only output to stdout. | 
					
						
							|  |  |  | 		mrootJSON, _ := json.MarshalIndent(mroot, "", "  ") | 
					
						
							|  |  |  | 		fmt.Println(string(mrootJSON)) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) string { | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		bzzapi           = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 		client           = swarm.NewClient(bzzapi) | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		longestPathEntry = api.ManifestEntry{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	mroot, err := client.DownloadManifest(mhash) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Manifest download failed: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//TODO: check if the "hash" to add is valid and present in swarm | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	_, err = client.DownloadManifest(hash) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Hash to add is not present: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// See if we path is in this Manifest or do we have to dig deeper | 
					
						
							|  |  |  | 	for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 		if path == entry.Path { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 			utils.Fatalf("Path %s already present, not adding anything", path) | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-11-30 10:22:26 +00:00
										 |  |  | 			if entry.ContentType == bzzManifestJSON { | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 				prfxlen := strings.HasPrefix(path, entry.Path) | 
					
						
							|  |  |  | 				if prfxlen && len(path) > len(longestPathEntry.Path) { | 
					
						
							|  |  |  | 					longestPathEntry = entry | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if longestPathEntry.Path != "" { | 
					
						
							|  |  |  | 		// Load the child Manifest add the entry there | 
					
						
							|  |  |  | 		newPath := path[len(longestPathEntry.Path):] | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		newHash := addEntryToManifest(ctx, longestPathEntry.Hash, newPath, hash, ctype) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Replace the hash for parent Manifests | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		newMRoot := &api.Manifest{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 			if longestPathEntry.Path == entry.Path { | 
					
						
							|  |  |  | 				entry.Hash = newHash | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			newMRoot.Entries = append(newMRoot.Entries, entry) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mroot = newMRoot | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// Add the entry in the leaf Manifest | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		newEntry := api.ManifestEntry{ | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 			Hash:        hash, | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 			Path:        path, | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 			ContentType: ctype, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mroot.Entries = append(mroot.Entries, newEntry) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	newManifestHash, err := client.UploadManifest(mroot) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Manifest upload failed: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 	return newManifestHash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) string { | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		bzzapi           = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | 
					
						
							|  |  |  | 		client           = swarm.NewClient(bzzapi) | 
					
						
							|  |  |  | 		newEntry         = api.ManifestEntry{} | 
					
						
							|  |  |  | 		longestPathEntry = api.ManifestEntry{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	mroot, err := client.DownloadManifest(mhash) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Manifest download failed: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//TODO: check if the "hash" with which to update is valid and present in swarm | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// See if we path is in this Manifest or do we have to dig deeper | 
					
						
							|  |  |  | 	for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 		if path == entry.Path { | 
					
						
							|  |  |  | 			newEntry = entry | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-11-30 10:22:26 +00:00
										 |  |  | 			if entry.ContentType == bzzManifestJSON { | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 				prfxlen := strings.HasPrefix(path, entry.Path) | 
					
						
							|  |  |  | 				if prfxlen && len(path) > len(longestPathEntry.Path) { | 
					
						
							|  |  |  | 					longestPathEntry = entry | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if longestPathEntry.Path == "" && newEntry.Path == "" { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Path %s not present in the Manifest, not setting anything", path) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if longestPathEntry.Path != "" { | 
					
						
							|  |  |  | 		// Load the child Manifest add the entry there | 
					
						
							|  |  |  | 		newPath := path[len(longestPathEntry.Path):] | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		newHash := updateEntryInManifest(ctx, longestPathEntry.Hash, newPath, hash, ctype) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Replace the hash for parent Manifests | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		newMRoot := &api.Manifest{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 			if longestPathEntry.Path == entry.Path { | 
					
						
							|  |  |  | 				entry.Hash = newHash | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			newMRoot.Entries = append(newMRoot.Entries, entry) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mroot = newMRoot | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if newEntry.Path != "" { | 
					
						
							|  |  |  | 		// Replace the hash for leaf Manifest | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		newMRoot := &api.Manifest{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 			if newEntry.Path == entry.Path { | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 				myEntry := api.ManifestEntry{ | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 					Hash:        hash, | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 					Path:        entry.Path, | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 					ContentType: ctype, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				newMRoot.Entries = append(newMRoot.Entries, myEntry) | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				newMRoot.Entries = append(newMRoot.Entries, entry) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mroot = newMRoot | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	newManifestHash, err := client.UploadManifest(mroot) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Manifest upload failed: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 	return newManifestHash | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string { | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ( | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		bzzapi           = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") | 
					
						
							|  |  |  | 		client           = swarm.NewClient(bzzapi) | 
					
						
							|  |  |  | 		entryToRemove    = api.ManifestEntry{} | 
					
						
							|  |  |  | 		longestPathEntry = api.ManifestEntry{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	mroot, err := client.DownloadManifest(mhash) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Manifest download failed: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// See if we path is in this Manifest or do we have to dig deeper | 
					
						
							|  |  |  | 	for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 		if path == entry.Path { | 
					
						
							|  |  |  | 			entryToRemove = entry | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2017-11-30 10:22:26 +00:00
										 |  |  | 			if entry.ContentType == bzzManifestJSON { | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 				prfxlen := strings.HasPrefix(path, entry.Path) | 
					
						
							|  |  |  | 				if prfxlen && len(path) > len(longestPathEntry.Path) { | 
					
						
							|  |  |  | 					longestPathEntry = entry | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if longestPathEntry.Path == "" && entryToRemove.Path == "" { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Path %s not present in the Manifest, not removing anything", path) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if longestPathEntry.Path != "" { | 
					
						
							|  |  |  | 		// Load the child Manifest remove the entry there | 
					
						
							|  |  |  | 		newPath := path[len(longestPathEntry.Path):] | 
					
						
							| 
									
										
										
										
											2017-02-22 14:10:07 +02:00
										 |  |  | 		newHash := removeEntryFromManifest(ctx, longestPathEntry.Hash, newPath) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 		// Replace the hash for parent Manifests | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		newMRoot := &api.Manifest{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 			if longestPathEntry.Path == entry.Path { | 
					
						
							|  |  |  | 				entry.Hash = newHash | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			newMRoot.Entries = append(newMRoot.Entries, entry) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mroot = newMRoot | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if entryToRemove.Path != "" { | 
					
						
							|  |  |  | 		// remove the entry in this Manifest | 
					
						
							| 
									
										
										
										
											2017-04-06 23:22:22 +01:00
										 |  |  | 		newMRoot := &api.Manifest{} | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 		for _, entry := range mroot.Entries { | 
					
						
							|  |  |  | 			if entryToRemove.Path != entry.Path { | 
					
						
							|  |  |  | 				newMRoot.Entries = append(newMRoot.Entries, entry) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mroot = newMRoot | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 23:20:07 +01:00
										 |  |  | 	newManifestHash, err := client.UploadManifest(mroot) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 15:06:16 +02:00
										 |  |  | 		utils.Fatalf("Manifest upload failed: %v", err) | 
					
						
							| 
									
										
										
										
											2017-02-13 08:03:05 +05:30
										 |  |  | 	} | 
					
						
							|  |  |  | 	return newManifestHash | 
					
						
							|  |  |  | } |