Fixed bug reported by Stuart Midgley about what happens when there are no
[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, 0);
12
13 if (rc == MEMCACHED_NO_SERVERS)
14 return rc;
15
16 if (rc != MEMCACHED_SUCCESS)
17 rc= MEMCACHED_SOME_ERRORS;
18
19 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
20 for (x= 0; x < ptr->number_of_hosts; x++)
21 {
22 if (expiration)
23 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
24 "flush_all %llu\r\n", (unsigned long long)expiration);
25 else
26 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
27 "flush_all\r\n");
28
29 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
30 return MEMCACHED_WRITE_FAILURE;
31
32 sent_length= memcached_io_write(ptr, x, buffer, send_length, 1);
33
34 if (sent_length == -1 || sent_length != send_length)
35 return MEMCACHED_WRITE_FAILURE;
36
37 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
38
39 if (rc != MEMCACHED_SUCCESS)
40 rc= MEMCACHED_SOME_ERRORS;
41 }
42
43 LIBMEMCACHED_MEMCACHED_FLUSH_END();
44 return rc;
45 }