queue.c 9.1 KB

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