build.c 17 KB

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