/*
NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
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
goto Tx
}
fis, err = dir.Readdir(0)
- dir.Close()
+ dir.Close() // #nosec G104
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "read")
isBad = true
continue
}
fisInt, err := dir.Readdir(0)
- dir.Close()
+ dir.Close() // #nosec G104
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "read")
isBad = true
_, err = xdr.Unmarshal(fd, &pktEnc)
if err != nil || pktEnc.Magic != nncp.MagicNNCPEv4 {
ctx.LogD("nncp-xfer", sds, "is not a packet")
- fd.Close()
+ fd.Close() // #nosec G104
continue
}
if pktEnc.Nice > nice {
ctx.LogD("nncp-xfer", sds, "too nice")
- fd.Close()
+ fd.Close() // #nosec G104
continue
}
sds["size"] = fiInt.Size()
if !ctx.IsEnoughSpace(fiInt.Size()) {
ctx.LogE("nncp-xfer", sds, errors.New("is not enough space"), "")
- fd.Close()
+ fd.Close() // #nosec G104
continue
}
- fd.Seek(0, 0)
+ if _, err = fd.Seek(0, 0); err != nil {
+ log.Fatalln(err)
+ }
tmp, err := ctx.NewTmpFileWHash()
if err != nil {
log.Fatalln(err)
go func() {
_, err := io.CopyN(w, bufio.NewReader(fd), fiInt.Size())
if err == nil {
- w.Close()
- return
+ err = w.Close()
+ }
+ if err != nil {
+ ctx.LogE("nncp-xfer", sds, err, "copy")
+ w.CloseWithError(err) // #nosec G104
}
- ctx.LogE("nncp-xfer", sds, err, "copy")
- w.CloseWithError(err)
}()
- if _, err = nncp.CopyProgressed(tmp.W, r, nncp.SdsAdd(sds, nncp.SDS{
- "pkt": filename,
- "fullsize": sds["size"],
- }), ctx.ShowPrgrs); err != nil {
+ if _, err = nncp.CopyProgressed(
+ tmp.W, r, "Rx",
+ nncp.SdsAdd(sds, nncp.SDS{
+ "pkt": filename,
+ "fullsize": sds["size"],
+ }),
+ ctx.ShowPrgrs,
+ ); err != nil {
ctx.LogE("nncp-xfer", sds, err, "copy")
isBad = true
}
- fd.Close()
+ fd.Close() // #nosec G104
if isBad {
tmp.Cancel()
continue
ctx.LogD("nncp-xfer", sds, "skip")
continue
}
- dirLock, err := ctx.LockDir(&nodeId, nncp.TTx)
+ dirLock, err := ctx.LockDir(&nodeId, string(nncp.TTx))
if err != nil {
continue
}
sds["pkt"] = pktName
if job.PktEnc.Nice > nice {
ctx.LogD("nncp-xfer", sds, "too nice")
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
continue
}
if _, err = os.Stat(filepath.Join(dstPath, pktName)); err == nil || !os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "already exists")
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
continue
}
if _, err = os.Stat(filepath.Join(dstPath, pktName+nncp.SeenSuffix)); err == nil || !os.IsNotExist(err) {
ctx.LogD("nncp-xfer", sds, "already exists")
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
continue
}
tmp, err := nncp.TempFile(dstPath, "xfer")
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "mktemp")
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
isBad = true
break
}
ctx.LogD("nncp-xfer", sds, "created")
bufW := bufio.NewWriter(tmp)
copied, err := nncp.CopyProgressed(
- bufW,
- bufio.NewReader(job.Fd),
+ bufW, bufio.NewReader(job.Fd), "Tx",
nncp.SdsAdd(sds, nncp.SDS{"fullsize": job.Size}),
ctx.ShowPrgrs,
)
- job.Fd.Close()
+ job.Fd.Close() // #nosec G104
if err != nil {
ctx.LogE("nncp-xfer", sds, err, "copy")
- tmp.Close()
+ tmp.Close() // #nosec G104
isBad = true
continue
}
if err = bufW.Flush(); err != nil {
- tmp.Close()
+ tmp.Close() // #nosec G104
ctx.LogE("nncp-xfer", sds, err, "flush")
isBad = true
continue
}
if err = tmp.Sync(); err != nil {
- tmp.Close()
+ tmp.Close() // #nosec G104
ctx.LogE("nncp-xfer", sds, err, "sync")
isBad = true
continue
}
- tmp.Close()
+ if err = tmp.Close(); err != nil {
+ ctx.LogE("nncp-xfer", sds, err, "sync")
+ }
if err = os.Rename(tmp.Name(), filepath.Join(dstPath, pktName)); err != nil {
ctx.LogE("nncp-xfer", sds, err, "rename")
isBad = true
isBad = true
continue
}
- os.Remove(filepath.Join(dstPath, pktName+".part"))
+ os.Remove(filepath.Join(dstPath, pktName+".part")) // #nosec G104
delete(sds, "tmp")
ctx.LogI("nncp-xfer", nncp.SdsAdd(sds, nncp.SDS{"size": copied}), "")
if !*keep {