From: Sergey Matveev Date: Sat, 27 Apr 2019 17:06:22 +0000 (+0300) Subject: Free space checking X-Git-Tag: 4.0^2~9 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=f350e2aec8bc3c256f78a66e48f8b75def49708e;p=nncp.git Free space checking --- diff --git a/doc/news.ru.texi b/doc/news.ru.texi index 53b0c2a..4ac73b1 100644 --- a/doc/news.ru.texi +++ b/doc/news.ru.texi @@ -11,6 +11,9 @@ отсылать дешифрованные данные внешней команде. Старые версии не поддерживаются. @item +Проверка доступного места перед копированием во время работы +@command{nncp-xfer}, @command{nncp-daemon}, @command{nncp-call(er)}. +@item Зависимые библиотеки обновлены. @item Небольшие исправления ошибок. diff --git a/doc/news.texi b/doc/news.texi index 1cec8dd..602942d 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -12,6 +12,9 @@ encryption mode with 128 KiB blocks is used now, because previously @command{nncp-toss} did not verify encrypted packet's MAC before feeding decrypted data to external command. Older versions are not supported. @item +Available free space checking before copying in @command{nncp-xfer}, +@command{nncp-daemon}, @command{nncp-call(er)}. +@item Dependant libraries are updated. @item Minor bugfixes. diff --git a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go index 102292f..e4a8653 100644 --- a/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go +++ b/src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go @@ -162,6 +162,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") @@ -180,13 +181,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 +207,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") diff --git a/src/cypherpunks.ru/nncp/ctx.go b/src/cypherpunks.ru/nncp/ctx.go index 5d70596..fb7246a 100644 --- a/src/cypherpunks.ru/nncp/ctx.go +++ b/src/cypherpunks.ru/nncp/ctx.go @@ -21,8 +21,11 @@ package nncp import ( "errors" "io/ioutil" + "log" "os" "path/filepath" + + "golang.org/x/sys/unix" ) type Ctx struct { @@ -103,3 +106,11 @@ func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx ctx.Debug = debug return ctx, nil } + +func (ctx *Ctx) IsEnoughSpace(want int64) bool { + var s unix.Statfs_t + if err := unix.Statfs(ctx.Spool, &s); err != nil { + log.Fatalln(err) + } + return s.Bavail*int64(s.Bsize) > want +} diff --git a/src/cypherpunks.ru/nncp/humanizer.go b/src/cypherpunks.ru/nncp/humanizer.go index 8d66442..34ec158 100644 --- a/src/cypherpunks.ru/nncp/humanizer.go +++ b/src/cypherpunks.ru/nncp/humanizer.go @@ -149,6 +149,8 @@ func (ctx *Ctx) Humanize(s string) string { } if err, exists := sds["err"]; exists { msg += ": " + err + } else { + msg += " " + rem } case "nncp-bundle": switch sds["xx"] { @@ -206,6 +208,8 @@ func (ctx *Ctx) Humanize(s string) string { return s } msg += fmt.Sprintf("%s packets, %s", sds["pkts"], size) + case "sp-process": + msg = fmt.Sprintf("%s has %s (%s): %s", nodeS, sds["hash"], size, rem) case "sp-file": switch sds["xx"] { case "rx": diff --git a/src/cypherpunks.ru/nncp/sp.go b/src/cypherpunks.ru/nncp/sp.go index cd8272c..fdfb14a 100644 --- a/src/cypherpunks.ru/nncp/sp.go +++ b/src/cypherpunks.ru/nncp/sp.go @@ -824,6 +824,10 @@ func (state *SPState) ProcessSP(payload []byte) ([][]byte, error) { "part exists", ) } + if !state.ctx.IsEnoughSpace(int64(info.Size) - offset) { + state.ctx.LogI("sp-process", sdsp, "not enough space") + continue + } replies = append(replies, MarshalSP( SPTypeFreq, SPFreq{info.Hash, uint64(offset)},