build.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * build.c - building archives
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <time.h>
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <dirent.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <assert.h>
  35. #include <config.h>
  36. #include <dpkg.h>
  37. #include <dpkg-db.h>
  38. #include "dpkg-deb.h"
  39. #ifndef S_ISLNK
  40. # define S_ISLNK(mode) ((mode&0xF000) == S_IFLNK)
  41. #endif
  42. static void checkversion(const char *vstring, const char *valuename, int *errs) {
  43. const char *p;
  44. if (!vstring || !*vstring) return;
  45. for (p=vstring; *p; p++) if (isdigit(*p)) return;
  46. fprintf(stderr, _("dpkg-deb - error: %s (`%s') doesn't contain any digits\n"),
  47. valuename, vstring);
  48. (*errs)++;
  49. }
  50. void do_build(const char *const *argv) {
  51. static const char *const maintainerscripts[]= {
  52. PREINSTFILE, POSTINSTFILE, PRERMFILE, POSTRMFILE, 0
  53. };
  54. char *m;
  55. const char *debar, *directory, *const *mscriptp, *versionstring, *arch;
  56. char *controlfile, *tfbuf, *envbuf;
  57. struct pkginfo *checkedinfo;
  58. struct arbitraryfield *field;
  59. FILE *ar, *gz, *cf;
  60. int p1[2],p2[2], warns, errs, n, c, subdir, gzfd;
  61. pid_t c1,c2,c3,c4,c5;
  62. struct stat controlstab, datastab, mscriptstab, debarstab;
  63. char conffilename[MAXCONFFILENAME+1];
  64. time_t thetime= 0;
  65. directory= *argv++; if (!directory) badusage(_("--build needs a directory argument"));
  66. /* template for our tempfiles */
  67. if ((envbuf= getenv("TMPDIR")) == NULL)
  68. envbuf= (char *)P_tmpdir;
  69. tfbuf = (char *)malloc(strlen(envbuf)+13);
  70. strcpy(tfbuf,envbuf);
  71. strcat(tfbuf,"/dpkg.XXXXXX");
  72. subdir= 0;
  73. if ((debar= *argv++) !=0) {
  74. if (*argv) badusage(_("--build takes at most two arguments"));
  75. if (debar) {
  76. if (stat(debar,&debarstab)) {
  77. if (errno != ENOENT)
  78. ohshite(_("unable to check for existence of archive `%.250s'"),debar);
  79. } else if (S_ISDIR(debarstab.st_mode)) {
  80. subdir= 1;
  81. }
  82. }
  83. } else {
  84. m= m_malloc(strlen(directory) + sizeof(DEBEXT));
  85. strcpy(m,directory); strcat(m,DEBEXT);
  86. debar= m;
  87. }
  88. if (nocheckflag) {
  89. if (subdir)
  90. ohshit(_("target is directory - cannot skip control file check"));
  91. printf(_("dpkg-deb: warning, not checking contents of control area.\n"
  92. "dpkg-deb: building an unknown package in `%s'.\n"), debar);
  93. } else {
  94. controlfile= m_malloc(strlen(directory) + sizeof(BUILDCONTROLDIR) +
  95. sizeof(CONTROLFILE) + sizeof(CONFFILESFILE) +
  96. sizeof(POSTINSTFILE) + sizeof(PREINSTFILE) +
  97. sizeof(POSTRMFILE) + sizeof(PRERMFILE) +
  98. MAXCONFFILENAME + 5);
  99. strcpy(controlfile, directory);
  100. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONTROLFILE);
  101. warns= 0; errs= 0;
  102. parsedb(controlfile, pdb_recordavailable|pdb_rejectstatus,
  103. &checkedinfo, stderr, &warns);
  104. assert(checkedinfo->available.valid);
  105. if (strspn(checkedinfo->name,
  106. "abcdefghijklmnopqrstuvwxyz0123456789+-.")
  107. != strlen(checkedinfo->name))
  108. ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
  109. if (checkedinfo->priority == pri_other) {
  110. fprintf(stderr, _("warning, `%s' contains user-defined Priority value `%s'\n"),
  111. controlfile, checkedinfo->otherpriority);
  112. warns++;
  113. }
  114. for (field= checkedinfo->available.arbs; field; field= field->next) {
  115. fprintf(stderr, _("warning, `%s' contains user-defined field `%s'\n"),
  116. controlfile, field->name);
  117. warns++;
  118. }
  119. checkversion(checkedinfo->available.version.version,"(upstream) version",&errs);
  120. checkversion(checkedinfo->available.version.revision,"Debian revision",&errs);
  121. if (errs) ohshit(_("%d errors in control file"),errs);
  122. if (subdir) {
  123. versionstring= versiondescribe(&checkedinfo->available.version,vdew_never);
  124. arch= checkedinfo->available.architecture; if (!arch) arch= "";
  125. m= m_malloc(sizeof(DEBEXT)+1+strlen(debar)+1+strlen(checkedinfo->name)+
  126. strlen(versionstring)+1+strlen(arch));
  127. sprintf(m,"%s/%s_%s%s%s" DEBEXT,debar,checkedinfo->name,versionstring,
  128. arch[0] ? "_" : "", arch);
  129. debar= m;
  130. }
  131. printf(_("dpkg-deb: building package `%s' in `%s'.\n"), checkedinfo->name, debar);
  132. strcpy(controlfile, directory);
  133. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  134. if (lstat(controlfile,&mscriptstab)) ohshite("unable to stat control directory");
  135. if (!S_ISDIR(mscriptstab.st_mode)) ohshit("control directory is not a directory");
  136. if ((mscriptstab.st_mode & 07757) != 0755)
  137. ohshit(_("control directory has bad permissions %03lo (must be >=0755 "
  138. "and <=0775)"), (unsigned long)(mscriptstab.st_mode & 07777));
  139. for (mscriptp= maintainerscripts; *mscriptp; mscriptp++) {
  140. strcpy(controlfile, directory);
  141. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  142. strcat(controlfile, *mscriptp);
  143. if (!lstat(controlfile,&mscriptstab)) {
  144. if (S_ISLNK(mscriptstab.st_mode)) continue;
  145. if (!S_ISREG(mscriptstab.st_mode))
  146. ohshit(_("maintainer script `%.50s' is not a plain file or symlink"),*mscriptp);
  147. if ((mscriptstab.st_mode & 07557) != 0555)
  148. ohshit(_("maintainer script `%.50s' has bad permissions %03lo "
  149. "(must be >=0555 and <=0775)"),
  150. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  151. } else if (errno != ENOENT) {
  152. ohshite(_("maintainer script `%.50s' is not stattable"),*mscriptp);
  153. }
  154. }
  155. strcpy(controlfile, directory);
  156. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONFFILESFILE);
  157. if ((cf= fopen(controlfile,"r"))) {
  158. while (fgets(conffilename,MAXCONFFILENAME+1,cf)) {
  159. n= strlen(conffilename);
  160. if (!n) ohshite(_("empty string from fgets reading conffiles"));
  161. if (conffilename[n-1] != '\n') {
  162. fprintf(stderr, _("warning, conffile name `%.50s...' is too long, or missing final newline\n"),
  163. conffilename);
  164. warns++;
  165. while ((c= getc(cf)) != EOF && c != '\n');
  166. continue;
  167. }
  168. conffilename[n-1]= 0;
  169. strcpy(controlfile, directory);
  170. strcat(controlfile, "/");
  171. strcat(controlfile, conffilename);
  172. if (lstat(controlfile,&controlstab)) {
  173. if (errno == ENOENT)
  174. ohshit(_("conffile `%.250s' does not appear in package"),conffilename);
  175. else
  176. ohshite(_("conffile `%.250s' is not stattable"),conffilename);
  177. } else if (!S_ISREG(controlstab.st_mode)) {
  178. fprintf(stderr, _("warning, conffile `%s'"
  179. " is not a plain file\n"), conffilename);
  180. warns++;
  181. }
  182. }
  183. if (ferror(cf)) ohshite(_("error reading conffiles file"));
  184. fclose(cf);
  185. } else if (errno != ENOENT) {
  186. ohshite(_("error opening conffiles file"));
  187. }
  188. if (warns) {
  189. if (fprintf(stderr, _("dpkg-deb: ignoring %d warnings about the control"
  190. " file(s)\n"), warns) == EOF) werr("stderr");
  191. }
  192. }
  193. if (ferror(stdout)) werr("stdout");
  194. if (!(ar=fopen(debar,"wb"))) ohshite(_("unable to create `%.255s'"),debar);
  195. if (setvbuf(ar, 0, _IONBF, 0)) ohshite(_("unable to unbuffer `%.255s'"),debar);
  196. m_pipe(p1);
  197. if (!(c1= m_fork())) {
  198. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  199. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  200. if (chdir(BUILDCONTROLDIR)) ohshite(_("failed to chdir to .../DEBIAN"));
  201. execlp(TAR,"tar","-cf","-",".",(char*)0); ohshite(_("failed to exec tar -cf"));
  202. }
  203. close(p1[1]);
  204. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (control)"));
  205. if ((gz= fdopen(gzfd,"a")) == NULL) ohshite(_("failed to open tmpfile "
  206. "(control), %s"), tfbuf);
  207. /* make sure it's gone, the fd will remain until we close it */
  208. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (control), %s"),
  209. tfbuf);
  210. /* reset this, so we can use it elsewhere */
  211. strcpy(tfbuf,envbuf);
  212. strcat(tfbuf,"/dpkg.XXXXXX");
  213. if (!(c2= m_fork())) {
  214. m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]);
  215. execlp(GZIP,"gzip","-9c",(char*)0); ohshite(_("failed to exec gzip -9c"));
  216. }
  217. close(p1[0]);
  218. waitsubproc(c2,"gzip -9c",0);
  219. waitsubproc(c1,"tar -cf",0);
  220. if (fstat(gzfd,&controlstab)) ohshite(_("failed to fstat tmpfile (control)"));
  221. if (oldformatflag) {
  222. if (fprintf(ar, "%-8s\n%ld\n", OLDARCHIVEVERSION, (long)controlstab.st_size) == EOF)
  223. werr(debar);
  224. } else {
  225. thetime= time(0);
  226. if (fprintf(ar,
  227. "!<arch>\n"
  228. "debian-binary %-12lu0 0 100644 %-10ld`\n"
  229. ARCHIVEVERSION "\n"
  230. "%s"
  231. ADMINMEMBER "%-12lu0 0 100644 %-10ld`\n",
  232. thetime,
  233. (long)sizeof(ARCHIVEVERSION),
  234. (sizeof(ARCHIVEVERSION)&1) ? "\n" : "",
  235. (unsigned long)thetime,
  236. (long)controlstab.st_size) == EOF)
  237. werr(debar);
  238. }
  239. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (control)"));
  240. do_fd_copy(gzfd, fileno(ar), _("control"));
  241. if (!oldformatflag) {
  242. fclose(gz);
  243. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (data)"));
  244. if ((gz= fdopen(gzfd,"a")) == NULL) ohshite(_("failed to open tmpfile "
  245. "(data), %s"), tfbuf);
  246. /* make sure it's gone, the fd will remain until we close it */
  247. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (data), %s"),
  248. tfbuf);
  249. /* reset these, in case we want to use the later */
  250. strcpy(tfbuf,envbuf);
  251. strcat(tfbuf,"/dpkg.XXXXXX");
  252. }
  253. m_pipe(p2);
  254. if (!(c4= m_fork())) {
  255. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  256. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  257. execlp(TAR,"tar","--exclude",BUILDCONTROLDIR,"-cf","-",".",(char*)0);
  258. ohshite(_("failed to exec tar --exclude"));
  259. }
  260. close(p2[1]);
  261. if (!(c5= m_fork())) {
  262. char *combuf;
  263. m_dup2(p2[0],0); close(p2[0]);
  264. m_dup2(oldformatflag ? fileno(ar) : gzfd,1);
  265. combuf = strdup("-9c");
  266. if(compression != NULL) {
  267. if(*compression == '0') {
  268. do_fd_copy(0, 1, _("no compression copy loop"));
  269. exit(0);
  270. }
  271. combuf[1] = *compression;
  272. }
  273. execlp(GZIP,"gzip",combuf,(char*)0);
  274. ohshite(_("failed to exec gzip %s from tar --exclude"), combuf);
  275. }
  276. close(p2[0]);
  277. waitsubproc(c5,"gzip -9c from tar --exclude",0);
  278. waitsubproc(c4,"tar --exclude",0);
  279. if (!oldformatflag) {
  280. if (fstat(gzfd,&datastab)) ohshite("_(failed to fstat tmpfile (data))");
  281. if (fprintf(ar,
  282. "%s"
  283. DATAMEMBER "%-12lu0 0 100644 %-10ld`\n",
  284. (controlstab.st_size & 1) ? "\n" : "",
  285. (unsigned long)thetime,
  286. (long)datastab.st_size) == EOF)
  287. werr(debar);
  288. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (data)"));
  289. if (!(c3= m_fork())) {
  290. m_dup2(gzfd,0); m_dup2(fileno(ar),1);
  291. execlp(CAT,"cat",(char*)0); ohshite(_("failed to exec cat (data)"));
  292. }
  293. waitsubproc(c3,"cat (data)",0);
  294. if (datastab.st_size & 1)
  295. if (putc('\n',ar) == EOF)
  296. werr(debar);
  297. }
  298. if (fclose(ar)) werr(debar);
  299. exit(0);
  300. }