]> Cypherpunks repositories - nncp.git/commitdiff
Ability to read configuration from file descriptor
authorSergey Matveev <stargrave@stargrave.org>
Sun, 28 Sep 2025 08:40:42 +0000 (11:40 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Sun, 28 Sep 2025 09:00:50 +0000 (12:00 +0300)
doc/cmd/index.texi
doc/news.ru.texi
doc/news.texi
src/ctx.go
src/log.go
src/nncp.go

index fa2b38ee0394090d6f86cb44752a1e5484d559e9..984a3bc2aaa05a7ae18144d80a18c455885feb51 100644 (file)
@@ -8,8 +8,10 @@ Nearly all commands have the following common options:
 @vindex NNCPCFG
 @item -cfg
     Path to configuration file. May be overridden by @env{$NNCPCFG}
-    environment variable. If file file is an encrypted @ref{EBlob,
-    eblob}, then ask for passphrase to decrypt it first.
+    environment variable. If file file is an encrypted @ref{EBlob, eblob},
+    then ask for passphrase to decrypt it first.
+    If it equals to @code{FD:x}, then read configration file from opened
+    file descriptor @code{x}.
 @item -debug
     Print debug messages. Normally this option should not be used.
 @item -minsize
index de3e026f2b3933ad883f186e2946ec98aced887a..5db31b51a42625bbc49eb57fb6b07c4e668c86e3 100644 (file)
@@ -1,6 +1,16 @@
 @node Новости
 @section Новости
 
+@node Релиз 8.13.0
+@subsection Релиз 8.13.0
+@itemize
+
+@item
+Возможно передавать конфигурационный факт через открытый файловый
+дескриптор (@env{$NNCPCFG=FD:0} например для чтения из stdin).
+
+@end itemize
+
 @node Релиз 8.12.1
 @subsection Релиз 8.12.1
 @itemize
index 61a1191820d69330f820653831f473f391d6e217..20f274ecb70df3d6122b7c7bdfd8d81cb7d6f418 100644 (file)
@@ -4,6 +4,16 @@
 
 See also this page @ref{Новости, in russian}.
 
+@node Release 8_13_0
+@section Release 8.13.0
+@itemize
+
+@item
+Ability to pass configuration file through opened file descriptor
+(@env{$NNCPCFG=FD:0} for example to read it from stdin).
+
+@end itemize
+
 @node Release 8_12_1
 @section Release 8.12.1
 @itemize
index 680d940e5c2e73decce2d5678fc33ac15cb3e34c..8de89a05087803d91eb8d302d30d7f7122e8b00d 100644 (file)
@@ -18,6 +18,7 @@ package nncp
 import (
        "errors"
        "fmt"
+       "io"
        "io/fs"
        "os"
        "path/filepath"
@@ -27,6 +28,8 @@ import (
        "syscall"
 )
 
+const FdPrefix = "FD:"
+
 type Ctx struct {
        Self   *NodeOur
        SelfId *NodeId
@@ -95,6 +98,21 @@ func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
        return err
 }
 
+func openPossibleFd(pth, name string) (*os.File, error) {
+       if !strings.HasPrefix(pth, FdPrefix) {
+               return nil, nil
+       }
+       ptr, err := strconv.ParseUint(strings.TrimPrefix(pth, FdPrefix), 10, 64)
+       if err != nil {
+               return nil, err
+       }
+       fd := os.NewFile(uintptr(ptr), name)
+       if fd == nil {
+               err = fmt.Errorf("can not open: %s: %s", name, pth)
+       }
+       return fd, err
+}
+
 func CtxFromCmdline(
        cfgPath, spoolPath, logPath string,
        quiet, showPrgrs, omitPrgrs, debug bool,
@@ -106,25 +124,41 @@ func CtxFromCmdline(
        if showPrgrs && omitPrgrs {
                return nil, errors.New("simultaneous -progress and -noprogress")
        }
-       fi, err := os.Stat(cfgPath)
-       if err != nil {
-               return nil, err
-       }
        var cfg *CfgJSON
-       if fi.IsDir() {
-               cfg, err = DirToCfg(cfgPath)
-               if err != nil {
-                       return nil, err
+       if fd, err := openPossibleFd(cfgPath, CfgPathEnv); err == nil {
+               if fd == nil {
+                       fi, err := os.Stat(cfgPath)
+                       if err != nil {
+                               return nil, err
+                       }
+                       if fi.IsDir() {
+                               cfg, err = DirToCfg(cfgPath)
+                               if err != nil {
+                                       return nil, err
+                               }
+                       } else {
+                               cfgRaw, err := os.ReadFile(cfgPath)
+                               if err != nil {
+                                       return nil, err
+                               }
+                               cfg, err = CfgParse(cfgRaw)
+                               if err != nil {
+                                       return nil, err
+                               }
+                       }
+               } else {
+                       cfgRaw, err := io.ReadAll(fd)
+                       fd.Close()
+                       if err != nil {
+                               return nil, err
+                       }
+                       cfg, err = CfgParse(cfgRaw)
+                       if err != nil {
+                               return nil, err
+                       }
                }
        } else {
-               cfgRaw, err := os.ReadFile(cfgPath)
-               if err != nil {
-                       return nil, err
-               }
-               cfg, err = CfgParse(cfgRaw)
-               if err != nil {
-                       return nil, err
-               }
+               return nil, err
        }
        ctx, err := Cfg2Ctx(cfg)
        if err != nil {
@@ -146,17 +180,12 @@ func CtxFromCmdline(
        } else {
                ctx.LogPath = logPath
        }
-       if strings.HasPrefix(ctx.LogPath, LogFdPrefix) {
-               ptr, err := strconv.ParseUint(
-                       strings.TrimPrefix(ctx.LogPath, LogFdPrefix), 10, 64,
-               )
-               if err != nil {
-                       return nil, err
-               }
-               LogFd = os.NewFile(uintptr(ptr), CfgLogEnv)
-               if LogFd == nil {
-                       return nil, errors.New("can not open:" + ctx.LogPath)
+       if fd, err := openPossibleFd(ctx.LogPath, CfgLogEnv); err == nil {
+               if fd != nil {
+                       LogFd = fd
                }
+       } else {
+               return nil, err
        }
        if showPrgrs {
                ctx.ShowPrgrs = true
index 94309a6e53a8d91cf879345651ff0b371551f070..201fb1928d7257407d4f960af85d99f2693b826e 100644 (file)
@@ -26,8 +26,6 @@ import (
        "golang.org/x/sys/unix"
 )
 
-const LogFdPrefix = "FD:"
-
 var (
        LogFd     *os.File
        LogFdLock sync.Mutex
index b5e917a504729b5b2d8518a17b7b3d5d13858f7e..653f2e11beb2a8fa7ce60447e74a02e00800c99c 100644 (file)
@@ -39,7 +39,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.`
 )
 
 var (
-       Version string = "8.12.1"
+       Version string = "8.13.0"
 
        Base32Codec *base32.Encoding = base32.StdEncoding.WithPadding(base32.NoPadding)
 )