]> Cypherpunks.ru repositories - nncp.git/commitdiff
Free space checking
authorSergey Matveev <stargrave@stargrave.org>
Sat, 27 Apr 2019 17:06:22 +0000 (20:06 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sat, 27 Apr 2019 17:37:25 +0000 (20:37 +0300)
doc/news.ru.texi
doc/news.texi
src/cypherpunks.ru/nncp/cmd/nncp-xfer/main.go
src/cypherpunks.ru/nncp/ctx.go
src/cypherpunks.ru/nncp/humanizer.go
src/cypherpunks.ru/nncp/sp.go

index 53b0c2a7b7a0ea23931b2e646d59631257a007f7..4ac73b1419e4e48dd66866c9b35a76c25dd0797b 100644 (file)
@@ -11,6 +11,9 @@
 отсылать дешифрованные данные внешней команде. Старые версии не
 поддерживаются.
 @item
+Проверка доступного места перед копированием во время работы
+@command{nncp-xfer}, @command{nncp-daemon}, @command{nncp-call(er)}.
+@item
 Зависимые библиотеки обновлены.
 @item
 Небольшие исправления ошибок.
index 1cec8ddc84f908bda111954702a92bd918ba9bd7..602942d96cddd8041aad9ab09aed21227ea44d22 100644 (file)
@@ -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.
index 102292f2747c8fcda59e28e5508dd29e9f39c155..e4a8653e73df3df9f6c45ab8c0da5d10f480bbb3 100644 (file)
@@ -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")
index 5d705968d680d2885494cefe31333a54ff622e6a..fb7246a5f15ff0e2943fe18ef3400a0075fc0020 100644 (file)
@@ -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
+}
index 8d66442f954a2da5b79f536929e95f62c398c7f7..34ec158bcd913971d810796408bbb2c64735f11e 100644 (file)
@@ -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":
index cd8272c989326e93c9b0b5bc916a0718e700037b..fdfb14ab538a0ada1c501de3c8d246dbd92ab99c 100644 (file)
@@ -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)},