queue.c 9.4 KB

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