queue.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <inttypes.h>
  26. #include <string.h>
  27. #include <fcntl.h>
  28. #include <dirent.h>
  29. #include <unistd.h>
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/dpkg.h>
  35. #include <dpkg/dpkg-db.h>
  36. #include <dpkg/dir.h>
  37. #include <dpkg/buffer.h>
  38. #include <dpkg/options.h>
  39. #include "dpkg-split.h"
  40. /*
  41. * The queue, by default located in /var/lib/dpkg/parts/, is a plain
  42. * directory with one file per part.
  43. *
  44. * Each part is named “<md5sum>.<maxpartlen>.<thispartn>.<maxpartn>”,
  45. * with all numbers in hex.
  46. */
  47. static bool
  48. decompose_filename(const char *filename, struct partqueue *pq)
  49. {
  50. const char *p;
  51. char *q;
  52. if (strspn(filename, "0123456789abcdef") != MD5HASHLEN ||
  53. filename[MD5HASHLEN] != '.')
  54. return false;
  55. q = nfmalloc(MD5HASHLEN + 1);
  56. memcpy(q, filename, MD5HASHLEN);
  57. q[MD5HASHLEN] = '\0';
  58. pq->info.md5sum= q;
  59. p = filename + MD5HASHLEN + 1;
  60. pq->info.maxpartlen = strtoimax(p, &q, 16);
  61. if (q == p || *q++ != '.')
  62. return false;
  63. p = q;
  64. pq->info.thispartn = (int)strtol(p, &q, 16);
  65. if (q == p || *q++ != '.')
  66. return false;
  67. p = q;
  68. pq->info.maxpartn = (int)strtol(p, &q, 16);
  69. if (q == p || *q)
  70. return false;
  71. return true;
  72. }
  73. void scandepot(void) {
  74. DIR *depot;
  75. struct dirent *de;
  76. assert(!queue);
  77. depot = opendir(opt_depotdir);
  78. if (!depot)
  79. ohshite(_("unable to read depot directory `%.250s'"), opt_depotdir);
  80. while ((de= readdir(depot))) {
  81. struct partqueue *pq;
  82. char *p;
  83. if (de->d_name[0] == '.') continue;
  84. pq= nfmalloc(sizeof(struct partqueue));
  85. pq->info.fmtversion= pq->info.package= pq->info.version= NULL;
  86. pq->info.arch = NULL;
  87. pq->info.orglength= pq->info.thispartoffset= pq->info.thispartlen= 0;
  88. pq->info.headerlen= 0;
  89. p = nfmalloc(strlen(opt_depotdir) + 1 + strlen(de->d_name) + 1);
  90. sprintf(p, "%s/%s", opt_depotdir, de->d_name);
  91. pq->info.filename= p;
  92. if (!decompose_filename(de->d_name,pq)) {
  93. pq->info.md5sum= NULL;
  94. pq->info.maxpartlen= pq->info.thispartn= pq->info.maxpartn= 0;
  95. }
  96. pq->nextinqueue= queue;
  97. queue= pq;
  98. }
  99. closedir(depot);
  100. }
  101. static bool
  102. partmatches(struct partinfo *pi, struct partinfo *refi)
  103. {
  104. return (pi->md5sum &&
  105. !strcmp(pi->md5sum,refi->md5sum) &&
  106. pi->maxpartn == refi->maxpartn &&
  107. pi->maxpartlen == refi->maxpartlen);
  108. }
  109. int
  110. do_auto(const char *const *argv)
  111. {
  112. const char *partfile;
  113. struct partinfo *refi, **partlist, *otherthispart;
  114. struct partqueue *pq;
  115. unsigned int i;
  116. int j;
  117. FILE *part;
  118. if (!opt_outputfile)
  119. badusage(_("--auto requires the use of the --output option"));
  120. if (!(partfile= *argv++) || *argv)
  121. badusage(_("--auto requires exactly one part file argument"));
  122. refi= nfmalloc(sizeof(struct partqueue));
  123. part= fopen(partfile,"r");
  124. if (!part) ohshite(_("unable to read part file `%.250s'"),partfile);
  125. if (!read_info(part,partfile,refi)) {
  126. if (!opt_npquiet)
  127. printf(_("File `%.250s' is not part of a multipart archive.\n"),partfile);
  128. m_output(stdout, _("<standard output>"));
  129. return 1;
  130. }
  131. fclose(part);
  132. scandepot();
  133. partlist= nfmalloc(sizeof(struct partinfo*)*refi->maxpartn);
  134. for (i = 0; i < refi->maxpartn; i++)
  135. partlist[i] = NULL;
  136. for (pq= queue; pq; pq= pq->nextinqueue) {
  137. struct partinfo *npi, *pi = &pq->info;
  138. if (!partmatches(pi,refi)) continue;
  139. npi= nfmalloc(sizeof(struct partinfo));
  140. mustgetpartinfo(pi->filename,npi);
  141. addtopartlist(partlist,npi,refi);
  142. }
  143. /* If we already have a copy of this version we ignore it and prefer the
  144. * new one, but we still want to delete the one in the depot, so we
  145. * save its partinfo (with the filename) for later. This also prevents
  146. * us from accidentally deleting the source file. */
  147. otherthispart= partlist[refi->thispartn-1];
  148. partlist[refi->thispartn-1]= refi;
  149. for (j=refi->maxpartn-1; j>=0 && partlist[j]; j--);
  150. if (j>=0) {
  151. int fd_src, fd_dst;
  152. int ap;
  153. char *p, *q;
  154. m_asprintf(&p, "%s/t.%lx", opt_depotdir, (long)getpid());
  155. m_asprintf(&q, "%s/%s.%jx.%x.%x", opt_depotdir, refi->md5sum,
  156. (intmax_t)refi->maxpartlen, refi->thispartn, refi->maxpartn);
  157. fd_src = open(partfile, O_RDONLY);
  158. if (fd_src < 0)
  159. ohshite(_("unable to reopen part file `%.250s'"), partfile);
  160. fd_dst = creat(p, 0644);
  161. if (fd_dst < 0)
  162. ohshite(_("unable to open new depot file `%.250s'"), p);
  163. fd_fd_copy(fd_src, fd_dst, refi->filesize, _("extracting split part"));
  164. if (fsync(fd_dst))
  165. ohshite(_("unable to sync file '%s'"), p);
  166. if (close(fd_dst))
  167. ohshite(_("unable to close file '%s'"), p);
  168. close(fd_src);
  169. if (rename(p,q)) ohshite(_("unable to rename new depot file `%.250s' to `%.250s'"),p,q);
  170. free(q);
  171. free(p);
  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(opt_depotdir);
  179. } else {
  180. /* We have all the parts. */
  181. reassemble(partlist, opt_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. return 0;
  191. }
  192. int
  193. do_queue(const char *const *argv)
  194. {
  195. struct partqueue *pq;
  196. const char *head;
  197. struct stat stab;
  198. off_t bytes;
  199. if (*argv)
  200. badusage(_("--%s takes no arguments"), cipaction->olong);
  201. scandepot();
  202. head= N_("Junk files left around in the depot directory:\n");
  203. for (pq= queue; pq; pq= pq->nextinqueue) {
  204. if (pq->info.md5sum) continue;
  205. fputs(gettext(head),stdout); head= "";
  206. if (lstat(pq->info.filename,&stab))
  207. ohshit(_("unable to stat `%.250s'"),pq->info.filename);
  208. if (S_ISREG(stab.st_mode)) {
  209. bytes= stab.st_size;
  210. printf(_(" %s (%jd bytes)\n"), pq->info.filename, (intmax_t)bytes);
  211. } else {
  212. printf(_(" %s (not a plain file)\n"),pq->info.filename);
  213. }
  214. }
  215. if (!*head) putchar('\n');
  216. head= N_("Packages not yet reassembled:\n");
  217. for (pq= queue; pq; pq= pq->nextinqueue) {
  218. struct partinfo ti;
  219. unsigned int i;
  220. if (!pq->info.md5sum) continue;
  221. mustgetpartinfo(pq->info.filename,&ti);
  222. fputs(gettext(head),stdout); head= "";
  223. printf(_(" Package %s: part(s) "), ti.package);
  224. bytes= 0;
  225. for (i=0; i<ti.maxpartn; i++) {
  226. struct partqueue *qq;
  227. for (qq= pq;
  228. qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
  229. qq= qq->nextinqueue);
  230. if (qq) {
  231. printf("%d ",i+1);
  232. if (lstat(qq->info.filename,&stab))
  233. ohshite(_("unable to stat `%.250s'"),qq->info.filename);
  234. if (!S_ISREG(stab.st_mode))
  235. ohshit(_("part file `%.250s' is not a plain file"),qq->info.filename);
  236. bytes+= stab.st_size;
  237. /* Don't find this package again. */
  238. qq->info.md5sum = NULL;
  239. }
  240. }
  241. printf(_("(total %jd bytes)\n"), (intmax_t)bytes);
  242. }
  243. m_output(stdout, _("<standard output>"));
  244. return 0;
  245. }
  246. enum discardwhich { ds_junk, ds_package, ds_all };
  247. static void discardsome(enum discardwhich which, const char *package) {
  248. struct partqueue *pq;
  249. for (pq= queue; pq; pq= pq->nextinqueue) {
  250. switch (which) {
  251. case ds_junk:
  252. if (pq->info.md5sum) continue;
  253. break;
  254. case ds_package:
  255. if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
  256. break;
  257. case ds_all:
  258. break;
  259. default:
  260. internerr("unknown discardwhich '%d'", which);
  261. }
  262. if (unlink(pq->info.filename))
  263. ohshite(_("unable to discard `%.250s'"),pq->info.filename);
  264. printf(_("Deleted %s.\n"),pq->info.filename);
  265. }
  266. }
  267. int
  268. do_discard(const char *const *argv)
  269. {
  270. const char *thisarg;
  271. struct partqueue *pq;
  272. scandepot();
  273. if (*argv) {
  274. for (pq= queue; pq; pq= pq->nextinqueue)
  275. if (pq->info.md5sum)
  276. mustgetpartinfo(pq->info.filename,&pq->info);
  277. discardsome(ds_junk,NULL);
  278. while ((thisarg= *argv++)) discardsome(ds_package,thisarg);
  279. } else {
  280. discardsome(ds_all,NULL);
  281. }
  282. return 0;
  283. }