| 1 | # Copyright © 2011 Raphaël Hertzog <hertzog@debian.org> | ||||||||||
| 2 | # Copyright © 2012 Guillem Jover <guillem@debian.org> | ||||||||||
| 3 | # | ||||||||||
| 4 | # This program is free software; you can redistribute it and/or modify | ||||||||||
| 5 | # it under the terms of the GNU General Public License as published by | ||||||||||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||
| 7 | # (at your option) any later version. | ||||||||||
| 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 <https://www.gnu.org/licenses/>. | ||||||||||
| 16 | |||||||||||
| 17 | =encoding utf8 | ||||||||||
| 18 | |||||||||||
| 19 | =head1 NAME | ||||||||||
| 20 | |||||||||||
| 21 | Dpkg::Lock - file locking support | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | This module implements locking functions used to support parallel builds. | ||||||||||
| 26 | |||||||||||
| 27 | B<Note>: This is a private module, its API can change at any time. | ||||||||||
| 28 | |||||||||||
| 29 | =cut | ||||||||||
| 30 | |||||||||||
| 31 | package Dpkg::Lock 0.01; | ||||||||||
| 32 | |||||||||||
| 33 | 1 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 1 | our @EXPORT = qw( | |||||||||
| 36 | file_lock | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 1 | ▶ | use Fcntl qw(:flock F_WRLCK F_SETLKW); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | use Dpkg::Gettext; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 1 | ▶ | use Dpkg::ErrorHandling; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub file_lock { | ||||||||
|
Subroutine
file_lock: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 46 | 0 | my ($fh, $filename) = @_; | |||||||||
| 47 | |||||||||||
| 48 | # We cannot have a strict dependency on libfile-fcntllock-perl because | ||||||||||
| 49 | # it contains an XS module, and dpkg-dev indirectly making use of it, | ||||||||||
| 50 | # which makes building new perl package that bump the perl ABI | ||||||||||
| 51 | # impossible as these packages cannot then be installed alongside. | ||||||||||
| 52 | # | ||||||||||
| 53 | # But if the XS module fails to load, because we are in the middle of an | ||||||||||
| 54 | # upgrade with an ABI breaking perl transition, fall back to try to use | ||||||||||
| 55 | # the pure Perl module, if available. | ||||||||||
| 56 | 0 | foreach my $module (qw(File::FcntlLock File::FcntlLock::Pure)) { | |||||||||
| 57 | 0 | eval qq{ | |||||||||
| 58 | require $module; | ||||||||||
| 59 | }; | ||||||||||
| 60 | 0 | ▶ | if (not $@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | my $fs = $module->new(l_type => F_WRLCK); | |||||||||
| 62 | 0 | ▶ | $fs->lock($fh, F_SETLKW) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | or syserr(g_('cannot get a write lock on %s'), $filename); | ||||||||||
| 64 | 0 | return; | |||||||||
| 65 | } | ||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | # On Linux systems the flock() locks get converted to file-range | ||||||||||
| 69 | # locks on NFS mounts. | ||||||||||
| 70 | 0 | ▶ | if ($^O ne 'linux') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | warning(g_('File::FcntlLock not available; using flock which is not NFS-safe')); | |||||||||
| 72 | } | ||||||||||
| 73 | 0 | ▶ | flock $fh, LOCK_EX | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | or syserr(g_('cannot get a write lock on %s'), $filename); | ||||||||||
| 75 | 0 | return; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | =head1 CHANGES | ||||||||||
| 79 | |||||||||||
| 80 | =head2 Version 0.xx | ||||||||||
| 81 | |||||||||||
| 82 | This is a private module. | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 1 | 1; | |||||||||