2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 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, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 "golang.org/x/sys/unix"
35 Neigh map[NodeId]*Node
36 Alias map[string]*NodeId
45 NotifyFile *FromToJSON
46 NotifyFreq *FromToJSON
47 NotifyExec map[string]*FromToJSON
50 func (ctx *Ctx) FindNode(id string) (*Node, error) {
51 nodeId, known := ctx.Alias[id]
53 return ctx.Neigh[*nodeId], nil
55 nodeId, err := NodeIdFromString(id)
59 node, known := ctx.Neigh[*nodeId]
61 return nil, errors.New("Unknown node")
66 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
67 dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
68 if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
69 ctx.LogE("dir-ensure", LEs{{"Dir", dirPath}}, err, "")
72 fd, err := os.Open(dirPath)
74 ctx.LogE("dir-ensure", LEs{{"Dir", dirPath}}, err, "")
84 quiet, showPrgrs, omitPrgrs, debug bool,
86 env := os.Getenv(CfgPathEnv)
90 if showPrgrs && omitPrgrs {
91 return nil, errors.New("simultaneous -progress and -noprogress")
93 cfgRaw, err := ioutil.ReadFile(cfgPath)
97 ctx, err := CfgParse(cfgRaw)
102 env = os.Getenv(CfgSpoolEnv)
107 ctx.Spool = spoolPath
110 env = os.Getenv(CfgLogEnv)
115 ctx.LogPath = logPath
120 if quiet || omitPrgrs {
121 ctx.ShowPrgrs = false
128 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
130 if err := unix.Statfs(ctx.Spool, &s); err != nil {
133 return int64(s.Bavail)*int64(s.Bsize) > want
136 func (ctx *Ctx) Umask() {
137 if ctx.UmaskForce != nil {
138 syscall.Umask(*ctx.UmaskForce)