build.c 17 KB

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