]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
cfbe1b57d3b3e9dbfabcfa8a277d7f198cc9118e
[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         "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
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 !os.IsNotExist(err) {
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 := ioutil.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 }