hirochachacha / go-smb2

SMB2/3 client library written in Go.
BSD 2-Clause "Simplified" License
351 stars 95 forks source link

this is a error,when i download a file. how can I fix it ? #37

Closed carlye closed 4 years ago

carlye commented 4 years ago

2020/08/30 15:59:04 read onecloud\tddownload\Train.to.Busan.2.Peninsula.2020.KOREAN.1080p.WEBRip.DD5.1.x264-CM\Train.to.Busan.2.Peninsula.2020.KOREAN.1080p.WEBRip .DD5.1.x264-CM.mkv: invalid response error: expected tree id: 124982927, got 0

maybe is same reason for func ListSharenames

panic: listSharenames srvsvc: invalid response error: expected tree id: 1300213661, got 0

carlye commented 4 years ago

package main

import ( "fmt" "github.com/hirochachacha/go-smb2" "io" "log" "net" "os" )

func main() { conn, err := net.Dial("tcp", "192.168.10.138:445") if err != nil { panic(err) } defer conn.Close() d := &smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ User: "root", Password: "123456", }, } s, err := d.Dial(conn) if err != nil { panic(err) } defer s.Logoff()

names, err := s.ListSharenames()
if err != nil {
    panic(err)
}

for _, name := range names {
    fmt.Println(name)
}

fs, err := s.Mount("43af")
if err != nil {
    panic(err)
}
defer fs.Umount()

list(fs, "onecloud\\tddownload")

list(fs, "onecloud\\tddownload\\Train.to.Busan.2.Peninsula.2020.KOREAN.1080p.WEBRip.DD5.1.x264-CM")

download(fs, "onecloud\\tddownload\\Train.to.Busan.2.Peninsula.2020.KOREAN.1080p.WEBRip.DD5.1.x264-CM\\Train.to.Busan.2.Peninsula.2020.KOREAN.1080p.WEBRip.DD5.1.x264-CM.mkv")

}

func list(share *smb2.Share, path string) { fileInfos, err := share.ReadDir(path)

if err != nil {
    panic(err)
}

for _, fileInfo := range fileInfos {
    fmt.Println(fileInfo.Name())
}

} func checkError(err error) { if err != nil { panic(err) } } func download(share *smb2.Share, path string) {

fi, err := share.Stat(path)
checkError(err)
if fi.IsDir() {
    log.Println("is a dir:", path)
    return
}
total := fi.Size()

log.Println("FileSize:", fi.Size())

f, err := share.Open(path)
checkError(err)
defer f.Close()

wf, err := os.Create("test.mv")
checkError(err)
defer wf.Close()

downloaded := int64(0)

buffer := make([]byte, 65536)
for {
    n, err := f.Read(buffer)
    if err != nil && err != io.EOF {
        log.Println(err)
    }
    if n <= 0 {
        break
    }
    downloaded += int64(n)

    wf.Write(buffer[0:n])
    fmt.Printf("\r[%01.2f] %d/%d",float32(downloaded)*100/float32(total),downloaded,total)
}
fmt.Println()

fmt.Println("finish:",path)

}

hirochachacha commented 4 years ago

Hello, what's your server? Windows 10? Today I learned that windows 10 sometimes responds with async packet even though request is sync packet, crazy. I want to know whether this is standard behavior. Also, if possible, could you give me a packet capture? So that I can confirm this issue.

hirochachacha commented 4 years ago

Never mind. In any case, solution wasn't hard.