]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
ioutil is deprecated since 1.16/1.17
[nncp.git] / src / ctx.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2023 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         "os"
24         "path/filepath"
25         "strconv"
26         "strings"
27
28         "syscall"
29 )
30
31 type Ctx struct {
32         Self   *NodeOur
33         SelfId *NodeId
34         Neigh  map[NodeId]*Node
35         Alias  map[string]*NodeId
36
37         AreaId2Area map[AreaId]*Area
38         AreaName2Id map[string]*AreaId
39
40         Spool      string
41         LogPath    string
42         UmaskForce *int
43         Quiet      bool
44         ShowPrgrs  bool
45         HdrUsage   bool
46         Debug      bool
47         NotifyFile *FromToJSON
48         NotifyFreq *FromToJSON
49         NotifyExec map[string]*FromToJSON
50
51         MCDRxIfis []string
52         MCDTxIfis map[string]int
53
54         YggdrasilAliases map[string]string
55 }
56
57 func (ctx *Ctx) FindNode(id string) (*Node, error) {
58         nodeId, known := ctx.Alias[id]
59         if known {
60                 return ctx.Neigh[*nodeId], nil
61         }
62         nodeId, err := NodeIdFromString(id)
63         if err != nil {
64                 return nil, err
65         }
66         node, known := ctx.Neigh[*nodeId]
67         if !known {
68                 return nil, errors.New("Unknown node")
69         }
70         return node, nil
71 }
72
73 func ensureDir(dirs ...string) error {
74         p := filepath.Join(dirs...)
75         fi, err := os.Stat(p)
76         if err == nil {
77                 if fi.IsDir() {
78                         return nil
79                 }
80                 return fmt.Errorf("%s: is not a directory", p)
81         }
82         if !os.IsNotExist(err) {
83                 return err
84         }
85         return os.MkdirAll(p, os.FileMode(0777))
86 }
87
88 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
89         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
90         err := ensureDir(dirPath)
91         if err != nil {
92                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, func(les LEs) string {
93                         return fmt.Sprintf("Ensuring directory %s existence", dirPath)
94                 })
95         }
96         return err
97 }
98
99 func CtxFromCmdline(
100         cfgPath, spoolPath, logPath string,
101         quiet, showPrgrs, omitPrgrs, debug bool,
102 ) (*Ctx, error) {
103         env := os.Getenv(CfgPathEnv)
104         if env != "" {
105                 cfgPath = env
106         }
107         if showPrgrs && omitPrgrs {
108                 return nil, errors.New("simultaneous -progress and -noprogress")
109         }
110         fi, err := os.Stat(cfgPath)
111         if err != nil {
112                 return nil, err
113         }
114         var cfg *CfgJSON
115         if fi.IsDir() {
116                 cfg, err = DirToCfg(cfgPath)
117                 if err != nil {
118                         return nil, err
119                 }
120         } else {
121                 cfgRaw, err := os.ReadFile(cfgPath)
122                 if err != nil {
123                         return nil, err
124                 }
125                 cfg, err = CfgParse(cfgRaw)
126                 if err != nil {
127                         return nil, err
128                 }
129         }
130         ctx, err := Cfg2Ctx(cfg)
131         if err != nil {
132                 return nil, err
133         }
134         if spoolPath == "" {
135                 env = os.Getenv(CfgSpoolEnv)
136                 if env != "" {
137                         ctx.Spool = env
138                 }
139         } else {
140                 ctx.Spool = spoolPath
141         }
142         if logPath == "" {
143                 env = os.Getenv(CfgLogEnv)
144                 if env != "" {
145                         ctx.LogPath = env
146                 }
147         } else {
148                 ctx.LogPath = logPath
149         }
150         if strings.HasPrefix(ctx.LogPath, LogFdPrefix) {
151                 ptr, err := strconv.ParseUint(
152                         strings.TrimPrefix(ctx.LogPath, LogFdPrefix), 10, 64,
153                 )
154                 if err != nil {
155                         return nil, err
156                 }
157                 LogFd = os.NewFile(uintptr(ptr), CfgLogEnv)
158                 if LogFd == nil {
159                         return nil, errors.New("can not open:" + ctx.LogPath)
160                 }
161         }
162         if showPrgrs {
163                 ctx.ShowPrgrs = true
164         }
165         if quiet || omitPrgrs {
166                 ctx.ShowPrgrs = false
167         }
168         ctx.Quiet = quiet
169         ctx.Debug = debug
170         return ctx, nil
171 }
172
173 func (ctx *Ctx) Umask() {
174         if ctx.UmaskForce != nil {
175                 syscall.Umask(*ctx.UmaskForce)
176         }
177 }