fix: move free after error message that needs data.
[platform/upstream/rpm.git] / build.c
1 #include "system.h"
2
3 #include <rpmbuild.h>
4 #include <rpmurl.h>
5
6 #include "build.h"
7 #include "install.h"
8
9 static int checkSpec(Header h)
10 {
11     char *rootdir = NULL;
12     rpmdb db = NULL;
13     int mode = O_RDONLY;
14     rpmTransactionSet ts;
15     struct rpmDependencyConflict * conflicts;
16     int numConflicts;
17     int rc;
18
19     if (!headerIsEntry(h, RPMTAG_REQUIREFLAGS))
20         return 0;
21
22     if (rpmdbOpen(rootdir, &db, mode, 0644)) {
23         const char *dn;
24         dn = rpmGetPath( (rootdir ? rootdir : ""), "%{_dbpath}", NULL);
25         rpmMessage(RPMMESS_ERROR, _("cannot open %s/packages.rpm\n"), dn);
26         xfree(dn);
27         exit(EXIT_FAILURE);
28     }
29     ts = rpmtransCreateSet(db, rootdir);
30
31     rc = rpmtransAddPackage(ts, h, NULL, NULL, 0, NULL);
32
33     rc = rpmdepCheck(ts, &conflicts, &numConflicts);
34     if (rc == 0 && conflicts) {
35         rpmMessage(RPMMESS_ERROR, _("failed build dependencies:\n"));
36         printDepProblems(stderr, conflicts, numConflicts);
37         rpmdepFreeConflicts(conflicts, numConflicts);
38         rc = 1;
39     }
40
41     if (ts)
42         rpmtransFree(ts);
43     if (db)
44         rpmdbClose(db);
45
46     return rc;
47 }
48
49 /*
50  * Kurwa, durni ameryka?ce sobe zawsze my?l?, ?e ca?y ?wiat mówi po
51  * angielsku...
52  */
53 /* XXX this is still a dumb test but at least it's i18n aware */
54 static int isSpecFile(const char *specfile)
55 {
56     char buf[256];
57     const char * s;
58     FD_t fd;
59     int count;
60     int checking;
61
62     fd = Fopen(specfile, "r.ufdio");
63     if (fd == NULL || Ferror(fd)) {
64         fprintf(stderr, _("Unable to open spec file %s: %s\n"), specfile, Fstrerror(fd));
65         return 0;
66     }
67     count = Fread(buf, sizeof(buf[0]), sizeof(buf), fd);
68     Fclose(fd);
69
70     checking = 1;
71     for (s = buf; count--; s++) {
72         switch (*s) {
73         case '\r':
74         case '\n':
75             checking = 1;
76             break;
77         case ':':
78             checking = 0;
79             break;
80         default:
81             if (checking && !(isprint(*s) || isspace(*s))) return 0;
82             break;
83         }
84     }
85     return 1;
86 }
87
88 static int buildForTarget(const char *arg, struct rpmBuildArguments *ba,
89         const char *passPhrase, int fromTarball, char *cookie,
90         int force, int nodeps)
91 {
92     int buildAmount = ba->buildAmount;
93     const char *buildRootURL = NULL;
94     int test = ba->noBuild;
95     const char * specFile;
96     const char * specURL;
97     int specut;
98     char buf[BUFSIZ];
99     Spec spec = NULL;
100     int rc;
101
102     rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
103
104     if (ba->buildRootOverride)
105         buildRootURL = rpmGenPath(NULL, ba->buildRootOverride, NULL);
106
107     if (fromTarball) {
108         FILE *fp;
109         const char *specDir;
110         const char * tmpSpecFile;
111         char * cmd, *s;
112         int res;
113         static const char *zcmds[] = { "cat", "gunzip", "bunzip2", "cat" };
114
115         specDir = rpmGetPath("%{_specdir}", NULL);
116
117         {   char tfn[64];
118             strcpy(tfn, "rpm-spec.XXXXXX");
119             tmpSpecFile = rpmGetPath("%{_specdir}/", mktemp(tfn), NULL);
120         }
121
122         isCompressed(arg, &res);
123
124         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
125         sprintf(cmd, "%s < %s | tar xOvf - Specfile 2>&1 > %s",
126                         zcmds[res & 0x3], arg, tmpSpecFile);
127         if (!(fp = popen(cmd, "r"))) {
128             fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
129                         strerror(errno));
130             xfree(specDir);
131             xfree(tmpSpecFile);
132             return 1;
133         }
134         if ((!fgets(buf, sizeof(buf) - 1, fp)) || !strchr(buf, '/')) {
135             /* Try again */
136             pclose(fp);
137
138             sprintf(cmd, "%s < %s | tar xOvf - \\*.spec 2>&1 > %s",
139                     zcmds[res & 0x3], arg, tmpSpecFile);
140             if (!(fp = popen(cmd, "r"))) {
141                 fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
142                         strerror(errno));
143                 xfree(specDir);
144                 xfree(tmpSpecFile);
145                 return 1;
146             }
147             if (!fgets(buf, sizeof(buf) - 1, fp)) {
148                 /* Give up */
149                 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
150                 unlink(tmpSpecFile);
151                 xfree(specDir);
152                 xfree(tmpSpecFile);
153                 return 1;
154             }
155         }
156         pclose(fp);
157
158         cmd = s = buf;
159         while (*cmd) {
160             if (*cmd == '/') s = cmd + 1;
161             cmd++;
162         }
163
164         cmd = s;
165
166         /* remove trailing \n */
167         s = cmd + strlen(cmd) - 1;
168         *s = '\0';
169
170         specURL = s = alloca(strlen(specDir) + strlen(cmd) + 5);
171         sprintf(s, "%s/%s", specDir, cmd);
172         res = rename(tmpSpecFile, s);
173         xfree(specDir);
174         
175         if (res) {
176             fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
177                     tmpSpecFile, s, strerror(errno));
178             unlink(tmpSpecFile);
179             xfree(tmpSpecFile);
180             return 1;
181         }
182         xfree(tmpSpecFile);
183
184         /* Make the directory which contains the tarball the source 
185            directory for this run */
186
187         if (*arg != '/') {
188             (void)getcwd(buf, BUFSIZ);
189             strcat(buf, "/");
190             strcat(buf, arg);
191         } else 
192             strcpy(buf, arg);
193
194         cmd = buf + strlen(buf) - 1;
195         while (*cmd != '/') cmd--;
196         *cmd = '\0';
197
198         addMacro(NULL, "_sourcedir", NULL, buf, RMIL_TARBALL);
199     } else {
200         specURL = arg;
201     }
202
203     specut = urlPath(specURL, &specFile);
204     if (*specFile != '/') {
205         char *s = alloca(BUFSIZ);
206         (void)getcwd(s, BUFSIZ);
207         strcat(s, "/");
208         strcat(s, arg);
209         specURL = s;
210     }
211
212     if (specut != URL_IS_DASH) {
213         struct stat st;
214         Stat(specURL, &st);
215         if (! S_ISREG(st.st_mode)) {
216             fprintf(stderr, _("File is not a regular file: %s\n"), specURL);
217             rc = 1;
218             goto exit;
219         }
220
221         /* Try to verify that the file is actually a specfile */
222         if (!isSpecFile(specURL)) {
223             fprintf(stderr, _("File %s does not appear to be a specfile.\n"),
224                 specURL);
225             rc = 1;
226             goto exit;
227         }
228     }
229     
230     /* Parse the spec file */
231 #define _anyarch(_f)    \
232 (((_f)&(RPMBUILD_PREP|RPMBUILD_BUILD|RPMBUILD_INSTALL|RPMBUILD_PACKAGEBINARY)) == 0)
233     if (parseSpec(&spec, specURL, ba->rootdir, buildRootURL, 0, passPhrase,
234                 cookie, _anyarch(buildAmount), force)) {
235         rc = 1;
236         goto exit;
237     }
238 #undef  _anyarch
239
240     /* Assemble source header from parsed components */
241     initSourceHeader(spec);
242
243     /* Check build prerequisites */
244     if (!nodeps && checkSpec(spec->sourceHeader)) {
245         rc = 1;
246         goto exit;
247     }
248
249     if (buildSpec(spec, buildAmount, test)) {
250         rc = 1;
251         goto exit;
252     }
253     
254     if (fromTarball) Unlink(specURL);
255     rc = 0;
256
257 exit:
258     if (spec)
259         freeSpec(spec);
260     if (buildRootURL)
261         xfree(buildRootURL);
262     return rc;
263 }
264
265 /** */
266 int build(const char * arg, struct rpmBuildArguments * ba,
267         const char * passPhrase, int fromTarball, char * cookie,
268         const char * rcfile, int force, int nodeps)
269 {
270     char *t, *te;
271     int rc = 0;
272     char *targets = ba->targets;
273 #define buildCleanMask  (RPMBUILD_RMSOURCE|RPMBUILD_RMSPEC)
274     int cleanFlags = ba->buildAmount & buildCleanMask;
275
276     if (targets == NULL) {
277         rc =  buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
278                 force, nodeps);
279         goto exit;
280     }
281
282     /* parse up the build operators */
283
284     printf(_("Building target platforms: %s\n"), targets);
285
286     ba->buildAmount &= ~buildCleanMask;
287     for (t = targets; *t != '\0'; t = te) {
288         char *target;
289         if ((te = strchr(t, ',')) == NULL)
290             te = t + strlen(t);
291         target = alloca(te-t+1);
292         strncpy(target, t, (te-t));
293         target[te-t] = '\0';
294         if (*te)
295             te++;
296         else    /* XXX Perform clean-up after last target build. */
297             ba->buildAmount |= cleanFlags;
298
299         printf(_("Building for target %s\n"), target);
300
301         /* Read in configuration for target. */
302         rpmFreeMacros(NULL);
303         rpmReadConfigFiles(rcfile, target);
304         rc = buildForTarget(arg, ba, passPhrase, fromTarball, cookie,
305                 force, nodeps);
306         if (rc)
307             break;
308     }
309
310 exit:
311     /* Restore original configuration. */
312     rpmFreeMacros(NULL);
313     rpmReadConfigFiles(rcfile, NULL);
314     return rc;
315 }
316
317 #define POPT_USECATALOG         1000
318 #define POPT_NOLANG             1001
319 #define POPT_RMSOURCE           1002
320 #define POPT_RMBUILD            1003
321 #define POPT_BUILDROOT          1004
322 #define POPT_BUILDARCH          1005
323 #define POPT_BUILDOS            1006
324 #define POPT_TARGETPLATFORM     1007
325 #define POPT_NOBUILD            1008
326 #define POPT_SHORTCIRCUIT       1009
327 #define POPT_RMSPEC             1010
328
329 extern int noLang;
330 static int noBuild = 0;
331 static int useCatalog = 0;
332
333 static void buildArgCallback( /*@unused@*/ poptContext con,
334         /*@unused@*/ enum poptCallbackReason reason,
335         const struct poptOption * opt, const char * arg, const void * data)
336 {
337     struct rpmBuildArguments * rba = (struct rpmBuildArguments *) data;
338
339     switch (opt->val) {
340     case POPT_USECATALOG: rba->useCatalog = 1; break;
341     case POPT_NOBUILD: rba->noBuild = 1; break;
342     case POPT_NOLANG: rba->noLang = 1; break;
343     case POPT_SHORTCIRCUIT: rba->shortCircuit = 1; break;
344     case POPT_RMSOURCE: rba->buildAmount |= RPMBUILD_RMSOURCE; break;
345     case POPT_RMSPEC: rba->buildAmount |= RPMBUILD_RMSPEC; break;
346     case POPT_RMBUILD: rba->buildAmount |= RPMBUILD_RMBUILD; break;
347     case POPT_BUILDROOT:
348         if (rba->buildRootOverride) {
349             fprintf(stderr, _("buildroot already specified"));
350             exit(EXIT_FAILURE);
351             /*@notreached@*/
352         }
353         rba->buildRootOverride = xstrdup(arg);
354         break;
355     case POPT_BUILDARCH:
356         fprintf(stderr, _("--buildarch has been obsoleted.  Use the --target option instead.\n")); 
357         exit(EXIT_FAILURE);
358         /*@notreached@*/ break;
359     case POPT_BUILDOS:
360         fprintf(stderr, _("--buildos has been obsoleted.  Use the --target option instead.\n")); 
361         exit(EXIT_FAILURE);
362         /*@notreached@*/ break;
363     case POPT_TARGETPLATFORM:
364         if (rba->targets) {
365             int len = strlen(rba->targets) + 1 + strlen(arg) + 1;
366             rba->targets = xrealloc(rba->targets, len);
367             strcat(rba->targets, ",");
368         } else {
369             rba->targets = xmalloc(strlen(arg) + 1);
370             rba->targets[0] = '\0';
371         }
372         strcat(rba->targets, arg);
373         break;
374     }
375 }
376
377 /** */
378 struct poptOption rpmBuildPoptTable[] = {
379         { NULL, '\0', POPT_ARG_CALLBACK | POPT_CBFLAG_INC_DATA,
380                 buildArgCallback, 0, NULL, NULL },
381         { "buildarch", '\0', POPT_ARG_STRING, 0,  POPT_BUILDARCH,
382                 N_("override build architecture"), "ARCH" },
383         { "buildos", '\0', POPT_ARG_STRING, 0,  POPT_BUILDOS,
384                 N_("override build operating system"), "OS" },
385         { "buildroot", '\0', POPT_ARG_STRING, 0,  POPT_BUILDROOT,
386                 N_("override build root"), "DIRECTORY" },
387         { "clean", '\0', 0, 0, POPT_RMBUILD,
388                 N_("remove build tree when done"), NULL},
389         { "nobuild", '\0', 0, &noBuild,  POPT_NOBUILD,
390                 N_("do not execute any stages of the build"), NULL },
391         { "nolang", '\0', 0, &noLang, POPT_NOLANG,
392                 N_("do not accept I18N msgstr's from specfile"), NULL},
393         { "rmsource", '\0', 0, 0, POPT_RMSOURCE,
394                 N_("remove sources when done"), NULL},
395         { "rmspec", '\0', 0, 0, POPT_RMSPEC,
396                 N_("remove specfile when done"), NULL},
397         { "short-circuit", '\0', 0, 0,  POPT_SHORTCIRCUIT,
398                 N_("skip straight to specified stage (only for c,i)"), NULL },
399         { "target", '\0', POPT_ARG_STRING, 0,  POPT_TARGETPLATFORM,
400                 N_("override target platform"), "CPU-VENDOR-OS" },
401         { "usecatalog", '\0', 0, &useCatalog, POPT_USECATALOG,
402                 N_("lookup I18N strings in specfile catalog"), NULL},
403         { 0, 0, 0, 0, 0,        NULL, NULL }
404 };