queue.c 9.8 KB

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