deal-with-linux-large-uids-20020115
[openafs.git] / src / tests / afsconf.pm
1 # CMUCS AFStools
2 # Copyright (c) 1996, Carnegie Mellon University
3 # All rights reserved.
4 #
5 # See CMUCS/CMU_copyright.pm for use and distribution information
6
7 package OpenAFS::afsconf;
8
9 =head1 NAME
10
11 OpenAFS::afsconf - Access to AFS config info
12
13 =head1 SYNOPSIS
14
15   use OpenAFS::afsconf;
16
17   $cell = AFS_conf_localcell();
18   $cell = AFS_conf_canoncell($cellname);
19   @servers = AFS_conf_cellservers($cellname);
20   @cells = AFS_conf_listcells();
21   %info = AFS_conf_cacheinfo();
22
23 =head1 DESCRIPTION
24
25 This module provides access to information about the local workstation's
26 AFS configuration.  This includes information like the name of the
27 local cell, where AFS is mounted, and access to information in the
28 F<CellServDB>.  All information returned by this module is based on the
29 configuration files, and does not necessarily reflect changes made
30 on the afsd command line or using B<fs> commands.
31
32 =cut
33
34 use OpenAFS::CMU_copyright;
35 use OpenAFS::config;
36 use OpenAFS::util qw(:DEFAULT :afs_internal);
37 use Exporter;
38
39 $VERSION   = '';
40 $VERSION   = '1.00';
41 @ISA       = qw(Exporter);
42 @EXPORT    = qw(&AFS_conf_localcell
43                 &AFS_conf_canoncell
44                 &AFS_conf_listcells
45                 &AFS_conf_cellservers
46                 &AFS_conf_cacheinfo);
47
48
49 # _confpath($file) - Return path to a configuration file
50 sub _confpath {
51   my($file) = @_;
52
53   if ($conf_paths{$file}) {
54     $conf_paths{$file};
55   } elsif ($AFS_Parms{confdir} && -r "$AFS_Parms{confdir}/$file") {
56     $conf_paths{$file} = "$AFS_Parms{confdir}/$file";
57   } elsif (-r "$def_ConfDir/$file") {
58     $conf_paths{$file} = "$def_ConfDir/$file";
59   } else {
60     die "Unable to locate $file\n";
61   }
62 }
63
64 =head2 AFS_conf_localcell()
65
66 Return the canonical name of the local cell.  This depends on the contents
67 of the F<ThisCell> file in the AFS configuration directory.
68
69 =cut
70
71 $AFS_Help{conf_localcell} = '=> $lclcell';
72 sub AFS_conf_localcell {
73   my($path) = _confpath(ThisCell);
74   my($result);
75
76   return '' if (!$path);
77   if (open(THISCELL, $path)) {
78     chomp($result = <THISCELL>);
79     close(THISCELL);
80     $result;
81   } else {
82     die "Unable to open $path: $!\n";
83   }
84 }
85
86 =head2 AFS_conf_canoncell($cellname)
87
88 Return the canonical name of the specified cell, as found in F<CellServDB>.
89 I<$cellname> may be any unique prefix of a cell name, as with various AFS
90 commands that take cell names as arguments.
91
92 =head2 AFS_conf_cellservers($cellname)
93
94 Return a list of servers in the specified cell.  As with B<AFS_conf_canoncell>,
95 I<$cellname> may be any unique prefix of a cell name.  The resulting list
96 contains server hostnames, as found in F<CellServDB>.
97
98 =cut
99
100 $AFS_Help{conf_canoncell} = '$cellname => $canon';
101 $AFS_Help{conf_cellservers} = '$cellname => @servers';
102
103 sub AFS_conf_canoncell   { &_findcell($_[0], 0); }
104 sub AFS_conf_cellservers { &_findcell($_[0], 1); }
105
106 sub _findcell {
107   my($cellname, $doservers) = @_;
108   my($path, $found, @servers, $looking);
109
110   return $canon_name{$cellname} if (!$doservers && $canon_name{$cellname});
111   $path = _confpath(CellServDB) || die "Unable to locate CellServDB\n";
112
113   if (open(CELLSERVDB, $path)) {
114     my($cellpat) = $cellname;
115     $cellpat =~ s/(\W)/\\$1/g;
116     while (<CELLSERVDB>) {
117       $looking = 0 if (/^\>/);
118       if (/^\>$cellpat/) {
119         if ($found) {
120           close(CELLSERVDB);
121           die "Cell name $cellname is not unique\n";
122         } else {
123           chop($found = $_);
124           $found =~ s/^\>(\S+).*/$1/;
125           $looking = 1 if ($doservers);
126         }
127       } elsif ($looking && (/^[\.\d]+\s*\#\s*(.*\S+)/ || /^([\.\d]+)/)) {
128         push(@servers, $1);
129       }
130     }
131     close(CELLSERVDB);
132     if ($found) {
133       $canon_name{$cellname} = $found;
134       $doservers ? @servers : ($found);
135     } else {
136       die "Cell $cellname not in CellServDB\n";
137     }
138   } else {
139     die "Unable to open $path: $!\n";
140   }
141 }
142
143 =head2 AFS_conf_listcells()
144
145 Return a list of canonical names (as found in F<CellServDB>) of all
146 known AFS cells.
147
148 =cut
149
150 $AFS_Help{conf_listcells} = '=> @cells';
151 sub AFS_conf_listcells {
152   my($path, @cells);
153
154   $path = _confpath(CellServDB) || die "Unable to locate CellServDB!\n";
155
156   if (open(CELLSERVDB, $path)) {
157     while (<CELLSERVDB>) {
158       if (/^\>(\S+)/) {
159         push(@cells, $1);
160       }
161     }
162     close(CELLSERVDB);
163     @cells;
164   } else {
165     die "Unable to open $path: $!\n";
166   }
167 }
168
169 =head2 AFS_conf_cacheinfo()
170
171 Return a table of information about the local workstation's cache
172 configuration.  This table may contain any or all of the following elements:
173
174 =over 14
175
176 =item afsroot
177
178 Mount point for the AFS root volume
179
180 =item cachedir
181
182 Location of the AFS cache directory
183
184 =item cachesize
185
186 AFS cache size
187
188 =item hardcachesize
189
190 Hard limit on AFS cache size (if specified; probably Mach-specific)
191
192 =item translator
193
194 Name of AFS/NFS translator server (if set)
195
196 =back
197
198 =cut
199
200 $AFS_Help{conf_cacheinfo} = '=> %info';
201 sub AFS_conf_cacheinfo {
202   my($path) = _confpath('cacheinfo');
203   my(%result, $line, $hcs);
204
205   if ($path) {
206     if (open(CACHEINFO, $path)) {
207       chop($line = <CACHEINFO>);
208       close(CACHEINFO);
209       (@result{'afsroot', 'cachedir', 'cachesize'} , $hcs) = split(/:/, $line);
210       $result{'hardcachesize'} = $hcs if ($hcs);
211     } else {
212       die "Unable to open $path: $!\n";
213     }
214   }
215   if ($ENV{'AFSSERVER'}) {
216     $result{'translator'} = $ENV{'AFSSERVER'};
217   } elsif (open(SRVFILE, "$ENV{HOME}/.AFSSERVER")
218            || open(SRVFILE, "/.AFSSERVER")) {
219     $result{'translator'} = <SRVFILE>;
220     close(SRVFILE);
221   }
222   %result;
223 }
224
225
226 1;
227
228 =head1 COPYRIGHT
229
230 The CMUCS AFStools, including this module are
231 Copyright (c) 1996, Carnegie Mellon University.  All rights reserved.
232 For use and redistribution information, see CMUCS/CMU_copyright.pm
233
234 =cut