ipfs.scm 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (guix ipfs)
  19. #:use-module (json)
  20. #:use-module (guix base64)
  21. #:use-module ((guix build utils) #:select (dump-port))
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-11)
  24. #:use-module (srfi srfi-26)
  25. #:use-module (rnrs io ports)
  26. #:use-module (rnrs bytevectors)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 ftw)
  29. #:use-module (web uri)
  30. #:use-module (web client)
  31. #:use-module (web response)
  32. #:export (%ipfs-base-url
  33. add-data
  34. add-file
  35. add-file-tree
  36. restore-file-tree
  37. content?
  38. content-name
  39. content-hash
  40. content-size
  41. add-empty-directory
  42. add-to-directory
  43. read-contents
  44. publish-name))
  45. ;;; Commentary:
  46. ;;;
  47. ;;; This module implements bindings for the HTTP interface of the IPFS
  48. ;;; gateway, documented here: <https://docs.ipfs.io/reference/api/http/>. It
  49. ;;; allows you to add and retrieve files over IPFS, and a few other things.
  50. ;;;
  51. ;;; Code:
  52. (define %ipfs-base-url
  53. ;; URL of the IPFS gateway.
  54. (make-parameter "http://localhost:5001"))
  55. (define* (call url decode #:optional (method http-post)
  56. #:key body (false-if-404? #t) (headers '()))
  57. "Invoke the endpoint at URL using METHOD. Decode the resulting JSON body
  58. using DECODE, a one-argument procedure that takes an input port; when DECODE
  59. is false, return the input port. When FALSE-IF-404? is true, return #f upon
  60. 404 responses."
  61. (let*-values (((response port)
  62. (method url #:streaming? #t
  63. #:body body
  64. ;; Always pass "Connection: close".
  65. #:keep-alive? #f
  66. #:headers `((connection close)
  67. ,@headers))))
  68. (cond ((= 200 (response-code response))
  69. (if decode
  70. (let ((result (decode port)))
  71. (close-port port)
  72. result)
  73. port))
  74. ((and false-if-404?
  75. (= 404 (response-code response)))
  76. (close-port port)
  77. #f)
  78. (else
  79. (close-port port)
  80. (throw 'ipfs-error url response)))))
  81. ;; Result of a file addition.
  82. (define-json-mapping <content> make-content content?
  83. json->content
  84. (name content-name "Name")
  85. (hash content-hash "Hash")
  86. (bytes content-bytes "Bytes")
  87. (size content-size "Size" string->number))
  88. ;; Result of a 'patch/add-link' operation.
  89. (define-json-mapping <directory> make-directory directory?
  90. json->directory
  91. (hash directory-hash "Hash")
  92. (links directory-links "Links" json->links))
  93. ;; A "link".
  94. (define-json-mapping <link> make-link link?
  95. json->link
  96. (name link-name "Name")
  97. (hash link-hash "Hash")
  98. (size link-size "Size" string->number))
  99. ;; A "binding", also known as a "name".
  100. (define-json-mapping <binding> make-binding binding?
  101. json->binding
  102. (name binding-name "Name")
  103. (value binding-value "Value"))
  104. (define (json->links json)
  105. (match json
  106. (#f '())
  107. (links (map json->link links))))
  108. (define %multipart-boundary
  109. ;; XXX: We might want to find a more reliable boundary.
  110. (string-append (make-string 24 #\-) "2698127afd7425a6"))
  111. (define (bytevector->form-data bv port)
  112. "Write to PORT a 'multipart/form-data' representation of BV."
  113. (display (string-append "--" %multipart-boundary "\r\n"
  114. "Content-Disposition: form-data\r\n"
  115. "Content-Type: application/octet-stream\r\n\r\n")
  116. port)
  117. (put-bytevector port bv)
  118. (display (string-append "\r\n--" %multipart-boundary "--\r\n")
  119. port))
  120. (define* (add-data data #:key (name "file.txt") recursive?)
  121. "Add DATA, a bytevector, to IPFS. Return a content object representing it."
  122. (call (string-append (%ipfs-base-url)
  123. "/api/v0/add?arg=" (uri-encode name)
  124. "&recursive="
  125. (if recursive? "true" "false"))
  126. json->content
  127. #:headers
  128. `((content-type
  129. . (multipart/form-data
  130. (boundary . ,%multipart-boundary))))
  131. #:body
  132. (call-with-bytevector-output-port
  133. (lambda (port)
  134. (bytevector->form-data data port)))))
  135. (define (not-dot? entry)
  136. (not (member entry '("." ".."))))
  137. (define (file-tree->sexp file)
  138. "Add FILE, recursively, to the IPFS, and return an sexp representing the
  139. directory's tree structure.
  140. Unlike IPFS's own \"UnixFS\" structure, this format preserves exactly what we
  141. need: like the nar format, it preserves the executable bit, but does not save
  142. the mtime or other Unixy attributes irrelevant in the store."
  143. ;; The natural approach would be to insert each directory listing as an
  144. ;; object of its own in IPFS. However, this does not buy us much in terms
  145. ;; of deduplication, but it does cause a lot of extra round trips when
  146. ;; fetching it. Thus, this sexp is \"flat\" in that only the leaves are
  147. ;; inserted into the IPFS.
  148. (let ((st (lstat file)))
  149. (match (stat:type st)
  150. ('directory
  151. (let* ((parent file)
  152. (entries (map (lambda (file)
  153. `(entry ,file
  154. ,(file-tree->sexp
  155. (string-append parent "/" file))))
  156. (scandir file not-dot?)))
  157. (size (fold (lambda (entry total)
  158. (match entry
  159. (('entry name (kind value size))
  160. (+ total size))))
  161. 0
  162. entries)))
  163. `(directory ,entries ,size)))
  164. ('symlink
  165. `(symlink ,(readlink file) 0))
  166. ('regular
  167. (let ((size (stat:size st)))
  168. (if (zero? (logand (stat:mode st) #o100))
  169. `(file ,(content-name (add-file file)) ,size)
  170. `(executable ,(content-name (add-file file)) ,size)))))))
  171. (define (add-file-tree file)
  172. "Add FILE to the IPFS, recursively, using our own canonical directory
  173. format. Return the resulting content object."
  174. (add-data (string->utf8 (object->string
  175. `(file-tree (version 0)
  176. ,(file-tree->sexp file))))))
  177. (define (restore-file-tree object file)
  178. "Restore to FILE the tree pointed to by OBJECT."
  179. (let restore ((tree (match (read (read-contents object))
  180. (('file-tree ('version 0) tree)
  181. tree)))
  182. (file file))
  183. (match tree
  184. (('file object size)
  185. (call-with-output-file file
  186. (lambda (output)
  187. (dump-port (read-contents object) output))))
  188. (('executable object size)
  189. (call-with-output-file file
  190. (lambda (output)
  191. (dump-port (read-contents object) output)))
  192. (chmod file #o555))
  193. (('symlink target size)
  194. (symlink target file))
  195. (('directory (('entry names entries) ...) size)
  196. (mkdir file)
  197. (for-each restore entries
  198. (map (cut string-append file "/" <>) names))))))
  199. (define* (add-file file #:key (name (basename file)))
  200. "Add FILE under NAME to the IPFS and return a content object for it."
  201. (add-data (match (call-with-input-file file get-bytevector-all)
  202. ((? eof-object?) #vu8())
  203. (bv bv))
  204. #:name name))
  205. (define* (add-empty-directory #:key (name "directory"))
  206. "Return a content object for an empty directory."
  207. (add-data #vu8() #:recursive? #t #:name name))
  208. (define* (add-to-directory directory file name)
  209. "Add FILE to DIRECTORY under NAME, and return the resulting directory.
  210. DIRECTORY and FILE must be hashes identifying objects in the IPFS store."
  211. (call (string-append (%ipfs-base-url)
  212. "/api/v0/object/patch/add-link?arg="
  213. (uri-encode directory)
  214. "&arg=" (uri-encode name) "&arg=" (uri-encode file)
  215. "&create=true")
  216. json->directory))
  217. (define* (read-contents object #:key offset length)
  218. "Return an input port to read the content of OBJECT from."
  219. (call (string-append (%ipfs-base-url)
  220. "/api/v0/cat?arg=" object)
  221. #f))
  222. (define* (publish-name object)
  223. "Publish OBJECT under the current peer ID."
  224. (call (string-append (%ipfs-base-url)
  225. "/api/v0/name/publish?arg=" object)
  226. json->binding))