From 4023962cbfa415155221d7c61f850f5af7820c8d Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Sun, 11 Jun 2017 12:46:12 +0300 Subject: [PATCH] Move lockdir to separate file --- src/cypherpunks.ru/nncp/lockdir.go | 54 ++++++++++++++++++++++++++++++ src/cypherpunks.ru/nncp/toss.go | 29 ---------------- 2 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 src/cypherpunks.ru/nncp/lockdir.go diff --git a/src/cypherpunks.ru/nncp/lockdir.go b/src/cypherpunks.ru/nncp/lockdir.go new file mode 100644 index 0000000..25316b8 --- /dev/null +++ b/src/cypherpunks.ru/nncp/lockdir.go @@ -0,0 +1,54 @@ +/* +NNCP -- Node to Node copy, utilities for store-and-forward data exchange +Copyright (C) 2016-2017 Sergey Matveev + +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 +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package nncp + +import ( + "os" + "path/filepath" + + "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" + dirLock, err := os.OpenFile( + lockPath, + os.O_CREATE|os.O_WRONLY, + os.FileMode(0600), + ) + if err != nil { + ctx.LogE("lockdir", SDS{"path": lockPath, "err": err}, "") + return nil, err + } + err = unix.Flock(int(dirLock.Fd()), unix.LOCK_EX|unix.LOCK_NB) + if err != nil { + ctx.LogE("lockdir", SDS{"path": lockPath, "err": err}, "") + dirLock.Close() + return nil, err + } + return dirLock, nil +} + +func (ctx *Ctx) UnlockDir(fd *os.File) { + if fd != nil { + unix.Flock(int(fd.Fd()), unix.LOCK_UN) + fd.Close() + } +} diff --git a/src/cypherpunks.ru/nncp/toss.go b/src/cypherpunks.ru/nncp/toss.go index 4ae9711..fec62ae 100644 --- a/src/cypherpunks.ru/nncp/toss.go +++ b/src/cypherpunks.ru/nncp/toss.go @@ -36,7 +36,6 @@ import ( "github.com/davecgh/go-xdr/xdr2" "github.com/dustin/go-humanize" "golang.org/x/crypto/blake2b" - "golang.org/x/sys/unix" ) func newNotification(fromTo *FromToYAML, subject string) io.Reader { @@ -48,34 +47,6 @@ func newNotification(fromTo *FromToYAML, subject string) io.Reader { )) } -func (ctx *Ctx) LockDir(nodeId *NodeId, xx TRxTx) (*os.File, error) { - ctx.ensureRxDir(nodeId) - lockPath := filepath.Join(ctx.Spool, nodeId.String(), string(xx)) + ".lock" - dirLock, err := os.OpenFile( - lockPath, - os.O_CREATE|os.O_WRONLY, - os.FileMode(0600), - ) - if err != nil { - ctx.LogE("lockdir", SDS{"path": lockPath, "err": err}, "") - return nil, err - } - err = unix.Flock(int(dirLock.Fd()), unix.LOCK_EX|unix.LOCK_NB) - if err != nil { - ctx.LogE("lockdir", SDS{"path": lockPath, "err": err}, "") - dirLock.Close() - return nil, err - } - return dirLock, nil -} - -func (ctx *Ctx) UnlockDir(fd *os.File) { - if fd != nil { - unix.Flock(int(fd.Fd()), unix.LOCK_UN) - fd.Close() - } -} - func (ctx *Ctx) Toss(nodeId *NodeId, nice uint8, dryRun bool) bool { dirLock, err := ctx.LockDir(nodeId, TRx) if err != nil { -- 2.48.1