]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
Logging to opened file descriptor
[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 (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
73         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
74         logMsg := func(les LEs) string {
75                 return fmt.Sprintf("Ensuring directory %s existence", dirPath)
76         }
77         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
78                 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, logMsg)
79                 return err
80         }
81         fd, err := os.Open(dirPath)
82         if err != nil {
83                 ctx.LogE("dir-ensure-open", LEs{{"Dir", dirPath}}, err, logMsg)
84                 return err
85         }
86         return fd.Close()
87 }
88
89 func CtxFromCmdline(
90         cfgPath, spoolPath, logPath string,
91         quiet, showPrgrs, omitPrgrs, debug bool,
92 ) (*Ctx, error) {
93         env := os.Getenv(CfgPathEnv)
94         if env != "" {
95                 cfgPath = env
96         }
97         if showPrgrs && omitPrgrs {
98                 return nil, errors.New("simultaneous -progress and -noprogress")
99         }
100         fi, err := os.Stat(cfgPath)
101         if err != nil {
102                 return nil, err
103         }
104         var cfg *CfgJSON
105         if fi.IsDir() {
106                 cfg, err = DirToCfg(cfgPath)
107                 if err != nil {
108                         return nil, err
109                 }
110         } else {
111                 cfgRaw, err := ioutil.ReadFile(cfgPath)
112                 if err != nil {
113                         return nil, err
114                 }
115                 cfg, err = CfgParse(cfgRaw)
116                 if err != nil {
117                         return nil, err
118                 }
119         }
120         ctx, err := Cfg2Ctx(cfg)
121         if err != nil {
122                 return nil, err
123         }
124         if spoolPath == "" {
125                 env = os.Getenv(CfgSpoolEnv)
126                 if env != "" {
127                         ctx.Spool = env
128                 }
129         } else {
130                 ctx.Spool = spoolPath
131         }
132         if logPath == "" {
133                 env = os.Getenv(CfgLogEnv)
134                 if env != "" {
135                         ctx.LogPath = env
136                 }
137         } else {
138                 ctx.LogPath = logPath
139         }
140         if strings.HasPrefix(ctx.LogPath, LogFdPrefix) {
141                 ptr, err := strconv.ParseUint(
142                         strings.TrimPrefix(ctx.LogPath, LogFdPrefix), 10, 64,
143                 )
144                 if err != nil {
145                         return nil, err
146                 }
147                 LogFd = os.NewFile(uintptr(ptr), CfgLogEnv)
148                 if LogFd == nil {
149                         return nil, errors.New("can not open:" + ctx.LogPath)
150                 }
151         }
152         if showPrgrs {
153                 ctx.ShowPrgrs = true
154         }
155         if quiet || omitPrgrs {
156                 ctx.ShowPrgrs = false
157         }
158         ctx.Quiet = quiet
159         ctx.Debug = debug
160         return ctx, nil
161 }
162
163 func (ctx *Ctx) Umask() {
164         if ctx.UmaskForce != nil {
165                 syscall.Umask(*ctx.UmaskForce)
166         }
167 }