]> cygwin.com Git - cygwin-apps/setup.git/blob - package_db.cc
2002-02-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / package_db.cc
1 /*
2 * Copyright (c) 2001, Robert Collins.
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 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by Robert Collins <rbtcollins@hotmail.com>
13 *
14 */
15
16 /* this is the package database class.
17 * It lists all known packages, including custom ones, ones from a mirror and
18 * installed ones.
19 */
20
21 #if 0
22 static const char *cvsid =
23 "\n%%% $Id$\n";
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <strings.h>
29 #include "concat.h"
30
31 #include "io_stream.h"
32 #include "compress.h"
33
34 #include "filemanip.h"
35
36 #include "package_version.h"
37 #include "cygpackage.h"
38 #include "package_db.h"
39 #include "package_meta.h"
40
41 /* static members */
42
43 packagedb::packagedb ()
44 {
45 io_stream *db = 0;
46 if (!installeddbread)
47 {
48 /* no parameters. Read in the local installation database. */
49 db = io_stream::open ("cygfile:///etc/setup/installed.db", "rt");
50 if (!db)
51 return;
52 /* flush_local_db_package_data */
53 char line[1000], pkgname[1000], inst[1000], src[1000];
54 int instsz, srcsz;
55
56 if (db->gets (line, 1000))
57 {
58 int dbver;
59 sscanf (line, "%s %d", pkgname, &instsz);
60 if (!strcasecmp (pkgname, "INSTALLED.DB") && instsz == 2)
61 dbver = 2;
62 else
63 dbver = 1;
64 delete db;
65 db = 0;
66 /* Later versions may not use installed.db other than to record the version. */
67 if (dbver == 1 || dbver == 2)
68 {
69 db =
70 io_stream::open ("cygfile:///etc/setup/installed.db", "rt");
71 if (dbver == 2)
72 db->gets (line, 1000);
73 while (db->gets (line, 1000))
74 {
75 int parseable;
76 src[0] = 0;
77 srcsz = 0;
78 sscanf (line, "%s %s %d %s %d", pkgname, inst, &instsz, src,
79 &srcsz);
80 fileparse f;
81 parseable = parse_filename (inst, f);
82 if (!parseable)
83 continue;
84
85 packagemeta *pkg = packages.getbykey (pkgname);
86 if (!pkg)
87 {
88 pkg = new packagemeta (pkgname, inst);
89 /* we should install a new handler then not check this...
90 */
91 //if (!pkg)
92 //die badly
93 }
94
95 cygpackage *binary =
96 new cygpackage (pkgname, inst, instsz, f.ver,
97 package_installed,
98 package_binary);
99
100 pkg->add_version (*binary);
101 pkg->set_installed (*binary);
102 pkg->desired = pkg->installed;
103 packages.registerbyobject (*pkg);
104
105 }
106 delete db;
107 db = 0;
108 }
109 else
110 // unknown dbversion
111 exit (1);
112 }
113 installeddbread = 1;
114 }
115 }
116
117 int
118 packagedb::flush ()
119 {
120 /* naive approach - just dump the lot */
121 char const *odbn = "cygfile:///etc/setup/installed.db";
122 char const *ndbn = "cygfile:///etc/setup/installed.db.new";
123
124 io_stream::mkpath_p (PATH_TO_FILE, ndbn);
125
126 io_stream *ndb = io_stream::open (ndbn, "wb");
127
128 // XXX if this failed, try removing any existing .new database?
129 if (!ndb)
130 return errno ? errno : 1;
131
132 ndb->write ("INSTALLED.DB 2\n", strlen ("INSTALLED.DB 2\n"));
133 for (size_t n = 1; n <= packages.number (); n++)
134 {
135 packagemeta & pkgm = *packages[n];
136 if (pkgm.installed)
137 {
138 char line[2048];
139
140 /* size here is irrelevant - as we can assume that this install source
141 * no longer exists, and it does not correlate to used disk space
142 * also note that we are writing a fictional install source
143 * to keep cygcheck happy.
144 */
145 sprintf (line, "%s %s %d\n", pkgm.name.cstr_oneuse(),
146 concat (pkgm.name.cstr_oneuse(), "-",
147 pkgm.installed->Canonical_version ().cstr_oneuse(),
148 ".tar.bz2", 0), 0);
149 ndb->write (line, strlen (line));
150 }
151 }
152
153 delete ndb;
154
155 io_stream::remove (odbn);
156
157 if (io_stream::move (ndbn, odbn))
158 return errno ? errno : 1;
159 return 0;
160 }
161
162 int
163 packagedb::installeddbread =
164 0;
165 list < packagemeta, String,
166 String::casecompare >
167 packagedb::packages;
168 list < Category, String,
169 String::casecompare >
170 packagedb::categories;
171 PackageDBActions
172 packagedb::task =
173 PackageDB_Install;
This page took 0.044227 seconds and 5 git commands to generate.