]> 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-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         "io/ioutil"
23         "log"
24         "os"
25         "path/filepath"
26
27         "syscall"
28
29         "golang.org/x/sys/unix"
30 )
31
32 type Ctx struct {
33         Self   *NodeOur
34         SelfId *NodeId
35         Neigh  map[NodeId]*Node
36         Alias  map[string]*NodeId
37
38         Spool      string
39         LogPath    string
40         UmaskForce *int
41         Quiet      bool
42         ShowPrgrs  bool
43         Debug      bool
44         NotifyFile *FromToJSON
45         NotifyFreq *FromToJSON
46         NotifyExec map[string]*FromToJSON
47 }
48
49 func (ctx *Ctx) FindNode(id string) (*Node, error) {
50         nodeId, known := ctx.Alias[id]
51         if known {
52                 return ctx.Neigh[*nodeId], nil
53         }
54         nodeId, err := NodeIdFromString(id)
55         if err != nil {
56                 return nil, err
57         }
58         node, known := ctx.Neigh[*nodeId]
59         if !known {
60                 return nil, errors.New("Unknown node")
61         }
62         return node, nil
63 }
64
65 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
66         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
67         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
68                 ctx.LogE("dir-ensure", SDS{"dir": dirPath}, err, "")
69                 return err
70         }
71         fd, err := os.Open(dirPath)
72         if err != nil {
73                 ctx.LogE("dir-ensure", SDS{"dir": dirPath}, err, "")
74                 return err
75         }
76         return fd.Close()
77 }
78
79 func CtxFromCmdline(
80         cfgPath,
81         spoolPath,
82         logPath string,
83         quiet, showPrgrs, omitPrgrs, debug bool,
84 ) (*Ctx, error) {
85         env := os.Getenv(CfgPathEnv)
86         if env != "" {
87                 cfgPath = env
88         }
89         if showPrgrs && omitPrgrs {
90                 return nil, errors.New("simultaneous -progress and -noprogress")
91         }
92         cfgRaw, err := ioutil.ReadFile(cfgPath)
93         if err != nil {
94                 return nil, err
95         }
96         ctx, err := CfgParse(cfgRaw)
97         if err != nil {
98                 return nil, err
99         }
100         if spoolPath == "" {
101                 env = os.Getenv(CfgSpoolEnv)
102                 if env != "" {
103                         ctx.Spool = env
104                 }
105         } else {
106                 ctx.Spool = spoolPath
107         }
108         if logPath == "" {
109                 env = os.Getenv(CfgLogEnv)
110                 if env != "" {
111                         ctx.LogPath = env
112                 }
113         } else {
114                 ctx.LogPath = logPath
115         }
116         if showPrgrs {
117                 ctx.ShowPrgrs = true
118         }
119         if quiet || omitPrgrs {
120                 ctx.ShowPrgrs = false
121         }
122         ctx.Quiet = quiet
123         ctx.Debug = debug
124         return ctx, nil
125 }
126
127 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
128         var s unix.Statfs_t
129         if err := unix.Statfs(ctx.Spool, &s); err != nil {
130                 log.Fatalln(err)
131         }
132         return int64(s.Bavail)*int64(s.Bsize) > want
133 }
134
135 func (ctx *Ctx) Umask() {
136         if ctx.UmaskForce != nil {
137                 syscall.Umask(*ctx.UmaskForce)
138         }
139 }