build.c 18 KB

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