github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/lock/lock_nix.go (about) 1 //go:build !windows && !plan9 && !solaris 2 // +build !windows,!plan9,!solaris 3 4 // Copyright (c) 2015-2021 MinIO, Inc. 5 // 6 // This file is part of MinIO Object Storage stack 7 // 8 // This program is free software: you can redistribute it and/or modify 9 // it under the terms of the GNU Affero General Public License as published by 10 // the Free Software Foundation, either version 3 of the License, or 11 // (at your option) any later version. 12 // 13 // This program is distributed in the hope that it will be useful 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU Affero General Public License for more details. 17 // 18 // You should have received a copy of the GNU Affero General Public License 19 // along with this program. If not, see <http://www.gnu.org/licenses/>. 20 21 package lock 22 23 import ( 24 "os" 25 "syscall" 26 ) 27 28 // Internal function implements support for both 29 // blocking and non blocking lock type. 30 func lockedOpenFile(path string, flag int, perm os.FileMode, lockType int) (*LockedFile, error) { 31 switch flag { 32 case syscall.O_RDONLY: 33 lockType |= syscall.LOCK_SH 34 case syscall.O_WRONLY: 35 fallthrough 36 case syscall.O_RDWR: 37 fallthrough 38 case syscall.O_WRONLY | syscall.O_CREAT: 39 fallthrough 40 case syscall.O_RDWR | syscall.O_CREAT: 41 lockType |= syscall.LOCK_EX 42 default: 43 return nil, &os.PathError{ 44 Op: "open", 45 Path: path, 46 Err: syscall.EINVAL, 47 } 48 } 49 50 f, err := os.OpenFile(path, flag|syscall.O_SYNC, perm) 51 if err != nil { 52 return nil, err 53 } 54 55 if err = syscall.Flock(int(f.Fd()), lockType); err != nil { 56 f.Close() 57 if err == syscall.EWOULDBLOCK { 58 err = ErrAlreadyLocked 59 } 60 return nil, err 61 } 62 63 st, err := os.Stat(path) 64 if err != nil { 65 f.Close() 66 return nil, err 67 } 68 69 if st.IsDir() { 70 f.Close() 71 return nil, &os.PathError{ 72 Op: "open", 73 Path: path, 74 Err: syscall.EISDIR, 75 } 76 } 77 78 return &LockedFile{File: f}, nil 79 } 80 81 // TryLockedOpenFile - tries a new write lock, functionality 82 // it is similar to LockedOpenFile with with syscall.LOCK_EX 83 // mode but along with syscall.LOCK_NB such that the function 84 // doesn't wait forever but instead returns if it cannot 85 // acquire a write lock. 86 func TryLockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { 87 return lockedOpenFile(path, flag, perm, syscall.LOCK_NB) 88 } 89 90 // LockedOpenFile - initializes a new lock and protects 91 // the file from concurrent access across mount points. 92 // This implementation doesn't support all the open 93 // flags and shouldn't be considered as replacement 94 // for os.OpenFile(). 95 func LockedOpenFile(path string, flag int, perm os.FileMode) (*LockedFile, error) { 96 return lockedOpenFile(path, flag, perm, 0) 97 } 98 99 // Open - Call os.OpenFile 100 func Open(path string, flag int, perm os.FileMode) (*os.File, error) { 101 return os.OpenFile(path, flag, perm) 102 }