Fixed bug where zero length key was provided.
[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 (key_length == 0)
15 return MEMCACHED_NO_KEY_PROVIDED;
16
17 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
18 return MEMCACHED_NO_SERVERS;
19
20 server_key= memcached_generate_hash(ptr, key, key_length);
21
22 if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
23 return rc;
24
25
26 if (expiration)
27 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
28 "delete %.*s %llu\r\n", (int)key_length, key,
29 (unsigned long long)expiration);
30 else
31 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
32 "delete %.*s\r\n", (int)key_length, key);
33
34 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
35 {
36 rc= MEMCACHED_WRITE_FAILURE;
37 goto error;
38 }
39
40 if ((ptr->flags & MEM_NO_BLOCK))
41 to_write= 0;
42 else
43 to_write= 1;
44
45 if ((sent_length= memcached_io_write(ptr, server_key, buffer, send_length, to_write)) == -1)
46 {
47 memcached_quit_server(ptr, server_key);
48 rc= MEMCACHED_WRITE_FAILURE;
49 goto error;
50 }
51
52 if ((ptr->flags & MEM_NO_BLOCK))
53 {
54 rc= MEMCACHED_SUCCESS;
55 memcached_server_response_increment(ptr, server_key);
56 }
57 else
58 {
59 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
60 if (rc == MEMCACHED_DELETED)
61 rc= MEMCACHED_SUCCESS;
62 }
63
64 LIBMEMCACHED_MEMCACHED_DELETE_END();
65
66 error:
67 return rc;
68 }