// mmc -- Mattermost client // Copyright (C) 2023 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as // published by the Free Software Foundation, either version 3 of the // License. // // 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package mmc import ( "io" "os" "golang.org/x/sys/unix" ) func Lock(what string) (func(), error) { fd, err := os.OpenFile( what, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.FileMode(0666), ) dummy := func() {} if err != nil { return dummy, err } flock := unix.Flock_t{ Type: unix.F_WRLCK, Whence: io.SeekStart, } if err = unix.FcntlFlock(fd.Fd(), unix.F_SETLK, &flock); err != nil { fd.Close() return dummy, err } return func() { flock.Type = unix.F_UNLCK unix.FcntlFlock(fd.Fd(), unix.F_SETLK, &flock) fd.Close() }, nil }