2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 "golang.org/x/sys/unix"
34 Neigh map[NodeId]*Node
35 Alias map[string]*NodeId
41 NotifyFile *FromToYAML
42 NotifyFreq *FromToYAML
45 func (ctx *Ctx) FindNode(id string) (*Node, error) {
46 nodeId, known := ctx.Alias[id]
48 return ctx.Neigh[*nodeId], nil
50 nodeId, err := NodeIdFromString(id)
54 node, known := ctx.Neigh[*nodeId]
56 return nil, errors.New("Unknown node")
61 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
62 dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
63 if err := os.MkdirAll(dirPath, os.FileMode(0700)); err != nil {
64 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
67 fd, err := os.Open(dirPath)
69 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
76 func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
77 env := os.Getenv(CfgPathEnv)
81 cfgRaw, err := ioutil.ReadFile(cfgPath)
85 ctx, err := CfgParse(cfgRaw)
90 env = os.Getenv(CfgSpoolEnv)
98 env = os.Getenv(CfgLogEnv)
103 ctx.LogPath = logPath
110 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
112 if err := unix.Statfs(ctx.Spool, &s); err != nil {
115 return s.Bavail*int64(s.Bsize) > want