00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00032 #include "config.h"
00033 #include <iostream>
00034 #include <string>
00035 #ifdef __GNUG__
00036 #pragma implementation "apt-pkg/cachefile.h"
00037 #endif
00038
00039 #include <apt-pkg/init.h>
00040 #include <apt-pkg/cachefile.h>
00041 #include <apt-pkg/error.h>
00042 #include <apt-pkg/sourcelist.h>
00043 #include <apt-pkg/pkgcachegen.h>
00044 #include <apt-pkg/pkgcache.h>
00045 #include <apt-pkg/configuration.h>
00046 #include <apt-pkg/policy.h>
00047 #include <apt-pkg/pkgsystem.h>
00048 #include <glib.h>
00049 #include <glib/gi18n.h>
00050 #include "langupdate.h"
00051
00053 static GList * apt_list = NULL;
00055 static GHashTable * src_table = NULL;
00057 static const gchar * apt_quiet = "-q -q";
00058 static pkgCache *LangCache = 0;
00059 static pkgSourceList *SrcList = 0;
00060
00061 static bool LoadPkgNames(void)
00062 {
00063 pkgCache &Cache = *LangCache;
00064 pkgCache::PkgIterator I = Cache.PkgBegin();
00065 bool All = _config->FindB("APT::Cache::AllNames","false");
00066 for (;I.end() != true; I++)
00067 {
00068 gchar * str;
00069 if (All == false && I->VersionList == 0)
00070 continue;
00071 str = g_strdup(I.Name());
00072 apt_list = g_list_prepend(apt_list, str);
00073 }
00074 return true;
00075 }
00076
00079 static bool LoadSourceNames (gint verbose)
00080 {
00081 gchar * apt, *config_str, *out, *src, *bin;
00082 gchar ** lines, **name;
00083 guint c;
00084 GList * p;
00085
00086 pkgSrcRecords SrcRecs(*SrcList);
00087 pkgSrcRecords::Parser *Parse;
00088 src_table = g_hash_table_new (g_str_hash, g_str_equal);
00089
00090 p = NULL;
00091 for (p = apt_list; p != NULL; p=p->next)
00092 {
00093 bin = g_strdup ((gchar*)p->data);
00094 while ((Parse = SrcRecs.Find(bin, false)) != 0)
00095 {
00096 string aptstr;
00097
00098
00099 if (verbose >= 4)
00100 g_message ("Inside SrcRecs.Find - not using workaround.");
00101 aptstr = Parse->AsStr();
00102 src = g_strdup(aptstr.data());
00103 g_hash_table_insert (src_table, bin, src);
00104 }
00105 }
00106 if (g_hash_table_size (src_table) > 0)
00107 return true;
00108
00109 p = NULL;
00110 for (p = apt_list; p != NULL; p=p->next)
00111 {
00112 bin = g_strdup ((gchar*)p->data);
00113 apt = g_strconcat ("apt-cache ", NULL);
00114 config_str = g_strconcat(apt, lu_get_aptstring(), " show ", bin, NULL);
00115 g_spawn_command_line_sync (config_str, &out, NULL, NULL, NULL);
00116 g_free (apt);
00117 g_free (config_str);
00118 lines = g_strsplit (out, "\n", -1);
00119 for (c = 0; c <= g_strv_length(lines); c++)
00120 {
00121 if (!lines[c])
00122 continue;
00123 if (g_str_has_prefix (lines[c], "Source: "))
00124 {
00125 name = g_strsplit (lines[c], ":", -1);
00126 src = g_strdup (name[1]);
00127 src = g_strstrip (src);
00128 if (verbose >= 4)
00129 g_printerr ("DEBUG:Adding %s (%s)\n", bin, src);
00130 g_hash_table_insert (src_table, bin, src);
00131 g_strfreev (name);
00132 }
00133 }
00134 g_strfreev (lines);
00135 }
00136 return true;
00137 }
00138
00139 void
00140 lu_clear_caches (void)
00141 {
00142 g_hash_table_destroy (src_table);
00143 }
00144
00145 gboolean
00146 apt_init (const gchar * sourcelist, const gchar * suite_codename, gint verbose)
00147 {
00148 FILE * fout = NULL;
00149 gchar * config_str, * path, *quiet, * apt, * out, *errstr;
00150 MMap *Map = 0;
00151 GError * em_gerr = NULL;
00152
00153
00154 out = errstr = NULL;
00155 path = g_strconcat ("/var/lib/"PACKAGE"/", suite_codename, "/lists/partial", NULL);
00156 g_mkdir_with_parents (path, 0755);
00157 g_free (path);
00158 path = g_strconcat ("/var/lib/"PACKAGE"/", suite_codename, "/archives/partial", NULL);
00159 g_mkdir_with_parents (path, 0755);
00160 g_free (path);
00161 fout = fopen ("/var/lib/"PACKAGE"/status", "a");
00162 fclose (fout);
00163 path = g_strconcat ("/var/lib/"PACKAGE"/", suite_codename, "/lists/lock", NULL);
00164 fout = fopen (path, "a");
00165 fclose (fout);
00166 g_free (path);
00167 path = g_strconcat ("/var/lib/"PACKAGE"/var/log/apt/", NULL);
00168 g_mkdir_with_parents (path, 0755);
00169 g_free (path);
00170 if (verbose <= 1)
00171 quiet = g_strdup (apt_quiet);
00172 else if (verbose >= 2)
00173 quiet = g_strndup(apt_quiet, 2);
00174 else
00175 quiet = g_strdup ("");
00176 apt = g_strconcat ("apt-get ", quiet, NULL);
00177 config_str = g_strconcat(apt, lu_get_aptstring(), " update", NULL);
00178 g_free (quiet);
00179 g_free (apt);
00180 g_spawn_command_line_sync (config_str, &out, &errstr, NULL, &em_gerr);
00181 if (em_gerr)
00182 {
00183 g_critical (em_gerr->message);
00184 g_message (_("Unable to execute command: %s\n%s"), config_str, out);
00185 if (errstr)
00186 g_message (_("Error message was: %s"), errstr);
00187 g_clear_error (&em_gerr);
00188 }
00189 if (verbose >= 4)
00190 g_print ("%s: %s\n", PACKAGE, config_str);
00191
00192 g_return_val_if_fail (pkgInitConfig(*_config), FALSE);
00193 g_return_val_if_fail (pkgInitSystem(*_config,_system), FALSE);
00194 if (verbose <= 1)
00195 _config->Set("quiet", 2);
00196 else
00197 _config->Set("quiet", 0);
00198 _config->Set("Apt::Architecture", HOST_CPU);
00199 _config->Set("help", true);
00200 _config->Set("APT::Get::List-Cleanup", "on");
00201 _config->Set("Apt::Install-Recommends","false");
00202 _config->Set("Dir", "/var/lib/"PACKAGE);
00203 _config->Set("Dir::Etc", "/var/lib/"PACKAGE);
00204 _config->Set("Dir::Etc::SourceList", "/var/lib/"PACKAGE"/sources.list");
00205 _config->Set("Dir::State", suite_codename);
00206 _config->Set("Dir::State::Status", "/var/lib/"PACKAGE"/status");
00207 _config->Set("Dir::Cache", suite_codename);
00208 if (_config->FindB("APT::Cache::Generate",true) == false)
00209 Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
00210 FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
00211 else
00212 {
00213
00214 SrcList = new pkgSourceList;
00215 if (SrcList->Read(sourcelist) == false)
00216 {
00217 g_error (_("Failed to read sources"));
00218 return false;
00219 }
00220
00221
00222 OpProgress Prog;
00223 pkgMakeStatusCache(*SrcList,Prog,&Map,true);
00224 }
00225
00226
00227 if (_error->PendingError())
00228 {
00229 _error->DumpErrors();
00230 return FALSE;
00231 }
00232 pkgCache Cache(Map);
00233 LangCache = &Cache;
00234
00235
00236 if (_error->empty() == false)
00237 {
00238 bool Errors = _error->PendingError();
00239 _error->DumpErrors();
00240 return Errors == true?100:0;
00241 }
00242 LoadPkgNames();
00243 LoadSourceNames(verbose);
00244 return TRUE;
00245 }
00246
00247 gint
00248 check_pkg_name (gconstpointer a, gconstpointer b)
00249 {
00250 gchar * test, * avail;
00251 test = (gchar*)a;
00252 avail = (gchar*)b;
00253 return g_strcmp0 (test, avail);
00254 }
00255
00256 gboolean
00257 aptcache_lookup (const gchar * pkg)
00258 {
00259 GList * available = NULL;
00260
00261 available = g_list_find_custom (apt_list, pkg, check_pkg_name);
00262 if (g_list_length (available))
00263 return TRUE;
00264 return FALSE;
00265 }
00266
00267 gchar *
00268 lu_get_sourcepkg (const gchar * binary)
00269 {
00270 gchar * src;
00271
00272 src = NULL;
00273 src = (gchar*)g_hash_table_lookup (src_table, binary);
00274 return src;
00275 }
00276