]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/lockdir.go
More errors checking
[nncp.git] / src / lockdir.go
index 50e90f4dfe0b33ddace932e657d0b100361b18c8..875a5bbde49979fb9121d2d185d5f04213d73d16 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2020 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -24,9 +24,12 @@ import (
        "golang.org/x/sys/unix"
 )
 
-func (ctx *Ctx) LockDir(nodeId *NodeId, xx TRxTx) (*os.File, error) {
-       ctx.ensureRxDir(nodeId)
-       lockPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx)) + ".lock"
+func (ctx *Ctx) LockDir(nodeId *NodeId, lockCtx string) (*os.File, error) {
+       if err := ctx.ensureRxDir(nodeId); err != nil {
+               ctx.LogE("lockdir", SDS{}, err, "")
+               return nil, err
+       }
+       lockPath := filepath.Join(ctx.Spool, nodeId.String(), lockCtx) + ".lock"
        dirLock, err := os.OpenFile(
                lockPath,
                os.O_CREATE|os.O_WRONLY,
@@ -39,7 +42,7 @@ func (ctx *Ctx) LockDir(nodeId *NodeId, xx TRxTx) (*os.File, error) {
        err = unix.Flock(int(dirLock.Fd()), unix.LOCK_EX|unix.LOCK_NB)
        if err != nil {
                ctx.LogE("lockdir", SDS{"path": lockPath}, err, "")
-               dirLock.Close()
+               dirLock.Close() // #nosec G104
                return nil, err
        }
        return dirLock, nil
@@ -47,7 +50,7 @@ func (ctx *Ctx) LockDir(nodeId *NodeId, xx TRxTx) (*os.File, error) {
 
 func (ctx *Ctx) UnlockDir(fd *os.File) {
        if fd != nil {
-               unix.Flock(int(fd.Fd()), unix.LOCK_UN)
-               fd.Close()
+               unix.Flock(int(fd.Fd()), unix.LOCK_UN) // #nosec G104
+               fd.Close() // #nosec G104
        }
 }