]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
8a1146e2b5651f44731cfe7de792ff3388c906a9
[nncp.git] / src / ctx.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 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         Debug      bool
43         NotifyFile *FromToJSON
44         NotifyFreq *FromToJSON
45         NotifyExec map[string]*FromToJSON
46 }
47
48 func (ctx *Ctx) FindNode(id string) (*Node, error) {
49         nodeId, known := ctx.Alias[id]
50         if known {
51                 return ctx.Neigh[*nodeId], nil
52         }
53         nodeId, err := NodeIdFromString(id)
54         if err != nil {
55                 return nil, err
56         }
57         node, known := ctx.Neigh[*nodeId]
58         if !known {
59                 return nil, errors.New("Unknown node")
60         }
61         return node, nil
62 }
63
64 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
65         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
66         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
67                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
68                 return err
69         }
70         fd, err := os.Open(dirPath)
71         if err != nil {
72                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
73                 return err
74         }
75         fd.Close()
76         return nil
77 }
78
79 func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
80         env := os.Getenv(CfgPathEnv)
81         if env != "" {
82                 cfgPath = env
83         }
84         cfgRaw, err := ioutil.ReadFile(cfgPath)
85         if err != nil {
86                 return nil, err
87         }
88         ctx, err := CfgParse(cfgRaw)
89         if err != nil {
90                 return nil, err
91         }
92         if spoolPath == "" {
93                 env = os.Getenv(CfgSpoolEnv)
94                 if env != "" {
95                         ctx.Spool = env
96                 }
97         } else {
98                 ctx.Spool = spoolPath
99         }
100         if logPath == "" {
101                 env = os.Getenv(CfgLogEnv)
102                 if env != "" {
103                         ctx.LogPath = env
104                 }
105         } else {
106                 ctx.LogPath = logPath
107         }
108         ctx.Quiet = quiet
109         ctx.Debug = debug
110         return ctx, nil
111 }
112
113 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
114         var s unix.Statfs_t
115         if err := unix.Statfs(ctx.Spool, &s); err != nil {
116                 log.Fatalln(err)
117         }
118         return int64(s.Bavail)*int64(s.Bsize) > want
119 }
120
121 func (ctx *Ctx) Umask() {
122         if ctx.UmaskForce != nil {
123                 syscall.Umask(*ctx.UmaskForce)
124         }
125 }