build.c 17 KB

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