- missing key(s) on keyring when verifying a signature is now an error.
[platform/upstream/rpm.git] / build.c
1 /** \ingroup rpmcli
2  * Parse spec file and build package.
3  */
4
5 #include "system.h"
6
7 #include <rpmcli.h>
8 #include <rpmbuild.h>
9
10 #include "build.h"
11 #include "debug.h"
12
13 /*@access rpmTransactionSet @*/ /* XXX compared with NULL @*/
14 /*@access rpmdb @*/             /* XXX compared with NULL @*/
15 /*@access FD_t @*/              /* XXX compared with NULL @*/
16
17 /**
18  */
19 static int checkSpec(rpmTransactionSet ts, Header h)
20         /*@globals fileSystem, internalState @*/
21         /*@modifies ts, h, fileSystem, internalState @*/
22 {
23     rpmProblem conflicts;
24     int numConflicts;
25     int rc;
26
27     if (!headerIsEntry(h, RPMTAG_REQUIRENAME)
28      && !headerIsEntry(h, RPMTAG_CONFLICTNAME))
29         return 0;
30
31     rc = rpmtransAddPackage(ts, h, NULL, 0, NULL);
32
33     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
34     /*@-branchstate@*/
35     if (rc == 0 && conflicts) {
36         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
37         printDepProblems(stderr, conflicts, numConflicts);
38         conflicts = rpmdepFreeConflicts(conflicts, numConflicts);
39         rc = 1;
40     }
41     /*@=branchstate@*/
42
43     return rc;
44 }
45
46 /*
47  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
48  * angielsku...
49  */
50 /* XXX this is still a dumb test but at least it's i18n aware */
51 /**
52  */
53 static int isSpecFile(const char * specfile)
54         /*@globals fileSystem @*/
55         /*@modifies fileSystem @*/
56 {
57     char buf[256];
58     const char * s;
59     FD_t fd;
60     int count;
61     int checking;
62
63     fd = Fopen(specfile, "r.ufdio");
64     if (fd == NULL || Ferror(fd)) {
65         rpmError(RPMERR_OPEN, _("Unable to open spec file %s: %s\n"),
66                 specfile, Fstrerror(fd));
67         return 0;
68     }
69     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
70     (void) Fclose(fd);
71
72     checking = 1;
73     for (s = buf; count--; s++) {
74         switch (*s) {
75         case '\r':
76         case '\n':
77             checking = 1;
78             /*@switchbreak@*/ break;
79         case ':':
80             checking = 0;
81             /*@switchbreak@*/ break;
82         default:
83             if (checking && !(isprint(*s) || isspace(*s))) return 0;
84             /*@switchbreak@*/ break;
85         }
86     }
87     return 1;
88 }
89
90 /**
91  */
92 static int buildForTarget(rpmTransactionSet ts, const char * arg, BTA_t ba)
93         /*@globals rpmGlobalMacroContext,
94                 fileSystem, internalState @*/
95         /*@modifies ts, rpmGlobalMacroContext,
96                 fileSystem, internalState @*/
97 {
98     const char * passPhrase = ba->passPhrase;
99     const char * cookie = ba->cookie;
100     int buildAmount = ba->buildAmount;
101     const char * buildRootURL = NULL;
102     const char * specFile;
103     const char * specURL;
104     int specut;
105     char buf[BUFSIZ];
106     Spec spec = NULL;
107     int rc;
108
109 #ifndef DYING
110     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
111 #endif
112
113     /*@-branchstate@*/
114     if (ba->buildRootOverride)
115         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
116     /*@=branchstate@*/
117
118     /*@-compmempass@*/ /* FIX: static zcmds heartburn */
119     if (ba->buildMode == 't') {
120         FILE *fp;
121         const char * specDir;
122         const char * tmpSpecFile;
123         char * cmd, * s;
124         rpmCompressedMagic res = COMPRESSED_OTHER;
125         /*@observer@*/ static const char *zcmds[] =
126                 { "cat", "gunzip", "bunzip2", "cat" };
127
128         specDir = rpmGetPath("%{_specdir}", NULL);
129
130         /* XXX Using mkstemp is difficult here. */
131         /* XXX FWIW, default %{_specdir} is root.root 0755 */
132         {   char tfn[64];
133             strcpy(tfn, "rpm-spec.XXXXXX");
134             /*@-unrecog@*/
135             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
136             /*@=unrecog@*/
137         }
138
139         (void) isCompressed(arg, &res);
140
141         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
142         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
143                         zcmds[res & 0x3], arg, tmpSpecFile);
144         if (!(fp = popen(cmd, "r"))) {
145             rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
146             specDir = _free(specDir);
147             tmpSpecFile = _free(tmpSpecFile);
148             return 1;
149         }
150         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
151             /* Try again */
152             (void) pclose(fp);
153
154             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
155                     zcmds[res & 0x3], arg, tmpSpecFile);
156             if (!(fp = popen(cmd, "r"))) {
157                 rpmError(RPMERR_POPEN, _("Failed to open tar pipe: %m\n"));
158                 specDir = _free(specDir);
159                 tmpSpecFile = _free(tmpSpecFile);
160                 return 1;
161             }
162             if (!fgets(buf, sizeof(buf) - 1, fp)) {
163                 /* Give up */
164                 rpmError(RPMERR_READ, _("Failed to read spec file from %s\n"),
165                         arg);
166                 (void) unlink(tmpSpecFile);
167                 specDir = _free(specDir);
168                 tmpSpecFile = _free(tmpSpecFile);
169                 return 1;
170             }
171         }
172         (void) pclose(fp);
173
174         cmd = s = buf;
175         while (*cmd != '\0') {
176             if (*cmd == '/') s = cmd + 1;
177             cmd++;
178         }
179
180         cmd = s;
181
182         /* remove trailing \n */
183         s = cmd + strlen(cmd) - 1;
184         *s = '\0';
185
186         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
187         sprintf(s, "%s/%s", specDir, cmd);
188         res = rename(tmpSpecFile, s);
189         specDir = _free(specDir);
190         
191         if (res) {
192             rpmError(RPMERR_RENAME, _("Failed to rename %s to %s: %m\n"),
193                         tmpSpecFile, s);
194             (void) unlink(tmpSpecFile);
195             tmpSpecFile = _free(tmpSpecFile);
196             return 1;
197         }
198         tmpSpecFile = _free(tmpSpecFile);
199
200         /* Make the directory which contains the tarball the source 
201            directory for this run */
202
203         if (*arg != '/') {
204             (void)getcwd(buf, BUFSIZ);
205             strcat(buf, "/");
206             strcat(buf, arg);
207         } else 
208             strcpy(buf, arg);
209
210         cmd = buf + strlen(buf) - 1;
211         while (*cmd != '/') cmd--;
212         *cmd = '\0';
213
214         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
215     } else {
216         specURL = arg;
217     }
218     /*@=compmempass@*/
219
220     specut = urlPath(specURL, &specFile);
221     if (*specFile != '/') {
222         char *s = alloca(BUFSIZ);
223         (void)getcwd(s, BUFSIZ);
224         strcat(s, "/");
225         strcat(s, arg);
226         specURL = s;
227     }
228
229     if (specut != URL_IS_DASH) {
230         struct stat st;
231         if (Stat(specURL, &st) < 0) {
232             rpmError(RPMERR_STAT, _("failed to stat %s: %m\n"), specURL);
233             rc = 1;
234             goto exit;
235         }
236         if (! S_ISREG(st.st_mode)) {
237             rpmError(RPMERR_NOTREG, _("File %s is not a regular file.\n"),
238                 specURL);
239             rc = 1;
240             goto exit;
241         }
242
243         /* Try to verify that the file is actually a specfile */
244         if (!isSpecFile(specURL)) {
245             rpmError(RPMERR_BADSPEC,
246                 _("File %s does not appear to be a specfile.\n"), specURL);
247             rc = 1;
248             goto exit;
249         }
250     }
251     
252     /* Parse the spec file */
253 #define _anyarch(_f)    \
254 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
255     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
256                 cookie, _anyarch(buildAmount), ba->force)) {
257         rc = 1;
258         goto exit;
259     }
260 #undef  _anyarch
261
262     /* Assemble source header from parsed components */
263     initSourceHeader(spec);
264
265     /* Check build prerequisites */
266     if (!ba->noDeps && checkSpec(ts, spec->sourceHeader)) {
267         rc = 1;
268         goto exit;
269     }
270
271     if (buildSpec(spec, buildAmount, ba->noBuild)) {
272         rc = 1;
273         goto exit;
274     }
275     
276     if (ba->buildMode == 't')
277         (void) Unlink(specURL);
278     rc = 0;
279
280 exit:
281     spec = freeSpec(spec);
282     buildRootURL = _free(buildRootURL);
283     return rc;
284 }
285
286 int build(rpmTransactionSet ts, const char * arg, BTA_t ba, const char * rcfile)
287 {
288     char *t, *te;
289     int rc = 0;
290     char * targets = ba->targets;
291 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
292     int cleanFlags = ba->buildAmount & buildCleanMask;
293
294     if (targets == NULL) {
295         rc =  buildForTarget(ts, arg, ba);
296         goto exit;
297     }
298
299     /* parse up the build operators */
300
301     printf(_("Building target platforms: %s\n"), targets);
302
303     ba->buildAmount &= ~buildCleanMask;
304     for (t = targets; *t != '\0'; t = te) {
305         char *target;
306         if ((te = strchr(t, ',')) == NULL)
307             te = t + strlen(t);
308         target = alloca(te-t+1);
309         strncpy(target, t, (te-t));
310         target[te-t] = '\0';
311         if (*te != '\0')
312             te++;
313         else    /* XXX Perform clean-up after last target build. */
314             ba->buildAmount |= cleanFlags;
315
316         printf(_("Building for target %s\n"), target);
317
318         /* Read in configuration for target. */
319         rpmFreeMacros(NULL);
320         (void) rpmReadConfigFiles(rcfile, target);
321         rc = buildForTarget(ts, arg, ba);
322         if (rc)
323             break;
324     }
325
326 exit:
327     /* Restore original configuration. */
328     rpmFreeMacros(NULL);
329     (void) rpmReadConfigFiles(rcfile, NULL);
330
331     return rc;
332 }