debian: Get installed package list in the ph_init handler.
[febootstrap.git] / src / febootstrap_debian.ml
1 (* febootstrap 3
2  * Copyright (C) 2009-2011 Red Hat Inc.
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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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
17  *)
18
19 (* Debian support. *)
20
21 open Unix
22 open Printf
23
24 open Febootstrap_package_handlers
25 open Febootstrap_utils
26 open Febootstrap_cmdline
27
28 (* Create a temporary directory for use by all the functions in this file. *)
29 let tmpdir = tmpdir ()
30
31 let debian_detect () =
32   file_exists "/etc/debian_version" &&
33     Config.aptitude <> "no" && Config.apt_cache <> "no" && Config.dpkg <> "no"
34
35 let installed_pkgs = ref []
36
37 let debian_init () =
38   installed_pkgs :=
39     run_command_get_lines "dpkg-query --show --showformat='${Package}\\n'"
40
41 let get_installed_pkgs () =
42   match !installed_pkgs with
43     | [] -> assert false
44     | pkgs -> pkgs
45
46 let rec debian_resolve_dependencies_and_download names =
47   let cmd =
48     sprintf "%s depends --recurse -i %s | grep -v '^[<[:space:]]'"
49       Config.apt_cache
50       (String.concat " " (List.map Filename.quote names)) in
51   let pkgs = run_command_get_lines cmd in
52   let pkgs =
53     if Config.apt_cache_depends_recurse_broken then
54       workaround_broken_apt_cache_depends_recurse (sort_uniq pkgs)
55     else
56       pkgs in
57
58   (* Exclude packages matching [--exclude] regexps on the command line. *)
59   let pkgs =
60     List.filter (
61       fun name ->
62         not (List.exists (fun re -> Str.string_match re name 0) excludes)
63     ) pkgs in
64
65   let present_pkgs, download_pkgs = List.partition (
66     fun pkg -> List.exists ((=) pkg) (get_installed_pkgs ())
67   ) pkgs in
68
69   debug "wanted packages (present / download): %s / %s\n"
70     (String.concat " " present_pkgs)
71     (String.concat " " download_pkgs);
72
73   (* Download the packages. *)
74   if (List.length download_pkgs > 0)
75   then (
76     let cmd =
77       sprintf "umask 0000; cd %s && %s download %s"
78         (Filename.quote tmpdir)
79         Config.aptitude
80         (String.concat " " (List.map Filename.quote download_pkgs)) in
81     run_command cmd
82   );
83
84   (* Find out what aptitude downloaded. *)
85   let files = Sys.readdir tmpdir in
86
87   let download_pkgs = List.map (
88     fun pkg ->
89       (* Look for 'pkg_*.deb' in the list of files. *)
90       let pre = pkg ^ "_" in
91       let r = ref "" in
92       try
93         for i = 0 to Array.length files - 1 do
94           if string_prefix pre files.(i) then (
95             r := files.(i);
96             files.(i) <- "";
97             raise Exit
98           )
99         done;
100         eprintf "febootstrap: aptitude: error: no file was downloaded corresponding to package %s\n" pkg;
101         exit 1
102       with
103           Exit -> !r
104   ) download_pkgs in
105
106   List.sort compare (List.append present_pkgs download_pkgs)
107
108 (* On Ubuntu 10.04 LTS, apt-cache depends --recurse is broken.  It
109  * doesn't return the full list of dependencies.  Therefore recurse
110  * into these dependencies one by one until we reach a fixpoint.
111  *)
112 and workaround_broken_apt_cache_depends_recurse names =
113   debug "workaround for broken 'apt-cache depends --recurse' command:\n  %s"
114     (String.concat " " names);
115
116   let names' =
117     List.map (
118       fun name ->
119         let cmd =
120           sprintf "%s depends --recurse -i %s | grep -v '^[<[:space:]]'"
121             Config.apt_cache (Filename.quote name) in
122         run_command_get_lines cmd
123     ) names in
124   let names' = List.flatten names' in
125   let names' = sort_uniq names' in
126   if names <> names' then
127     workaround_broken_apt_cache_depends_recurse names'
128   else
129     names
130
131 let debian_list_files_downloaded pkg =
132   debug "unpacking %s ..." pkg;
133
134   (* We actually need to extract the file in order to get the
135    * information about modes etc.
136    *)
137   let pkgdir = tmpdir // pkg ^ ".d" in
138   mkdir pkgdir 0o755;
139   let cmd =
140     sprintf "umask 0000; dpkg-deb --fsys-tarfile %s | (cd %s && tar xf -)"
141       (tmpdir // pkg) pkgdir in
142   run_command cmd;
143
144   let cmd = sprintf "cd %s && find ." pkgdir in
145   let lines = run_command_get_lines cmd in
146
147   let files = List.map (
148     fun path ->
149       assert (path.[0] = '.');
150       (* No leading '.' *)
151       let path =
152         if path = "." then "/"
153         else String.sub path 1 (String.length path - 1) in
154
155       (* Find out what it is and get the canonical filename. *)
156       let statbuf = lstat (pkgdir // path) in
157       let is_dir = statbuf.st_kind = S_DIR in
158
159       (* No per-file metadata like in RPM, but we can synthesize it
160        * from the path.
161        *)
162       let config = statbuf.st_kind = S_REG && string_prefix "/etc/" path in
163
164       let mode = statbuf.st_perm in
165
166       (path, { ft_dir = is_dir; ft_config = config; ft_mode = mode;
167                ft_ghost = false; ft_size = statbuf.st_size })
168   ) lines in
169
170   files
171
172 let debian_list_files_installed pkg =
173   debug "using installed package %s ..." pkg;
174   let cmd = sprintf "dpkg-query --listfiles %s" pkg in
175   let lines = run_command_get_lines cmd in
176   (* filter out lines not directly describing fs objects such as
177      "package diverts others to: /path/to/..." *)
178   let lines = List.filter (
179     fun l -> l.[0] = '/' && l.[1] != '.'
180   ) lines in
181   let files = List.map (
182     fun path ->
183       let statbuf = lstat path in
184       let is_dir = statbuf.st_kind = S_DIR in
185       let config = statbuf.st_kind = S_REG && string_prefix "/etc/" path in
186       let mode = statbuf.st_perm in
187       (path, { ft_dir = is_dir; ft_config = config; ft_mode = mode;
188                ft_ghost = false; ft_size = statbuf.st_size })
189   ) lines in
190   files
191
192 let debian_list_files pkg =
193   if use_installed && List.exists ((=) pkg) (get_installed_pkgs ()) then
194     debian_list_files_installed pkg
195   else
196     debian_list_files_downloaded pkg
197
198 (* Easy because we already unpacked the archive above. *)
199 let debian_get_file_from_package pkg file =
200   if use_installed && List.exists (fun p -> p = pkg) (get_installed_pkgs ())
201   then
202     file
203   else
204     tmpdir // pkg ^ ".d" // file
205
206 let () =
207   let ph = {
208     ph_detect = debian_detect;
209     ph_init = debian_init;
210     ph_resolve_dependencies_and_download =
211       debian_resolve_dependencies_and_download;
212     ph_list_files = debian_list_files;
213     ph_get_file_from_package = debian_get_file_from_package;
214   } in
215   register_package_handler "debian" ph