Consolidated includes for librpmbuild API.
[platform/upstream/rpm.git] / build.c
1 #include "system.h"
2
3 #include "build/rpmbuild.h"
4
5 #include "build.h"
6 #include "intl.h"
7
8 int build(char *arg, int buildAmount, char *passPhrase,
9                  char *buildRoot, int fromTarball, int test, char *cookie) {
10     FILE *f;
11     char * specfile;
12     int res = 0;
13     struct stat statbuf;
14     char * specDir;
15     char * tmpSpecFile;
16     char * cmd;
17     char * s;
18     int count, fd;
19     char buf[BUFSIZ];
20     Spec spec = NULL;
21
22     if (fromTarball) {
23         specDir = rpmGetVar(RPMVAR_SPECDIR);
24         tmpSpecFile = alloca(BUFSIZ);
25         sprintf(tmpSpecFile, "%s/rpm-spec-file-%d", specDir, (int) getpid());
26
27         cmd = alloca(strlen(arg) + 50 + strlen(tmpSpecFile));
28         sprintf(cmd, "gunzip < %s | tar xOvf - Specfile 2>&1 > %s", arg,
29                         tmpSpecFile);
30         if (!(f = popen(cmd, "r"))) {
31             fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
32                         strerror(errno));
33             return 1;
34         }
35         if ((!fgets(buf, sizeof(buf) - 1, f)) || !strchr(buf, '/')) {
36             /* Try again */
37             pclose(f);
38
39             sprintf(cmd, "gunzip < %s | tar xOvf - \\*.spec 2>&1 > %s", arg,
40                     tmpSpecFile);
41             if (!(f = popen(cmd, "r"))) {
42                 fprintf(stderr, _("Failed to open tar pipe: %s\n"), 
43                         strerror(errno));
44                 return 1;
45             }
46             if (!fgets(buf, sizeof(buf) - 1, f)) {
47                 /* Give up */
48                 fprintf(stderr, _("Failed to read spec file from %s\n"), arg);
49                 unlink(tmpSpecFile);
50                 return 1;
51             }
52         }
53         pclose(f);
54
55         cmd = specfile = buf;
56         while (*cmd) {
57             if (*cmd == '/') specfile = cmd + 1;
58             cmd++;
59         }
60
61         cmd = specfile;
62
63         /* remove trailing \n */
64         specfile = cmd + strlen(cmd) - 1;
65         *specfile = '\0';
66
67         specfile = alloca(strlen(specDir) + strlen(cmd) + 5);
68         sprintf(specfile, "%s/%s", specDir, cmd);
69         
70         if (rename(tmpSpecFile, specfile)) {
71             fprintf(stderr, _("Failed to rename %s to %s: %s\n"),
72                     tmpSpecFile, specfile, strerror(errno));
73             unlink(tmpSpecFile);
74             return 1;
75         }
76
77         /* Make the directory which contains the tarball the source 
78            directory for this run */
79
80         if (*arg != '/') {
81             getcwd(buf, BUFSIZ);
82             strcat(buf, "/");
83             strcat(buf, arg);
84         } else 
85             strcpy(buf, arg);
86
87         cmd = buf + strlen(buf) - 1;
88         while (*cmd != '/') cmd--;
89         *cmd = '\0';
90
91         rpmSetVar(RPMVAR_SOURCEDIR, buf);
92     } else if (arg[0] == '/') {
93         specfile = arg;
94     } else {
95         specfile = alloca(BUFSIZ);
96         getcwd(specfile, BUFSIZ);
97         strcat(specfile, "/");
98         strcat(specfile, arg);
99     }
100
101     stat(specfile, &statbuf);
102     if (! S_ISREG(statbuf.st_mode)) {
103         fprintf(stderr, _("File is not a regular file: %s\n"), specfile);
104         return 1;
105     }
106     
107     if ((fd = open(specfile, O_RDONLY)) < 0) {
108         fprintf(stderr, _("Unable to open spec file: %s\n"), specfile);
109         return 1;
110     }
111     count = read(fd, buf, sizeof(buf) < 128 ? sizeof(buf) : 128);
112     close(fd);
113     s = buf;
114     while(count--) {
115         if (! (isprint(*s) || isspace(*s))) {
116             fprintf(stderr, _("File contains non-printable characters(%c): %s\n"), *s,
117                     specfile);
118             return 1;
119         }
120         s++;
121     }
122
123     if (parseSpec(&spec, specfile, buildRoot, 0, passPhrase, cookie)) {
124         return 1;
125     }
126
127     if (buildSpec(spec, buildAmount, test)) {
128         freeSpec(spec);
129         return 1;
130     }
131     
132     if (fromTarball) unlink(specfile);
133
134     freeSpec(spec);
135     
136     return res;
137 }