cppcheck: fix warnings
[awesomized/libmemcached] / libmemcached / touch.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39
40 static memcached_return_t ascii_touch(memcached_instance_st* instance,
41 const char *key, size_t key_length,
42 time_t expiration)
43 {
44 char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
45 int expiration_buffer_length= snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", (unsigned long long)expiration);
46 if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer)+1 or expiration_buffer_length < 0)
47 {
48 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
49 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
50 }
51
52 libmemcached_io_vector_st vector[]=
53 {
54 { NULL, 0 },
55 { memcached_literal_param("touch ") },
56 { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
57 { key, key_length },
58 { expiration_buffer, size_t(expiration_buffer_length) },
59 { memcached_literal_param("\r\n") }
60 };
61
62 memcached_return_t rc;
63 if (memcached_failed(rc= memcached_vdo(instance, vector, 6, true)))
64 {
65 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
66 }
67
68 return rc;
69 }
70
71 static memcached_return_t binary_touch(memcached_instance_st* instance,
72 const char *key, size_t key_length,
73 time_t expiration)
74 {
75 protocol_binary_request_touch request= {}; //{.bytes= {0}};
76
77 initialize_binary_request(instance, request.message.header);
78
79 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_TOUCH;
80 request.message.header.request.extlen= 4;
81 request.message.header.request.keylen= htons((uint16_t)(key_length +memcached_array_size(instance->root->_namespace)));
82 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
83 request.message.header.request.bodylen= htonl((uint32_t)(key_length +memcached_array_size(instance->root->_namespace) +request.message.header.request.extlen));
84 request.message.body.expiration= htonl((uint32_t) expiration);
85
86 libmemcached_io_vector_st vector[]=
87 {
88 { NULL, 0 },
89 { request.bytes, sizeof(request.bytes) },
90 { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
91 { key, key_length }
92 };
93
94 memcached_return_t rc;
95 if (memcached_failed(rc= memcached_vdo(instance, vector, 4, true)))
96 {
97 return memcached_set_error(*instance, MEMCACHED_WRITE_FAILURE, MEMCACHED_AT);
98 }
99
100 return rc;
101 }
102
103 memcached_return_t memcached_touch(memcached_st *ptr,
104 const char *key, size_t key_length,
105 time_t expiration)
106 {
107 return memcached_touch_by_key(ptr, key, key_length, key, key_length, expiration);
108 }
109
110 memcached_return_t memcached_touch_by_key(memcached_st *shell,
111 const char *group_key, size_t group_key_length,
112 const char *key, size_t key_length,
113 time_t expiration)
114 {
115 Memcached* ptr= memcached2Memcached(shell);
116 LIBMEMCACHED_MEMCACHED_TOUCH_START();
117
118 memcached_return_t rc;
119 if (memcached_failed(rc= initialize_query(ptr, true)))
120 {
121 return rc;
122 }
123
124 if (memcached_failed(rc= memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
125 {
126 return memcached_set_error(*ptr, rc, MEMCACHED_AT);
127 }
128
129 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
130 memcached_instance_st* instance= memcached_instance_fetch(ptr, server_key);
131
132 if (ptr->flags.binary_protocol)
133 {
134 rc= binary_touch(instance, key, key_length, expiration);
135 }
136 else
137 {
138 rc= ascii_touch(instance, key, key_length, expiration);
139 }
140
141 if (memcached_failed(rc))
142 {
143 return memcached_set_error(*instance, rc, MEMCACHED_AT, memcached_literal_param("Error occcured while writing touch command to server"));
144 }
145
146 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
147 rc= memcached_response(instance, buffer, sizeof(buffer), NULL);
148
149 if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_NOTFOUND)
150 {
151 return rc;
152 }
153
154 return memcached_set_error(*instance, rc, MEMCACHED_AT, memcached_literal_param("Error occcured while reading response"));
155 }