func (c *ClientRunContext) sendFileContents(file string,fh *os.File,stats os.FileInfo) (err error) {
// c.connWriter is a bufio.Writer with attached tcp connection
if _, err = c.connWriter.WriteString("c" + file + "\n"); err != nil {return err}size := stats.Size()log.Debug("Length of file = %v", size)if _, err := c.connWriter.WriteString(strconv.FormatInt(size, 10) + "\n"); err != nil {return err}log.Debug("Length sent")sz, err := io.Copy(c.connWriter, fh)if err != nil {log.Error("Problem sending %v", err)return err}if sz != size {
// Something went wrong, all that follows is trying to pinpoint the error, not original code
The "err != nil" block is never entered, but the sz != size block is (after the first few hundred files are processed succesfully). If I try to copy the file once more to the bufio.Writer after the error occurs, the problem remains and io.Copy still does nothing.log.Debug("Sent size = %v", sz)log.Debug("trying again")a, b := io.Copy(c.connWriter, fh)log.Debug("", a, b) // still not workingnewf, err := os.Create("C:\\tmp\\f.txt")log.Debug("", newf, err) // oka, b = io.Copy(newf, fh)log.Debug("", a, b) // has copied all bytespanic("end")}}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
func (c *ClientRunContext) sendFileContents(file string,fh *os.File,stats os.FileInfo) (err error) {
if _, err = c.connWriter.WriteString("c" + file + "\n"); err != nil {return err}
size := stats.Size()log.Debug("Length of file = %v", size)if _, err := c.connWriter.WriteString(strconv.FormatInt(size, 10) + "\n"); err != nil {return err}
log.Debug("Length sent")
buf := new(bytes.Buffer)sz, err := io.Copy(buf, fh)
if err != nil {log.Error("Problem copying to buf", err)
return err}
if sz != size {
log.Debug("Copy to buf does not match:")
log.Debug("Sent size = %v", sz)
panic("end")} else {log.Debug("Copy to buf succesful")}sz, err = io.Copy(c.connWriter, buf)if err != nil {log.Error("Problem copying from buf to connection %v", err)
return err}
if sz != size {
log.Debug("Copy from buf does not match:")
log.Debug("Sent size = %v", sz)log.Debug("trying again")
sz, err := c.connWriter.WriteString("hello")log.Debug("Sending a random string: %v bytes and error %v", sz, err)panic("end")}//if sentSize, err := io.CopyN(c.connWriter, fh, size); err != nil {// log.Error("Only %v bytes sent io %v, %v", sentSize, size, err)// return err//}log.Debug("File contents sent (but not flushed)")return nil}
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I just tried that, and you are right, it is the copy *to* the bufio.Writer that fails. Code follows:
func (c *ClientRunContext) evaluateAndSend(filename string,file *os.File,info os.FileInfo,est EvaluateAndSendType) error {fmt.Println("Sending", filename)if est == DecideToSendContents && c.run.Type == store.Fullfiles {filemodTime := info.ModTime()relRunTime := c.run.RelativeTofileChanged := filemodTime.After(relRunTime)log.Debug("File modified at %v", filemodTime)log.Debug("Relative run time %v", relRunTime)log.Debug("File changed = %v", fileChanged)
if fileChanged {return c.sendFileContents(filename, file, info)} else {return c.sendFileNothing(filename, file, info)}} else {return c.sendFileContents(filename, file, info)}}
func sendFileSlice(includes []string,exclude func(string) bool,shouldBeBackedUp func(string, *os.File, os.FileInfo) error) error {for _, include := range includes {log.Debug("Processing include %s", include)f, err := os.Open(include)if err != nil {log.Error("Could not open include %s because %s",include, err.Error())continue}defer f.Close()fi, err := f.Stat()if err != nil {log.Error("Could not stat %s", include)continue}switch mode := fi.Mode(); {case mode.IsDir():contents, err := f.Readdirnames(0)if err != nil {log.Error("Could not read contents of directory %s because %s",include, err.Error())continue}var toVisit []stringfor _, file := range contents {absfile := filepath.Join(include, file)if !exclude(absfile) {toVisit = append(toVisit, absfile)} else {log.Debug("%s was excluded", absfile)}}if err = sendFileSlice(toVisit[:], exclude, shouldBeBackedUp); err != nil {return err}case mode.IsRegular():if !exclude(include) {err := shouldBeBackedUp(include, f, fi)if err != nil {log.Warning("Error while sending: %v", err.Error())return err}// Close the file here already, if we wait until we leave the// ftion, due to the BFS it'll be too latef.Close()} else {log.Debug("%s was excluded", include)}}}return nil}
if rt, ok := dst.(ReaderFrom); ok { return rt.ReadFrom(src) }which makes sense because the destination (connWriter) is a bufio Object which supports the ReaderFrom interface. So the Copy function itself actually does almost nothing.
2013/07/24 14:12:32 writer.go:573: Entered WriteString2013/07/24 14:12:32 writer.go:590: Copied 106 bytes2013/07/24 14:12:32 writer.go:592: b.n is now 3192013/07/24 14:12:32 writer.go:594: Returning 1062013/07/24 14:12:32 client.go:218: Length of file = 36592013/07/24 14:12:32 writer.go:573: Entered WriteString2013/07/24 14:12:32 writer.go:590: Copied 5 bytes2013/07/24 14:12:32 writer.go:592: b.n is now 3242013/07/24 14:12:32 writer.go:594: Returning 52013/07/24 14:12:32 client.go:224: Length sent2013/07/24 14:12:32 client.go:241: Copy to buf succesful2013/07/24 14:12:32 writer.go:600: Entering readFrom2013/07/24 14:12:32 writer.go:610: Starting to read in buf at 324, len of buf = 40962013/07/24 14:12:32 writer.go:612: Read 3659 bytes into buf, err = <nil>2013/07/24 14:12:32 writer.go:618: b.n is now 39832013/07/24 14:12:32 writer.go:621: 113 bytes unused in buffer2013/07/24 14:12:32 writer.go:610: Starting to read in buf at 3983, len of buf = 40962013/07/24 14:12:32 writer.go:612: Read 0 bytes into buf, err = EOF2013/07/24 14:12:32 writer.go:614: breaking2013/07/24 14:12:32 writer.go:637: Erasing EOF error2013/07/24 14:12:32 writer.go:640: Returning 3659, <nil>2013/07/24 14:12:32 client.go:246: %!(EXTRA int64=3659, <nil>)2013/07/24 14:12:32 client.go:265: File contents sent (but not flushed)2013/07/24 14:12:32 source.go:30: Processing include E:\cygwin\home\ives\pieces\cute-chess.googlecode.com\svn\trunk\Chess\PieceSets\Chesscube\White Knight.xamlSending E:\cygwin\home\ives\pieces\cute-chess.googlecode.com\svn\trunk\Chess\PieceSets\Chesscube\White Knight.xaml2013/07/24 14:12:32 writer.go:573: Entered WriteString2013/07/24 14:12:32 writer.go:590: Copied 108 bytes2013/07/24 14:12:32 writer.go:592: b.n is now 40912013/07/24 14:12:32 writer.go:594: Returning 1082013/07/24 14:12:32 client.go:218: Length of file = 50712013/07/24 14:12:32 writer.go:573: Entered WriteString2013/07/24 14:12:32 writer.go:590: Copied 5 bytes2013/07/24 14:12:32 writer.go:592: b.n is now 40962013/07/24 14:12:32 writer.go:594: Returning 52013/07/24 14:12:32 client.go:224: Length sent2013/07/24 14:12:32 client.go:241: Copy to buf succesful2013/07/24 14:12:32 writer.go:600: Entering readFrom2013/07/24 14:12:32 writer.go:610: Starting to read in buf at 4096, len of buf = 40962013/07/24 14:12:32 writer.go:612: Read 0 bytes into buf, err = <nil>2013/07/24 14:12:32 writer.go:614: breaking2013/07/24 14:12:32 writer.go:640: Returning 0, <nil>2013/07/24 14:12:32 client.go:246: %!(EXTRA int64=0, <nil>)2013/07/24 14:12:32 client.go:254: Copy from buf does not match:2013/07/24 14:12:32 client.go:255: Sent size = 0