queue.c 9.1 KB

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