]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
nncp-cfgdir
[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         "log"
25         "os"
26         "path/filepath"
27
28         "syscall"
29
30         "golang.org/x/sys/unix"
31 )
32
33 type Ctx struct {
34         Self   *NodeOur
35         SelfId *NodeId
36         Neigh  map[NodeId]*Node
37         Alias  map[string]*NodeId
38
39         AreaId2Area map[AreaId]*Area
40         AreaName2Id map[string]*AreaId
41
42         Spool      string
43         LogPath    string
44         UmaskForce *int
45         Quiet      bool
46         ShowPrgrs  bool
47         HdrUsage   bool
48         Debug      bool
49         NotifyFile *FromToJSON
50         NotifyFreq *FromToJSON
51         NotifyExec map[string]*FromToJSON
52
53         MCDRxIfis []string
54         MCDTxIfis map[string]int
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 (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
74         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
75         logMsg := func(les LEs) string {
76                 return fmt.Sprintf("Ensuring directory %s existence", dirPath)
77         }
78         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
79                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, logMsg)
80                 return err
81         }
82         fd, err := os.Open(dirPath)
83         if err != nil {
84                 ctx.LogE("dir-ensure-open", LEs{{"Dir", dirPath}}, err, logMsg)
85                 return err
86         }
87         return fd.Close()
88 }
89
90 func CtxFromCmdline(
91         cfgPath, spoolPath, logPath string,
92         quiet, showPrgrs, omitPrgrs, debug bool,
93 ) (*Ctx, error) {
94         env := os.Getenv(CfgPathEnv)
95         if env != "" {
96                 cfgPath = env
97         }
98         if showPrgrs && omitPrgrs {
99                 return nil, errors.New("simultaneous -progress and -noprogress")
100         }
101         fi, err := os.Stat(cfgPath)
102         if err != nil {
103                 return nil, err
104         }
105         var cfg *CfgJSON
106         if fi.IsDir() {
107                 cfg, err = DirToCfg(cfgPath)
108                 if err != nil {
109                         return nil, err
110                 }
111         } else {
112                 cfgRaw, err := ioutil.ReadFile(cfgPath)
113                 if err != nil {
114                         return nil, err
115                 }
116                 cfg, err = CfgParse(cfgRaw)
117                 if err != nil {
118                         return nil, err
119                 }
120         }
121         ctx, err := Cfg2Ctx(cfg)
122         if err != nil {
123                 return nil, err
124         }
125         if spoolPath == "" {
126                 env = os.Getenv(CfgSpoolEnv)
127                 if env != "" {
128                         ctx.Spool = env
129                 }
130         } else {
131                 ctx.Spool = spoolPath
132         }
133         if logPath == "" {
134                 env = os.Getenv(CfgLogEnv)
135                 if env != "" {
136                         ctx.LogPath = env
137                 }
138         } else {
139                 ctx.LogPath = logPath
140         }
141         if showPrgrs {
142                 ctx.ShowPrgrs = true
143         }
144         if quiet || omitPrgrs {
145                 ctx.ShowPrgrs = false
146         }
147         ctx.Quiet = quiet
148         ctx.Debug = debug
149         return ctx, nil
150 }
151
152 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
153         var s unix.Statfs_t
154         if err := unix.Statfs(ctx.Spool, &s); err != nil {
155                 log.Fatalln("Can not stat spool:", err)
156         }
157         return int64(s.Bavail)*int64(s.Bsize) > want
158 }
159
160 func (ctx *Ctx) Umask() {
161         if ctx.UmaskForce != nil {
162                 syscall.Umask(*ctx.UmaskForce)
163         }
164 }