source: trunk/third/firefox/config/mozLock.pm @ 21695

Revision 21695, 2.8 KB checked in by rbasch, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21694, which included commits to RCS files with non-trunk default branches.
Line 
1#
2# The contents of this file are subject to the Mozilla Public
3# License Version 1.1 (the "License"); you may not use this file
4# except in compliance with the License. You may obtain a copy of
5# the License at http://www.mozilla.org/MPL/
6#
7# Software distributed under the License is distributed on an "AS
8# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9# implied. See the License for the specific language governing
10# rights and limitations under the License.
11#
12# The Original Code is mozilla.org code.
13#
14# The Initial Developer of the Original Code is Netscape
15# Communications Corporation.  Portions created by Netscape are
16# Copyright (C) 2001 Netscape Communications Corporation. All
17# Rights Reserved.
18#
19# Contributor(s):
20#       Christopher Seawood <cls@seawood.org>
21
22package mozLock;
23
24use strict;
25use IO::File;
26use Cwd;
27
28BEGIN {
29    use Exporter ();
30    use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
32    $VERSION = 1.00;
33    @ISA = qw(Exporter);
34    @EXPORT = qw(&mozLock &mozUnlock);
35    %EXPORT_TAGS = ( );
36    @EXPORT_OK = qw();
37}
38
39my $lockcounter = 0;
40my $locklimit = 100;
41my $locksleep = 0.1;
42my %lockhash;
43
44# File::Spec->rel2abs appears to be broken in ActiveState Perl 5.22
45# so roll our own
46sub priv_abspath($) {
47    my ($file) = @_;
48    my ($dir, $out);
49    my (@inlist, @outlist);
50
51    # Force files to have unix paths.
52    $file =~ s/\\/\//g;
53
54    # Check if file is already absolute
55    if ($file =~ m/^\// || substr($file, 1, 1) eq ':') {
56        return $file;
57    }
58    $out = cwd . "/$file";
59
60    # Do what File::Spec->canonpath should do
61    @inlist = split(/\//, $out);
62    foreach $dir (@inlist) {
63        if ($dir eq '..') {
64            pop @outlist;
65        } else {
66            push @outlist, $dir;
67        }
68    }
69    $out = join '/',@outlist;
70    return $out;
71}
72
73sub mozLock($) {
74    my ($inlockfile) = @_;
75    my ($lockhandle, $lockfile);
76    $lockfile = priv_abspath($inlockfile);
77    #print "LOCK: $lockfile\n";
78    $lockcounter = 0;
79    $lockhandle = new IO::File || die "Could not create filehandle for $lockfile: $!\n";   
80    while ($lockcounter < $locklimit) {
81        if (! -e $lockfile) {
82            open($lockhandle, ">$lockfile") || die "$lockfile: $!\n";
83            $lockhash{$lockfile} = $lockhandle;
84            last;
85        }
86        $lockcounter++;
87        select(undef,undef,undef, $locksleep);
88    }
89    if ($lockcounter >= $locklimit) {
90        undef $lockhandle;
91        die "$0: Could not get lockfile $lockfile.\nRemove $lockfile to clear up\n";
92    }
93}
94
95sub mozUnlock($) {
96    my ($inlockfile) = @_;
97    my ($lockhandle, $lockfile);
98    #$lockfile = File::Spec->rel2abs($inlockfile);
99    $lockfile = priv_abspath($inlockfile);
100    #print "UNLOCK: $lockfile\n";
101    $lockhandle = $lockhash{$lockfile};
102    if (defined($lockhandle)) {
103        close($lockhandle);
104        $lockhash{$lockfile} = undef;
105        unlink($lockfile);
106    } else {
107        print "WARNING: $0: lockhandle for $lockfile not defined.  Lock may not be removed.\n";
108    }
109}
110
111END {};
112
1131;
Note: See TracBrowser for help on using the repository browser.