]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
NetBSD compatible IsEnoughSpace()
[nncp.git] / src / ctx.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package nncp
19
20 import (
21         "errors"
22         "fmt"
23         "io/ioutil"
24         "os"
25         "path/filepath"
26
27         "syscall"
28 )
29
30 type Ctx struct {
31         Self   *NodeOur
32         SelfId *NodeId
33         Neigh  map[NodeId]*Node
34         Alias  map[string]*NodeId
35
36         AreaId2Area map[AreaId]*Area
37         AreaName2Id map[string]*AreaId
38
39         Spool      string
40         LogPath    string
41         UmaskForce *int
42         Quiet      bool
43         ShowPrgrs  bool
44         HdrUsage   bool
45         Debug      bool
46         NotifyFile *FromToJSON
47         NotifyFreq *FromToJSON
48         NotifyExec map[string]*FromToJSON
49
50         MCDRxIfis []string
51         MCDTxIfis map[string]int
52 }
53
54 func (ctx *Ctx) FindNode(id string) (*Node, error) {
55         nodeId, known := ctx.Alias[id]
56         if known {
57                 return ctx.Neigh[*nodeId], nil
58         }
59         nodeId, err := NodeIdFromString(id)
60         if err != nil {
61                 return nil, err
62         }
63         node, known := ctx.Neigh[*nodeId]
64         if !known {
65                 return nil, errors.New("Unknown node")
66         }
67         return node, nil
68 }
69
70 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
71         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
72         logMsg := func(les LEs) string {
73                 return fmt.Sprintf("Ensuring directory %s existence", dirPath)
74         }
75         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
76                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, logMsg)
77                 return err
78         }
79         fd, err := os.Open(dirPath)
80         if err != nil {
81                 ctx.LogE("dir-ensure-open", LEs{{"Dir", dirPath}}, err, logMsg)
82                 return err
83         }
84         return fd.Close()
85 }
86
87 func CtxFromCmdline(
88         cfgPath, spoolPath, logPath string,
89         quiet, showPrgrs, omitPrgrs, debug bool,
90 ) (*Ctx, error) {
91         env := os.Getenv(CfgPathEnv)
92         if env != "" {
93                 cfgPath = env
94         }
95         if showPrgrs && omitPrgrs {
96                 return nil, errors.New("simultaneous -progress and -noprogress")
97         }
98         fi, err := os.Stat(cfgPath)
99         if err != nil {
100                 return nil, err
101         }
102         var cfg *CfgJSON
103         if fi.IsDir() {
104                 cfg, err = DirToCfg(cfgPath)
105                 if err != nil {
106                         return nil, err
107                 }
108         } else {
109                 cfgRaw, err := ioutil.ReadFile(cfgPath)
110                 if err != nil {
111                         return nil, err
112                 }
113                 cfg, err = CfgParse(cfgRaw)
114                 if err != nil {
115                         return nil, err
116                 }
117         }
118         ctx, err := Cfg2Ctx(cfg)
119         if err != nil {
120                 return nil, err
121         }
122         if spoolPath == "" {
123                 env = os.Getenv(CfgSpoolEnv)
124                 if env != "" {
125                         ctx.Spool = env
126                 }
127         } else {
128                 ctx.Spool = spoolPath
129         }
130         if logPath == "" {
131                 env = os.Getenv(CfgLogEnv)
132                 if env != "" {
133                         ctx.LogPath = env
134                 }
135         } else {
136                 ctx.LogPath = logPath
137         }
138         if showPrgrs {
139                 ctx.ShowPrgrs = true
140         }
141         if quiet || omitPrgrs {
142                 ctx.ShowPrgrs = false
143         }
144         ctx.Quiet = quiet
145         ctx.Debug = debug
146         return ctx, nil
147 }
148
149 func (ctx *Ctx) Umask() {
150         if ctx.UmaskForce != nil {
151                 syscall.Umask(*ctx.UmaskForce)
152         }
153 }