1 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2  *
3  *  Libmemcached library
4  *
5  *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions are
9  *  met:
10  *
11  *      * Redistributions of source code must retain the above copyright
12  *  notice, this list of conditions and the following disclaimer.
13  *
14  *      * Redistributions in binary form must reproduce the above
15  *  copyright notice, this list of conditions and the following disclaimer
16  *  in the documentation and/or other materials provided with the
17  *  distribution.
18  *
19  *      * The names of its contributors may not be used to endorse or
20  *  promote products derived from this software without specific prior
21  *  written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 
37 #include <libmemcached/common.h>
38 
memcached_flush_binary(Memcached * ptr,time_t expiration,const bool reply)39 static memcached_return_t memcached_flush_binary(Memcached *ptr,
40                                                  time_t expiration,
41                                                  const bool reply)
42 {
43   protocol_binary_request_flush request= {};
44 
45   request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
46   request.message.header.request.extlen= 4;
47   request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
48   request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
49   request.message.body.expiration= htonl((uint32_t) expiration);
50 
51   memcached_return_t rc= MEMCACHED_SUCCESS;
52 
53   for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
54   {
55     memcached_instance_st* instance= memcached_instance_fetch(ptr, x);
56     initialize_binary_request(instance, request.message.header);
57 
58     if (reply)
59     {
60       request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
61     }
62     else
63     {
64       request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
65     }
66 
67     libmemcached_io_vector_st vector[]=
68     {
69       { NULL, 0 },
70       { request.bytes, sizeof(request.bytes) }
71     };
72 
73     memcached_return_t rrc;
74     if (memcached_failed(rrc= memcached_vdo(instance, vector, 2, true)))
75     {
76       if (instance->error_messages == NULL or instance->root->error_messages == NULL)
77       {
78         memcached_set_error(*instance, rrc, MEMCACHED_AT);
79       }
80       memcached_io_reset(instance);
81       rc= MEMCACHED_SOME_ERRORS;
82     }
83   }
84 
85   for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
86   {
87     memcached_instance_st* instance= memcached_instance_fetch(ptr, x);
88 
89     if (instance->response_count() > 0)
90     {
91       (void)memcached_response(instance, NULL, 0, NULL);
92     }
93   }
94 
95   return rc;
96 }
97 
memcached_flush_textual(Memcached * ptr,time_t expiration,const bool reply)98 static memcached_return_t memcached_flush_textual(Memcached *ptr,
99                                                   time_t expiration,
100                                                   const bool reply)
101 {
102   char buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
103   int send_length= 0;
104   if (expiration)
105   {
106     send_length= snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long)expiration);
107   }
108 
109   if (size_t(send_length) >= sizeof(buffer) or send_length < 0)
110   {
111     return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
112                                memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
113   }
114 
115   memcached_return_t rc= MEMCACHED_SUCCESS;
116   for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
117   {
118     memcached_instance_st* instance= memcached_instance_fetch(ptr, x);
119 
120     libmemcached_io_vector_st vector[]=
121     {
122       { NULL, 0 },
123       { memcached_literal_param("flush_all ") },
124       { buffer, size_t(send_length) },
125       { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
126       { memcached_literal_param("\r\n") }
127     };
128 
129     memcached_return_t rrc= memcached_vdo(instance, vector, 5, true);
130     if (memcached_success(rrc) and reply == true)
131     {
132       char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
133       rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
134     }
135 
136     if (memcached_failed(rrc))
137     {
138       // If an error has already been reported, then don't add to it
139       if (instance->error_messages == NULL or instance->root->error_messages == NULL)
140       {
141         memcached_set_error(*instance, rrc, MEMCACHED_AT);
142       }
143       rc= MEMCACHED_SOME_ERRORS;
144     }
145   }
146 
147   return rc;
148 }
149 
memcached_flush(memcached_st * shell,time_t expiration)150 memcached_return_t memcached_flush(memcached_st *shell, time_t expiration)
151 {
152   Memcached* ptr= memcached2Memcached(shell);
153   memcached_return_t rc;
154   if (memcached_failed(rc= initialize_query(ptr, true)))
155   {
156     return rc;
157   }
158 
159   bool reply= memcached_is_replying(ptr);
160 
161   LIBMEMCACHED_MEMCACHED_FLUSH_START();
162   if (memcached_is_binary(ptr))
163   {
164     rc= memcached_flush_binary(ptr, expiration, reply);
165   }
166   else
167   {
168     rc= memcached_flush_textual(ptr, expiration, reply);
169   }
170   LIBMEMCACHED_MEMCACHED_FLUSH_END();
171 
172   return rc;
173 }
174