Test cases now pass
[m6w6/libmemcached] / lib / memcached_get.c
1 #include <memcached.h>
2
3 char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
4 size_t *value_length,
5 uint16_t *flags,
6 memcached_return *error)
7 {
8 size_t send_length;
9 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
10 char *string_ptr;
11 unsigned int server_key;
12
13 *value_length= 0;
14 *error= memcached_connect(ptr);
15
16 if (*error != MEMCACHED_SUCCESS)
17 return NULL;
18
19 server_key= memcached_generate_hash(key, key_length) % ptr->number_of_hosts;
20
21 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
22 key_length, key);
23 if (*error != MEMCACHED_SUCCESS)
24 return NULL;
25
26 if ((send(ptr->hosts[server_key].fd, buffer, send_length, 0) == -1))
27 {
28 fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key);
29 *error= MEMCACHED_WRITE_FAILURE;
30 return NULL;
31 }
32
33 memset(buffer, 0, MEMCACHED_DEFAULT_COMMAND_SIZE);
34 *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
35
36 if (*error == MEMCACHED_SUCCESS)
37 {
38 char *end_ptr;
39
40 string_ptr= buffer;
41 string_ptr+= 6; /* "VALUE " */
42
43 /* We do nothing with the key, since we only asked for one key */
44 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
45
46 /* Flags fetch */
47 string_ptr= end_ptr + 1;
48 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
49 *flags= (uint16_t)strtol(string_ptr, &end_ptr, 10);
50
51 /* Length fetch */
52 string_ptr= end_ptr + 1;
53 for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
54 *value_length= strtoll(string_ptr, &end_ptr, 10);
55
56 /* Skip past the \r\n */
57 string_ptr= end_ptr +2;
58
59 if (*value_length)
60 {
61 size_t need_to_copy;
62 char *pos_ptr;
63 char *value;
64
65 value= (char *)malloc(*value_length * sizeof(char));
66
67 if (!value)
68 {
69 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
70 return NULL;
71 }
72
73 need_to_copy= (*value_length < (size_t)(buffer-string_ptr))
74 ? *value_length
75 : (size_t)(buffer-string_ptr) ;
76
77 pos_ptr= memcpy(value, string_ptr, need_to_copy);
78
79 if ( need_to_copy > *value_length)
80 {
81 size_t read_length;
82 size_t need_to_read;
83
84 need_to_read= *value_length - need_to_copy;
85
86 read_length= read(ptr->hosts[server_key].fd, pos_ptr, need_to_read);
87 if (read_length != need_to_read)
88 {
89 free(value);
90 *error= MEMCACHED_PARTIAL_READ;
91
92 return NULL;
93 }
94 }
95
96 return value;
97 }
98 }
99
100 return NULL;
101 }