]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
cb5c4282b868627e32691fe1e78aff346891bfa5
[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,
92         spoolPath,
93         logPath string,
94         quiet, showPrgrs, omitPrgrs, debug bool,
95 ) (*Ctx, error) {
96         env := os.Getenv(CfgPathEnv)
97         if env != "" {
98                 cfgPath = env
99         }
100         if showPrgrs && omitPrgrs {
101                 return nil, errors.New("simultaneous -progress and -noprogress")
102         }
103         cfgRaw, err := ioutil.ReadFile(cfgPath)
104         if err != nil {
105                 return nil, err
106         }
107         ctx, err := CfgParse(cfgRaw)
108         if err != nil {
109                 return nil, err
110         }
111         if spoolPath == "" {
112                 env = os.Getenv(CfgSpoolEnv)
113                 if env != "" {
114                         ctx.Spool = env
115                 }
116         } else {
117                 ctx.Spool = spoolPath
118         }
119         if logPath == "" {
120                 env = os.Getenv(CfgLogEnv)
121                 if env != "" {
122                         ctx.LogPath = env
123                 }
124         } else {
125                 ctx.LogPath = logPath
126         }
127         if showPrgrs {
128                 ctx.ShowPrgrs = true
129         }
130         if quiet || omitPrgrs {
131                 ctx.ShowPrgrs = false
132         }
133         ctx.Quiet = quiet
134         ctx.Debug = debug
135         return ctx, nil
136 }
137
138 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
139         var s unix.Statfs_t
140         if err := unix.Statfs(ctx.Spool, &s); err != nil {
141                 log.Fatalln("Can not stat spool:", err)
142         }
143         return int64(s.Bavail)*int64(s.Bsize) > want
144 }
145
146 func (ctx *Ctx) Umask() {
147         if ctx.UmaskForce != nil {
148                 syscall.Umask(*ctx.UmaskForce)
149         }
150 }