guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet] 14/17: Code cleaning: various improvements and bug fixes. * ide


From: Rémi Birot-Delrue
Subject: [gnunet] 14/17: Code cleaning: various improvements and bug fixes. * identity.scm: `open-identity-service` now throws an exception on failure. * binding-utils.scm: just add `destructuring-bind`. * common.scm: `time-rel` now throws an exception instead of returning a meaningless negative result; add `setup-log`. * container/metadata.scm: `metadata-set!` now throws an exception on error. * tests/container-metadata.scm: add tests for `metadata-copy`, `metadata-clear`, `metadata-equal?` and `add-publication-date!`
Date: Wed, 12 Aug 2015 18:24:42 +0000

remibd pushed a commit to branch master
in repository gnunet.

commit 9cef3b7d4339016e5f4d5a05dc7e4505923e2a16
Author: Rémi Birot-Delrue <address@hidden>
Date:   Mon Aug 10 19:18:22 2015 +0200

    Code cleaning: various improvements and bug fixes.
    * identity.scm: `open-identity-service` now throws an exception on failure.
    * binding-utils.scm: just add `destructuring-bind`.
    * common.scm: `time-rel` now throws an exception instead of returning a
                  meaningless negative result; add `setup-log`.
    * container/metadata.scm: `metadata-set!` now throws an exception on error.
    * tests/container-metadata.scm: add tests for `metadata-copy`,
                                    `metadata-clear`, `metadata-equal?`
                                    and `add-publication-date!`
---
 gnu/gnunet/binding-utils.scm      |    7 +++++++
 gnu/gnunet/common.scm             |   31 ++++++++++++++++++++++++++++++-
 gnu/gnunet/container/metadata.scm |    6 ++++--
 gnu/gnunet/identity.scm           |    9 ++++++---
 tests/container-metadata.scm      |   15 +++++++++++++++
 5 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/gnu/gnunet/binding-utils.scm b/gnu/gnunet/binding-utils.scm
index 0ab899f..1dd9d87 100644
--- a/gnu/gnunet/binding-utils.scm
+++ b/gnu/gnunet/binding-utils.scm
@@ -30,6 +30,8 @@
             rassoc
             rassoc-ref
 
+            destructuring-bind
+            
             string->pointer*
             pointer->string*
             make-c-struct*
@@ -93,3 +95,8 @@ if STRING is empty (\"\")."
                    (if (eq? %null-pointer x*)
                        (or% y ...)
                        x*)))))
+
+;;; Utilities
+
+(define-syntax-rule (destructuring-bind pattern value body body* ...)
+  (match value (pattern body body* ...)))
diff --git a/gnu/gnunet/common.scm b/gnu/gnunet/common.scm
index 0d1a6b4..17c88e4 100644
--- a/gnu/gnunet/common.scm
+++ b/gnu/gnunet/common.scm
@@ -45,6 +45,8 @@
             define-gnunet-fs
             define-gnunet-id
 
+            setup-log
+
             %make-blob-pointer
             %malloc
             %free
@@ -67,7 +69,11 @@
          (seconds* (+ (* minutes* 60)   seconds))
          (milli*   (+ (* seconds* 1000) milli))
          (micro*   (+ (* milli*   1000) micro)))
-    micro*))
+    (when (negative? micro*)
+      (scm-error 'out-of-range "time-rel"
+                 "result (~a) is negative" (list micro*)
+                 (list hours minutes seconds milli micro)))
+    (inexact->exact micro*)))
 
 (define ecdsa-public-key (generate (/ 256 8 4) uint32))
 (define eddsa-public-key ecdsa-public-key)
@@ -102,6 +108,8 @@
 (define-foreign-definer define-gnunet-fs gnunet-fs-ffi)
 (define-foreign-definer define-gnunet-id gnunet-identity-ffi)
 
