build.c 18 KB

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