]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
Merge branch 'develop'
[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         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
51 func (ctx *Ctx) FindNode(id string) (*Node, error) {
52         nodeId, known := ctx.Alias[id]
53         if known {
54                 return ctx.Neigh[*nodeId], nil
55         }
56         nodeId, err := NodeIdFromString(id)
57         if err != nil {
58                 return nil, err
59         }
60         node, known := ctx.Neigh[*nodeId]
61         if !known {
62                 return nil, errors.New("Unknown node")
63         }
64         return node, nil
65 }
66
67 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
68         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
69         logMsg := func(les LEs) string {
70                 return fmt.Sprintf("Ensuring directory %s existence", dirPath)
71         }
72         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
73                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, logMsg)
74                 return err
75         }
76         fd, err := os.Open(dirPath)
77         if err != nil {
78                 ctx.LogE("dir-ensure-open", LEs{{"Dir", dirPath}}, err, logMsg)
79                 return err
80         }
81         return fd.Close()
82 }
83
84 func CtxFromCmdline(
85         cfgPath,
86         spoolPath,
87         logPath string,
88         quiet, showPrgrs, omitPrgrs, debug bool,
89 ) (*Ctx, error) {
90         env := os.Getenv(CfgPathEnv)
91         if env != "" {
92                 cfgPath = env
93         }
94         if showPrgrs && omitPrgrs {
95                 return nil, errors.New("simultaneous -progress and -noprogress")
96         }
97         cfgRaw, err := ioutil.ReadFile(cfgPath)
98         if err != nil {
99                 return nil, err
100         }
101         ctx, err := CfgParse(cfgRaw)
102         if err != nil {
103                 return nil, err
104         }
105         if spoolPath == "" {
106                 env = os.Getenv(CfgSpoolEnv)
107                 if env != "" {
108                         ctx.Spool = env
109                 }
110         } else {
111                 ctx.Spool = spoolPath
112         }
113         if logPath == "" {
114                 env = os.Getenv(CfgLogEnv)
115                 if env != "" {
116                         ctx.LogPath = env
117                 }
118         } else {
119                 ctx.LogPath = logPath
120         }
121         if showPrgrs {
122                 ctx.ShowPrgrs = true
123         }
124         if quiet || omitPrgrs {
125                 ctx.ShowPrgrs = false
126         }
127         ctx.Quiet = quiet
128         ctx.Debug = debug
129         return ctx, nil
130 }
131
132 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
133         var s unix.Statfs_t
134         if err := unix.Statfs(ctx.Spool, &s); err != nil {
135                 log.Fatalln(err)
136         }
137         return int64(s.Bavail)*int64(s.Bsize) > want
138 }
139
140 func (ctx *Ctx) Umask() {
141         if ctx.UmaskForce != nil {
142                 syscall.Umask(*ctx.UmaskForce)
143         }
144 }