+(define-gnunet %log-setup "GNUNET_log_setup" : '(* * *) -> int)
+
 (define-gnunet %xfree   "GNUNET_xfree_"   : (list '* '* int)     -> void)
 (define-gnunet %xmalloc "GNUNET_xmalloc_" : (list size_t '* int) -> '*)
 
@@ -111,6 +119,27 @@
   "GNUNET_STRINGS_string_to_data" : (list '* size_t '* size_t) -> int)
 
 
+(define log-level-alist
+  (list (cons #:none        (string->pointer "NONE"))
+        (cons #:error       (string->pointer "ERROR"))
+        (cons #:warning     (string->pointer "WARNING"))
+        (cons #:info        (string->pointer "INFO"))
+        (cons #:debug       (string->pointer "DEBUG"))
+        (cons #:invalid     (string->pointer "INVALID"))
+        (cons #:bulk        (string->pointer "BULK"))
+        (cons #:unspecified (string->pointer "UNSPECIFIED"))))
+
+(define* (setup-log client-name log-level #:optional (log-file ""))
+  "Setup GNUnet’s logging. CLIENT-NAME is the name of the program you’re
+writing, LOG-LEVEL is a keyword from (#:none #:error #:warning #:info #:debug
+#:invalid #:bulk), LOG-FILE is either a filename or #f for `stderr'."
+  (define (log-level->pointer key)
+    (or (assq-ref log-level-alist key)
+        (assq-ref log-level-alist #:unspecified)))
+  (%log-setup (string->pointer client-name)
+              (log-level->pointer log-level)
+              (string->pointer* log-file)))
+
 (define (bool->int x) (if x gnunet-yes gnunet-no))
 (define (int->bool x)
   (cond ((= gnunet-yes x)          #t)
diff --git a/gnu/gnunet/container/metadata.scm 
b/gnu/gnunet/container/metadata.scm
index e9a31e5..9437ead 100644
--- a/gnu/gnunet/container/metadata.scm
+++ b/gnu/gnunet/container/metadata.scm
@@ -148,8 +148,10 @@
         (bytevector-length      (metadata-item-data item))))
 
 (define (metadata-set! metadata item)
-  (apply %metadata-insert (unwrap-metadata metadata)
-         (metadata-item->list item)))
+  (let ((res (apply %metadata-insert (unwrap-metadata metadata)
+                    (metadata-item->list item))))
+    (when (= res gnunet-system-error)
+      (throw 'entry-already-exist "metadata-set!" metadata item))))
   
 (define (metadata-ref metadata type)
   (pointer->string
diff --git a/gnu/gnunet/identity.scm b/gnu/gnunet/identity.scm
index 06cfe8c..68850c1 100644
--- a/gnu/gnunet/identity.scm
+++ b/gnu/gnunet/identity.scm
@@ -98,9 +98,12 @@ assigned by the user for this ego (or #f if the user just 
deleted this ego).
 
 Return a handle to the identity service that’s needed by every identity related
 function."
-  (%identity-connect (unwrap-configuration config)
-                    (identity-callback->pointer identity-callback)
-                    %null-pointer))
+  (or%
+   (%identity-connect (unwrap-configuration config)
+                      (identity-callback->pointer identity-callback)
+                      %null-pointer)
+   (throw 'invalid-result "open-identity-service" "%identity-connect"
+          %null-pointer)))
 
 (define (close-identity-service identity-handle)
   "Disconnect from the identity service."
diff --git a/tests/container-metadata.scm b/tests/container-metadata.scm
index 76ef233..900b4e5 100644
--- a/tests/container-metadata.scm
+++ b/tests/container-metadata.scm
@@ -45,4 +45,19 @@
 (test-equal '("foo" "bar")
            (metadata-map (lambda (name . _) name) test-meta))
 
+;; copy
+(define test-meta-copy (metadata-copy test-meta))
+(test-equal "foo.scm" (metadata-ref test-meta-copy #:original-filename))
+
+;; equal?
+(test-assert (metadata-equal? test-meta test-meta-copy))
+
+;; clear!
+(metadata-clear! test-meta-copy)
+(test-equal #f (metadata-ref test-meta-copy #:original-filename))
+
+;; add-publication-date!
+(metadata-add-publication-date! test-meta-copy)
+(test-assert (metadata-ref test-meta-copy #:publication-date))
+
 (test-end)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]