queue.c 9.3 KB

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