queue.c 8.6 KB

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