]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/ctx.go
Forbid any later GNU GPL versions autousage
[nncp.git] / src / cypherpunks.ru / nncp / 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         "golang.org/x/sys/unix"
28 )
29
30 type Ctx struct {
31         Self   *NodeOur
32         SelfId *NodeId
33         Neigh  map[NodeId]*Node
34         Alias  map[string]*NodeId
35
36         Spool      string
37         LogPath    string
38         Quiet      bool
39         Debug      bool
40         NotifyFile *FromToYAML
41         NotifyFreq *FromToYAML
42 }
43
44 func (ctx *Ctx) FindNode(id string) (*Node, error) {
45         nodeId, known := ctx.Alias[id]
46         if known {
47                 return ctx.Neigh[*nodeId], nil
48         }
49         nodeId, err := NodeIdFromString(id)
50         if err != nil {
51                 return nil, err
52         }
53         node, known := ctx.Neigh[*nodeId]
54         if !known {
55                 return nil, errors.New("Unknown node")
56         }
57         return node, nil
58 }
59
60 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
61         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
62         if err := os.MkdirAll(dirPath, os.FileMode(0700)); err != nil {
63                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
64                 return err
65         }
66         fd, err := os.Open(dirPath)
67         if err != nil {
68                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
69                 return err
70         }
71         fd.Close()
72         return nil
73 }
74
75 func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
76         env := os.Getenv(CfgPathEnv)
77         if env != "" {
78                 cfgPath = env
79         }
80         cfgRaw, err := ioutil.ReadFile(cfgPath)
81         if err != nil {
82                 return nil, err
83         }
84         ctx, err := CfgParse(cfgRaw)
85         if err != nil {
86                 return nil, err
87         }
88         if spoolPath == "" {
89                 env = os.Getenv(CfgSpoolEnv)
90                 if env != "" {
91                         ctx.Spool = env
92                 }
93         } else {
94                 ctx.Spool = spoolPath
95         }
96         if logPath == "" {
97                 env = os.Getenv(CfgLogEnv)
98                 if env != "" {
99                         ctx.LogPath = env
100                 }
101         } else {
102                 ctx.LogPath = logPath
103         }
104         ctx.Quiet = quiet
105         ctx.Debug = debug
106         return ctx, nil
107 }
108
109 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
110         var s unix.Statfs_t
111         if err := unix.Statfs(ctx.Spool, &s); err != nil {
112                 log.Fatalln(err)
113         }
114         return int64(s.Bavail)*int64(s.Bsize) > want
115 }