queue.c 9.3 KB

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