]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
Unify copyright comment format
[nncp.git] / src / ctx.go
1 // NNCP -- Node to Node copy, utilities for store-and-forward data exchange
2 // Copyright (C) 2016-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package nncp
17
18 import (
19         "errors"
20         "fmt"
21         "io/fs"
22         "os"
23         "path/filepath"
24         "strconv"
25         "strings"
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         YggdrasilAliases map[string]string
54 }
55
56 func (ctx *Ctx) FindNode(id string) (*Node, error) {
57         nodeId, known := ctx.Alias[id]
58         if known {
59                 return ctx.Neigh[*nodeId], nil
60         }
61         nodeId, err := NodeIdFromString(id)
62         if err != nil {
63                 return nil, err
64         }
65         node, known := ctx.Neigh[*nodeId]
66         if !known {
67                 return nil, errors.New("Unknown node")
68         }
69         return node, nil
70 }
71
72 func ensureDir(dirs ...string) error {
73         p := filepath.Join(dirs...)
74         fi, err := os.Stat(p)
75         if err == nil {
76                 if fi.IsDir() {
77                         return nil
78                 }
79                 return fmt.Errorf("%s: is not a directory", p)
80         }
81         if !errors.Is(err, fs.ErrNotExist) {
82                 return err
83         }
84         return os.MkdirAll(p, os.FileMode(0777))
85 }
86
87 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
88         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
89         err := ensureDir(dirPath)
90         if err != nil {
91                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, func(les LEs) string {
92                         return fmt.Sprintf("Ensuring directory %s existence", dirPath)
93                 })
94         }
95         return err
96 }
97
98 func CtxFromCmdline(
99         cfgPath, spoolPath, logPath string,
100         quiet, showPrgrs, omitPrgrs, debug bool,
101 ) (*Ctx, error) {
102         env := os.Getenv(CfgPathEnv)
103         if env != "" {
104                 cfgPath = env
105         }
106         if showPrgrs && omitPrgrs {
107                 return nil, errors.New("simultaneous -progress and -noprogress")
108         }
109         fi, err := os.Stat(cfgPath)
110         if err != nil {
111                 return nil, err
112         }
113         var cfg *CfgJSON
114         if fi.IsDir() {
115                 cfg, err = DirToCfg(cfgPath)
116                 if err != nil {
117                         return nil, err
118                 }
119         } else {
120                 cfgRaw, err := os.ReadFile(cfgPath)
121                 if err != nil {
122                         return nil, err
123                 }
124                 cfg, err = CfgParse(cfgRaw)
125                 if err != nil {
126                         return nil, err
127                 }
128         }
129         ctx, err := Cfg2Ctx(cfg)
130         if err != nil {
131                 return nil, err
132         }
133         if spoolPath == "" {
134                 env = os.Getenv(CfgSpoolEnv)
135                 if env != "" {
136                         ctx.Spool = env
137                 }
138         } else {
139                 ctx.Spool = spoolPath
140         }
141         if logPath == "" {
142                 env = os.Getenv(CfgLogEnv)
143                 if env != "" {
144                         ctx.LogPath = env
145                 }
146         } else {
147                 ctx.LogPath = logPath
148         }
149         if strings.HasPrefix(ctx.LogPath, LogFdPrefix) {
150                 ptr, err := strconv.ParseUint(
151                         strings.TrimPrefix(ctx.LogPath, LogFdPrefix), 10, 64,
152                 )
153                 if err != nil {
154                         return nil, err
155                 }
156                 LogFd = os.NewFile(uintptr(ptr), CfgLogEnv)
157                 if LogFd == nil {
158                         return nil, errors.New("can not open:" + ctx.LogPath)
159                 }
160         }
161         if showPrgrs {
162                 ctx.ShowPrgrs = true
163         }
164         if quiet || omitPrgrs {
165                 ctx.ShowPrgrs = false
166         }
167         ctx.Quiet = quiet
168         ctx.Debug = debug
169         return ctx, nil
170 }
171
172 func (ctx *Ctx) Umask() {
173         if ctx.UmaskForce != nil {
174                 syscall.Umask(*ctx.UmaskForce)
175         }
176 }