修复windows向Linux传输发生文件夹错误

master
兔子 5 years ago
parent 196a469c34
commit eec673f640

@ -146,7 +146,7 @@ func init() {
tcpcmd.AddCommand(tcpsendcmd, tcprecvcmd) tcpcmd.AddCommand(tcpsendcmd, tcprecvcmd)
} }
var cansend, backsck bool = false, false var cansend, backsck, foldercreate bool = false, false, false
var tcpsendcmd = &cobra.Command{ var tcpsendcmd = &cobra.Command{
Use: "send", Use: "send",
Short: "通过tcp发送文件", Short: "通过tcp发送文件",
@ -183,6 +183,8 @@ var tcpsendcmd = &cobra.Command{
cansend = true cansend = true
} else if data == "backsck" { } else if data == "backsck" {
backsck = true backsck = true
} else if data == "folderok" {
foldercreate = true
} else { } else {
fmt.Println(data) fmt.Println(data)
os.Exit(1) os.Exit(1)
@ -294,7 +296,15 @@ func Scan(path, prefix string, mytcp net.Conn, reg string) {
for _, v := range dir { for _, v := range dir {
if v.IsDir() && (v.Name() != "." || v.Name() != "..") { if v.IsDir() && (v.Name() != "." || v.Name() != "..") {
mytcp.Write([]byte("b612&" + prefix + v.Name() + "&0")) mytcp.Write([]byte("b612&" + prefix + v.Name() + "&0"))
time.Sleep(time.Millisecond * 10) count := 0
for !foldercreate {
time.Sleep(time.Millisecond * 100)
count++
if count%30 == 0 {
mytcp.Write([]byte("b612&" + prefix + v.Name() + "&0"))
}
}
foldercreate = false
Scan(path+"/"+v.Name(), prefix+v.Name(), mytcp, reg) Scan(path+"/"+v.Name(), prefix+v.Name(), mytcp, reg)
} else { } else {
if reg != "" { if reg != "" {
@ -345,7 +355,7 @@ var tcprecvcmd = &cobra.Command{
name := "" name := ""
var fpdst *os.File var fpdst *os.File
defer conns.Close() defer conns.Close()
var sum int var sum int64
for { for {
buf := make([]byte, 393216) buf := make([]byte, 393216)
n, err := conns.Read(buf) n, err := conns.Read(buf)
@ -379,7 +389,14 @@ var tcprecvcmd = &cobra.Command{
} }
conns.Write([]byte("oksend!")) conns.Write([]byte("oksend!"))
} else if str[0] == "b612" { } else if str[0] == "b612" {
str[1] = ".\\" + str[1]
if runtime.GOOS != "windows" {
str[1] = strings.Replace(str[1], "\\", "/", -1)
}
fmt.Println("进入文件夹:" + str[1] + "\n")
os.MkdirAll(str[1], 0644) os.MkdirAll(str[1], 0644)
time.Sleep(time.Millisecond * 50)
conns.Write([]byte("folderok"))
} else if str[0] == "jane" { } else if str[0] == "jane" {
return return
} }
@ -396,7 +413,7 @@ var tcprecvcmd = &cobra.Command{
if ok { if ok {
if n > 8 { if n > 8 {
fpdst.Write(buf[0 : n-8]) fpdst.Write(buf[0 : n-8])
sum += n - 8 sum += int64(n - 8)
} }
fmt.Printf("已写入:%d 字节\n", sum) fmt.Printf("已写入:%d 字节\n", sum)
fmt.Println(name + "接收成功\n") fmt.Println(name + "接收成功\n")
@ -408,7 +425,7 @@ var tcprecvcmd = &cobra.Command{
} }
} }
fpdst.Write(buf[0:n]) fpdst.Write(buf[0:n])
sum += n sum += int64(n)
fmt.Printf("已写入:%d 字节\r", sum) fmt.Printf("已写入:%d 字节\r", sum)
} }
} }

Loading…
Cancel
Save