queue.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <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) ohshite(_("unable to read part file `%.250s'"),partfile);
  136. if (!read_info(part,partfile,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. fclose(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. m_asprintf(&p, "%s/t.%lx", opt_depotdir, (long)getpid());
  167. m_asprintf(&q, "%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)) ohshite(_("unable to rename new depot file `%.250s' to `%.250s'"),p,q);
  184. free(q);
  185. free(p);
  186. printf(_("Part %d of package %s filed (still want "),refi->thispartn,refi->package);
  187. /* There are still some parts missing. */
  188. for (i=0, ap=0; i<refi->maxpartn; i++)
  189. if (!partlist[i])
  190. printf("%s%d", !ap++ ? "" : i == (unsigned int)j ? _(" and ") : ", ", i + 1);
  191. printf(").\n");
  192. dir_sync_path(opt_depotdir);
  193. } else {
  194. /* We have all the parts. */
  195. reassemble(partlist, opt_outputfile);
  196. /* OK, delete all the parts (except the new one, which we never copied). */
  197. partlist[refi->thispartn-1]= otherthispart;
  198. for (i=0; i<refi->maxpartn; i++)
  199. if (partlist[i])
  200. if (unlink(partlist[i]->filename))
  201. ohshite(_("unable to delete used-up depot file `%.250s'"),partlist[i]->filename);
  202. }
  203. m_output(stderr, _("<standard error>"));
  204. return 0;
  205. }
  206. int
  207. do_queue(const char *const *argv)
  208. {
  209. struct partqueue *queue;
  210. struct partqueue *pq;
  211. const char *head;
  212. struct stat stab;
  213. off_t bytes;
  214. if (*argv)
  215. badusage(_("--%s takes no arguments"), cipaction->olong);
  216. queue = scandepot();
  217. head= N_("Junk files left around in the depot directory:\n");
  218. for (pq= queue; pq; pq= pq->nextinqueue) {
  219. if (pq->info.md5sum) continue;
  220. fputs(gettext(head),stdout); head= "";
  221. if (lstat(pq->info.filename,&stab))
  222. ohshit(_("unable to stat `%.250s'"),pq->info.filename);
  223. if (S_ISREG(stab.st_mode)) {
  224. bytes= stab.st_size;
  225. printf(_(" %s (%jd bytes)\n"), pq->info.filename, (intmax_t)bytes);
  226. } else {
  227. printf(_(" %s (not a plain file)\n"),pq->info.filename);
  228. }
  229. }
  230. if (!*head) putchar('\n');
  231. head= N_("Packages not yet reassembled:\n");
  232. for (pq= queue; pq; pq= pq->nextinqueue) {
  233. struct partinfo ti;
  234. unsigned int i;
  235. if (!pq->info.md5sum) continue;
  236. mustgetpartinfo(pq->info.filename,&ti);
  237. fputs(gettext(head),stdout); head= "";
  238. printf(_(" Package %s: part(s) "), ti.package);
  239. bytes= 0;
  240. for (i=0; i<ti.maxpartn; i++) {
  241. struct partqueue *qq;
  242. for (qq= pq;
  243. qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
  244. qq= qq->nextinqueue);
  245. if (qq) {
  246. printf("%d ",i+1);
  247. if (lstat(qq->info.filename,&stab))
  248. ohshite(_("unable to stat `%.250s'"),qq->info.filename);
  249. if (!S_ISREG(stab.st_mode))
  250. ohshit(_("part file `%.250s' is not a plain file"),qq->info.filename);
  251. bytes+= stab.st_size;
  252. /* Don't find this package again. */
  253. qq->info.md5sum = NULL;
  254. }
  255. }
  256. printf(_("(total %jd bytes)\n"), (intmax_t)bytes);
  257. }
  258. m_output(stdout, _("<standard output>"));
  259. return 0;
  260. }
  261. enum discard_which {
  262. ds_junk,
  263. ds_package,
  264. ds_all,
  265. };
  266. static void
  267. discard_parts(struct partqueue *queue, enum discard_which which,
  268. const char *package)
  269. {
  270. struct partqueue *pq;
  271. for (pq= queue; pq; pq= pq->nextinqueue) {
  272. switch (which) {
  273. case ds_junk:
  274. if (pq->info.md5sum) continue;
  275. break;
  276. case ds_package:
  277. if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
  278. break;
  279. case ds_all:
  280. break;
  281. default:
  282. internerr("unknown discard_which '%d'", which);
  283. }
  284. if (unlink(pq->info.filename))
  285. ohshite(_("unable to discard `%.250s'"),pq->info.filename);
  286. printf(_("Deleted %s.\n"),pq->info.filename);
  287. }
  288. }
  289. int
  290. do_discard(const char *const *argv)
  291. {
  292. const char *thisarg;
  293. struct partqueue *queue;
  294. struct partqueue *pq;
  295. queue = scandepot();
  296. if (*argv) {
  297. for (pq= queue; pq; pq= pq->nextinqueue)
  298. if (pq->info.md5sum)
  299. mustgetpartinfo(pq->info.filename,&pq->info);
  300. discard_parts(queue, ds_junk, NULL);
  301. while ((thisarg = *argv++))
  302. discard_parts(queue, ds_package, thisarg);
  303. } else {
  304. discard_parts(queue, ds_all, NULL);
  305. }
  306. return 0;
  307. }