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