queue.c 9.0 KB

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