queue.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * dpkg-split - splitting and joining of multipart *.deb archives
  3. * queue.c - queue management
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2014 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 <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/stat.h>
  24. #include <errno.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. errno = 0;
  62. pq->info.maxpartlen = strtoimax(p, &q, 16);
  63. if (q == p || *q++ != '.' || errno != 0)
  64. return false;
  65. p = q;
  66. pq->info.thispartn = (int)strtol(p, &q, 16);
  67. if (q == p || *q++ != '.' || errno != 0)
  68. return false;
  69. p = q;
  70. pq->info.maxpartn = (int)strtol(p, &q, 16);
  71. if (q == p || *q || errno != 0)
  72. return false;
  73. return true;
  74. }
  75. static struct partqueue *
  76. scandepot(void)
  77. {
  78. DIR *depot;
  79. struct dirent *de;
  80. struct partqueue *queue = NULL;
  81. depot = opendir(opt_depotdir);
  82. if (!depot)
  83. ohshite(_("unable to read depot directory '%.250s'"), opt_depotdir);
  84. while ((de= readdir(depot))) {
  85. struct partqueue *pq;
  86. char *p;
  87. if (de->d_name[0] == '.') continue;
  88. pq= nfmalloc(sizeof(struct partqueue));
  89. pq->info.fmtversion.major = 0;
  90. pq->info.fmtversion.minor = 0;
  91. pq->info.package = NULL;
  92. pq->info.version = NULL;
  93. pq->info.arch = NULL;
  94. pq->info.orglength= pq->info.thispartoffset= pq->info.thispartlen= 0;
  95. pq->info.headerlen= 0;
  96. p = nfmalloc(strlen(opt_depotdir) + 1 + strlen(de->d_name) + 1);
  97. sprintf(p, "%s/%s", opt_depotdir, de->d_name);
  98. pq->info.filename= p;
  99. if (!decompose_filename(de->d_name,pq)) {
  100. pq->info.md5sum= NULL;
  101. pq->info.maxpartlen= pq->info.thispartn= pq->info.maxpartn= 0;
  102. }
  103. pq->nextinqueue= queue;
  104. queue= pq;
  105. }
  106. closedir(depot);
  107. return queue;
  108. }
  109. static bool
  110. partmatches(struct partinfo *pi, struct partinfo *refi)
  111. {
  112. return (pi->md5sum &&
  113. strcmp(pi->md5sum, refi->md5sum) == 0 &&
  114. pi->maxpartn == refi->maxpartn &&
  115. pi->maxpartlen == refi->maxpartlen);
  116. }
  117. int
  118. do_auto(const char *const *argv)
  119. {
  120. const char *partfile;
  121. struct partinfo *refi, **partlist, *otherthispart;
  122. struct partqueue *queue;
  123. struct partqueue *pq;
  124. struct dpkg_ar *part;
  125. unsigned int i;
  126. int j;
  127. if (!opt_outputfile)
  128. badusage(_("--auto requires the use of the --output option"));
  129. partfile = *argv++;
  130. if (partfile == NULL || *argv)
  131. badusage(_("--auto requires exactly one part file argument"));
  132. refi= nfmalloc(sizeof(struct partqueue));
  133. part = dpkg_ar_open(partfile);
  134. if (!part)
  135. ohshite(_("unable to read part file '%.250s'"), partfile);
  136. if (!read_info(part, refi)) {
  137. if (!opt_npquiet)
  138. printf(_("File '%.250s' is not part of a multipart archive.\n"), partfile);
  139. m_output(stdout, _("<standard output>"));
  140. return 1;
  141. }
  142. dpkg_ar_close(part);
  143. queue = scandepot();
  144. partlist= nfmalloc(sizeof(struct partinfo*)*refi->maxpartn);
  145. for (i = 0; i < refi->maxpartn; i++)
  146. partlist[i] = NULL;
  147. for (pq= queue; pq; pq= pq->nextinqueue) {
  148. struct partinfo *npi, *pi = &pq->info;
  149. if (!partmatches(pi,refi)) continue;
  150. npi= nfmalloc(sizeof(struct partinfo));
  151. mustgetpartinfo(pi->filename,npi);
  152. addtopartlist(partlist,npi,refi);
  153. }
  154. /* If we already have a copy of this version we ignore it and prefer the
  155. * new one, but we still want to delete the one in the depot, so we
  156. * save its partinfo (with the filename) for later. This also prevents
  157. * us from accidentally deleting the source file. */
  158. otherthispart= partlist[refi->thispartn-1];
  159. partlist[refi->thispartn-1]= refi;
  160. for (j=refi->maxpartn-1; j>=0 && partlist[j]; j--);
  161. if (j>=0) {
  162. struct dpkg_error err;
  163. int fd_src, fd_dst;
  164. int ap;
  165. char *p, *q;
  166. p = str_fmt("%s/t.%lx", opt_depotdir, (long)getpid());
  167. q = str_fmt("%s/%s.%jx.%x.%x", opt_depotdir, refi->md5sum,
  168. (intmax_t)refi->maxpartlen, refi->thispartn, refi->maxpartn);
  169. fd_src = open(partfile, O_RDONLY);
  170. if (fd_src < 0)
  171. ohshite(_("unable to reopen part file '%.250s'"), partfile);
  172. fd_dst = creat(p, 0644);
  173. if (fd_dst < 0)
  174. ohshite(_("unable to open new depot file '%.250s'"), p);
  175. if (fd_fd_copy(fd_src, fd_dst, refi->filesize, &err) < 0)
  176. ohshit(_("cannot extract split package part '%s': %s"),
  177. partfile, err.str);
  178. if (fsync(fd_dst))
  179. ohshite(_("unable to sync file '%s'"), p);
  180. if (close(fd_dst))
  181. ohshite(_("unable to close file '%s'"), p);
  182. close(fd_src);
  183. if (rename(p, q))
  184. ohshite(_("unable to rename new depot file '%.250s' to '%.250s'"), p, q);
  185. free(q);
  186. free(p);
  187. printf(_("Part %d of package %s filed (still want "),refi->thispartn,refi->package);
  188. /* There are still some parts missing. */
  189. for (i=0, ap=0; i<refi->maxpartn; i++)
  190. if (!partlist[i])
  191. printf("%s%d", !ap++ ? "" : i == (unsigned int)j ? _(" and ") : ", ", i + 1);
  192. printf(").\n");
  193. dir_sync_path(opt_depotdir);
  194. } else {
  195. /* We have all the parts. */
  196. reassemble(partlist, opt_outputfile);
  197. /* OK, delete all the parts (except the new one, which we never copied). */
  198. partlist[refi->thispartn-1]= otherthispart;
  199. for (i=0; i<refi->maxpartn; i++)
  200. if (partlist[i])
  201. if (unlink(partlist[i]->filename))
  202. ohshite(_("unable to delete used-up depot file '%.250s'"),
  203. partlist[i]->filename);
  204. }
  205. m_output(stderr, _("<standard error>"));
  206. return 0;
  207. }
  208. int
  209. do_queue(const char *const *argv)
  210. {
  211. struct partqueue *queue;
  212. struct partqueue *pq;
  213. const char *head;
  214. struct stat stab;
  215. off_t bytes;
  216. if (*argv)
  217. badusage(_("--%s takes no arguments"), cipaction->olong);
  218. queue = scandepot();
  219. head= N_("Junk files left around in the depot directory:\n");
  220. for (pq= queue; pq; pq= pq->nextinqueue) {
  221. if (pq->info.md5sum) continue;
  222. fputs(gettext(head),stdout); head= "";
  223. if (lstat(pq->info.filename,&stab))
  224. ohshit(_("unable to stat '%.250s'"), pq->info.filename);
  225. if (S_ISREG(stab.st_mode)) {
  226. bytes= stab.st_size;
  227. printf(_(" %s (%jd bytes)\n"), pq->info.filename, (intmax_t)bytes);
  228. } else {
  229. printf(_(" %s (not a plain file)\n"),pq->info.filename);
  230. }
  231. }
  232. if (!*head) putchar('\n');
  233. head= N_("Packages not yet reassembled:\n");
  234. for (pq= queue; pq; pq= pq->nextinqueue) {
  235. struct partinfo ti;
  236. unsigned int i;
  237. if (!pq->info.md5sum) continue;
  238. mustgetpartinfo(pq->info.filename,&ti);
  239. fputs(gettext(head),stdout); head= "";
  240. printf(_(" Package %s: part(s) "), ti.package);
  241. bytes= 0;
  242. for (i=0; i<ti.maxpartn; i++) {
  243. struct partqueue *qq;
  244. for (qq= pq;
  245. qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
  246. qq= qq->nextinqueue);
  247. if (qq) {
  248. printf("%d ",i+1);
  249. if (lstat(qq->info.filename,&stab))
  250. ohshite(_("unable to stat '%.250s'"), qq->info.filename);
  251. if (!S_ISREG(stab.st_mode))
  252. ohshit(_("part file '%.250s' is not a plain file"), qq->info.filename);
  253. bytes+= stab.st_size;
  254. /* Don't find this package again. */
  255. qq->info.md5sum = NULL;
  256. }
  257. }
  258. printf(_("(total %jd bytes)\n"), (intmax_t)bytes);
  259. }
  260. m_output(stdout, _("<standard output>"));
  261. return 0;
  262. }
  263. enum discard_which {
  264. DISCARD_PART_JUNK,
  265. DISCARD_PART_PACKAGE,
  266. DISCARD_PART_ALL,
  267. };
  268. static void
  269. discard_parts(struct partqueue *queue, enum discard_which which,
  270. const char *package)
  271. {
  272. struct partqueue *pq;
  273. for (pq= queue; pq; pq= pq->nextinqueue) {
  274. switch (which) {
  275. case DISCARD_PART_JUNK:
  276. if (pq->info.md5sum) continue;
  277. break;
  278. case DISCARD_PART_PACKAGE:
  279. if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
  280. break;
  281. case DISCARD_PART_ALL:
  282. break;
  283. default:
  284. internerr("unknown discard_which '%d'", which);
  285. }
  286. if (unlink(pq->info.filename))
  287. ohshite(_("unable to discard '%.250s'"), pq->info.filename);
  288. printf(_("Deleted %s.\n"),pq->info.filename);
  289. }
  290. }
  291. int
  292. do_discard(const char *const *argv)
  293. {
  294. const char *thisarg;
  295. struct partqueue *queue;
  296. struct partqueue *pq;
  297. queue = scandepot();
  298. if (*argv) {
  299. for (pq= queue; pq; pq= pq->nextinqueue)
  300. if (pq->info.md5sum)
  301. mustgetpartinfo(pq->info.filename,&pq->info);
  302. discard_parts(queue, DISCARD_PART_JUNK, NULL);
  303. while ((thisarg = *argv++))
  304. discard_parts(queue, DISCARD_PART_PACKAGE, thisarg);
  305. } else {
  306. discard_parts(queue, DISCARD_PART_ALL, NULL);
  307. }
  308. return 0;
  309. }