build.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * build.c - building archives
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 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 published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but 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 License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <errno.h>
  27. #include <limits.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <dirent.h>
  31. #include <unistd.h>
  32. #include <stdbool.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include <dpkg/path.h>
  39. #include <dpkg/buffer.h>
  40. #include <dpkg/subproc.h>
  41. #include <dpkg/compress.h>
  42. #include <dpkg/ar.h>
  43. #include <dpkg/myopt.h>
  44. #include "dpkg-deb.h"
  45. /* Simple structure to store information about a file.
  46. */
  47. struct file_info {
  48. struct file_info *next;
  49. struct stat st;
  50. char* fn;
  51. };
  52. static const char *arbitrary_fields[] = {
  53. "Package-Type",
  54. "Subarchitecture",
  55. "Kernel-Version",
  56. "Installer-Menu-Item",
  57. "Homepage",
  58. "Tag",
  59. NULL
  60. };
  61. static const char private_prefix[] = "Private-";
  62. static bool
  63. known_arbitrary_field(const struct arbitraryfield *field)
  64. {
  65. const char **known;
  66. /* Always accept fields starting with a private field prefix. */
  67. if (strncasecmp(field->name, private_prefix, strlen(private_prefix)) == 0)
  68. return true;
  69. for (known= arbitrary_fields; *known; known++)
  70. if (strcasecmp(field->name, *known) == 0)
  71. return true;
  72. return false;
  73. }
  74. static struct file_info *
  75. file_info_new(const char *filename)
  76. {
  77. struct file_info *fi;
  78. fi = m_malloc(sizeof(*fi));
  79. fi->fn = m_strdup(filename);
  80. fi->next = NULL;
  81. return fi;
  82. }
  83. static void
  84. file_info_free(struct file_info *fi)
  85. {
  86. free(fi->fn);
  87. free(fi);
  88. }
  89. static struct file_info *
  90. file_info_find_name(struct file_info *list, const char *filename)
  91. {
  92. struct file_info *node;
  93. for (node = list; node; node = node->next)
  94. if (strcmp(node->fn, filename) == 0)
  95. return node;
  96. return NULL;
  97. }
  98. /*
  99. * Read the next filename from a filedescriptor and create a file_info struct
  100. * for it. If there is nothing to read return NULL.
  101. */
  102. static struct file_info *
  103. getfi(const char *root, int fd)
  104. {
  105. static char* fn = NULL;
  106. static size_t fnlen = 0;
  107. size_t i= 0;
  108. struct file_info *fi;
  109. size_t rl = strlen(root);
  110. if (fn == NULL) {
  111. fnlen = rl + MAXFILENAME;
  112. fn = m_malloc(fnlen);
  113. } else if (fnlen < (rl + MAXFILENAME)) {
  114. fnlen = rl + MAXFILENAME;
  115. fn = m_realloc(fn, fnlen);
  116. }
  117. i=sprintf(fn,"%s/",root);
  118. while (1) {
  119. int res;
  120. if (i>=fnlen) {
  121. fnlen += MAXFILENAME;
  122. fn = m_realloc(fn, fnlen);
  123. }
  124. if ((res=read(fd, (fn+i), sizeof(*fn)))<0) {
  125. if ((errno==EINTR) || (errno==EAGAIN))
  126. continue;
  127. else
  128. return NULL;
  129. }
  130. if (res == 0) /* EOF -> parent died. */
  131. return NULL;
  132. if (fn[i] == '\0')
  133. break;
  134. i++;
  135. if (i >= MAXFILENAME)
  136. ohshit(_("file name '%.50s...' is too long"), fn + rl + 1);
  137. }
  138. fi = file_info_new(fn + rl + 1);
  139. if (lstat(fn, &(fi->st)) != 0)
  140. ohshite(_("unable to stat file name '%.250s'"), fn);
  141. return fi;
  142. }
  143. /*
  144. * Add a new file_info struct to a single linked list of file_info structs.
  145. * We perform a slight optimization to work around a `feature' in tar: tar
  146. * always recurses into subdirectories if you list a subdirectory. So if an
  147. * entry is added and the previous entry in the list is its subdirectory we
  148. * remove the subdirectory.
  149. *
  150. * After a file_info struct is added to a list it may no longer be freed, we
  151. * assume full responsibility for its memory.
  152. */
  153. static void
  154. add_to_filist(struct file_info **start, struct file_info **end,
  155. struct file_info *fi)
  156. {
  157. if (*start==NULL)
  158. *start=*end=fi;
  159. else
  160. *end=(*end)->next=fi;
  161. }
  162. /*
  163. * Free the memory for all entries in a list of file_info structs.
  164. */
  165. static void
  166. free_filist(struct file_info *fi)
  167. {
  168. while (fi) {
  169. struct file_info *fl;
  170. fl=fi; fi=fi->next;
  171. file_info_free(fl);
  172. }
  173. }
  174. /* Overly complex function that builds a .deb file
  175. */
  176. void do_build(const char *const *argv) {
  177. static const char *const maintainerscripts[]= {
  178. PREINSTFILE, POSTINSTFILE, PRERMFILE, POSTRMFILE, NULL
  179. };
  180. char *m;
  181. const char *debar, *directory, *const *mscriptp, *versionstring, *arch;
  182. bool subdir;
  183. char *controlfile, *tfbuf;
  184. struct pkginfo *checkedinfo;
  185. struct arbitraryfield *field;
  186. FILE *ar, *cf;
  187. int p1[2], p2[2], p3[2], warns, n, c, gzfd;
  188. pid_t c1,c2,c3;
  189. struct stat controlstab, mscriptstab, debarstab;
  190. char conffilename[MAXCONFFILENAME+1];
  191. struct file_info *fi;
  192. struct file_info *symlist = NULL;
  193. struct file_info *symlist_end = NULL;
  194. /* Decode our arguments */
  195. directory = *argv++;
  196. if (!directory)
  197. badusage(_("--%s needs a <directory> argument"), cipaction->olong);
  198. subdir = false;
  199. debar = *argv++;
  200. if (debar != NULL) {
  201. if (*argv) badusage(_("--build takes at most two arguments"));
  202. if (debar) {
  203. if (stat(debar,&debarstab)) {
  204. if (errno != ENOENT)
  205. ohshite(_("unable to check for existence of archive `%.250s'"),debar);
  206. } else if (S_ISDIR(debarstab.st_mode)) {
  207. subdir = true;
  208. }
  209. }
  210. } else {
  211. m= m_malloc(strlen(directory) + sizeof(DEBEXT));
  212. strcpy(m, directory);
  213. path_rtrim_slash_slashdot(m);
  214. strcat(m, DEBEXT);
  215. debar= m;
  216. }
  217. /* Perform some sanity checks on the to-be-build package.
  218. */
  219. if (nocheckflag) {
  220. if (subdir)
  221. ohshit(_("target is directory - cannot skip control file check"));
  222. warning(_("not checking contents of control area."));
  223. printf(_("dpkg-deb: building an unknown package in '%s'.\n"), debar);
  224. } else {
  225. controlfile= m_malloc(strlen(directory) + sizeof(BUILDCONTROLDIR) +
  226. sizeof(CONTROLFILE) + sizeof(CONFFILESFILE) +
  227. sizeof(POSTINSTFILE) + sizeof(PREINSTFILE) +
  228. sizeof(POSTRMFILE) + sizeof(PRERMFILE) +
  229. MAXCONFFILENAME + 5);
  230. /* Lets start by reading in the control-file so we can check its contents */
  231. strcpy(controlfile, directory);
  232. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONTROLFILE);
  233. warns = 0;
  234. parsedb(controlfile, pdb_recordavailable|pdb_rejectstatus,
  235. &checkedinfo, stderr, &warns);
  236. if (strspn(checkedinfo->name,
  237. "abcdefghijklmnopqrstuvwxyz0123456789+-.")
  238. != strlen(checkedinfo->name))
  239. ohshit(_("package name has characters that aren't lowercase alphanums or `-+.'"));
  240. if (checkedinfo->priority == pri_other) {
  241. warning(_("'%s' contains user-defined Priority value '%s'"),
  242. controlfile, checkedinfo->otherpriority);
  243. warns++;
  244. }
  245. for (field= checkedinfo->available.arbs; field; field= field->next) {
  246. if (known_arbitrary_field(field))
  247. continue;
  248. warning(_("'%s' contains user-defined field '%s'"),
  249. controlfile, field->name);
  250. warns++;
  251. }
  252. if (subdir) {
  253. versionstring= versiondescribe(&checkedinfo->available.version,vdew_never);
  254. arch= checkedinfo->available.architecture; if (!arch) arch= "";
  255. m= m_malloc(sizeof(DEBEXT)+1+strlen(debar)+1+strlen(checkedinfo->name)+
  256. strlen(versionstring)+1+strlen(arch));
  257. sprintf(m,"%s/%s_%s%s%s" DEBEXT,debar,checkedinfo->name,versionstring,
  258. arch[0] ? "_" : "", arch);
  259. debar= m;
  260. }
  261. printf(_("dpkg-deb: building package `%s' in `%s'.\n"), checkedinfo->name, debar);
  262. /* Check file permissions */
  263. strcpy(controlfile, directory);
  264. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  265. if (lstat(controlfile, &mscriptstab))
  266. ohshite(_("unable to stat control directory"));
  267. if (!S_ISDIR(mscriptstab.st_mode))
  268. ohshit(_("control directory is not a directory"));
  269. if ((mscriptstab.st_mode & 07757) != 0755)
  270. ohshit(_("control directory has bad permissions %03lo (must be >=0755 "
  271. "and <=0775)"), (unsigned long)(mscriptstab.st_mode & 07777));
  272. for (mscriptp= maintainerscripts; *mscriptp; mscriptp++) {
  273. strcpy(controlfile, directory);
  274. strcat(controlfile, "/" BUILDCONTROLDIR "/");
  275. strcat(controlfile, *mscriptp);
  276. if (!lstat(controlfile,&mscriptstab)) {
  277. if (S_ISLNK(mscriptstab.st_mode)) continue;
  278. if (!S_ISREG(mscriptstab.st_mode))
  279. ohshit(_("maintainer script `%.50s' is not a plain file or symlink"),*mscriptp);
  280. if ((mscriptstab.st_mode & 07557) != 0555)
  281. ohshit(_("maintainer script `%.50s' has bad permissions %03lo "
  282. "(must be >=0555 and <=0775)"),
  283. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  284. } else if (errno != ENOENT) {
  285. ohshite(_("maintainer script `%.50s' is not stattable"),*mscriptp);
  286. }
  287. }
  288. /* Check if conffiles contains sane information */
  289. strcpy(controlfile, directory);
  290. strcat(controlfile, "/" BUILDCONTROLDIR "/" CONFFILESFILE);
  291. if ((cf= fopen(controlfile,"r"))) {
  292. struct file_info *conffiles_head = NULL;
  293. struct file_info *conffiles_tail = NULL;
  294. while (fgets(conffilename,MAXCONFFILENAME+1,cf)) {
  295. n= strlen(conffilename);
  296. if (!n) ohshite(_("empty string from fgets reading conffiles"));
  297. if (conffilename[n-1] != '\n') {
  298. warning(_("conffile name '%.50s...' is too long, or missing final newline"),
  299. conffilename);
  300. warns++;
  301. while ((c= getc(cf)) != EOF && c != '\n');
  302. continue;
  303. }
  304. conffilename[n - 1] = '\0';
  305. strcpy(controlfile, directory);
  306. strcat(controlfile, "/");
  307. strcat(controlfile, conffilename);
  308. if (lstat(controlfile,&controlstab)) {
  309. if (errno == ENOENT) {
  310. if((n > 1) && isspace(conffilename[n-2]))
  311. warning(_("conffile filename '%s' contains trailing white spaces"),
  312. conffilename);
  313. ohshit(_("conffile `%.250s' does not appear in package"), conffilename);
  314. } else
  315. ohshite(_("conffile `%.250s' is not stattable"), conffilename);
  316. } else if (!S_ISREG(controlstab.st_mode)) {
  317. warning(_("conffile '%s' is not a plain file"), conffilename);
  318. warns++;
  319. }
  320. if (file_info_find_name(conffiles_head, conffilename))
  321. warning(_("conffile name '%s' is duplicated"), conffilename);
  322. else {
  323. struct file_info *conffile;
  324. conffile = file_info_new(conffilename);
  325. add_to_filist(&conffiles_head, &conffiles_tail, conffile);
  326. }
  327. }
  328. free_filist(conffiles_head);
  329. if (ferror(cf)) ohshite(_("error reading conffiles file"));
  330. fclose(cf);
  331. } else if (errno != ENOENT) {
  332. ohshite(_("error opening conffiles file"));
  333. }
  334. if (warns)
  335. warning(P_("ignoring %d warning about the control file(s)\n",
  336. "ignoring %d warnings about the control file(s)\n", warns),
  337. warns);
  338. }
  339. m_output(stdout, _("<standard output>"));
  340. /* Now that we have verified everything its time to actually
  341. * build something. Lets start by making the ar-wrapper.
  342. */
  343. if (!(ar=fopen(debar,"wb"))) ohshite(_("unable to create `%.255s'"),debar);
  344. if (setvbuf(ar, NULL, _IONBF, 0))
  345. ohshite(_("unable to unbuffer `%.255s'"), debar);
  346. /* Fork a tar to package the control-section of the package */
  347. unsetenv("TAR_OPTIONS");
  348. m_pipe(p1);
  349. c1 = subproc_fork();
  350. if (!c1) {
  351. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  352. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  353. if (chdir(BUILDCONTROLDIR)) ohshite(_("failed to chdir to .../DEBIAN"));
  354. execlp(TAR, "tar", "-cf", "-", "--format=gnu", ".", NULL);
  355. ohshite(_("failed to exec tar -cf"));
  356. }
  357. close(p1[1]);
  358. /* Create a temporary file to store the control data in. Immediately unlink
  359. * our temporary file so others can't mess with it.
  360. */
  361. tfbuf = path_make_temp_template("dpkg-deb");
  362. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (control)"));
  363. /* make sure it's gone, the fd will remain until we close it */
  364. if (unlink(tfbuf)) ohshit(_("failed to unlink tmpfile (control), %s"),
  365. tfbuf);
  366. free(tfbuf);
  367. /* And run gzip to compress our control archive */
  368. c2 = subproc_fork();
  369. if (!c2) {
  370. m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]); close(gzfd);
  371. compress_filter(&compressor_gzip, 0, 1, 9, _("control"));
  372. }
  373. close(p1[0]);
  374. subproc_wait_check(c2, "gzip -9c", 0);
  375. subproc_wait_check(c1, "tar -cf", 0);
  376. if (lseek(gzfd, 0, SEEK_SET))
  377. ohshite(_("failed to rewind tmpfile (control)"));
  378. /* We have our first file for the ar-archive. Write a header for it to the
  379. * package and insert it.
  380. */
  381. if (oldformatflag) {
  382. if (fstat(gzfd, &controlstab))
  383. ohshite(_("failed to fstat tmpfile (control)"));
  384. if (fprintf(ar, "%-8s\n%ld\n", OLDARCHIVEVERSION, (long)controlstab.st_size) == EOF)
  385. werr(debar);
  386. fd_fd_copy(gzfd, fileno(ar), -1, _("control"));
  387. } else {
  388. const char deb_magic[] = ARCHIVEVERSION "\n";
  389. dpkg_ar_put_magic(debar, fileno(ar));
  390. dpkg_ar_member_put_mem(debar, fileno(ar), DEBMAGIC,
  391. deb_magic, strlen(deb_magic));
  392. dpkg_ar_member_put_file(debar, fileno(ar), ADMINMEMBER, gzfd);
  393. }
  394. /* Control is done, now we need to archive the data. Start by creating
  395. * a new temporary file. Immediately unlink the temporary file so others
  396. * can't mess with it. */
  397. if (!oldformatflag) {
  398. close(gzfd);
  399. tfbuf = path_make_temp_template("dpkg-deb");
  400. if ((gzfd= mkstemp(tfbuf)) == -1) ohshite(_("failed to make tmpfile (data)"));
  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. free(tfbuf);
  405. }
  406. /* Fork off a tar. We will feed it a list of filenames on stdin later.
  407. */
  408. m_pipe(p1);
  409. m_pipe(p2);
  410. c1 = subproc_fork();
  411. if (!c1) {
  412. m_dup2(p1[0],0); close(p1[0]); close(p1[1]);
  413. m_dup2(p2[1],1); close(p2[0]); close(p2[1]);
  414. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  415. execlp(TAR, "tar", "-cf", "-", "--format=gnu", "--null", "-T", "-", "--no-recursion", NULL);
  416. ohshite(_("failed to exec tar -cf"));
  417. }
  418. close(p1[0]);
  419. close(p2[1]);
  420. /* Of course we should not forget to compress the archive as well.. */
  421. c2 = subproc_fork();
  422. if (!c2) {
  423. close(p1[1]);
  424. m_dup2(p2[0],0); close(p2[0]);
  425. m_dup2(oldformatflag ? fileno(ar) : gzfd,1);
  426. compress_filter(compressor, 0, 1, compress_level, _("data"));
  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. c3 = subproc_fork();
  434. if (!c3) {
  435. m_dup2(p3[1],1); close(p3[0]); close(p3[1]);
  436. if (chdir(directory)) ohshite(_("failed to chdir to `%.255s'"),directory);
  437. execlp(FIND, "find", ".", "-path", "./" BUILDCONTROLDIR, "-prune", "-o",
  438. "-print0", NULL);
  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(&symlist, &symlist_end, fi);
  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. file_info_free(fi);
  452. }
  453. close(p3[0]);
  454. subproc_wait_check(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. subproc_wait_check(c2, _("<compress> from tar -cf"), 0);
  462. subproc_wait_check(c1, "tar -cf", 0);
  463. /* Okay, we have data.tar.gz as well now, add it to the ar wrapper */
  464. if (!oldformatflag) {
  465. char datamember[16 + 1];
  466. sprintf(datamember, "%s%s", DATAMEMBER, compressor->extension);
  467. if (lseek(gzfd,0,SEEK_SET)) ohshite(_("failed to rewind tmpfile (data)"));
  468. dpkg_ar_member_put_file(debar, fileno(ar), datamember, gzfd);
  469. }
  470. if (fflush(ar))
  471. ohshite(_("unable to flush file '%s'"), debar);
  472. if (fsync(fileno(ar)))
  473. ohshite(_("unable to sync file '%s'"), debar);
  474. if (fclose(ar)) werr(debar);
  475. exit(0);
  476. }