2 * Copyright (C) 2009-2011 Red Hat Inc.
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 open Febootstrap_package_handlers
25 open Febootstrap_utils
26 open Febootstrap_cmdline
28 (* Create a temporary directory for use by all the functions in this file. *)
29 let tmpdir = tmpdir ()
31 let debian_detect () =
32 file_exists "/etc/debian_version" &&
33 Config.aptitude <> "no" && Config.apt_cache <> "no" && Config.dpkg <> "no"
35 let installed_pkgs = ref []
39 run_command_get_lines "dpkg-query --show --showformat='${Package}\\n'"
41 let get_installed_pkgs () =
42 match !installed_pkgs with
46 let rec debian_resolve_dependencies_and_download names =
48 sprintf "%s depends --recurse -i %s | grep -v '^[<[:space:]]'"
50 (String.concat " " (List.map Filename.quote names)) in
51 let pkgs = run_command_get_lines cmd in
53 if Config.apt_cache_depends_recurse_broken then
54 workaround_broken_apt_cache_depends_recurse (sort_uniq pkgs)
58 (* Exclude packages matching [--exclude] regexps on the command line. *)
62 not (List.exists (fun re -> Str.string_match re name 0) excludes)
65 let present_pkgs, download_pkgs =
66 if not use_installed then
70 fun pkg -> List.exists ((=) pkg) (get_installed_pkgs ())
73 debug "wanted packages (present / download): %s / %s\n"
74 (String.concat " " present_pkgs)
75 (String.concat " " download_pkgs);
77 (* Download the packages. *)
78 if (List.length download_pkgs > 0)
81 sprintf "umask 0000; cd %s && %s download %s"
82 (Filename.quote tmpdir)
84 (String.concat " " (List.map Filename.quote download_pkgs)) in
88 (* Find out what aptitude downloaded. *)
89 let files = Sys.readdir tmpdir in
91 let download_pkgs = List.map (
93 (* Look for 'pkg_*.deb' in the list of files. *)
94 let pre = pkg ^ "_" in
97 for i = 0 to Array.length files - 1 do
98 if string_prefix pre files.(i) then (
104 eprintf "febootstrap: aptitude: error: no file was downloaded corresponding to package %s\n" pkg;
110 List.sort compare (List.append present_pkgs download_pkgs)
112 (* On Ubuntu 10.04 LTS, apt-cache depends --recurse is broken. It
113 * doesn't return the full list of dependencies. Therefore recurse
114 * into these dependencies one by one until we reach a fixpoint.
116 and workaround_broken_apt_cache_depends_recurse names =
117 debug "workaround for broken 'apt-cache depends --recurse' command:\n %s"
118 (String.concat " " names);
124 sprintf "%s depends --recurse -i %s | grep -v '^[<[:space:]]'"
125 Config.apt_cache (Filename.quote name) in
126 run_command_get_lines cmd
128 let names' = List.flatten names' in
129 let names' = sort_uniq names' in
130 if names <> names' then
131 workaround_broken_apt_cache_depends_recurse names'
135 let debian_list_files_downloaded pkg =
136 debug "unpacking %s ..." pkg;
138 (* We actually need to extract the file in order to get the
139 * information about modes etc.
141 let pkgdir = tmpdir // pkg ^ ".d" in
144 sprintf "umask 0000; dpkg-deb --fsys-tarfile %s | (cd %s && tar xf -)"
145 (tmpdir // pkg) pkgdir in
148 let cmd = sprintf "cd %s && find ." pkgdir in
149 let lines = run_command_get_lines cmd in
151 let files = List.map (
153 assert (path.[0] = '.');
156 if path = "." then "/"
157 else String.sub path 1 (String.length path - 1) in
159 (* Find out what it is and get the canonical filename. *)
160 let statbuf = lstat (pkgdir // path) in
161 let is_dir = statbuf.st_kind = S_DIR in
163 (* No per-file metadata like in RPM, but we can synthesize it
166 let config = statbuf.st_kind = S_REG && string_prefix "/etc/" path in
168 let mode = statbuf.st_perm in
170 (path, { ft_dir = is_dir; ft_config = config; ft_mode = mode;
171 ft_ghost = false; ft_size = statbuf.st_size })
176 let debian_list_files_installed pkg =
177 debug "using installed package %s ..." pkg;
178 let cmd = sprintf "dpkg-query --listfiles %s" pkg in
179 let lines = run_command_get_lines cmd in
180 (* filter out lines not directly describing fs objects such as
181 "package diverts others to: /path/to/..." *)
182 let lines = List.filter (
183 fun l -> l.[0] = '/' && l.[1] != '.'
185 let files = List.map (
187 let statbuf = lstat path in
188 let is_dir = statbuf.st_kind = S_DIR in
189 let config = statbuf.st_kind = S_REG && string_prefix "/etc/" path in
190 let mode = statbuf.st_perm in
191 (path, { ft_dir = is_dir; ft_config = config; ft_mode = mode;
192 ft_ghost = false; ft_size = statbuf.st_size })
196 let debian_list_files pkg =
197 if use_installed && List.exists ((=) pkg) (get_installed_pkgs ()) then
198 debian_list_files_installed pkg
200 debian_list_files_downloaded pkg
202 (* Easy because we already unpacked the archive above. *)
203 let debian_get_file_from_package pkg file =
204 if use_installed && List.exists (fun p -> p = pkg) (get_installed_pkgs ())
208 tmpdir // pkg ^ ".d" // file
212 ph_detect = debian_detect;
213 ph_init = debian_init;
214 ph_resolve_dependencies_and_download =
215 debian_resolve_dependencies_and_download;
216 ph_list_files = debian_list_files;
217 ph_get_file_from_package = debian_get_file_from_package;
219 register_package_handler "debian" ph