9b9c4baca91c3efb181961abef7ecf69a2aa6d62
[awesomized/libmemcached] / lib / memcached_flush.c
1 #include "common.h"
2
3 memcached_return memcached_flush(memcached_st *ptr, time_t expiration)
4 {
5 unsigned int x;
6 size_t send_length, sent_length;
7 memcached_return rc;
8 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
9 LIBMEMCACHED_MEMCACHED_FLUSH_START();
10
11 rc= memcached_connect(ptr);
12
13 if (rc != MEMCACHED_SUCCESS)
14 rc= MEMCACHED_SOME_ERRORS;
15
16 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
17 for (x= 0; x < ptr->number_of_hosts; x++)
18 {
19 if (expiration)
20 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
21 "flush_all %llu\r\n", (unsigned long long)expiration);
22 else
23 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
24 "flush_all\r\n");
25
26 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
27 return MEMCACHED_WRITE_FAILURE;
28
29 sent_length= memcached_io_write(ptr, x, buffer, send_length, 1);
30
31 if (sent_length == -1 || sent_length != send_length)
32 return MEMCACHED_WRITE_FAILURE;
33
34 if (ptr->flags & MEM_NO_BLOCK)
35 WATCHPOINT;
36 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
37
38 if (rc != MEMCACHED_SUCCESS)
39 rc= MEMCACHED_SOME_ERRORS;
40 }
41
42 LIBMEMCACHED_MEMCACHED_FLUSH_END();
43 return rc;
44 }