]> Cypherpunks.ru repositories - nncp.git/blob - src/ctx.go
Ability to forcefully override umask
[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         "golang.org/x/sys/unix"
28         "syscall"
29 )
30
31 type Ctx struct {
32         Self   *NodeOur
33         SelfId *NodeId
34         Neigh  map[NodeId]*Node
35         Alias  map[string]*NodeId
36
37         Spool      string
38         LogPath    string
39         UmaskForce *int
40         Quiet      bool
41         Debug      bool
42         NotifyFile *FromToJSON
43         NotifyFreq *FromToJSON
44 }
45
46 func (ctx *Ctx) FindNode(id string) (*Node, error) {
47         nodeId, known := ctx.Alias[id]
48         if known {
49                 return ctx.Neigh[*nodeId], nil
50         }
51         nodeId, err := NodeIdFromString(id)
52         if err != nil {
53                 return nil, err
54         }
55         node, known := ctx.Neigh[*nodeId]
56         if !known {
57                 return nil, errors.New("Unknown node")
58         }
59         return node, nil
60 }
61
62 func (ctx *Ctx) ensureRxDir(nodeId *NodeId) error {
63         dirPath := filepath.Join(ctx.Spool, nodeId.String(), string(TRx))
64         if err := os.MkdirAll(dirPath, os.FileMode(0777)); err != nil {
65                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
66                 return err
67         }
68         fd, err := os.Open(dirPath)
69         if err != nil {
70                 ctx.LogE("dir-ensure", SDS{"dir": dirPath, "err": err}, "")
71                 return err
72         }
73         fd.Close()
74         return nil
75 }
76
77 func CtxFromCmdline(cfgPath, spoolPath, logPath string, quiet, debug bool) (*Ctx, error) {
78         env := os.Getenv(CfgPathEnv)
79         if env != "" {
80                 cfgPath = env
81         }
82         cfgRaw, err := ioutil.ReadFile(cfgPath)
83         if err != nil {
84                 return nil, err
85         }
86         ctx, err := CfgParse(cfgRaw)
87         if err != nil {
88                 return nil, err
89         }
90         if spoolPath == "" {
91                 env = os.Getenv(CfgSpoolEnv)
92                 if env != "" {
93                         ctx.Spool = env
94                 }
95         } else {
96                 ctx.Spool = spoolPath
97         }
98         if logPath == "" {
99                 env = os.Getenv(CfgLogEnv)
100                 if env != "" {
101                         ctx.LogPath = env
102                 }
103         } else {
104                 ctx.LogPath = logPath
105         }
106         ctx.Quiet = quiet
107         ctx.Debug = debug
108         return ctx, nil
109 }
110
111 func (ctx *Ctx) IsEnoughSpace(want int64) bool {
112         var s unix.Statfs_t
113         if err := unix.Statfs(ctx.Spool, &s); err != nil {
114                 log.Fatalln(err)
115         }
116         return int64(s.Bavail)*int64(s.Bsize) > want
117 }
118
119 func (ctx *Ctx) Umask() {
120         if ctx.UmaskForce != nil {
121                 syscall.Umask(*ctx.UmaskForce)
122         }
123 }