2 # Functions for FreeBSD package management
5 chop($system_arch = `uname -m`);
6 $package_dir = "/var/db/pkg";
8 sub list_package_system_commands
10 return ("pkg_info", "pkg_add");
13 # list_packages([package]*)
14 # Fills the array %packages with a list of all packages
18 local $arg = @_ ? join(" ", map { quotemeta($_) } @_) : "-a";
20 &open_execute_command(PKGINFO, "pkg_info -I $arg", 1, 1);
22 if (/^(\S+)\s+(.*)/) {
23 $packages{$i,'name'} = $1;
24 $packages{$i,'class'} = "";
25 $packages{$i,'desc'} = $2;
33 # package_info(package)
34 # Returns an array of package information in the order
35 # name, class, description, arch, version, vendor, installtime
38 local $qm = quotemeta($_[0]);
39 local $out = &backquote_command("pkg_info $qm 2>&1", 1);
41 local @rv = ( $_[0] );
43 push(@rv, $out =~ /Description:\n([\0-\177]*\S)/i ? $1 : $text{'bsd_unknown'});
44 push(@rv, $system_arch);
45 push(@rv, $_[0] =~ /-([^\-]+)$/ ? $1 : $text{'bsd_unknown'});
47 local @st = stat(&translate_filename("$package_dir/$_[0]"));
48 push(@rv, @st ? ctime($st[9]) : $text{'bsd_unknown'});
52 # check_files(package)
53 # Fills in the %files array with information about the files belonging
54 # to some package. Values in %files are path type user group mode size error
59 local $qm = quotemeta($_[0]);
60 &open_execute_command(PKGINFO, "pkg_info -L $qm", 1, 1);
61 while($file = <PKGINFO>) {
63 next if ($file !~ /^\//);
64 local $real = &translate_filename($file);
65 local @st = stat($real);
66 $files{$i,'path'} = $file;
67 $files{$i,'type'} = -l $real ? 3 :
69 $files{$i,'user'} = getpwuid($st[4]);
70 $files{$i,'group'} = getgrgid($st[5]);
71 $files{$i,'mode'} = sprintf "%o", $st[2] & 07777;
72 $files{$i,'size'} = $st[7];
73 $files{$i,'link'} = readlink($real);
79 # package_files(package)
80 # Returns a list of all files in some package
84 local $qn = quotemeta($pkg);
86 &open_execute_command(RPM, "pkg_info -L $qn", 1, 1);
97 # installed_file(file)
98 # Given a filename, fills %file with details of the given file and returns 1.
99 # If the file is not known to the package system, returns 0
100 # Usable values in %file are path type user group mode size packages
103 local (%packages, $file, $i, @pkgin);
104 local $n = &list_packages();
105 for($i=0; $i<$n; $i++) {
106 &open_execute_command(PKGINFO, "pkg_info -L $packages{$i,'name'}", 1, 1);
107 while($file = <PKGINFO>) {
109 if ($file eq $_[0]) {
111 push(@pkgin, $packages{$i,'name'});
117 local $real = &translate_filename($_[0]);
118 local @st = stat($real);
119 $file{'path'} = $_[0];
120 $file{'type'} = -l $real ? 3 :
122 $file{'user'} = getpwuid($st[4]);
123 $file{'group'} = getgrgid($st[5]);
124 $file{'mode'} = sprintf "%o", $st[2] & 07777;
125 $file{'size'} = $st[7];
126 $file{'link'} = readlink($real);
127 $file{'packages'} = join(" ", @pkgin);
138 local $real = &translate_filename($_[0]);
139 local $qm = quotemeta($_[0]);
141 # A directory .. see if it contains any tgz files
143 local @list = grep { /\.tgz$/ || /\.tbz$/ } readdir(DIR);
145 return @list ? 1 : 0;
147 elsif ($_[0] =~ /\*|\?/) {
148 # a wildcard .. see what it matches
149 local @list = glob($real);
150 return @list ? 1 : 0;
153 # just a normal file - see if it is a package
155 foreach $cmd ('gunzip', 'bunzip2') {
156 next if (!&has_command($cmd));
157 local ($desc, $contents);
158 &open_execute_command(TAR, "$cmd -c $qm | tar tf -", 1, 1);
160 $desc++ if (/^\+DESC/);
161 $contents++ if (/^\+CONTENTS/);
164 return 1 if ($desc && $contents);
170 # file_packages(file)
171 # Returns a list of all packages in the given file, in the form
172 # package description
175 local $real = &translate_filename($_[0]);
176 local $qm = quotemeta($_[0]);
178 # Scan directory for packages
181 while($f = readdir(DIR)) {
182 if ($f =~ /\.tgz$/i || $f =~ /\.tbz$/i) {
183 local @pkg = &file_packages("$_[0]/$f");
190 elsif ($real =~ /\*|\?/) {
191 # Expand glob of packages
192 # XXX won't work in translation
194 foreach $f (glob($real)) {
195 local @pkg = &file_packages($f);
203 foreach $cmd ('gunzip', 'bunzip2') {
204 next if (!&has_command($cmd));
205 local $temp = &transname();
206 &make_dir($temp, 0700);
207 local $rv = &execute_command("cd $temp && $cmd -c $qm | tar xf - +CONTENTS +COMMENT");
212 local ($comment, $name);
213 &open_readfile(COMMENT, "$temp/+COMMENT");
214 ($comment = <COMMENT>) =~ s/\r|\n//g;
216 &open_readfile(CONTENTS, "$temp/+CONTENTS");
218 $name = $1 if (/^\@name\s+(\S+)/);
222 return ( "$name $comment" );
228 # install_options(file, package)
229 # Outputs HTML for choosing install options
232 print &ui_table_row($text{'bsd_scripts'},
233 &ui_radio("scripts", 0, [ [ 0, $text{'yes'} ], [ 1, $text{'no'} ] ]));
235 print &ui_table_row($text{'bsd_force'},
236 &ui_yesno_radio("force", 0));
239 # install_package(file, package)
240 # Installs the package in the given file, with options from %in
243 local $in = $_[2] ? $_[2] : \%in;
244 local $args = ($in->{"scripts"} ? " -I" : "").
245 ($in->{"force"} ? " -f" : "");
246 local $out = &backquote_logged("pkg_add $args $_[0] 2>&1");
248 return "<pre>$out</pre>";
253 # install_packages(file, [&inputs])
254 # Installs all the packages in the given file or glob
257 local $in = $_[2] ? $_[2] : \%in;
258 local $args = ($in->{"scripts"} ? " -I" : "").
259 ($in->{"force"} ? " -f" : "");
262 $file = "$_[0]/*.tgz";
267 local $out = &backquote_logged("pkg_add $args $file 2>&1");
269 return "<pre>$out</pre>";
274 # delete_package(package)
275 # Totally remove some package
278 local $out = &backquote_logged("pkg_delete $_[0] 2>&1");
279 if ($?) { return "<pre>$out</pre>"; }
285 return &text('bsd_manager', "FreeBSD");
290 return "pkg_add pkg_info pkg_delete";