gnunet.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
  3. ;;; Copyright © 2018 Mark H Weaver <mhw@netris.org>
  4. ;;; Copyright © 2018 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
  5. ;;; Copyright © 2020 Simon Tournier <zimon.toutoune@gmail.com>
  6. ;;; Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. (define-module (guix build gnunet)
  23. #:use-module (guix build utils)
  24. #:use-module (srfi srfi-34)
  25. #:use-module (ice-9 format)
  26. #:use-module (rnrs io ports)
  27. #:export (gnunet-fetch))
  28. ;;; Commentary:
  29. ;;;
  30. ;;; This is the build-side support code of (guix gnunet-download). It allows
  31. ;;; files of which the GNUnet chk-URI is known to be downloaded from the GNUnet
  32. ;;; file-sharing system. The code has been derived from (guix build hg).
  33. ;;;
  34. ;;; Code:
  35. ;; Copied from (guix utils)
  36. (define (call-with-temporary-output-file proc)
  37. "Call PROC with a name of a temporary file and open output port to that
  38. file; close the file and delete it when leaving the dynamic extent of this
  39. call."
  40. (let* ((directory (or (getenv "TMPDIR") "/tmp"))
  41. (template (string-append directory "/guix-file.XXXXXX"))
  42. (out (mkstemp! template)))
  43. (dynamic-wind
  44. (lambda ()
  45. #t)
  46. (lambda ()
  47. (proc template out))
  48. (lambda ()
  49. (false-if-exception (close out))
  50. (false-if-exception (delete-file template))))))
  51. (define (gnunet-fs-up? port)
  52. "#t if the GNUnet FS daemon seems to be up at @var{port}, #f otherwise"
  53. (let ((s (socket PF_INET SOCK_STREAM 0)))
  54. (catch 'system-error
  55. (lambda ()
  56. (connect s AF_INET INADDR_LOOPBACK port)
  57. (close-port s)
  58. #t)
  59. (lambda (tag function msg msg+ errno)
  60. (close-port s)
  61. (if (and (equal? function "connect")
  62. (equal? errno (list ECONNREFUSED)))
  63. #f
  64. (throw tag function msg msg+ errno))))))
  65. ;; TODO: gnunet directories, time-outs, perhaps use guile-gnunet
  66. (define* (gnunet-fetch uri file
  67. #:key (gnunet-download-command "gnunet-download"))
  68. "Fetch a file identified by a GNUnet chk-URI @var{URI} into @var{file}.
  69. @var{uri} must not be a directory. Return #t on success, #f otherwise."
  70. (guard (c ((invoke-error? c)
  71. (format (current-error-port)
  72. "gnunet-fetch: '~a~{ ~a~}' failed with exit code ~a~%"
  73. (invoke-error-program c)
  74. (invoke-error-arguments c)
  75. (or (invoke-error-exit-status c)
  76. (invoke-error-stop-signal c)
  77. (invoke-error-term-signal c)))
  78. (false-if-exception (delete-file-recursively file))
  79. #f))
  80. (define port
  81. (let ((p (getenv "gnunet port")))
  82. (and p (< 0 (string-length p))
  83. (string->number p))))
  84. (define anonymity
  85. (let ((a (getenv "GNUNET_ANONYMITY")))
  86. (cond ((equal? a "") "1")
  87. ((not a) "1")
  88. (else a))))
  89. ;; Check if the GNUnet daemon is up,
  90. ;; otherwise gnunet-download might wait forever.
  91. (if (or (not port) (gnunet-fs-up? port))
  92. (call-with-temporary-output-file
  93. (lambda (config-file-name config-output-port)
  94. ;; Tell gnunet-download how to contact the FS daemon
  95. (display (getenv "gnunet configuration") config-output-port)
  96. (flush-output-port config-output-port)
  97. (invoke gnunet-download-command uri
  98. "-c" config-file-name
  99. "-V" ;; print progress information
  100. "-a" anonymity
  101. "-o" file)
  102. #t))
  103. (begin
  104. (format (current-error-port)
  105. "gnunet-fetch: file-sharing daemon is down.~%")
  106. #f))))
  107. ;;; gnunet.scm ends here