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/>.
30 "golang.org/x/sys/unix"
36 Neigh map[NodeId]*Node
37 Alias map[string]*NodeId
39 AreaId2Area map[AreaId]*Area
40 AreaName2Id map[string]*AreaId
49 NotifyFile *FromToJSON
50 NotifyFreq *FromToJSON
51 NotifyExec map[string]*FromToJSON
54 MCDTxIfis map[string]int
57 func (ctx *Ctx) FindNode(id string) (*Node, error) {
58 nodeId, known := ctx.Alias[id]
60 return ctx.Neigh[*nodeId], nil
62 nodeId, err := NodeIdFromString(id)
66 node, known := ctx.Neigh[*nodeId]
68 return nil, errors.New("Unknown node")
73 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
74 dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
75 logMsg := func(les LEs) string {
76 return fmt.Sprintf("Ensuring directory %s existence", dirPath)
78 if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
79 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, logMsg)
82 fd, err := os.Open(dirPath)
84 ctx.LogE("dir-ensure-open", LEs{{"Dir", dirPath}}, err, logMsg)
94 quiet, showPrgrs, omitPrgrs, debug bool,
96 env := os.Getenv(CfgPathEnv)
100 if showPrgrs && omitPrgrs {
101 return nil, errors.New("simultaneous -progress and -noprogress")
103 cfgRaw, err := ioutil.ReadFile(cfgPath)
107 ctx, err := CfgParse(cfgRaw)
112 env = os.Getenv(CfgSpoolEnv)
117 ctx.Spool = spoolPath
120 env = os.Getenv(CfgLogEnv)
125 ctx.LogPath = logPath
130 if quiet || omitPrgrs {
131 ctx.ShowPrgrs = false
138 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
140 if err := unix.Statfs(ctx.Spool, &s); err != nil {
141 log.Fatalln("Can not stat spool:", err)
143 return int64(s.Bavail)*int64(s.Bsize) > want
146 func (ctx *Ctx) Umask() {
147 if ctx.UmaskForce != nil {
148 syscall.Umask(*ctx.UmaskForce)