]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
Operations progress
[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         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         fd.Close()
77         return nil
78 }
79
80 func CtxFromCmdline(
81         cfgPath,
82         spoolPath,
83         logPath string,
84         quiet, showPrgrs, omitPrgrs, debug bool,
85 ) (*Ctx, error) {
86         env := os.Getenv(CfgPathEnv)
87         if env != "" {
88                 cfgPath = env
89         }
90         if showPrgrs && omitPrgrs {
91                 return nil, errors.New("simultaneous -progress and -noprogress")
92         }
93         cfgRaw, err := ioutil.ReadFile(cfgPath)
94         if err != nil {
95                 return nil, err
96         }
97         ctx, err := CfgParse(cfgRaw)
98         if err != nil {
99                 return nil, err
100         }
101         if spoolPath == "" {
102                 env = os.Getenv(CfgSpoolEnv)
103                 if env != "" {
104                         ctx.Spool = env
105                 }
106         } else {
107                 ctx.Spool = spoolPath
108         }
109         if logPath == "" {
110                 env = os.Getenv(CfgLogEnv)
111                 if env != "" {
112                         ctx.LogPath = env
113                 }
114         } else {
115                 ctx.LogPath = logPath
116         }
117         if showPrgrs {
118                 ctx.ShowPrgrs = true
119         }
120         if quiet || omitPrgrs {
121                 ctx.ShowPrgrs = false
122         }
123         ctx.Quiet = quiet
124         ctx.Debug = debug
125         return ctx, nil
126 }
127
128 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
129         var s unix.Statfs_t
130         if err := unix.Statfs(ctx.Spool, &s); err != nil {
131                 log.Fatalln(err)
132         }
133         return int64(s.Bavail)*int64(s.Bsize) > want
134 }
135
136 func (ctx *Ctx) Umask() {
137         if ctx.UmaskForce != nil {
138                 syscall.Umask(*ctx.UmaskForce)
139         }
140 }