queue.c 8.6 KB

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