build.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. * Copyright (C) 2000 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <sys/wait.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #include <unistd.h>
  32. #include <dirent.h>
  33. #include <limits.h>
  34. #include <ctype.h>
  35. #include <assert.h>
  36. #include <config.h>
  37. #include <dpkg.h>
  38. #include <dpkg-db.h>
  39. #include "dpkg-deb.h"
  40. #ifndef S_ISLNK
  41. # define S_ISLNK(mode) ((mode&0xF000) == S_IFLNK)
  42. #endif
  43. /* Simple structure to store information about a file.
  44. */
  45. struct _finfo {
  46. struct stat st;
  47. char* fn;
  48. struct _finfo* next;
  49. };
  50. /* Do a quick check if vstring is a valid versionnumber. Valid in this case
  51. * means it contains at least one digit. If an error is found increment
  52. * *errs.
  53. */
  54. static void checkversion(const char *vstring, const char *valuename, int *errs) {
  55. const char *p;
  56. if (!vstring || !*vstring) return;
  57. for (p=vstring; *p; p++) if (isdigit(*p)) return;
  58. fprintf(stderr, _("dpkg-deb - error: %s (`%s') doesn't contain any digits\n"),
  59. valuename, vstring);
  60. (*errs)++;
  61. }
  62. /* Read the next filename from a filedescriptor and create a _info struct
  63. * for it. If there is nothing to read return NULL.
  64. */
  65. static struct _finfo* getfi(const char* root, int fd) {
  66. static char* fn = NULL;
  67. static int fnlen = 0;
  68. int i = 0;
  69. struct _finfo *fi;
  70. size_t rl = strlen(root);
  71. if (fn == NULL) {
  72. fnlen=rl+2048;
  73. fn=(char*)malloc(fnlen);
  74. } else if (fnlen < (rl+2048)) {
  75. fnlen=rl+2048;
  76. fn=(char*)realloc(fn,fnlen);
  77. }
  78. i=sprintf(fn,"%s/",root);
  79. while (1) {
  80. int res;
  81. if (i>=fnlen) {
  82. fnlen+=2048;
  83. fn=(char*)realloc(fn,fnlen);
  84. }
  85. if ((res=read(fd, (fn+i), sizeof(*fn)))<0) {
  86. if ((errno==EINTR) || (errno==EAGAIN))
  87. continue;
  88. else
  89. return NULL;
  90. }
  91. if (res==0) // EOF -> parent died
  92. return NULL;
  93. if (fn[i]==0)
  94. break;
  95. i++;
  96. assert(i<2048);
  97. }
  98. fi=(struct _finfo*)malloc(sizeof(struct _finfo));
  99. lstat(fn, &(fi->st));
  100. fi->fn=strdup(fn+rl+1);
  101. fi->next=NULL;
  102. return fi;
  103. }
  104. /* Add a new _finfo struct to a single linked list of _finfo structs.
  105. * We perform a slight optimization to work around a `feature' in tar: tar
  106. * always recurses into subdirectories if you list a subdirectory. So if an
  107. * entry is added and the previous entry in the list is its subdirectory we
  108. * remove the subdirectory.
  109. *
  110. * After a _finfo struct is added to a list it may no longer be freed, we
  111. * assume full responsibility for its memory.
  112. */
  113. static void add_to_filist(struct _finfo* fi, struct _finfo** start, struct _finfo **end) {
  114. if (*start==NULL)
  115. *start=*end=fi;
  116. else
  117. *end=(*end)->next=fi;
  118. }
  119. /* Free the memory for all entries in a list of _finfo structs
  120. */
  121. static void free_filist(struct _finfo* fi) {
  122. while (fi) {
  123. struct _finfo* fl;
  124. free(fi->fn);
  125. fl=fi; fi=fi->next;
  126. free(fl);
  127. }
  128. }
  129. /* Overly complex function that builds a .deb file
  130. */
  131. void do_build(const char *const *argv) {
  132. static const char *const maintainerscripts[]= {
  133. PREINSTFILE, POSTINSTFILE, PRERMFILE, POSTRMFILE, 0
  134. };
  135. char *m;
  136. const char *debar, *directory, *const *mscriptp, *versionstring, *arch;
  137. char *controlfile, *tfbuf, *envbuf;
  138. struct pkginfo *checkedinfo;
  139. struct arbitraryfield *field;
  140. FILE *ar, *gz, *cf;
  141. int p1[2],p2[2], warns, errs, n, c, subdir, gzfd;
  142. pid_t c1,c2,c3,c4,c5;
  143. struct stat controlstab, datastab, mscriptstab, debarstab;
  144. char conffilename[MAXCONFFILENAME+1];
  145. time_t thetime= 0;
  146. struct _finfo *fi;
  147. struct _finfo *nosymlist = NULL;
  148. struct _finfo *nosymlist_end = NULL;
  149. struct _finfo *symlist = NULL;
  150. struct _finfo *symlist_end = NULL;
  151. /* Decode our arguments */
  152. directory= *argv++; if (!directory) badusage(_("--build needs a directory argument"));
  153. /* template for our tempfiles */
  154. if ((envbuf= getenv("TMPDIR")) == NULL)
  155. envbuf= (char *)P_tmpdir;
  156. tfbuf = (char *)malloc(strlen(envbuf)+13);
  157. strcpy(tfbuf,envbuf);
  158. strcat(tfbuf,"/dpkg.XXXXXX");
  159. subdir= 0;
  160. if ((debar= *argv++) !=0) {
  161. if (*argv) badusage(_("--build takes at most two arguments"));
  162. if (debar) {
  163. if (stat(debar,&debarstab)) {
  164. if (errno != ENOENT)
  165. ohshite(_("unable to check for existence of archive `%.250s'"),debar);
  166. } else if (S_ISDIR(debarstab.st_mode)) {
  167. subdir= 1;
  168. }
  169. }
  170. } else {
  171. m= m_malloc(strlen(directory) + sizeof(DEBEXT));
  172. strcpy(m,directory); strcat(m,DEBEXT);
  173. debar= m;
  174. }
  175. /* Perform some sanity checks on the to-be-build package.
  176. */
  177. if (nocheckflag) {
  178. if (subdir)
  179. ohshit(_("target is directory - cannot skip control file check"));
  180. printf(_("dpkg-deb: warning, not checking contents of control area.\n"
  181. "dpkg-deb: building an unknown package in `%s'.\n"), debar);
  182. } else {
  183. controlfile= m_malloc(strlen(directory) + sizeof(BUILDCONTROLDIR) +
  184. sizeof(CONTROLFILE) + sizeof(CONFFILESFILE) +
  185. sizeof(POSTINSTFILE) + sizeof(PREINSTFILE) +
  186. sizeof(POSTRMFILE) + sizeof(PRERMFILE) +
  187. MAXCONFFILENAME + 5);
  188. /* Lets start by reading in the control-file so we can check its contents */
  189. strcpy(controlfile, directory);
  190. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONTROLFILE);
  191. warns= 0; errs= 0;
  192. parsedb(controlfile, pdb_recordavailable|pdb_rejectstatus,
  193. &checkedinfo, stderr, &warns);
  194. assert(checkedinfo->available.valid);
  195. if (strspn(checkedinfo->name,
  196. "abcdefghijklmnopqrstuvwxyz0123456789+-.")
  197. != strlen(checkedinfo->name))
  198. ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
  199. if (checkedinfo->priority == pri_other) {
  200. fprintf(stderr, _("warning, `%s' contains user-defined Priority value `%s'\n"),
  201. controlfile, checkedinfo->otherpriority);
  202. warns++;
  203. }
  204. for (field= checkedinfo->available.arbs; field; field= field->next) {
  205. fprintf(stderr, _("warning, `%s' contains user-defined field `%s'\n"),
  206. controlfile, field->name);
  207. warns++;
  208. }
  209. checkversion(checkedinfo->available.version.version,"(upstream) version",&errs);
  210. checkversion(checkedinfo->available.version.revision,"Debian revision",&errs);
  211. if (errs) ohshit(_("%d errors in control file"),errs);
  212. if (subdir) {
  213. versionstring= versiondescribe(&checkedinfo->available.version,vdew_never);
  214. arch= checkedinfo->available.architecture; if (!arch) arch= "";
  215. m= m_malloc(sizeof(DEBEXT)+1+strlen(debar)+1+strlen(checkedinfo->name)+
  216. strlen(versionstring)+1+strlen(arch));
  217. sprintf(m,"%s/%s_%s%s%s" DEBEXT,debar,checkedinfo->name,versionstring,
  218. arch[0] ? "_" : "", arch);
  219. debar= m;
  220. }
  221. printf(_("dpkg-deb: building package `%s' in `%s'.\n"), checkedinfo->name, debar);
  222. /* Check file permissions */
  223. strcpy(controlfile, directory);
  224. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  225. if (lstat(controlfile,&mscriptstab)) ohshite("unable to stat control directory");
  226. if (!S_ISDIR(mscriptstab.st_mode)) ohshit("control directory is not a directory");
  227. if ((mscriptstab.st_mode & 07757) != 0755)
  228. ohshit(_("control directory has bad permissions %03lo (must be >=0755 "
  229. "and <=0775)"), (unsigned long)(mscriptstab.st_mode & 07777));
  230. for (mscriptp= maintainerscripts; *mscriptp; mscriptp++) {
  231. strcpy(controlfile, directory);
  232. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  233. strcat(controlfile, *mscriptp);
  234. if (!lstat(controlfile,&mscriptstab)) {
  235. if (S_ISLNK(mscriptstab.st_mode)) continue;
  236. if (!S_ISREG(mscriptstab.st_mode))
  237. ohshit(_("maintainer script `%.50s' is not a plain file or symlink"),*mscriptp);
  238. if ((mscriptstab.st_mode & 07557) != 0555)
  239. ohshit(_("maintainer script `%.50s' has bad permissions %03lo "
  240. "(must be >=0555 and <=0775)"),
  241. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  242. } else if (errno != ENOENT) {
  243. ohshite(_("maintainer script `%.50s' is not stattable"),*mscriptp);
  244. }
  245. }
  246. /* Check if conffiles contains sane information */
  247. strcpy(controlfile, directory);
  248. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONFFILESFILE);
  249. if ((cf= fopen(controlfile,"r"))) {
  250. while (fgets(conffilename,MAXCONFFILENAME+1,cf)) {
  251. n= strlen(conffilename);
  252. if (!n) ohshite(_("empty string from fgets reading conffiles"));
  253. if (conffilename[n-1] != '\n') {
  254. fprintf(stderr, _("warning, conffile name `%.50s...' is too long, or missing final newline\n"),
  255. conffilename);
  256. warns++;
  257. while ((c= getc(cf)) != EOF && c != '\n');
  258. continue;
  259. }
  260. conffilename[n-1]= 0;
  261. strcpy(controlfile, directory);
  262. strcat(controlfile, "/");
  263. strcat(controlfile, conffilename);
  264. if (lstat(controlfile,&controlstab)) {
  265. if (errno == ENOENT)
  266. ohshit(_("conffile `%.250s' does not appear in package"),conffilename);
  267. else
  268. ohshite(_("conffile `%.250s' is not stattable"),conffilename);
  269. } else if (!S_ISREG(controlstab.st_mode)) {
  270. fprintf(stderr, _("warning, conffile `%s'"
  271. " is not a plain file\n"), conffilename);
  272. warns++;
  273. }
  274. }
  275. if (ferror(cf)) ohshite(_("error reading conffiles file"));
  276. fclose(cf);
  277. } else if (errno != ENOENT) {
  278. ohshite(_("error opening conffiles file"));
  279. }
  280. if (warns) {
  281. if (fprintf(stderr, _("dpkg-deb: ignoring %d warnings about the control"
  282. " file(s)\n"), warns) == EOF) werr("stderr");
  283. }
  284. }
  285. if (ferror(stdout)) werr("stdout");
  286. /* Now that we have verified everything its time to actually
  287. * build something. Lets start by making the ar-wrapper.
  288. */
  289. if (!(ar=fopen(debar,"wb"))) ohshite(_("unable to create `%.255s'"),debar);
  290. if (setvbuf(ar, 0, _IONBF, 0)) ohshite(_("unable to unbuffer `%.255s'"),debar);
  291. /* Fork a tar to package the control-section of the package */
  292. m_pipe(p1);
  293. if (!(c1= m_fork())) {
  294. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  295. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  296. if (chdir(BUILDCONTROLDIR)) ohshite(_("failed to chdir to .../DEBIAN"));
  297. execlp(TAR,"tar","-cf","-",".",(char*)0); ohshite(_("failed to exec tar -cf"));
  298. }
  299. close(p1[1]);
  300. /* Create a temporary file to store the control data in. Immediately unlink
  301. * our temporary file so others can't mess with it.
  302. */
  303. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (control)"));
  304. if ((gz= fdopen(gzfd,"a")) == NULL) ohshite(_("failed to open tmpfile "
  305. "(control), %s"), tfbuf);
  306. /* make sure it's gone, the fd will remain until we close it */
  307. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (control), %s"),
  308. tfbuf);
  309. /* reset this, so we can use it elsewhere */
  310. strcpy(tfbuf,envbuf);
  311. strcat(tfbuf,"/dpkg.XXXXXX");
  312. /* And run gzip to compress our control archive */
  313. if (!(c2= m_fork())) {
  314. m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]); close(gzfd);
  315. execlp(GZIP,"gzip","-9c",(char*)0); ohshite(_("failed to exec gzip -9c"));
  316. }
  317. close(p1[0]);
  318. waitsubproc(c2,"gzip -9c",0);
  319. waitsubproc(c1,"tar -cf",0);
  320. if (fstat(gzfd,&controlstab)) ohshite(_("failed to fstat tmpfile (control)"));
  321. /* We have our first file for the ar-archive. Write a header for it to the
  322. * package and insert it.
  323. */
  324. if (oldformatflag) {
  325. if (fprintf(ar, "%-8s\n%ld\n", OLDARCHIVEVERSION, (long)controlstab.st_size) == EOF)
  326. werr(debar);
  327. } else {
  328. thetime= time(0);
  329. if (fprintf(ar,
  330. "!<arch>\n"
  331. "debian-binary %-12lu0 0 100644 %-10ld`\n"
  332. ARCHIVEVERSION "\n"
  333. "%s"
  334. ADMINMEMBER "%-12lu0 0 100644 %-10ld`\n",
  335. thetime,
  336. (long)sizeof(ARCHIVEVERSION),
  337. (sizeof(ARCHIVEVERSION)&1) ? "\n" : "",
  338. (unsigned long)thetime,
  339. (long)controlstab.st_size) == EOF)
  340. werr(debar);
  341. }
  342. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (control)"));
  343. do_fd_copy(gzfd, fileno(ar), _("control"));
  344. /* Control is done, now we need to archive the data. Start by creating
  345. * a new temporary file. Immediately unlink the temporary file so others
  346. * can't mess with it. */
  347. if (!oldformatflag) {
  348. fclose(gz);
  349. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (data)"));
  350. if ((gz= fdopen(gzfd,"a")) == NULL) ohshite(_("failed to open tmpfile "
  351. "(data), %s"), tfbuf);
  352. /* make sure it's gone, the fd will remain until we close it */
  353. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (data), %s"),
  354. tfbuf);
  355. /* reset these, in case we want to use the later */
  356. strcpy(tfbuf,envbuf);
  357. strcat(tfbuf,"/dpkg.XXXXXX");
  358. }
  359. /* We need to reorder the files so we can make sure that symlinks
  360. * will not appear before their target.
  361. */
  362. m_pipe(p2);
  363. if (!(c4= m_fork())) {
  364. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  365. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  366. execlp(FIND,"find",".","-path","./" BUILDCONTROLDIR,"-prune","-o","-print0",(char*)0);
  367. ohshite(_("failed to exec find"));
  368. }
  369. close(p2[1]);
  370. while ((fi=getfi(directory, p2[0]))!=NULL)
  371. if (S_ISLNK(fi->st.st_mode))
  372. add_to_filist(fi,&symlist,&symlist_end);
  373. else
  374. add_to_filist(fi,&nosymlist,&nosymlist_end);
  375. close(p2[0]);
  376. waitsubproc(c4,"find",0);
  377. /* Fork off a tar. We will feed it a list of filenames on stdin later.
  378. */
  379. m_pipe(p1);
  380. m_pipe(p2);
  381. if (!(c4= m_fork())) {
  382. m_dup2(p1[0],0); close(p1[0]); close(p1[1]);
  383. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  384. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  385. execlp(TAR,"tar","-cf", "-", "-T", "-", "--null", "--no-recursion", (char*)0);
  386. ohshite(_("failed to exec tar -cf"));
  387. }
  388. close(p1[0]);
  389. close(p2[1]);
  390. /* Of course we should not forget to compress the archive as well.. */
  391. if (!(c5= m_fork())) {
  392. char *combuf;
  393. close(p1[1]);
  394. m_dup2(p2[0],0); close(p2[0]);
  395. m_dup2(oldformatflag ? fileno(ar) : gzfd,1);
  396. combuf = strdup("-9c");
  397. if(compression != NULL) {
  398. if(*compression == '0') {
  399. do_fd_copy(0, 1, _("no compression copy loop"));
  400. exit(0);
  401. }
  402. combuf[1] = *compression;
  403. }
  404. execlp(GZIP,"gzip",combuf,(char*)0);
  405. ohshite(_("failed to exec gzip %s from tar -cf"), combuf);
  406. }
  407. close(p2[0]);
  408. /* All the pipes are set, lets feed tar its filenames */
  409. for (fi= nosymlist;fi;fi= fi->next)
  410. if (write(p1[1], fi->fn, strlen(fi->fn)+1) ==- 1)
  411. ohshite(_("failed to write filename to tar pipe (data)"));
  412. for (fi= symlist;fi;fi= fi->next)
  413. if (write(p1[1], fi->fn, strlen(fi->fn)+1) == -1)
  414. ohshite(_("failed to write filename to tar pipe (data)"));
  415. /* All done, clean up wait for tar and gzip to finish their job */
  416. close(p1[1]);
  417. free_filist(nosymlist);
  418. free_filist(symlist);
  419. waitsubproc(c5,"gzip -9c from tar -cf",0);
  420. waitsubproc(c4,"tar -cf",0);
  421. /* Okay, we have data.tar.gz as well now, add it to the ar wrapper */
  422. if (!oldformatflag) {
  423. if (fstat(gzfd,&datastab)) ohshite("_(failed to fstat tmpfile (data))");
  424. if (fprintf(ar,
  425. "%s"
  426. DATAMEMBER "%-12lu0 0 100644 %-10ld`\n",
  427. (controlstab.st_size & 1) ? "\n" : "",
  428. (unsigned long)thetime,
  429. (long)datastab.st_size) == EOF)
  430. werr(debar);
  431. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (data)"));
  432. if (!(c3= m_fork())) {
  433. m_dup2(gzfd,0); m_dup2(fileno(ar),1);
  434. execlp(CAT,"cat",(char*)0); ohshite(_("failed to exec cat (data)"));
  435. }
  436. waitsubproc(c3,"cat (data)",0);
  437. if (datastab.st_size & 1)
  438. if (putc('\n',ar) == EOF)
  439. werr(debar);
  440. }
  441. if (fclose(ar)) werr(debar);
  442. exit(0);
  443. }