build.c 18 KB

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