build.c 17 KB

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