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