Fixed bug reported by Stuart Midgley about what happens when there are no
[awesomized/libmemcached] / lib / memcached_delete.c
1 #include "common.h"
2
3 memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length,
4 time_t expiration)
5 {
6 char to_write;
7 size_t send_length, sent_length;
8 memcached_return rc;
9 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
10 unsigned int server_key;
11
12 LIBMEMCACHED_MEMCACHED_DELETE_START();
13
14 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
15 return MEMCACHED_NO_SERVERS;
16
17 server_key= memcached_generate_hash(ptr, key, key_length);
18
19 if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
20 return rc;
21
22
23 if (expiration)
24 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
25 "delete %.*s %llu\r\n", (int)key_length, key,
26 (unsigned long long)expiration);
27 else
28 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
29 "delete %.*s\r\n", (int)key_length, key);
30
31 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
32 {
33 rc= MEMCACHED_WRITE_FAILURE;
34 goto error;
35 }
36
37 if ((ptr->flags & MEM_NO_BLOCK))
38 to_write= 0;
39 else
40 to_write= 1;
41
42 if ((sent_length= memcached_io_write(ptr, server_key, buffer, send_length, to_write)) == -1)
43 {
44 memcached_quit_server(ptr, server_key);
45 rc= MEMCACHED_WRITE_FAILURE;
46 goto error;
47 }
48
49 if ((ptr->flags & MEM_NO_BLOCK))
50 {
51 rc= MEMCACHED_SUCCESS;
52 memcached_server_response_increment(ptr, server_key);
53 }
54 else
55 {
56 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
57 if (rc == MEMCACHED_DELETED)
58 rc= MEMCACHED_SUCCESS;
59 }
60
61 LIBMEMCACHED_MEMCACHED_DELETE_END();
62
63 error:
64 return rc;
65 }