queue.c 8.7 KB

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