queue.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Queue, in /var/lib/dpkg/parts, is a plain directory with one
  23. * file per part.
  24. *
  25. * parts are named
  26. * <md5sum>.<maxpartlen>.<thispartn>.<maxpartn>
  27. * all numbers in hex
  28. */
  29. #include <config.h>
  30. #include <compat.h>
  31. #include <dpkg/i18n.h>
  32. #include <myopt.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <limits.h>
  36. #include <assert.h>
  37. #include <unistd.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. #include <dirent.h>
  41. #include <string.h>
  42. #include <dpkg.h>
  43. #include <dpkg-db.h>
  44. #include "dpkg-split.h"
  45. static int decompose_filename(const char *filename, struct partqueue *pq) {
  46. const char *p;
  47. char *q;
  48. if (strspn(filename,"0123456789abcdef") != 32 || filename[32] != '.') return 0;
  49. q= nfmalloc(33);
  50. memcpy(q, filename, 32);
  51. q[32] = '\0';
  52. pq->info.md5sum= q;
  53. p= filename+33;
  54. pq->info.maxpartlen= strtol(p,&q,16); if (q==p || *q++ != '.') return 0;
  55. p=q; pq->info.thispartn= (int)strtol(p,&q,16); if (q==p || *q++ != '.') return 0;
  56. p=q; pq->info.maxpartn= (int)strtol(p,&q,16); if (q==p || *q) return 0;
  57. return 1;
  58. }
  59. void scandepot(void) {
  60. DIR *depot;
  61. struct dirent *de;
  62. struct partqueue *pq;
  63. char *p;
  64. assert(!queue);
  65. depot= opendir(depotdir);
  66. if (!depot) ohshite(_("unable to read depot directory `%.250s'"),depotdir);
  67. while ((de= readdir(depot))) {
  68. if (de->d_name[0] == '.') continue;
  69. pq= nfmalloc(sizeof(struct partqueue));
  70. pq->info.fmtversion= pq->info.package= pq->info.version= NULL;
  71. pq->info.orglength= pq->info.thispartoffset= pq->info.thispartlen= 0;
  72. pq->info.headerlen= 0;
  73. p= nfmalloc(strlen(depotdir)+strlen(de->d_name)+1);
  74. strcpy(p,depotdir);
  75. strcat(p,de->d_name);
  76. pq->info.filename= p;
  77. if (!decompose_filename(de->d_name,pq)) {
  78. pq->info.md5sum= NULL;
  79. pq->info.maxpartlen= pq->info.thispartn= pq->info.maxpartn= 0;
  80. }
  81. pq->nextinqueue= queue;
  82. queue= pq;
  83. }
  84. }
  85. static int partmatches(struct partinfo *pi, struct partinfo *refi) {
  86. return (pi->md5sum &&
  87. !strcmp(pi->md5sum,refi->md5sum) &&
  88. pi->maxpartn == refi->maxpartn &&
  89. pi->maxpartlen == refi->maxpartlen);
  90. }
  91. void do_auto(const char *const *argv) {
  92. const char *partfile;
  93. struct partinfo *pi, *refi, *npi, **partlist, *otherthispart;
  94. struct partqueue *pq;
  95. unsigned int i;
  96. int j, ap;
  97. long nr;
  98. FILE *part;
  99. void *buffer;
  100. char *p, *q;
  101. if (!outputfile) badusage(_("--auto requires the use of the --output option"));
  102. if (!(partfile= *argv++) || *argv)
  103. badusage(_("--auto requires exactly one part file argument"));
  104. refi= nfmalloc(sizeof(struct partqueue));
  105. part= fopen(partfile,"r");
  106. if (!part) ohshite(_("unable to read part file `%.250s'"),partfile);
  107. if (!read_info(part,partfile,refi)) {
  108. if (!npquiet)
  109. printf(_("File `%.250s' is not part of a multipart archive.\n"),partfile);
  110. if (fclose(stdout)) werr("stdout");
  111. exit(1);
  112. }
  113. fclose(part);
  114. scandepot();
  115. partlist= nfmalloc(sizeof(struct partinfo*)*refi->maxpartn);
  116. for (i = 0; i < refi->maxpartn; i++)
  117. partlist[i] = NULL;
  118. for (pq= queue; pq; pq= pq->nextinqueue) {
  119. pi= &pq->info;
  120. if (!partmatches(pi,refi)) continue;
  121. npi= nfmalloc(sizeof(struct partinfo));
  122. mustgetpartinfo(pi->filename,npi);
  123. addtopartlist(partlist,npi,refi);
  124. }
  125. /* If we already have a copy of this version we ignore it and prefer the
  126. * new one, but we still want to delete the one in the depot, so we
  127. * save its partinfo (with the filename) for later. This also prevents
  128. * us from accidentally deleting the source file.
  129. */
  130. otherthispart= partlist[refi->thispartn-1];
  131. partlist[refi->thispartn-1]= refi;
  132. for (j=refi->maxpartn-1; j>=0 && partlist[j]; j--);
  133. if (j>=0) {
  134. part= fopen(partfile,"r");
  135. if (!part) ohshite(_("unable to reopen part file `%.250s'"),partfile);
  136. buffer= nfmalloc(refi->filesize);
  137. nr= fread(buffer,1,refi->filesize,part);
  138. if (nr != refi->filesize) rerreof(part,partfile);
  139. if (getc(part) != EOF) ohshit(_("part file `%.250s' has trailing garbage"),partfile);
  140. if (ferror(part)) rerr(partfile);
  141. fclose(part);
  142. p= nfmalloc(strlen(depotdir)+50);
  143. q= nfmalloc(strlen(depotdir)+200);
  144. sprintf(p,"%st.%lx",depotdir,(long)getpid());
  145. sprintf(q,"%s%s.%lx.%x.%x",depotdir,refi->md5sum,
  146. refi->maxpartlen,refi->thispartn,refi->maxpartn);
  147. part= fopen(p,"w");
  148. if (!part) ohshite(_("unable to open new depot file `%.250s'"),p);
  149. nr= fwrite(buffer,1,refi->filesize,part);
  150. if (nr != refi->filesize) werr(p);
  151. if (fclose(part)) werr(p);
  152. if (rename(p,q)) ohshite(_("unable to rename new depot file `%.250s' to `%.250s'"),p,q);
  153. printf(_("Part %d of package %s filed (still want "),refi->thispartn,refi->package);
  154. /* There are still some parts missing. */
  155. for (i=0, ap=0; i<refi->maxpartn; i++)
  156. if (!partlist[i])
  157. printf("%s%d", !ap++ ? "" : i == (unsigned int)j ? _(" and ") : ", ", i + 1);
  158. printf(").\n");
  159. } else {
  160. /* We have all the parts. */
  161. reassemble(partlist,outputfile);
  162. /* OK, delete all the parts (except the new one, which we never copied). */
  163. partlist[refi->thispartn-1]= otherthispart;
  164. for (i=0; i<refi->maxpartn; i++)
  165. if (partlist[i])
  166. if (unlink(partlist[i]->filename))
  167. ohshite(_("unable to delete used-up depot file `%.250s'"),partlist[i]->filename);
  168. }
  169. if (ferror(stderr)) werr("stderr");
  170. }
  171. void do_queue(const char *const *argv) {
  172. struct partqueue *pq, *qq;
  173. struct partinfo ti;
  174. const char *head;
  175. struct stat stab;
  176. unsigned long bytes;
  177. unsigned int i;
  178. if (*argv)
  179. badusage(_("--%s takes no arguments"), cipaction->olong);
  180. scandepot();
  181. head= N_("Junk files left around in the depot directory:\n");
  182. for (pq= queue; pq; pq= pq->nextinqueue) {
  183. if (pq->info.md5sum) continue;
  184. fputs(gettext(head),stdout); head= "";
  185. if (lstat(pq->info.filename,&stab))
  186. ohshit(_("unable to stat `%.250s'"),pq->info.filename);
  187. if (S_ISREG(stab.st_mode)) {
  188. bytes= stab.st_size;
  189. printf(_(" %s (%lu bytes)\n"),pq->info.filename,bytes);
  190. } else {
  191. printf(_(" %s (not a plain file)\n"),pq->info.filename);
  192. }
  193. }
  194. if (!*head) putchar('\n');
  195. head= N_("Packages not yet reassembled:\n");
  196. for (pq= queue; pq; pq= pq->nextinqueue) {
  197. if (!pq->info.md5sum) continue;
  198. mustgetpartinfo(pq->info.filename,&ti);
  199. fputs(gettext(head),stdout); head= "";
  200. printf(_(" Package %s: part(s) "), ti.package);
  201. bytes= 0;
  202. for (i=0; i<ti.maxpartn; i++) {
  203. for (qq= pq;
  204. qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
  205. qq= qq->nextinqueue);
  206. if (qq) {
  207. printf("%d ",i+1);
  208. if (lstat(qq->info.filename,&stab))
  209. ohshite(_("unable to stat `%.250s'"),qq->info.filename);
  210. if (!S_ISREG(stab.st_mode))
  211. ohshit(_("part file `%.250s' is not a plain file"),qq->info.filename);
  212. bytes+= stab.st_size;
  213. qq->info.md5sum= NULL; /* don't find this package again */
  214. }
  215. }
  216. printf(_("(total %lu bytes)\n"),bytes);
  217. }
  218. if (fclose(stdout)) werr("stdout");
  219. }
  220. enum discardwhich { ds_junk, ds_package, ds_all };
  221. static void discardsome(enum discardwhich which, const char *package) {
  222. struct partqueue *pq;
  223. for (pq= queue; pq; pq= pq->nextinqueue) {
  224. switch (which) {
  225. case ds_junk:
  226. if (pq->info.md5sum) continue;
  227. break;
  228. case ds_package:
  229. if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
  230. break;
  231. case ds_all:
  232. break;
  233. default:
  234. internerr("unknown discardwhich '%d'", which);
  235. }
  236. if (unlink(pq->info.filename))
  237. ohshite(_("unable to discard `%.250s'"),pq->info.filename);
  238. printf(_("Deleted %s.\n"),pq->info.filename);
  239. }
  240. }
  241. void do_discard(const char *const *argv) {
  242. const char *thisarg;
  243. struct partqueue *pq;
  244. scandepot();
  245. if (*argv) {
  246. for (pq= queue; pq; pq= pq->nextinqueue)
  247. if (pq->info.md5sum)
  248. mustgetpartinfo(pq->info.filename,&pq->info);
  249. discardsome(ds_junk,NULL);
  250. while ((thisarg= *argv++)) discardsome(ds_package,thisarg);
  251. } else {
  252. discardsome(ds_all,NULL);
  253. }
  254. }