]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
ensureDir helper
[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         "fmt"
23         "io/ioutil"
24         "os"
25         "path/filepath"
26         "strconv"
27         "strings"
28
29         "syscall"
30 )
31
32 type Ctx struct {
33         Self   *NodeOur
34         SelfId *NodeId
35         Neigh  map[NodeId]*Node
36         Alias  map[string]*NodeId
37
38         AreaId2Area map[AreaId]*Area
39         AreaName2Id map[string]*AreaId
40
41         Spool      string
42         LogPath    string
43         UmaskForce *int
44         Quiet      bool
45         ShowPrgrs  bool
46         HdrUsage   bool
47         Debug      bool
48         NotifyFile *FromToJSON
49         NotifyFreq *FromToJSON
50         NotifyExec map[string]*FromToJSON
51
52         MCDRxIfis []string
53         MCDTxIfis map[string]int
54 }
55
56 func (ctx *Ctx) FindNode(id string) (*Node, error) {
57         nodeId, known := ctx.Alias[id]
58         if known {
59                 return ctx.Neigh[*nodeId], nil
60         }
61         nodeId, err := NodeIdFromString(id)
62         if err != nil {
63                 return nil, err
64         }
65         node, known := ctx.Neigh[*nodeId]
66         if !known {
67                 return nil, errors.New("Unknown node")
68         }
69         return node, nil
70 }
71
72 func ensureDir(p string) error {
73         fi, err := os.Stat(p)
74         if err == nil {
75                 if fi.IsDir() {
76                         return nil
77                 }
78                 return fmt.Errorf("%s: is not a directory", p)
79         }
80         if !os.IsNotExist(err) {
81                 return err
82         }
83         return os.MkdirAll(p, os.FileMode(0777))
84 }
85
86 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
87         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
88         err := ensureDir(dirPath)
89         if err != nil {
90                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, func(les LEs) string {
91                         return fmt.Sprintf("Ensuring directory %s existence", dirPath)
92                 })
93         }
94         return err
95 }
96
97 func CtxFromCmdline(
98         cfgPath, spoolPath, logPath string,
99         quiet, showPrgrs, omitPrgrs, debug bool,
100 ) (*Ctx, error) {
101         env := os.Getenv(CfgPathEnv)
102         if env != "" {
103                 cfgPath = env
104         }
105         if showPrgrs && omitPrgrs {
106                 return nil, errors.New("simultaneous -progress and -noprogress")
107         }
108         fi, err := os.Stat(cfgPath)
109         if err != nil {
110                 return nil, err
111         }
112         var cfg *CfgJSON
113         if fi.IsDir() {
114                 cfg, err = DirToCfg(cfgPath)
115                 if err != nil {
116                         return nil, err
117                 }
118         } else {
119                 cfgRaw, err := ioutil.ReadFile(cfgPath)
120                 if err != nil {
121                         return nil, err
122                 }
123                 cfg, err = CfgParse(cfgRaw)
124                 if err != nil {
125                         return nil, err
126                 }
127         }
128         ctx, err := Cfg2Ctx(cfg)
129         if err != nil {
130                 return nil, err
131         }
132         if spoolPath == "" {
133                 env = os.Getenv(CfgSpoolEnv)
134                 if env != "" {
135                         ctx.Spool = env
136                 }
137         } else {
138                 ctx.Spool = spoolPath
139         }
140         if logPath == "" {
141                 env = os.Getenv(CfgLogEnv)
142                 if env != "" {
143                         ctx.LogPath = env
144                 }
145         } else {
146                 ctx.LogPath = logPath
147         }
148         if strings.HasPrefix(ctx.LogPath, LogFdPrefix) {
149                 ptr, err := strconv.ParseUint(
150                         strings.TrimPrefix(ctx.LogPath, LogFdPrefix), 10, 64,
151                 )
152                 if err != nil {
153                         return nil, err
154                 }
155                 LogFd = os.NewFile(uintptr(ptr), CfgLogEnv)
156                 if LogFd == nil {
157                         return nil, errors.New("can not open:" + ctx.LogPath)
158                 }
159         }
160         if showPrgrs {
161                 ctx.ShowPrgrs = true
162         }
163         if quiet || omitPrgrs {
164                 ctx.ShowPrgrs = false
165         }
166         ctx.Quiet = quiet
167         ctx.Debug = debug
168         return ctx, nil
169 }
170
171 func (ctx *Ctx) Umask() {
172         if ctx.UmaskForce != nil {
173                 syscall.Umask(*ctx.UmaskForce)
174         }
175 }