X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fnncp%2Fcmd%2Fnncp-xfer%2Fmain.go;h=2072a3cc956280ba6e318c21c2a51363bcaac2ce;hb=dd92823db3d72fb21a4c712a7fb052dce16443dd;hp=a8406281dae470368e818150ed5cc24bb433e8d5;hpb=a46ffb69cbf3501ad02386eb9d3dabd1f3ebc90e;p=nncp.git diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go index a840628..2072a3c 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go @@ -4,8 +4,7 @@ Copyright (C) 2016-2019 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. +the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -162,6 +161,7 @@ func main() { } filename := filepath.Join(dir.Name(), fiInt.Name()) sds["file"] = filename + delete(sds, "size") fd, err := os.Open(filename) if err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "open") @@ -170,7 +170,7 @@ func main() { } var pktEnc nncp.PktEnc _, err = xdr.Unmarshal(fd, &pktEnc) - if err != nil || pktEnc.Magic != nncp.MagicNNCPEv3 { + if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 { ctx.LogD("nncp-xfer", sds, "is not a packet") fd.Close() continue @@ -180,13 +180,18 @@ func main() { fd.Close() continue } + sds["size"] = strconv.FormatInt(fiInt.Size(), 10) + if !ctx.IsEnoughSpace(fiInt.Size()) { + ctx.LogE("nncp-xfer", sds, "is not enough space") + fd.Close() + continue + } fd.Seek(0, 0) tmp, err := ctx.NewTmpFileWHash() if err != nil { log.Fatalln(err) } - copied, err := io.Copy(tmp.W, bufio.NewReader(fd)) - if err != nil { + if _, err = io.CopyN(tmp.W, bufio.NewReader(fd), fiInt.Size()); err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy") isBad = true fd.Close() @@ -201,9 +206,7 @@ func main() { )); err != nil { log.Fatalln(err) } - ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{ - "size": strconv.FormatInt(copied, 10), - }), "") + ctx.LogI("nncp-xfer", sds, "") if !*keep { if err = os.Remove(filename); err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "remove") @@ -309,14 +312,19 @@ Tx: isBad = true continue } - err = bufW.Flush() - tmp.Sync() - tmp.Close() - if err != nil { - ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "copy") + if err = bufW.Flush(); err != nil { + tmp.Close() + ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "flush") + isBad = true + continue + } + if err = tmp.Sync(); err != nil { + tmp.Close() + ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "sync") isBad = true continue } + tmp.Close() if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil { ctx.LogE("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"err": err}), "rename") isBad = true