build.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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=(char*)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=(struct _finfo*)malloc(sizeof(struct _finfo));
  119. lstat(fn, &(fi->st));
  120. fi->fn=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, 0
  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 = (char *)malloc(strlen(envbuf)+13);
  176. strcpy(tfbuf,envbuf);
  177. strcat(tfbuf,"/dpkg.XXXXXX");
  178. subdir= 0;
  179. if ((debar= *argv++) !=0) {
  180. if (*argv) badusage(_("--build takes at most two arguments"));
  181. if (debar) {
  182. if (stat(debar,&debarstab)) {
  183. if (errno != ENOENT)
  184. ohshite(_("unable to check for existence of archive `%.250s'"),debar);
  185. } else if (S_ISDIR(debarstab.st_mode)) {
  186. subdir= 1;
  187. }
  188. }
  189. } else {
  190. m= m_malloc(strlen(directory) + sizeof(DEBEXT));
  191. strcpy(m,directory); strcat(m,DEBEXT);
  192. debar= m;
  193. }
  194. /* Perform some sanity checks on the to-be-build package.
  195. */
  196. if (nocheckflag) {
  197. if (subdir)
  198. ohshit(_("target is directory - cannot skip control file check"));
  199. printf(_("dpkg-deb: warning, not checking contents of control area.\n"
  200. "dpkg-deb: building an unknown package in `%s'.\n"), debar);
  201. } else {
  202. controlfile= m_malloc(strlen(directory) + sizeof(BUILDCONTROLDIR) +
  203. sizeof(CONTROLFILE) + sizeof(CONFFILESFILE) +
  204. sizeof(POSTINSTFILE) + sizeof(PREINSTFILE) +
  205. sizeof(POSTRMFILE) + sizeof(PRERMFILE) +
  206. MAXCONFFILENAME + 5);
  207. /* Lets start by reading in the control-file so we can check its contents */
  208. strcpy(controlfile, directory);
  209. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONTROLFILE);
  210. warns= 0; errs= 0;
  211. parsedb(controlfile, pdb_recordavailable|pdb_rejectstatus,
  212. &checkedinfo, stderr, &warns);
  213. assert(checkedinfo->available.valid);
  214. if (strspn(checkedinfo->name,
  215. "abcdefghijklmnopqrstuvwxyz0123456789+-.")
  216. != strlen(checkedinfo->name))
  217. ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
  218. if (checkedinfo->priority == pri_other) {
  219. fprintf(stderr, _("warning, `%s' contains user-defined Priority value `%s'\n"),
  220. controlfile, checkedinfo->otherpriority);
  221. warns++;
  222. }
  223. for (field= checkedinfo->available.arbs; field; field= field->next) {
  224. if (known_arbitrary_field(field))
  225. continue;
  226. fprintf(stderr, _("warning, `%s' contains user-defined field `%s'\n"),
  227. controlfile, field->name);
  228. warns++;
  229. }
  230. checkversion(checkedinfo->available.version.version,"(upstream) version",&errs);
  231. checkversion(checkedinfo->available.version.revision,"Debian revision",&errs);
  232. if (errs) ohshit(_("%d errors in control file"),errs);
  233. if (subdir) {
  234. versionstring= versiondescribe(&checkedinfo->available.version,vdew_never);
  235. arch= checkedinfo->available.architecture; if (!arch) arch= "";
  236. m= m_malloc(sizeof(DEBEXT)+1+strlen(debar)+1+strlen(checkedinfo->name)+
  237. strlen(versionstring)+1+strlen(arch));
  238. sprintf(m,"%s/%s_%s%s%s" DEBEXT,debar,checkedinfo->name,versionstring,
  239. arch[0] ? "_" : "", arch);
  240. debar= m;
  241. }
  242. printf(_("dpkg-deb: building package `%s' in `%s'.\n"), checkedinfo->name, debar);
  243. /* Check file permissions */
  244. strcpy(controlfile, directory);
  245. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  246. if (lstat(controlfile,&mscriptstab)) ohshite("unable to stat control directory");
  247. if (!S_ISDIR(mscriptstab.st_mode)) ohshit("control directory is not a directory");
  248. if ((mscriptstab.st_mode & 07757) != 0755)
  249. ohshit(_("control directory has bad permissions %03lo (must be >=0755 "
  250. "and <=0775)"), (unsigned long)(mscriptstab.st_mode & 07777));
  251. for (mscriptp= maintainerscripts; *mscriptp; mscriptp++) {
  252. strcpy(controlfile, directory);
  253. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  254. strcat(controlfile, *mscriptp);
  255. if (!lstat(controlfile,&mscriptstab)) {
  256. if (S_ISLNK(mscriptstab.st_mode)) continue;
  257. if (!S_ISREG(mscriptstab.st_mode))
  258. ohshit(_("maintainer script `%.50s' is not a plain file or symlink"),*mscriptp);
  259. if ((mscriptstab.st_mode & 07557) != 0555)
  260. ohshit(_("maintainer script `%.50s' has bad permissions %03lo "
  261. "(must be >=0555 and <=0775)"),
  262. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  263. } else if (errno != ENOENT) {
  264. ohshite(_("maintainer script `%.50s' is not stattable"),*mscriptp);
  265. }
  266. }
  267. /* Check if conffiles contains sane information */
  268. strcpy(controlfile, directory);
  269. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONFFILESFILE);
  270. if ((cf= fopen(controlfile,"r"))) {
  271. while (fgets(conffilename,MAXCONFFILENAME+1,cf)) {
  272. n= strlen(conffilename);
  273. if (!n) ohshite(_("empty string from fgets reading conffiles"));
  274. if (conffilename[n-1] != '\n') {
  275. fprintf(stderr, _("warning, conffile name `%.50s...' is too long, or missing final newline\n"),
  276. conffilename);
  277. warns++;
  278. while ((c= getc(cf)) != EOF && c != '\n');
  279. continue;
  280. }
  281. conffilename[n-1]= 0;
  282. strcpy(controlfile, directory);
  283. strcat(controlfile, "/");
  284. strcat(controlfile, conffilename);
  285. if (lstat(controlfile,&controlstab)) {
  286. if (errno == ENOENT) {
  287. if((n > 1) && isspace(conffilename[n-2]))
  288. fprintf(stderr, _("warning, "
  289. "conffile filename `%s' contains trailing white spaces\n"), conffilename);
  290. ohshit(_("conffile `%.250s' does not appear in package"), conffilename);
  291. } else
  292. ohshite(_("conffile `%.250s' is not stattable"), conffilename);
  293. } else if (!S_ISREG(controlstab.st_mode)) {
  294. fprintf(stderr, _("warning, conffile `%s'"
  295. " is not a plain file\n"), conffilename);
  296. warns++;
  297. }
  298. }
  299. if (ferror(cf)) ohshite(_("error reading conffiles file"));
  300. fclose(cf);
  301. } else if (errno != ENOENT) {
  302. ohshite(_("error opening conffiles file"));
  303. }
  304. if (warns) {
  305. if (fprintf(stderr, _("dpkg-deb: ignoring %d warnings about the control"
  306. " file(s)\n"), warns) == EOF) werr("stderr");
  307. }
  308. }
  309. if (ferror(stdout)) werr("stdout");
  310. /* Now that we have verified everything its time to actually
  311. * build something. Lets start by making the ar-wrapper.
  312. */
  313. if (!(ar=fopen(debar,"wb"))) ohshite(_("unable to create `%.255s'"),debar);
  314. if (setvbuf(ar, 0, _IONBF, 0)) ohshite(_("unable to unbuffer `%.255s'"),debar);
  315. /* Fork a tar to package the control-section of the package */
  316. m_pipe(p1);
  317. if (!(c1= m_fork())) {
  318. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  319. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  320. if (chdir(BUILDCONTROLDIR)) ohshite(_("failed to chdir to .../DEBIAN"));
  321. execlp(TAR, "tar", "-cf", "-", ".", NULL);
  322. ohshite(_("failed to exec tar -cf"));
  323. }
  324. close(p1[1]);
  325. /* Create a temporary file to store the control data in. Immediately unlink
  326. * our temporary file so others can't mess with it.
  327. */
  328. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (control)"));
  329. if ((gz= fdopen(gzfd,"a")) == NULL) ohshite(_("failed to open tmpfile "
  330. "(control), %s"), tfbuf);
  331. /* make sure it's gone, the fd will remain until we close it */
  332. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (control), %s"),
  333. tfbuf);
  334. /* reset this, so we can use it elsewhere */
  335. strcpy(tfbuf,envbuf);
  336. strcat(tfbuf,"/dpkg.XXXXXX");
  337. /* And run gzip to compress our control archive */
  338. if (!(c2= m_fork())) {
  339. m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]); close(gzfd);
  340. compress_cat(compress_type_gzip, 0, 1, "9", _("control"));
  341. }
  342. close(p1[0]);
  343. waitsubproc(c2,"gzip -9c",0);
  344. waitsubproc(c1,"tar -cf",0);
  345. if (fstat(gzfd,&controlstab)) ohshite(_("failed to fstat tmpfile (control)"));
  346. /* We have our first file for the ar-archive. Write a header for it to the
  347. * package and insert it.
  348. */
  349. if (oldformatflag) {
  350. if (fprintf(ar, "%-8s\n%ld\n", OLDARCHIVEVERSION, (long)controlstab.st_size) == EOF)
  351. werr(debar);
  352. } else {
  353. thetime= time(0);
  354. if (fprintf(ar,
  355. "!<arch>\n"
  356. "debian-binary %-12lu0 0 100644 %-10ld`\n"
  357. ARCHIVEVERSION "\n"
  358. "%s"
  359. ADMINMEMBER "%-12lu0 0 100644 %-10ld`\n",
  360. thetime,
  361. (long)sizeof(ARCHIVEVERSION),
  362. (sizeof(ARCHIVEVERSION)&1) ? "\n" : "",
  363. (unsigned long)thetime,
  364. (long)controlstab.st_size) == EOF)
  365. werr(debar);
  366. }
  367. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (control)"));
  368. fd_fd_copy(gzfd, fileno(ar), -1, _("control"));
  369. /* Control is done, now we need to archive the data. Start by creating
  370. * a new temporary file. Immediately unlink the temporary file so others
  371. * can't mess with it. */
  372. if (!oldformatflag) {
  373. fclose(gz);
  374. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (data)"));
  375. if ((gz= fdopen(gzfd,"a")) == NULL) ohshite(_("failed to open tmpfile "
  376. "(data), %s"), tfbuf);
  377. /* make sure it's gone, the fd will remain until we close it */
  378. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (data), %s"),
  379. tfbuf);
  380. /* reset these, in case we want to use the later */
  381. strcpy(tfbuf,envbuf);
  382. strcat(tfbuf,"/dpkg.XXXXXX");
  383. }
  384. /* Fork off a tar. We will feed it a list of filenames on stdin later.
  385. */
  386. m_pipe(p1);
  387. m_pipe(p2);
  388. if (!(c1= m_fork())) {
  389. m_dup2(p1[0],0); close(p1[0]); close(p1[1]);
  390. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  391. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  392. execlp(TAR, "tar", "-cf", "-", "--null", "-T", "-", "--no-recursion", NULL);
  393. ohshite(_("failed to exec tar -cf"));
  394. }
  395. close(p1[0]);
  396. close(p2[1]);
  397. /* Of course we should not forget to compress the archive as well.. */
  398. if (!(c2= m_fork())) {
  399. close(p1[1]);
  400. m_dup2(p2[0],0); close(p2[0]);
  401. m_dup2(oldformatflag ? fileno(ar) : gzfd,1);
  402. compress_cat(compress_type, 0, 1, compression, _("data"));
  403. }
  404. close(p2[0]);
  405. /* All the pipes are set, now lets run find, and start feeding
  406. * filenames to tar.
  407. */
  408. m_pipe(p3);
  409. if (!(c3= m_fork())) {
  410. m_dup2(p3[1],1); close(p3[0]); close(p3[1]);
  411. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  412. execlp(FIND, "find", ".", "-path", "./" BUILDCONTROLDIR, "-prune", "-o",
  413. "-print0", NULL);
  414. ohshite(_("failed to exec find"));
  415. }
  416. close(p3[1]);
  417. /* We need to reorder the files so we can make sure that symlinks
  418. * will not appear before their target.
  419. */
  420. while ((fi=getfi(directory, p3[0]))!=NULL)
  421. if (S_ISLNK(fi->st.st_mode))
  422. add_to_filist(fi,&symlist,&symlist_end);
  423. else {
  424. if (write(p1[1], fi->fn, strlen(fi->fn)+1) ==- 1)
  425. ohshite(_("failed to write filename to tar pipe (data)"));
  426. }
  427. close(p3[0]);
  428. waitsubproc(c3,"find",0);
  429. for (fi= symlist;fi;fi= fi->next)
  430. if (write(p1[1], fi->fn, strlen(fi->fn)+1) == -1)
  431. ohshite(_("failed to write filename to tar pipe (data)"));
  432. /* All done, clean up wait for tar and gzip to finish their job */
  433. close(p1[1]);
  434. free_filist(symlist);
  435. waitsubproc(c2,"<compress> from tar -cf",0);
  436. waitsubproc(c1,"tar -cf",0);
  437. /* Okay, we have data.tar.gz as well now, add it to the ar wrapper */
  438. if (!oldformatflag) {
  439. const char *datamember;
  440. switch (compress_type) {
  441. case compress_type_gzip:
  442. datamember = DATAMEMBER_GZ;
  443. break;
  444. case compress_type_bzip2:
  445. datamember = DATAMEMBER_BZ2;
  446. break;
  447. case compress_type_lzma:
  448. datamember = DATAMEMBER_LZMA;
  449. break;
  450. case compress_type_cat:
  451. datamember = DATAMEMBER_CAT;
  452. break;
  453. default:
  454. ohshit(_("Internal error, compress_type `%i' unknown!"), compress_type);
  455. }
  456. if (fstat(gzfd,&datastab)) ohshite("_(failed to fstat tmpfile (data))");
  457. if (fprintf(ar,
  458. "%s"
  459. "%s" "%-12lu0 0 100644 %-10ld`\n",
  460. (controlstab.st_size & 1) ? "\n" : "",
  461. datamember,
  462. (unsigned long)thetime,
  463. (long)datastab.st_size) == EOF)
  464. werr(debar);
  465. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (data)"));
  466. fd_fd_copy(gzfd, fileno(ar), -1, _("cat (data)"));
  467. if (datastab.st_size & 1)
  468. if (putc('\n',ar) == EOF)
  469. werr(debar);
  470. }
  471. if (fclose(ar)) werr(debar);
  472. exit(0);
  473. }