* Set a encryption passphrase.
*/
__LA_DECL int archive_write_set_passphrase(struct archive *_a, const char *p);
+__LA_DECL int archive_write_set_passphrase_callback(struct archive *,
+ void *client_data, archive_passphrase_callback *);
/*-
* ARCHIVE_WRITE_DISK API
* Encryption passphrase.
*/
char *passphrase;
+ archive_passphrase_callback *passphrase_callback;
+ void *passphrase_client_data;
};
/*
.\"
.\" $FreeBSD$
.\"
-.Dd September 13, 2014
+.Dd September 21, 2014
.Dt ARCHIVE_WRITE_SET_PASSPHRASE 3
.Os
.Sh NAME
-.Nm archive_write_set_passphrase
+.Nm archive_write_set_passphrase ,
+.Nm archive_write_set_passphrase_callback
.Nd functions for writing encrypted archives
.Sh LIBRARY
Streaming Archive Library (libarchive, -larchive)
.Fa "struct archive *"
.Fa "const char *passphrase"
.Fc
+.Ft int
+.Fo archive_write_set_passphrase_callback
+.Fa "struct archive *"
+.Fa "void *client_data"
+.Fa "archive_passphrase_callback *"
+.Fc
.Sh DESCRIPTION
+.Bl -tag -width indent
+.It Fn archive_write_set_passphrase
Set a passphrase for writing an encryption archive.
If
.Ar passphrase
Otherwise,
.Cm ARCHIVE_OK
will be returned.
+.It Fn archive_write_set_passphrase_callback
+Register callback function that will be invoked to get a passphrase
+for encrption if the passphrase was not set by the
+.Fn archive_write_set_passphrase
+function.
.\" .Sh ERRORS
.Sh SEE ALSO
.Xr tar 1 ,
return (ARCHIVE_OK);
}
+
+int
+archive_write_set_passphrase_callback(struct archive *_a, void *client_data,
+ archive_passphrase_callback *cb)
+{
+ struct archive_write *a = (struct archive_write *)_a;
+
+ archive_check_magic(_a, ARCHIVE_WRITE_MAGIC, ARCHIVE_STATE_NEW,
+ "archive_write_set_passphrase_callback");
+
+ a->passphrase_callback = cb;
+ a->passphrase_client_data = client_data;
+ return (ARCHIVE_OK);
+}
+
+
const char *
__archive_write_get_passphrase(struct archive_write *a)
{
- return (a->passphrase);
+ if (a->passphrase != NULL)
+ return (a->passphrase);
+
+ if (a->passphrase_callback != NULL) {
+ const char *p;
+ p = a->passphrase_callback(&a->archive,
+ a->passphrase_client_data);
+ if (p != NULL) {
+ a->passphrase = strdup(p);
+ if (a->passphrase == NULL) {
+ archive_set_error(&a->archive, ENOMEM,
+ "Can't allocate data for passphrase");
+ return (NULL);
+ }
+ return (a->passphrase);
+ }
+ }
+ return (NULL);
}
test(1);
test(0);
}
+
+
+static const char *
+callback1(struct archive *a, void *_client_data)
+{
+ int *cnt;
+
+ (void)a; /* UNUSED */
+
+ cnt = (int *)_client_data;
+ *cnt += 1;
+ return ("passCallBack");
+}
+
+DEFINE_TEST(test_archive_write_set_passphrase_callback)
+{
+ struct archive* a = archive_write_new();
+ struct archive_write* aw = (struct archive_write *)a;
+ int cnt = 0;
+
+ archive_write_set_format_zip(a);
+
+ assertEqualInt(ARCHIVE_OK,
+ archive_write_set_passphrase_callback(a, &cnt, callback1));
+ /* Check a passphrase. */
+ assertEqualString("passCallBack", __archive_write_get_passphrase(aw));
+ assertEqualInt(1, cnt);
+ /* Callback function should be called just once. */
+ assertEqualString("passCallBack", __archive_write_get_passphrase(aw));
+ assertEqualInt(1, cnt);
+
+ archive_write_free(a);
+}