]> granicus.if.org Git - p11-kit/commitdiff
extract: Allow p11_save_write() to automatically calculate length
authorStef Walter <stefw@gnome.org>
Fri, 15 Mar 2013 07:18:42 +0000 (08:18 +0100)
committerStef Walter <stefw@gnome.org>
Fri, 15 Mar 2013 16:33:47 +0000 (17:33 +0100)
Also if automatically calculating length, then ignore input
that is NULL, as something that shouldn't be written out.

This allows easier chaining of optional output, such as comments.

https://bugs.freedesktop.org/show_bug.cgi?id=62029

tools/save.c
tools/save.h
tools/tests/files/empty-file [new file with mode: 0644]
tools/tests/files/simple-string [new file with mode: 0644]
tools/tests/test-save.c

index 34caca1c39416ee853c63c9f5d1b77e7ae347445..1f886ad8a3f55283cb614fde87f8276c384db8c2 100644 (file)
@@ -66,7 +66,7 @@ struct _p11_save_dir {
 bool
 p11_save_write_and_finish (p11_save_file *file,
                            const void *data,
-                           size_t length)
+                           ssize_t length)
 {
        bool ret;
 
@@ -128,7 +128,7 @@ p11_save_open_file (const char *path,
 bool
 p11_save_write (p11_save_file *file,
                 const void *data,
-                size_t length)
+                ssize_t length)
 {
        const unsigned char *buf = data;
        ssize_t written = 0;
@@ -137,6 +137,13 @@ p11_save_write (p11_save_file *file,
        if (!file)
                return false;
 
+       /* Automatically calculate length */
+       if (length < 0) {
+               if (!data)
+                       return true;
+               length = strlen (data);
+       }
+
        while (written < length) {
                res = write (file->fd, buf + written, length - written);
                if (res <= 0) {
index bfa099091ff4a34ba10730c48421bf5dffb6898e..f68d0547dabbe8ae99cc0b4cb7914f2faebb90e9 100644 (file)
@@ -49,11 +49,11 @@ p11_save_file *  p11_save_open_file         (const char *path,
 
 bool             p11_save_write             (p11_save_file *file,
                                              const void *data,
-                                             size_t length);
+                                             ssize_t length);
 
 bool             p11_save_write_and_finish  (p11_save_file *file,
                                              const void *data,
-                                             size_t length);
+                                             ssize_t length);
 
 bool             p11_save_finish_file       (p11_save_file *file,
                                              bool commit);
diff --git a/tools/tests/files/empty-file b/tools/tests/files/empty-file
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tools/tests/files/simple-string b/tools/tests/files/simple-string
new file mode 100644 (file)
index 0000000..be13474
--- /dev/null
@@ -0,0 +1 @@
+The simple string is hairy
\ No newline at end of file
index 41f4e08c15aee3c74e956f630e325fffc7f5fe5f..88d1ecd27e24a7f398b50695e4a63f8ad76f0939 100644 (file)
@@ -190,6 +190,54 @@ test_file_overwrite (CuTest *tc)
        teardown (tc);
 }
 
+static void
+test_file_auto_empty (CuTest *tc)
+{
+       p11_save_file *file;
+       char *filename;
+       bool ret;
+
+       setup (tc);
+
+       if (asprintf (&filename, "%s/%s", test.directory, "extract-file") < 0)
+               CuFail (tc, "asprintf() failed");
+
+       file = p11_save_open_file (filename, 0);
+       CuAssertPtrNotNull (tc, file);
+
+       ret = p11_save_write_and_finish (file, NULL, -1);
+       CuAssertIntEquals (tc, true, ret);
+       free (filename);
+
+       test_check_file (tc, test.directory, "extract-file", SRCDIR "/files/empty-file");
+
+       teardown (tc);
+}
+
+static void
+test_file_auto_length (CuTest *tc)
+{
+       p11_save_file *file;
+       char *filename;
+       bool ret;
+
+       setup (tc);
+
+       if (asprintf (&filename, "%s/%s", test.directory, "extract-file") < 0)
+               CuFail (tc, "asprintf() failed");
+
+       file = p11_save_open_file (filename, 0);
+       CuAssertPtrNotNull (tc, file);
+
+       ret = p11_save_write_and_finish (file, "The simple string is hairy", -1);
+       CuAssertIntEquals (tc, true, ret);
+       free (filename);
+
+       test_check_file (tc, test.directory, "extract-file", SRCDIR "/files/simple-string");
+
+       teardown (tc);
+}
+
 static void
 test_write_with_null (CuTest *tc)
 {
@@ -496,6 +544,8 @@ main (void)
        SUITE_ADD_TEST (suite, test_file_exists);
        SUITE_ADD_TEST (suite, test_file_bad_directory);
        SUITE_ADD_TEST (suite, test_file_overwrite);
+       SUITE_ADD_TEST (suite, test_file_auto_empty);
+       SUITE_ADD_TEST (suite, test_file_auto_length);
        SUITE_ADD_TEST (suite, test_write_with_null);
        SUITE_ADD_TEST (suite, test_write_and_finish_with_null);
        SUITE_ADD_TEST (suite, test_file_abort);