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