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