]>
Commit | Line | Data |
---|---|---|
7939f6d1 RC |
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 | |
7939f6d1 RC |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <unistd.h> | |
28 | #include <strings.h> | |
7939f6d1 RC |
29 | |
30 | #include "io_stream.h" | |
31 | #include "compress.h" | |
32 | ||
33 | #include "filemanip.h" | |
34 | ||
fa0c0d10 | 35 | #include "package_version.h" |
7939f6d1 RC |
36 | #include "cygpackage.h" |
37 | #include "package_db.h" | |
38 | #include "package_meta.h" | |
39 | ||
40 | /* static members */ | |
41 | ||
7939f6d1 RC |
42 | packagedb::packagedb () |
43 | { | |
df62e023 | 44 | io_stream *db = 0; |
7939f6d1 RC |
45 | if (!installeddbread) |
46 | { | |
47 | /* no parameters. Read in the local installation database. */ | |
48 | db = io_stream::open ("cygfile:///etc/setup/installed.db", "rt"); | |
49 | if (!db) | |
50 | return; | |
51 | /* flush_local_db_package_data */ | |
52 | char line[1000], pkgname[1000], inst[1000], src[1000]; | |
53 | int instsz, srcsz; | |
7939f6d1 | 54 | |
24cbae7f RC |
55 | if (db->gets (line, 1000)) |
56 | { | |
57 | int dbver; | |
58 | sscanf (line, "%s %d", pkgname, &instsz); | |
59 | if (!strcasecmp (pkgname, "INSTALLED.DB") && instsz == 2) | |
60 | dbver = 2; | |
61 | else | |
62 | dbver = 1; | |
63 | delete db; | |
fa0c0d10 | 64 | db = 0; |
24cbae7f RC |
65 | /* Later versions may not use installed.db other than to record the version. */ |
66 | if (dbver == 1 || dbver == 2) | |
7939f6d1 | 67 | { |
24cbae7f RC |
68 | db = |
69 | io_stream::open ("cygfile:///etc/setup/installed.db", "rt"); | |
70 | if (dbver == 2) | |
71 | db->gets (line, 1000); | |
72 | while (db->gets (line, 1000)) | |
7939f6d1 | 73 | { |
24cbae7f RC |
74 | int parseable; |
75 | src[0] = 0; | |
76 | srcsz = 0; | |
77 | sscanf (line, "%s %s %d %s %d", pkgname, inst, &instsz, src, | |
78 | &srcsz); | |
79 | fileparse f; | |
80 | parseable = parse_filename (inst, f); | |
81 | if (!parseable) | |
82 | continue; | |
83 | ||
cbfc4215 | 84 | packagemeta *pkg = packages.getbykey (pkgname); |
24cbae7f RC |
85 | if (!pkg) |
86 | { | |
fa0c0d10 | 87 | pkg = new packagemeta (pkgname, inst); |
24cbae7f RC |
88 | /* we should install a new handler then not check this... |
89 | */ | |
90 | //if (!pkg) | |
91 | //die badly | |
92 | } | |
93 | ||
94 | cygpackage *binary = | |
95 | new cygpackage (pkgname, inst, instsz, f.ver, | |
96 | package_installed, | |
97 | package_binary); | |
98 | ||
99 | pkg->add_version (*binary); | |
100 | pkg->set_installed (*binary); | |
bb849dbd | 101 | pkg->desired = pkg->installed; |
cbfc4215 | 102 | packages.registerbyobject (*pkg); |
7939f6d1 | 103 | |
7939f6d1 | 104 | } |
24cbae7f | 105 | delete db; |
fa0c0d10 | 106 | db = 0; |
24cbae7f RC |
107 | } |
108 | else | |
109 | // unknown dbversion | |
110 | exit (1); | |
7939f6d1 | 111 | } |
7b606ae5 | 112 | installeddbread = 1; |
7939f6d1 | 113 | } |
7939f6d1 RC |
114 | } |
115 | ||
7c7034e8 RC |
116 | int |
117 | packagedb::flush () | |
118 | { | |
119 | /* naive approach - just dump the lot */ | |
120 | char const *odbn = "cygfile:///etc/setup/installed.db"; | |
121 | char const *ndbn = "cygfile:///etc/setup/installed.db.new"; | |
122 | ||
123 | io_stream::mkpath_p (PATH_TO_FILE, ndbn); | |
124 | ||
125 | io_stream *ndb = io_stream::open (ndbn, "wb"); | |
126 | ||
3c054baf | 127 | // XXX if this failed, try removing any existing .new database? |
7c7034e8 RC |
128 | if (!ndb) |
129 | return errno ? errno : 1; | |
130 | ||
131 | ndb->write ("INSTALLED.DB 2\n", strlen ("INSTALLED.DB 2\n")); | |
4f4e55c2 | 132 | for (size_t n = 1; n <= packages.number (); n++) |
7c7034e8 | 133 | { |
df62e023 RC |
134 | packagemeta & pkgm = *packages[n]; |
135 | if (pkgm.installed) | |
136 | { | |
137 | char line[2048]; | |
138 | ||
139 | /* size here is irrelevant - as we can assume that this install source | |
140 | * no longer exists, and it does not correlate to used disk space | |
141 | * also note that we are writing a fictional install source | |
142 | * to keep cygcheck happy. | |
143 | */ | |
3c054baf | 144 | sprintf (line, "%s %s %d\n", pkgm.name.cstr_oneuse(), |
1ac649ed RC |
145 | (pkgm.name + "-" + pkgm.installed->Canonical_version () + |
146 | ".tar.bz2").cstr_oneuse(), 0); | |
df62e023 RC |
147 | ndb->write (line, strlen (line)); |
148 | } | |
7c7034e8 RC |
149 | } |
150 | ||
151 | delete ndb; | |
152 | ||
153 | io_stream::remove (odbn); | |
154 | ||
155 | if (io_stream::move (ndbn, odbn)) | |
156 | return errno ? errno : 1; | |
157 | return 0; | |
158 | } | |
159 | ||
df62e023 RC |
160 | int |
161 | packagedb::installeddbread = | |
162 | 0; | |
3c054baf RC |
163 | list < packagemeta, String, |
164 | String::casecompare > | |
df62e023 | 165 | packagedb::packages; |
3c054baf RC |
166 | list < Category, String, |
167 | String::casecompare > | |
4fe323f9 | 168 | packagedb::categories; |
df62e023 RC |
169 | PackageDBActions |
170 | packagedb::task = | |
171 | PackageDB_Install; |