queue.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * queue.c - queue management
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/stat.h>
  23. #include <assert.h>
  24. #include <limits.h>
  25. #include <inttypes.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <dirent.h>
  29. #include <unistd.h>
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/dpkg.h>
  35. #include <dpkg/dpkg-db.h>
  36. #include <dpkg/dir.h>
  37. #include <dpkg/buffer.h>
  38. #include <dpkg/myopt.h>
  39. #include "dpkg-split.h"
  40. /*
  41. * The queue, by default located in /var/lib/dpkg/parts/, is a plain
  42. * directory with one file per part.
  43. *
  44. * Each part is named “<md5sum>.<maxpartlen>.<thispartn>.<maxpartn>”,
  45. * with all numbers in hex.
  46. */
  47. static bool
  48. decompose_filename(const char *filename, struct partqueue *pq)
  49. {
  50. const char *p;
  51. char *q;
  52. if (strspn(filename, "0123456789abcdef") != MD5HASHLEN ||
  53. filename[MD5HASHLEN] != '.')
  54. return false;
  55. q = nfmalloc(MD5HASHLEN + 1);
  56. memcpy(q, filename, MD5HASHLEN);
  57. q[MD5HASHLEN] = '\0';
  58. pq->info.md5sum= q;
  59. p = filename + MD5HASHLEN + 1;
  60. pq->info.maxpartlen = strtoimax(p, &q, 16);
  61. if (q == p || *q++ != '.')
  62. return false;
  63. p = q;
  64. pq->info.thispartn = (int)strtol(p, &q, 16);
  65. if (q == p || *q++ != '.')
  66. return false;
  67. p = q;
  68. pq->info.maxpartn = (int)strtol(p, &q, 16);
  69. if (q == p || *q)
  70. return false;
  71. return true;
  72. }
  73. void scandepot(void) {
  74. DIR *depot;
  75. struct dirent *de;
  76. assert(!queue);
  77. depot = opendir(opt_depotdir);
  78. if (!depot)
  79. ohshite(_("unable to read depot directory `%.250s'"), opt_depotdir);
  80. while ((de= readdir(depot))) {
  81. struct partqueue *pq;
  82. char *p;
  83. if (de->d_name[0] == '.') continue;
  84. pq= nfmalloc(sizeof(struct partqueue));
  85. pq->info.fmtversion= pq->info.package= pq->info.version= NULL;
  86. pq->info.orglength= pq->info.thispartoffset= pq->info.thispartlen= 0;
  87. pq->info.headerlen= 0;
  88. p = nfmalloc(strlen(opt_depotdir) + strlen(de->d_name) + 1);
  89. strcpy(p, opt_depotdir);
  90. strcat(p,de->d_name);
  91. pq->info.filename= p;
  92. if (!decompose_filename(de->d_name,pq)) {
  93. pq->info.md5sum= NULL;
  94. pq->info.maxpartlen= pq->info.thispartn= pq->info.maxpartn= 0;
  95. }
  96. pq->nextinqueue= queue;
  97. queue= pq;
  98. }
  99. closedir(depot);
  100. }
  101. static bool
  102. partmatches(struct partinfo *pi, struct partinfo *refi)
  103. {
  104. return (pi->md5sum &&
  105. !strcmp(pi->md5sum,refi->md5sum) &&
  106. pi->maxpartn == refi->maxpartn &&
  107. pi->maxpartlen == refi->maxpartlen);
  108. }
  109. void do_auto(const char *const *argv) {
  110. const char *partfile;
  111. struct partinfo *refi, **partlist, *otherthispart;
  112. struct partqueue *pq;
  113. unsigned int i;
  114. int j;
  115. FILE *part;
  116. if (!opt_outputfile)
  117. badusage(_("--auto requires the use of the --output option"));
  118. if (!(partfile= *argv++) || *argv)
  119. badusage(_("--auto requires exactly one part file argument"));
  120. refi= nfmalloc(sizeof(struct partqueue));
  121. part= fopen(partfile,"r");
  122. if (!part) ohshite(_("unable to read part file `%.250s'"),partfile);
  123. if (!read_info(part,partfile,refi)) {
  124. if (!opt_npquiet)
  125. printf(_("File `%.250s' is not part of a multipart archive.\n"),partfile);
  126. m_output(stdout, _("<standard output>"));
  127. exit(1);
  128. }
  129. fclose(part);
  130. scandepot();
  131. partlist= nfmalloc(sizeof(struct partinfo*)*refi->maxpartn);
  132. for (i = 0; i < refi->maxpartn; i++)
  133. partlist[i] = NULL;
  134. for (pq= queue; pq; pq= pq->nextinqueue) {
  135. struct partinfo *npi, *pi = &pq->info;
  136. if (!partmatches(pi,refi)) continue;
  137. npi= nfmalloc(sizeof(struct partinfo));
  138. mustgetpartinfo(pi->filename,npi);
  139. addtopartlist(partlist,npi,refi);
  140. }
  141. /* If we already have a copy of this version we ignore it and prefer the
  142. * new one, but we still want to delete the one in the depot, so we
  143. * save its partinfo (with the filename) for later. This also prevents
  144. * us from accidentally deleting the source file. */
  145. otherthispart= partlist[refi->thispartn-1];
  146. partlist[refi->thispartn-1]= refi;
  147. for (j=refi->maxpartn-1; j>=0 && partlist[j]; j--);
  148. if (j>=0) {
  149. int fd_src, fd_dst;
  150. int ap;
  151. char *p, *q;
  152. m_asprintf(&p, "%st.%lx", opt_depotdir, (long)getpid());
  153. m_asprintf(&q, "%s%s.%jx.%x.%x", opt_depotdir, refi->md5sum,
  154. (intmax_t)refi->maxpartlen, refi->thispartn, refi->maxpartn);
  155. fd_src = open(partfile, O_RDONLY);
  156. if (fd_src < 0)
  157. ohshite(_("unable to reopen part file `%.250s'"), partfile);
  158. fd_dst = creat(p, 0644);
  159. if (fd_dst)
  160. ohshite(_("unable to open new depot file `%.250s'"), p);
  161. fd_fd_copy(fd_src, fd_dst, refi->filesize, _("extracting split part"));
  162. if (fsync(fd_dst))
  163. ohshite(_("unable to sync file '%s'"), p);
  164. if (close(fd_dst))
  165. ohshite(_("unable to close file '%s'"), p);
  166. close(fd_src);
  167. if (rename(p,q)) ohshite(_("unable to rename new depot file `%.250s' to `%.250s'"),p,q);
  168. free(q);
  169. free(p);
  170. printf(_("Part %d of package %s filed (still want "),refi->thispartn,refi->package);
  171. /* There are still some parts missing. */
  172. for (i=0, ap=0; i<refi->maxpartn; i++)
  173. if (!partlist[i])
  174. printf("%s%d", !ap++ ? "" : i == (unsigned int)j ? _(" and ") : ", ", i + 1);
  175. printf(").\n");
  176. dir_sync_path(opt_depotdir);
  177. } else {
  178. /* We have all the parts. */
  179. reassemble(partlist, opt_outputfile);
  180. /* OK, delete all the parts (except the new one, which we never copied). */
  181. partlist[refi->thispartn-1]= otherthispart;
  182. for (i=0; i<refi->maxpartn; i++)
  183. if (partlist[i])
  184. if (unlink(partlist[i]->filename))
  185. ohshite(_("unable to delete used-up depot file `%.250s'"),partlist[i]->filename);
  186. }
  187. m_output(stderr, _("<standard error>"));
  188. }
  189. void do_queue(const char *const *argv) {
  190. struct partqueue *pq;
  191. const char *head;
  192. struct stat stab;
  193. off_t bytes;
  194. if (*argv)
  195. badusage(_("--%s takes no arguments"), cipaction->olong);
  196. scandepot();
  197. head= N_("Junk files left around in the depot directory:\n");
  198. for (pq= queue; pq; pq= pq->nextinqueue) {
  199. if (pq->info.md5sum) continue;
  200. fputs(gettext(head),stdout); head= "";
  201. if (lstat(pq->info.filename,&stab))
  202. ohshit(_("unable to stat `%.250s'"),pq->info.filename);
  203. if (S_ISREG(stab.st_mode)) {
  204. bytes= stab.st_size;
  205. printf(_(" %s (%jd bytes)\n"), pq->info.filename, (intmax_t)bytes);
  206. } else {
  207. printf(_(" %s (not a plain file)\n"),pq->info.filename);
  208. }
  209. }
  210. if (!*head) putchar('\n');
  211. head= N_("Packages not yet reassembled:\n");
  212. for (pq= queue; pq; pq= pq->nextinqueue) {
  213. struct partinfo ti;
  214. unsigned int i;
  215. if (!pq->info.md5sum) continue;
  216. mustgetpartinfo(pq->info.filename,&ti);
  217. fputs(gettext(head),stdout); head= "";
  218. printf(_(" Package %s: part(s) "), ti.package);
  219. bytes= 0;
  220. for (i=0; i<ti.maxpartn; i++) {
  221. struct partqueue *qq;
  222. for (qq= pq;
  223. qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
  224. qq= qq->nextinqueue);
  225. if (qq) {
  226. printf("%d ",i+1);
  227. if (lstat(qq->info.filename,&stab))
  228. ohshite(_("unable to stat `%.250s'"),qq->info.filename);
  229. if (!S_ISREG(stab.st_mode))
  230. ohshit(_("part file `%.250s' is not a plain file"),qq->info.filename);
  231. bytes+= stab.st_size;
  232. /* Don't find this package again. */
  233. qq->info.md5sum = NULL;
  234. }
  235. }
  236. printf(_("(total %jd bytes)\n"), (intmax_t)bytes);
  237. }
  238. m_output(stdout, _("<standard output>"));
  239. }
  240. enum discardwhich { ds_junk, ds_package, ds_all };
  241. static void discardsome(enum discardwhich which, const char *package) {
  242. struct partqueue *pq;
  243. for (pq= queue; pq; pq= pq->nextinqueue) {
  244. switch (which) {
  245. case ds_junk:
  246. if (pq->info.md5sum) continue;
  247. break;
  248. case ds_package:
  249. if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
  250. break;
  251. case ds_all:
  252. break;
  253. default:
  254. internerr("unknown discardwhich '%d'", which);
  255. }
  256. if (unlink(pq->info.filename))
  257. ohshite(_("unable to discard `%.250s'"),pq->info.filename);
  258. printf(_("Deleted %s.\n"),pq->info.filename);
  259. }
  260. }
  261. void do_discard(const char *const *argv) {
  262. const char *thisarg;
  263. struct partqueue *pq;
  264. scandepot();
  265. if (*argv) {
  266. for (pq= queue; pq; pq= pq->nextinqueue)
  267. if (pq->info.md5sum)
  268. mustgetpartinfo(pq->info.filename,&pq->info);
  269. discardsome(ds_junk,NULL);
  270. while ((thisarg= *argv++)) discardsome(ds_package,thisarg);
  271. } else {
  272. discardsome(ds_all,NULL);
  273. }
  274. }