Skip to content

Commit bceb240

Browse files
committed
Add link traversal option
License: MIT Signed-off-by: Dominic Della Valle <ddvpublic@gmail.com>
1 parent 6aaaa76 commit bceb240

File tree

6 files changed

+53
-21
lines changed

6 files changed

+53
-21
lines changed

cli/parse.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ import (
1010
"sort"
1111
"strings"
1212

13-
"github.com/ipfs/go-ipfs-cmds"
14-
1513
osh "github.com/Kubuxu/go-os-helper"
1614
"github.com/ipfs/go-ipfs-cmdkit"
1715
"github.com/ipfs/go-ipfs-cmdkit/files"
16+
"github.com/ipfs/go-ipfs-cmds"
1817
logging "github.com/ipfs/go-log"
1918
)
2019

@@ -79,6 +78,11 @@ func isRecursive(req *cmds.Request) bool {
7978
return rec && ok
8079
}
8180

81+
func linkResolveDepth(req *cmds.Request) int {
82+
linkOpt, _ := req.Options[cmds.DerefLong].(int) //TODO: safety concern
83+
return linkOpt
84+
}
85+
8286
type parseState struct {
8387
cmdline []string
8488
i int
@@ -265,7 +269,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
265269
fpath = stdin.Name()
266270
file = files.NewReaderFile("", fpath, r, nil)
267271
} else {
268-
nf, err := appendFile(fpath, argDef, isRecursive(req), isHidden(req))
272+
nf, err := appendFile(fpath, argDef, isRecursive(req), isHidden(req), linkResolveDepth(req))
269273
if err != nil {
270274
return err
271275
}
@@ -455,7 +459,7 @@ func getArgDef(i int, argDefs []cmdkit.Argument) *cmdkit.Argument {
455459
const notRecursiveFmtStr = "'%s' is a directory, use the '-%s' flag to specify directories"
456460
const dirNotSupportedFmtStr = "Invalid path '%s', argument '%s' does not support directories"
457461

458-
func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (files.File, error) {
462+
func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool, resolveDepth int) (files.File, error) {
459463
if fpath == "." {
460464
cwd, err := os.Getwd()
461465
if err != nil {
@@ -484,7 +488,7 @@ func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool) (
484488
}
485489
}
486490

487-
return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat)
491+
return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat, resolveDepth)
488492
}
489493

490494
// Inform the user if a file is waiting on input

cli/parse_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"os"
9+
"path/filepath"
910
"runtime"
1011
"strings"
1112
"testing"
@@ -518,3 +519,40 @@ func TestBodyArgs(t *testing.T) {
518519
}
519520
}
520521
}
522+
523+
func TestLinkArgs(t *testing.T) {
524+
root, err := ioutil.TempDir("", "cmds-link-test")
525+
if err != nil {
526+
t.Fatalf("cannot create symlink test root: %v", err)
527+
}
528+
defer os.RemoveAll(root)
529+
530+
realDir := filepath.Join(root, "RealDir")
531+
dirLink := filepath.Join(root, "DirLink")
532+
os.Mkdir(realDir, 0755)
533+
err = os.Symlink(realDir, dirLink)
534+
if err != nil {
535+
t.Fatalf("cannot create symlink %q->%q: %v", dirLink, realDir, err)
536+
}
537+
538+
dirArgs := &cmdkit.Argument{Type: cmdkit.ArgFile, Recursive: true}
539+
linkArgs := &cmdkit.Argument{Type: cmdkit.ArgFile}
540+
541+
ld, err := appendFile(realDir, dirArgs, true, true, true)
542+
if err != nil {
543+
t.Fatalf("cannot append directory %q: %v", realDir, err)
544+
}
545+
546+
if !ld.IsDirectory() {
547+
t.Fatalf("symlink was not resolved when intended")
548+
}
549+
550+
ld, err = appendFile(dirLink, linkArgs, true, true, false)
551+
if err != nil {
552+
t.Fatalf("cannot append link %q: %v", dirLink, err)
553+
}
554+
555+
if ld.IsDirectory() {
556+
t.Fatalf("symlink was resolved when not intended")
557+
}
558+
}

cli/run.go

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ func Run(ctx context.Context, root *cmds.Command,
3939
if timeoutStr, ok := req.Options[cmds.TimeoutOpt]; ok {
4040
timeout, err := time.ParseDuration(timeoutStr.(string))
4141
if err != nil {
42-
printErr(err)
4342
return err
4443
}
4544
req.Context, cancel = context.WithTimeout(req.Context, timeout)

http/handler.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package http
22

33
import (
44
"context"
5-
"crypto/rand"
6-
"encoding/base32"
75
"errors"
86
"net/http"
97
"runtime/debug"
@@ -12,6 +10,7 @@ import (
1210

1311
cmds "github.com/ipfs/go-ipfs-cmds"
1412
logging "github.com/ipfs/go-log"
13+
"github.com/libp2p/go-libp2p-loggables"
1514
cors "github.com/rs/cors"
1615
)
1716

@@ -133,7 +132,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
133132
}
134133
defer cancel()
135134

136-
req.Context = logging.ContextWithLoggable(req.Context, uuidLoggable())
135+
req.Context = logging.ContextWithLoggable(req.Context, loggables.Uuid("requestId"))
137136
if cn, ok := w.(http.CloseNotifier); ok {
138137
clientGone := cn.CloseNotify()
139138
go func() {
@@ -161,15 +160,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
161160
h.root.Call(req, re, h.env)
162161
}
163162

164-
func uuidLoggable() logging.Loggable {
165-
ids := make([]byte, 16)
166-
rand.Read(ids)
167-
168-
return logging.Metadata{
169-
"requestId": base32.HexEncoding.EncodeToString(ids),
170-
}
171-
}
172-
173163
func sanitizedErrStr(err error) string {
174164
s := err.Error()
175165
s = strings.Split(s, "\n")[0]

http/parse.go

-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,6 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
210210
}
211211
e.Message = string(mes)
212212
e.Code = cmdkit.ErrNormal
213-
case res.dec == nil:
214-
return nil, fmt.Errorf("unknown error content type: %s", contentType)
215213
default:
216214
// handle marshalled errors
217215
err := res.dec.Decode(&e)

opts.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ const (
1414
TimeoutOpt = "timeout"
1515
OptShortHelp = "h"
1616
OptLongHelp = "help"
17+
//DerefShort = "H" //RESERVED
18+
DerefLong = "dereference-command-line"
1719
)
1820

1921
// options that are used by this package
2022
var OptionEncodingType = cmdkit.StringOption(EncLong, EncShort, "The encoding type the output should be encoded with (json, xml, or text)").WithDefault("text")
2123
var OptionRecursivePath = cmdkit.BoolOption(RecLong, RecShort, "Add directory paths recursively").WithDefault(false)
2224
var OptionStreamChannels = cmdkit.BoolOption(ChanOpt, "Stream channel output")
23-
var OptionTimeout = cmdkit.StringOption(TimeoutOpt, "set a global timeout on the command")
25+
var OptionTimeout = cmdkit.StringOption(TimeoutOpt, "Set a global timeout on the command")
26+
var OptionDerefArgs = cmdkit.IntOption(DerefLong, "Resolve symlinks instead of adding them as-is").WithDefault(0)

0 commit comments

Comments
 (0)