Commit 0d07107e authored by Martin Blix Grydeland's avatar Martin Blix Grydeland

Add VSL_Write* functions for writing binary log output.

parent a09e2e08
......@@ -279,6 +279,52 @@ int VSL_PrintTransactions(struct VSL_data *vsl,
* !=0: Return value from either VSL_Next or VSL_Print
*/
FILE *VSL_WriteOpen(struct VSL_data *vsl, const char *name, int append,
int unbuffered);
/*
* Open file name for writing using the VSL_Write* functions. If
* append is true, the file will be opened for appending.
*
* Arguments:
* vsl: The VSL data context
* name: The file name
* append: If true, the file will be appended instead of truncated
* unbuf: If true, use unbuffered mode
*
* Return values:
* NULL: Error - see VSL_Error
* non-NULL: Success
*/
int VSL_Write(struct VSL_data *vsl, const struct VSL_cursor *c, void *fo);
/*
* Write the currect record pointed to be c to the FILE* fo
*
* Return values:
* 0: Success
* -5: I/O error - see VSL_Error
*/
int VSL_WriteAll(struct VSL_data *vsl, struct VSL_cursor *c, void *fo);
/*
* Calls VSL_Next on c until c is exhausted. In turn calls
* VSL_Write on all records where VSL_Match returns true.
*
* Return values:
* 0: OK
* !=0: Return value from either VSL_Next or VSL_Write
*/
int VSL_WriteTransactions(struct VSL_data *vsl,
struct VSL_transaction *ptrans[], void *fo);
/*
* Write all transactions in ptrans using VSL_WriteAll
* Return values:
* 0: OK
* !=0: Return value from either VSL_Next or VSL_Write
*/
struct VSLQ *VSLQ_New(struct VSL_data *vsl, struct VSL_cursor **cp,
enum VSL_grouping_e grouping, const char *query);
/*
......
......@@ -106,6 +106,10 @@ LIBVARNISHAPI_1.3 {
VSL_PrintTerse;
VSL_PrintAll;
VSL_PrintTransactions;
VSL_WriteOpen;
VSL_Write;
VSL_WriteAll;
VSL_WriteTransactions;
VSLQ_New;
VSLQ_Delete;
VSLQ_Dispatch;
......
......@@ -320,3 +320,71 @@ VSL_PrintTransactions(struct VSL_data *vsl, struct VSL_transaction *pt[],
return (0);
}
FILE*
VSL_WriteOpen(struct VSL_data *vsl, const char *name, int append, int unbuf)
{
const char head[] = VSL_FILE_ID;
FILE* f;
f = fopen(name, append ? "a" : "w");
if (f == NULL) {
vsl_diag(vsl, "%s", strerror(errno));
return (NULL);
}
if (unbuf)
setbuf(f, NULL);
if (0 == ftell(f))
fwrite(head, 1, sizeof head, f);
return (f);
}
int
VSL_Write(struct VSL_data *vsl, const struct VSL_cursor *c, void *fo)
{
size_t r;
CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
if (c == NULL || c->rec.ptr == NULL)
return (0);
if (fo == NULL)
fo = stdout;
r = fwrite(c->rec.ptr, sizeof *c->rec.ptr,
VSL_NEXT(c->rec.ptr) - c->rec.ptr, fo);
if (r == 0)
return (-5);
return (0);
}
int
VSL_WriteAll(struct VSL_data *vsl, struct VSL_cursor *c, void *fo)
{
int i;
if (c == NULL)
return (0);
while (1) {
i = VSL_Next(c);
if (i <= 0)
return (i);
if (!VSL_Match(vsl, c))
continue;
i = VSL_Write(vsl, c, fo);
if (i != 0)
return (i);
}
}
int
VSL_WriteTransactions(struct VSL_data *vsl, struct VSL_transaction *pt[],
void *fo)
{
struct VSL_transaction *t;
int i;
if (pt == NULL)
return (0);
for (i = 0, t = pt[0]; i == 0 && t != NULL; t = *++pt)
i = VSL_WriteAll(vsl, t->c, fo);
return (i);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment