build.c 18 KB

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