aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnonymized <anonymous@example.com>2018-01-12 20:56:32 +0100
committerAnonymized <anonymous@example.com>2018-01-12 20:56:32 +0100
commitdf2d8d09c3a0909351c0218f0059b8dfe2346b74 (patch)
treef250499d5c6ed2e31e20d87b249d524708f60627
parent468966a8b009b992aab91e5f300161b6a8b53782 (diff)
downloadgnunet-guile2-df2d8d09c3a0909351c0218f0059b8dfe2346b74.tar.gz
gnunet-guile2-df2d8d09c3a0909351c0218f0059b8dfe2346b74.zip
add 'gnunet-guile publish CONFIGURATION FILENAME'
This is not a full re-implementation of gnunet-publish, it doesn't include support for: - setting namespace - using default configuration - settings anonymity - publishing directories is not supported
-rwxr-xr-xgnunet-guile107
-rw-r--r--gnunet.scm173
2 files changed, 259 insertions, 21 deletions
diff --git a/gnunet-guile b/gnunet-guile
index 6a05c39..d1566c6 100755
--- a/gnunet-guile
+++ b/gnunet-guile
@@ -25,16 +25,13 @@ exec guile -e '(@ (gnunet-guile) main)' -s "$0" "$@"
25(use-modules ((gnunet subcommand))) 25(use-modules ((gnunet subcommand)))
26 26
27 27
28;;; download 28;;; download, gnunet-download.c
29 29
30(define %download-options 30(define (download/task/clean-up fs)
31 '((help (single-char #\h) (value #f))))
32
33(define (task/clean-up fs)
34 (lambda () 31 (lambda ()
35 (fs-stop fs))) 32 (fs-stop fs)))
36 33
37(define (progress info) 34(define (download/progress info)
38 (let ((status (fs-progress-info-status info))) 35 (let ((status (fs-progress-info-status info)))
39 (cond 36 (cond
40 ((eq? status %fs-status-download-start) 37 ((eq? status %fs-status-download-start)
@@ -48,21 +45,24 @@ exec guile -e '(@ (gnunet-guile) main)' -s "$0" "$@"
48 (display "* Download complete!\n") 45 (display "* Download complete!\n")
49 (scheduler-shutdown)) 46 (scheduler-shutdown))
50 ((eq? status %fs-status-download-stopped) 47 ((eq? status %fs-status-download-stopped)
51 (scheduler-add-now (task/clean-up (fs-progress-info-fs info)))) 48 (scheduler-add-now (download/task/clean-up (fs-progress-info-fs info))))
52 ((or (eq? status %fs-status-download-active) 49 ((or (eq? status %fs-status-download-active)
53 (eq? status %fs-status-download-inactive)) 50 (eq? status %fs-status-download-inactive))
54 #t) 51 #t)
55 (else (format #t "* Unexpected status (~a)\n" status))))) 52 (else (format #t "* Unexpected status (~a)\n" status)))))
56 53
57(define (task/shutdown download-context) 54(define (download/task/shutdown download-context)
58 (lambda () 55 (lambda ()
59 (fs-download-stop download-context #true))) 56 (fs-download-stop download-context #true)))
60 57
61(define (task/download configuration uri filename) 58(define (download/task configuration uri filename)
62 (lambda () 59 (lambda ()
63 (let ((fs (fs-start configuration "gnunet-guile" progress))) 60 (let ((fs (fs-start configuration "gnunet-guile" download/progress)))
64 (let ((download-context (fs-download-start fs uri 0 #:filename filename))) 61 (let ((download-context (fs-download-start fs uri 0 #:filename filename)))
65 (scheduler-add-shutdown (task/shutdown download-context)))))) 62 (scheduler-add-shutdown (download/task/shutdown download-context))))))
63
64(define %download-options
65 '((help (single-char #\h) (value #f))))
66 66
67(define (download args) 67(define (download args)
68 "gnunet download CONFIGURATION URI FILENAME 68 "gnunet download CONFIGURATION URI FILENAME
@@ -82,12 +82,89 @@ Download the URI to FILENAME using CONFIGURATION.
82 (unless (or (uri-chk? uri) (uri-loc? uri)) 82 (unless (or (uri-chk? uri) (uri-loc? uri))
83 ;; TODO: proper exit 83 ;; TODO: proper exit
84 (throw 'gnunet-guile "Only CHK or LOC URIs supported")) 84 (throw 'gnunet-guile "Only CHK or LOC URIs supported"))
85 (scheduler-run (task/download configuration* uri filename))))))))) 85 (scheduler-run (download/task configuration* uri filename)))))))))
86
87;;; publish, gnunet-publish.c
88
89;; XXX: we have to use global, because gnunet doesn't expose it in
90;; directory-scan callback aka. publish/task/directory-scan
91(define %directory-scanner #f)
92
93(define (publish/progress info)
94 (let ((status (fs-progress-info-status info)))
95 (cond
96 ((eq? status %fs-status-publish-start)
97 (format #t "* Publishing started!\n"))
98 ((eq? status %fs-status-publish-progress)
99 (format #t "* Publishing in progress...\n"))
100 ((eq? status %fs-status-publish-error)
101 (format #t "* Error publishing: ~a!\n" (fs-progress-info-publish-error info))
102 (scheduler-shutdown))
103 ((eq? status %fs-status-publish-completed)
104 (format #t "* Publishing '~a' done.\n" (fs-progress-info-publish-filename info))
105 (format #t "* URI is '~a'\n" (fs-progress-info-publish-chk-uri info))
106 (scheduler-shutdown)))))
107
108(define (publish/task/shutdown publish-context)
109 (lambda ()
110 (fs-publish-stop publish-context)))
111
112(define (publish/task/directory-scan fs)
113 (lambda (filename directory? reason)
114 (cond
115 ((eq? reason %fs-directory-scanner-file-start)
116 (format #t "* Scanning ~a.\n" filename))
117 ((eq? reason %fs-directory-scanner-file-ignored)
118 (format #t "* There was trouble processing file ~a, skipping it.\n" filename))
119 ((eq? reason %fs-directory-scanner-all-counted)
120 (format #t "* Preprocessing complete.\n"))
121 ((eq? reason %fs-directory-scanner-extract-finished)
122 (format #t "* Extracting meta data from file `~a' complete.\n" filename))
123 ((eq? reason %fs-directory-scanner-finished)
124 (let ((share-tree (fs-directory-scan-get-result* %directory-scanner)))
125 (let ((fi (fs-file-information-create-from-file
126 fs
127 (fs-share-tree-item-filename share-tree)
128 (fs-share-tree-item-ksk-uri share-tree)
129 (fs-share-tree-item-meta share-tree)
130 %gnunet-yes
131 (fs-block-options))))
132 ;; TODO: call GNUNET_FS_file_information_inspect
133 (let ((publish-context (fs-publish-start fs fi)))
134 (scheduler-add-shutdown (publish/task/shutdown publish-context))))))
135 ((eq? reason %fs-directory-scanner-error)
136 (scheduler-shutdown))
137 (else (error "should not happen")))))
138
139(define (publish/task configuration filename)
140 (lambda ()
141 (let ((fs (fs-start configuration "gnunet-guile" publish/progress)))
142 (set! %directory-scanner
143 (fs-directory-scan-start filename
144 #t
145 (publish/task/directory-scan fs))))))
146
147(define (publish-exec configuration filename)
148 (let ((configuration* (configuration-create)))
149 (configuration-load! configuration* configuration)
150 (scheduler-run (publish/task configuration* filename))))
151
152(define %publish-options
153 '((help (single-char #\h) (value #f))))
86 154
87;;; publish 155(define (publish args)
156 "gnunet-guile publish CONFIGURATION FILE
88 157
89(define (publish . args) 158Publish on gnunet FILE using CONFIGURATION
90 (pk 'publish args)) 159"
160 (let ((options (getopt-long (cons "gnunet-guile publish" args)
161 %download-options
162 #:stop-at-first-non-option #f)))
163 (if (option-ref options 'help #f)
164 (display (procedure-documentation download))
165 (match args
166 ((configuration file)
167 (publish-exec configuration file))))))
91 168
92;;; cli 169;;; cli
93 170
diff --git a/gnunet.scm b/gnunet.scm
index ae9c218..e59e959 100644
--- a/gnunet.scm
+++ b/gnunet.scm
@@ -24,6 +24,7 @@
24(use-modules ((gnunet config))) 24(use-modules ((gnunet config)))
25 25
26 26
27;; XXX: This is broken, do not re-use, see "workaround dynamic-link*"
27(define* (dynamic-link* #:optional library-name) 28(define* (dynamic-link* #:optional library-name)
28 (let ((shared-object (if library-name (dynamic-link library-name) (dynamic-link)))) 29 (let ((shared-object (if library-name (dynamic-link library-name) (dynamic-link))))
29 (lambda (return-value function-name . arguments) 30 (lambda (return-value function-name . arguments)
@@ -45,6 +46,7 @@
45 "If value is a %null-pointer throw a 'gnunet-error. Return the value 46 "If value is a %null-pointer throw a 'gnunet-error. Return the value
46otherwise" 47otherwise"
47 (if (eq? value ffi:%null-pointer) 48 (if (eq? value ffi:%null-pointer)
49 ;; This is not very clean, it should stop the scheduler
48 (throw 'gnunet-error message) 50 (throw 'gnunet-error message)
49 value)) 51 value))
50 52
@@ -63,6 +65,9 @@ otherwise"
63 (let ((bv (ffi:pointer->bytevector pointer size))) 65 (let ((bv (ffi:pointer->bytevector pointer size)))
64 (make-bytestructure bv 0 desc)))) 66 (make-bytestructure bv 0 desc))))
65 67
68(define bytestructure->pointer (compose ffi:bytevector->pointer
69 bytestructure-bytevector))
70
66;;; gnunet-util bindings, gnunet_util_lib.h 71;;; gnunet-util bindings, gnunet_util_lib.h
67 72
68(define util (dynamic-link* %libgnunet-util)) 73(define util (dynamic-link* %libgnunet-util))
@@ -266,14 +271,24 @@ Return unique task identifier for the job, valid until THUNK is started."
266 271
267;; uri 272;; uri
268 273
274(define uri-destroy
275 (let ((func (fs ffi:void "GNUNET_FS_uri_destroy" '*)))
276 (ffi:procedure->pointer ffi:void func '(*))))
277
269(define-public string->uri 278(define-public string->uri
270 (let ((func (fs '* "GNUNET_FS_uri_parse" '* '*))) 279 (let ((func (fs '* "GNUNET_FS_uri_parse" '* '*)))
271 (lambda (string) 280 (lambda (string)
272 (let ((out (func (ffi:string->pointer string) ffi:%null-pointer))) 281 (let ((out (func (ffi:string->pointer string) ffi:%null-pointer)))
273 (when (eq? out ffi:%null-pointer) 282 (when (eq? out ffi:%null-pointer)
274 (throw 'gnunet-error "Failed to parse URI")) 283 (throw 'gnunet-error "Failed to parse URI"))
284 (ffi:set-pointer-finalizer! out uri-destroy)
275 out)))) 285 out))))
276 286
287(define-public uri->string
288 (let ((func (fs '* "GNUNET_FS_uri_to_string" '*)))
289 (lambda (uri)
290 (ffi:pointer->string (func uri)))))
291
277(define-public uri-chk? 292(define-public uri-chk?
278 (let ((func (fs ffi:int "GNUNET_FS_uri_test_chk" '*))) 293 (let ((func (fs ffi:int "GNUNET_FS_uri_test_chk" '*)))
279 (lambda (uri) 294 (lambda (uri)
@@ -286,12 +301,6 @@ Return unique task identifier for the job, valid until THUNK is started."
286 "Is this a location URI?" 301 "Is this a location URI?"
287 (integer->boolean (func uri))))) 302 (integer->boolean (func uri)))))
288 303
289(define-public uri-destroy
290 (let ((func (fs ffi:void "GNUNET_FS_uri_destroy" '*)))
291 (lambda (uri)
292 "Destroy uri"
293 (func uri))))
294
295(define-public uri-chk-get-file-size 304(define-public uri-chk-get-file-size
296 (let ((func (fs ffi:uint64 "GNUNET_FS_uri_chk_get_file_size" '*))) 305 (let ((func (fs ffi:uint64 "GNUNET_FS_uri_chk_get_file_size" '*)))
297 (lambda (uri) 306 (lambda (uri)
@@ -499,6 +508,21 @@ Return unique task identifier for the job, valid until THUNK is started."
499(define-public (fs-progress-info-download-context info) 508(define-public (fs-progress-info-download-context info)
500 (ffi:make-pointer (bytestructure-ref info 'value 'download 'download-context))) 509 (ffi:make-pointer (bytestructure-ref info 'value 'download 'download-context)))
501 510
511(define-public (fs-progress-info-publish-error info)
512 (ffi:pointer->string
513 (ffi:make-pointer
514 (bytestructure-ref info 'value 'publish 'specifics 'error 'message))))
515
516(define-public (fs-progress-info-publish-filename info)
517 (ffi:pointer->string
518 (ffi:make-pointer
519 (bytestructure-ref info 'value 'publish 'filename))))
520
521(define-public (fs-progress-info-publish-chk-uri info)
522 (uri->string
523 (ffi:make-pointer
524 (bytestructure-ref info 'value 'publish 'specifics 'completed 'chk-uri))))
525
502(define (progress-callback-wrapper callback) 526(define (progress-callback-wrapper callback)
503 (lambda (_ info) 527 (lambda (_ info)
504 (callback (pointer->bytestructure %fs-progress-info info)))) 528 (callback (pointer->bytestructure %fs-progress-info info))))
@@ -594,6 +618,143 @@ the level of anonymity for the download."
594 (lambda (download-context delete-incomplete-files?) 618 (lambda (download-context delete-incomplete-files?)
595 (func download-context (boolean->integer delete-incomplete-files?))))) 619 (func download-context (boolean->integer delete-incomplete-files?)))))
596 620
621(define-public %fs-publish-option-none 0)
622(define-public %fs-publish-option-simulate-only 1)
623
624(define-public fs-publish-start
625 (let ((func (fs '* "GNUNET_FS_publish_start" '* '* '* '* '* ffi:int)))
626 (lambda* (fs
627 file-information
628 #:key
629 (namespace #f)
630 (nid #f)
631 (nuid #f)
632 (options %fs-publish-option-none))
633 (let ((out (func fs
634 file-information
635 (if namespace namespace ffi:%null-pointer)
636 (if nid (ffi:string->pointer nid) ffi:%null-pointer)
637 (if nuid (ffi:string->pointer nuid) ffi:%null-pointer)
638 options)))
639 (check2 out "Could not start publishing.")))))
640
641(define-public fs-publish-stop
642 (let ((func (fs ffi:void "GNUNET_FS_publish_stop" '*)))
643 (lambda (publish-context)
644 "Stop a publication. Will abort incomplete publications (but not remove
645blocks that have already been published) or simply clean up the state for
646completed publications. Must NOT be called from within the event callback!"
647 (func publish-context))))
648
649(define-public %fs-directory-scanner-file-start 0)
650(define-public %fs-directory-scanner-file-ignored 1)
651(define-public %fs-directory-scanner-all-counted 2)
652(define-public %fs-directory-scanner-extract-finished 3)
653(define-public %fs-directory-scanner-finished 4)
654(define-public %fs-directory-scanner-error 5)
655
656(define %fs-share-tree-item (bs:struct
657 `((prev ,(bs:pointer (delay %fs-share-tree-item)))
658 (next ,(bs:pointer (delay %fs-share-tree-item)))
659 (parent ,(bs:pointer (delay %fs-share-tree-item)))
660 (children-head ,(bs:pointer (delay %fs-share-tree-item)))
661 (children-tail ,(bs:pointer (delay %fs-share-tree-item)))
662 (meta ,uintptr_t)
663 (ksk-uri ,uintptr_t)
664 (filename ,uintptr_t)
665 (short-filename ,uintptr_t)
666 (directory? ,int))))
667
668(define (maybe->string pointer)
669 (if (eq? pointer ffi:%null-pointer)
670 #f
671 (ffi:pointer->string pointer)))
672
673(define-public fs-directory-scan-start
674 (let ((func (fs '* "GNUNET_FS_directory_scan_start" '* ffi:int '* '* '*)))
675 (lambda (filename extractor? callback)
676 "Start a directory scanner
677
678Return a directory scanner object"
679 (func (ffi:string->pointer filename)
680 (boolean->integer (not extractor?))
681 ffi:%null-pointer
682 (ffi:procedure->pointer ffi:void
683 (lambda (_ filename directory? reason)
684 (callback (maybe->string filename)
685 (integer->boolean directory?)
686 reason))
687 (list '* '* ffi:int ffi:int))
688 ffi:%null-pointer))))
689
690(define-public fs-directory-scan-abort
691 (let ((func (fs ffi:void "GNUNET_FS_directory_scan_abort" '*)))
692 (lambda (directory-scanner)
693 "Abort the scan. Must not be called within the callback function of
694fs-directory-scan-start"
695 (func directory-scanner))))
696
697(define-public fs-directory-scan-get-result
698 (let ((func (fs '* "GNUNET_FS_directory_scan_get_result" '*))
699 (free (fs ffi:void "GNUNET_FS_share_tree_free" '*)))
700 (lambda (directory-scanner)
701 (let ((out (func directory-scanner)))
702 ;; XXX: workaround dynamic-link*
703 (ffi:set-pointer-finalizer! out (ffi:procedure->pointer ffi:void free '(*)))
704 out))))
705
706(define-public fs-share-tree-trim!
707 (let ((func (fs '* "GNUNET_FS_share_tree_trim" '*)))
708 (lambda (toplevel)
709 (func toplevel))))
710
711(define-public (fs-directory-scan-get-result* directory-scanner)
712 (let ((out (fs-directory-scan-get-result directory-scanner)))
713 (fs-share-tree-trim! out)
714 (pointer->bytestructure %fs-share-tree-item out)))
715
716(define-public (fs-share-tree-item-filename item)
717 (ffi:pointer->string (ffi:make-pointer (bytestructure-ref item 'filename))))
718
719(define-public (fs-share-tree-item-ksk-uri item)
720 (ffi:make-pointer (bytestructure-ref item 'ksk-uri)))
721
722(define-public (fs-share-tree-item-meta item)
723 (ffi:make-pointer (bytestructure-ref item 'meta)))
724
725(define %fs-block-options
726 (bs:struct `((expiration-time ,%time-relative)
727 (anonymity-level ,uint32)
728 (content-priority ,uint32)
729 (replication-level ,uint32))))
730
731(define-public (fs-block-options)
732 (let ((options (bytestructure %fs-block-options)))
733 (bytestructure-set! options 'expiration-time 'rel-value-us 0)
734 (bytestructure-set! options 'anonymity-level 1)
735 (bytestructure-set! options 'content-priority 365)
736 (bytestructure-set! options 'replication-level 1)
737 options))
738
739(define-public fs-file-information-create-from-file
740 (let ((func (fs '*
741 "GNUNET_FS_file_information_create_from_file"
742 '*
743 '*
744 '*
745 '*
746 '*
747 ffi:int
748 '*)))
749 (lambda (fs filename keywords meta do-index options)
750 (func fs
751 ffi:%null-pointer
752 (ffi:string->pointer filename)
753 keywords
754 meta
755 do-index
756 (bytestructure->pointer options)))))
757
597 758
598;; gnunet-identity bindings 759;; gnunet-identity bindings
599 760