queue.c 9.1 KB

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