]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/ctx.go
3f4def9d10754b48b4b7b129cbe152775bbb6a4a
[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, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 package nncp
20
21 import (
22         "errors"
23         "io/ioutil"
24         "log"
25         "os"
26         "path/filepath"
27
28         "golang.org/x/sys/unix"
29 )
30
31 type Ctx struct {
32         Self   *NodeOur
33         SelfId *NodeId
34         Neigh  map[NodeId]*Node
35         Alias  map[string]*NodeId
36
37         Spool      string
38         LogPath    string
39         Quiet      bool
40         Debug      bool
41         NotifyFile *FromToYAML
42         NotifyFreq *FromToYAML
43 }
44
45 func (ctx *Ctx) FindNode(id string) (*Node, error) {
46         nodeId, known := ctx.Alias[id]
47         if known {
48                 return ctx.Neigh[*nodeId], nil
49         }
50         nodeId, err := NodeIdFromString(id)
51         if err != nil {
52                 return nil, err
53         }
54         node, known := ctx.Neigh[*nodeId]
55         if !known {
56                 return nil, errors.New("Unknown node")
57         }
58         return node, nil
59 }
60
61 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
62         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
63         if err := os.MkdirAll(dirPath, os.FileMode(0700)); err != nil {
64                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
65                 return err
66         }
67         fd, err := os.Open(dirPath)
68         if err != nil {
69                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
70                 return err
71         }
72         fd.Close()
73         return nil
74 }
75
76 func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
77         env := os.Getenv(CfgPathEnv)
78         if env != "" {
79                 cfgPath = env
80         }
81         cfgRaw, err := ioutil.ReadFile(cfgPath)
82         if err != nil {
83                 return nil, err
84         }
85         ctx, err := CfgParse(cfgRaw)
86         if err != nil {
87                 return nil, err
88         }
89         if spoolPath == "" {
90                 env = os.Getenv(CfgSpoolEnv)
91                 if env != "" {
92                         ctx.Spool = env
93                 }
94         } else {
95                 ctx.Spool = spoolPath
96         }
97         if logPath == "" {
98                 env = os.Getenv(CfgLogEnv)
99                 if env != "" {
100                         ctx.LogPath = env
101                 }
102         } else {
103                 ctx.LogPath = logPath
104         }
105         ctx.Quiet = quiet
106         ctx.Debug = debug
107         return ctx, nil
108 }
109
110 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
111         var s unix.Statfs_t
112         if err := unix.Statfs(ctx.Spool, &s); err != nil {
113                 log.Fatalln(err)
114         }
115         return int64(s.Bavail)*int64(s.Bsize) > want
116 }