queue.c 9.0 KB

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