~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/lib/mtr_unique.pm

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- cperl -*-
 
2
# Copyright (C) 2006 MySQL AB
 
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; version 2 of the License.
 
7
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 
 
17
package mtr_unique;
 
18
 
 
19
use strict;
 
20
use Fcntl ':flock';
 
21
 
 
22
use base qw(Exporter);
 
23
our @EXPORT= qw(mtr_get_unique_id mtr_release_unique_id);
 
24
 
 
25
use My::Platform;
 
26
 
 
27
sub msg {
 
28
 # print "### unique($$) - ", join(" ", @_), "\n";
 
29
}
 
30
 
 
31
my $dir;
 
32
 
 
33
if(!IS_WINDOWS)
 
34
{
 
35
  $dir= "/tmp/mysql-unique-ids";
 
36
}
 
37
else
 
38
{
 
39
  # Try to use machine-wide directory location for unique IDs,
 
40
  # $ALLUSERSPROFILE . IF it is not available, fallback to $TEMP
 
41
  # which is typically a per-user temporary directory
 
42
  if (exists $ENV{'ALLUSERSPROFILE'} && -w $ENV{'ALLUSERSPROFILE'})
 
43
  {
 
44
    $dir= $ENV{'ALLUSERSPROFILE'}."/mysql-unique-ids";
 
45
  }
 
46
  else
 
47
  {
 
48
    $dir= $ENV{'TEMP'}."/mysql-unique-ids";
 
49
  }
 
50
}
 
51
 
 
52
my $mtr_unique_fh = undef;
 
53
 
 
54
END
 
55
{
 
56
  mtr_release_unique_id();
 
57
}
 
58
 
 
59
#
 
60
# Get a unique, numerical ID in a specified range.
 
61
#
 
62
# If no unique ID within the specified parameters can be
 
63
# obtained, return undef.
 
64
#
 
65
sub mtr_get_unique_id($$) {
 
66
  my ($min, $max)= @_;;
 
67
 
 
68
  msg("get $min-$max, $$");
 
69
 
 
70
  die "Can only get one unique id per process!" if defined $mtr_unique_fh;
 
71
 
 
72
 
 
73
  # Make sure our ID directory exists
 
74
  if (! -d $dir)
 
75
  {
 
76
    # If there is a file with the reserved
 
77
    # directory name, just delete the file.
 
78
    if (-e $dir)
 
79
    {
 
80
      unlink($dir);
 
81
    }
 
82
 
 
83
    mkdir $dir;
 
84
    chmod 0777, $dir;
 
85
 
 
86
    if(! -d $dir)
 
87
    {
 
88
      die "can't make directory $dir";
 
89
    }
 
90
  }
 
91
 
 
92
 
 
93
  my $fh;
 
94
  for(my $id = $min; $id <= $max; $id++)
 
95
  {
 
96
    open( $fh, ">$dir/$id");
 
97
    chmod 0666, "$dir/$id";
 
98
    # Try to lock the file exclusively. If lock succeeds, we're done.
 
99
    if (flock($fh, LOCK_EX|LOCK_NB))
 
100
    {
 
101
      # Store file handle - we would need it to release the ID (==unlock the file)
 
102
      $mtr_unique_fh = $fh;
 
103
      return $id;
 
104
    }
 
105
    else
 
106
    {
 
107
      close $fh;
 
108
    }
 
109
  }
 
110
  return undef;
 
111
}
 
112
 
 
113
 
 
114
#
 
115
# Release a unique ID.
 
116
#
 
117
sub mtr_release_unique_id()
 
118
{
 
119
  msg("release $$");
 
120
  if (defined $mtr_unique_fh)
 
121
  {
 
122
    close $mtr_unique_fh;
 
123
    $mtr_unique_fh = undef;
 
124
  }
 
125
}
 
126
 
 
127
 
 
128
1;
 
129