2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
35 Neigh map[NodeId]*Node
36 Alias map[string]*NodeId
38 AreaId2Area map[AreaId]*Area
39 AreaName2Id map[string]*AreaId
48 NotifyFile *FromToJSON
49 NotifyFreq *FromToJSON
50 NotifyExec map[string]*FromToJSON
53 MCDTxIfis map[string]int
56 func (ctx *Ctx) FindNode(id string) (*Node, error) {
57 nodeId, known := ctx.Alias[id]
59 return ctx.Neigh[*nodeId], nil
61 nodeId, err := NodeIdFromString(id)
65 node, known := ctx.Neigh[*nodeId]
67 return nil, errors.New("Unknown node")
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)
77 fd, err := os.Open(dirPath)
79 if !os.IsNotExist(err) {
82 if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
83 ctx.LogE("dir-ensure-mkdir", LEs{{"Dir", dirPath}}, err, logMsg)
86 fd, err = os.Open(dirPath)
88 ctx.LogE("dir-ensure-open", LEs{{"Dir", dirPath}}, err, logMsg)
96 cfgPath, spoolPath, logPath string,
97 quiet, showPrgrs, omitPrgrs, debug bool,
99 env := os.Getenv(CfgPathEnv)
103 if showPrgrs && omitPrgrs {
104 return nil, errors.New("simultaneous -progress and -noprogress")
106 fi, err := os.Stat(cfgPath)
112 cfg, err = DirToCfg(cfgPath)
117 cfgRaw, err := ioutil.ReadFile(cfgPath)
121 cfg, err = CfgParse(cfgRaw)
126 ctx, err := Cfg2Ctx(cfg)
131 env = os.Getenv(CfgSpoolEnv)
136 ctx.Spool = spoolPath
139 env = os.Getenv(CfgLogEnv)
144 ctx.LogPath = logPath
146 if strings.HasPrefix(ctx.LogPath, LogFdPrefix) {
147 ptr, err := strconv.ParseUint(
148 strings.TrimPrefix(ctx.LogPath, LogFdPrefix), 10, 64,
153 LogFd = os.NewFile(uintptr(ptr), CfgLogEnv)
155 return nil, errors.New("can not open:" + ctx.LogPath)
161 if quiet || omitPrgrs {
162 ctx.ShowPrgrs = false
169 func (ctx *Ctx) Umask() {
170 if ctx.UmaskForce != nil {
171 syscall.Umask(*ctx.UmaskForce)