Skip to content

os/exec: Cmd adds random slashes to paths on run #14575

@dogancelik

Description

@dogancelik

What version of Go are you using (go version)?

go version go1.6 windows/amd64

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\dogan\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=1
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1

What did you do?

Run 7za (7-zip) and extract files

package main

import (
    "fmt"
    "os/exec"
    "strings"
)

func main() {
    zipcmd := `7za x "%s" -aoa -r -o"%s" *.xml`
    zippath := "C:\\test.zip"
    outputpath := "C:\\testfolder\\"

    cmdStr := fmt.Sprintf(zipcmd, zippath, outputpath)
    cmdSlice := strings.Fields(cmdStr)
    fmt.Println("command:", cmdStr)

    cmd := exec.Command(cmdSlice[0], cmdSlice[1:]...)
    fmt.Println("cmd.Args:", cmd.Args)

    cmdoutput, err := cmd.CombinedOutput()
    fmt.Println("output:", string(cmdoutput), err)
}

What did you expect to see?

dogan@DOGAN-PC E:\Projects\Go\test
$ 7za x "C:\test.zip" -aoa -r -o"C:\testfolder\" *.xml

7-Zip (a) [32] 15.07 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-09-17

Scanning the drive for archives:
1 file, 6225 bytes (7 KiB)

Extracting archive: C:\test.zip
--
Path = C:\test.zip
Type = zip
Physical Size = 6225

Everything is Ok

Files: 4
Size:       9014
Compressed: 6225

What did you see instead?

dogan@DOGAN-PC E:\Projects\Go\test
$ go run test.go
cmdStr: 7za x "C:\test.zip" -aoa -r -o"C:\testfolder\" *.xml
cmd.Args: [7za x "C:\test.zip" -aoa -r -o"C:\testfolder\" *.xml]
output:
7-Zip (a) [32] 15.07 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-09-17

Scanning the drive for archives:

ERROR: The filename, directory name, or volume label syntax is incorrect.
\C:\test.zip



System ERROR:
The filename, directory name, or volume label syntax is incorrect.
 exit status 2   

cmd.CombinedOutput(), cmd.Output(), and cmd.Run() adds / prefixes \ to C:\test.zip

Here's example test.zip file: test.zip

I can't do put space between -o and "C:\testfolder" because 7-Zip doesn't work that way.

Second test, same error, this time, it puts \ at -o argument:

cmd := exec.Command("7za", "x", "C:\\test.zip", "-aoa", "-r", `-o"C:\testfolder\"`, "*.xml")
fmt.Println("cmd.Args:", cmd.Args)

cmdoutput, err := cmd.CombinedOutput()
fmt.Println("output:", string(cmdoutput), err)
dogan@DOGAN-PC E:\Projects\Go\test
$ go run test.go
cmd.Args: [7za x C:\test.zip -aoa -r -o"C:\testfolder\" *.xml]
output:
7-Zip (a) [32] 15.07 beta : Copyright (c) 1999-2015 Igor Pavlov : 2015-09-17

Scanning the drive for archives:
1 file, 3725 bytes (4 KiB)

Extracting archive: C:\test.zip
--
Path = C:\test.zip
Type = zip
Physical Size = 3725



ERROR:
Can not create output directory: \C:\testfolder\\\


System ERROR:
The filename, directory name, or volume label syntax is incorrect.
 exit status 2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions