Skip to content
Browse files

put win32,posix,standard all in env.c, minor fixes, and astyled

  • Loading branch information...
1 parent 19d14fd commit 3a849c0c4e42a613f12485a7c5572cea89ab5e19 @gerald-lindsly gerald-lindsly committed
Showing with 868 additions and 848 deletions.
  1. +3 −5 SConstruct
  2. +56 −56 src/bcon.c
  3. +12 −9 src/bson.c
  4. +11 −11 src/bson.h
  5. +7 −7 src/encoding.c
  6. +554 −0 src/env.c
  7. +0 −200 src/env_posix.c
  8. +0 −168 src/env_standard.c
  9. +0 −178 src/env_win32.c
  10. +19 −14 src/gridfs.c
  11. +6 −6 src/gridfs.h
  12. +92 −95 src/md5.c
  13. +14 −14 src/md5.h
  14. +76 −67 src/mongo.c
  15. +18 −18 src/mongo.h
View
8 SConstruct
@@ -86,16 +86,14 @@ env.AlwaysBuild("docs")
# ---- Platforms ----
PLATFORM_TESTS = []
+NET_LIB = "src/env.c"
if GetOption('standard_env'):
- NET_LIB = "src/env_standard.c"
+ env.Append( CPPFLAGS=" -D_POSIX_SOURCE" )
elif os.sys.platform in ["darwin", "linux2"]:
- NET_LIB = "src/env_posix.c"
PLATFORM_TESTS = [ "env_posix", "unix_socket" ]
elif 'win32' == os.sys.platform:
- NET_LIB = "src/env_win32.c"
PLATFORM_TESTS = [ "env_win32" ]
-else:
- NET_LIB = "src/env_standard.c"
+
# ---- Libraries ----
if os.sys.platform in ["darwin", "linux2"]:
View
112 src/bcon.c
@@ -46,8 +46,8 @@ bcon_token_t bcon_token(char *s) {
if (s == 0) return Token_EOD;
switch (s[0]) {
case ':': if (s[1] != '\0' && s[2] != '\0' && s[3] != '\0' && s[4] == '\0' &&
- s[3] == ':' && (s[1] == '_' || s[1] == 'P' || s[1] == 'R'))
- return Token_Typespec; break;
+ s[3] == ':' && (s[1] == '_' || s[1] == 'P' || s[1] == 'R'))
+ return Token_Typespec; break;
case '{': if (s[1] == '\0') return Token_OpenBrace; break;
case '}': if (s[1] == '\0') return Token_CloseBrace; break;
case '[': if (s[1] == '\0') return Token_OpenBracket; break;
@@ -69,14 +69,14 @@ bcon_error_t bson_bcon_key_value(bson *b, const char *key, const char *typespec,
case 's': bson_append_string( b, key, bci.s ); break; /* common case */
case 'f': bson_append_double( b, key, bci.f ); break;
case 'D':
- bson_append_start_object( b, key );
- ret = bson_append_bcon( b, bci.D );
- bson_append_finish_object( b );
+ bson_append_start_object( b, key );
+ ret = bson_append_bcon( b, bci.D );
+ bson_append_finish_object( b );
break;
case 'A':
- bson_append_start_array( b, key );
- ret = bson_append_bcon_array( b, bci.A );
- bson_append_finish_array( b );
+ bson_append_start_array( b, key );
+ ret = bson_append_bcon_array( b, bci.A );
+ bson_append_finish_array( b );
break;
case 'o': if (*bci.o == '\0') bson_oid_gen( &oid ); else bson_oid_from_string( &oid, bci.o ); bson_append_oid( b, key, &oid ); break;
case 'b': bson_append_bool( b, key, bci.b ); break;
@@ -93,14 +93,14 @@ bcon_error_t bson_bcon_key_value(bson *b, const char *key, const char *typespec,
case 'f': bson_append_double( b, key, *bci.Rf ); break;
case 's': bson_append_string( b, key, bci.Rs ); break;
case 'D':
- bson_append_start_object( b, key );
- ret = bson_append_bcon( b, bci.RD );
- bson_append_finish_object( b );
+ bson_append_start_object( b, key );
+ ret = bson_append_bcon( b, bci.RD );
+ bson_append_finish_object( b );
break;
case 'A':
- bson_append_start_array( b, key );
- ret = bson_append_bcon_array( b, bci.RA );
- bson_append_finish_array( b );
+ bson_append_start_array( b, key );
+ ret = bson_append_bcon_array( b, bci.RA );
+ bson_append_finish_array( b );
break;
case 'o': if (*bci.o == '\0') bson_oid_gen( &oid ); else bson_oid_from_string( &oid, bci.o ); bson_append_oid( b, key, &oid ); break;
case 'b': bson_append_bool( b, key, *bci.Rb ); break;
@@ -117,19 +117,19 @@ bcon_error_t bson_bcon_key_value(bson *b, const char *key, const char *typespec,
case 'f': bson_append_double( b, key, **bci.Pf ); break;
case 's': bson_append_string( b, key, *bci.Ps ); break;
case 'D':
- bson_append_start_object( b, key );
- ret = bson_append_bcon( b, *bci.PD );
- bson_append_finish_object( b );
+ bson_append_start_object( b, key );
+ ret = bson_append_bcon( b, *bci.PD );
+ bson_append_finish_object( b );
break;
case 'A':
- bson_append_start_array( b, key );
- ret = bson_append_bcon_array( b, *bci.PA );
- bson_append_finish_array( b );
+ bson_append_start_array( b, key );
+ ret = bson_append_bcon_array( b, *bci.PA );
+ bson_append_finish_array( b );
break;
case 'o': if (**bci.Po == '\0') bson_oid_gen( &oid );
- else bson_oid_from_string( &oid, *bci.Po );
- bson_append_oid( b, key, &oid );
- break;
+ else bson_oid_from_string( &oid, *bci.Po );
+ bson_append_oid( b, key, &oid );
+ break;
case 'b': bson_append_bool( b, key, **bci.Pb ); break;
case 't': bson_append_time_t( b, key, **bci.Pt ); break;
case 'x': if (*bci.Px != 0) bson_append_symbol( b, key, *bci.Px ); break;
@@ -383,41 +383,41 @@ void bcon_json_print(bcon *bc, int n) {
bcon *bcp;
putchar('{');
for (end_of_data = 0, bcp = bc; !end_of_data; bcp++) {
- bcon bci = *bcp;
- switch (t) {
- case 'l':
- if (key_value_count & 0x1) putchar(':');
- printf("%ld", bci.l);
- t = 0;
- key_value_count++;
- break;
- case 's': /* fall through */
- default:
- s = bci.s;
- switch (*s) {
- case ':':
- ++s;
- t = *++s;
- break;
- case '{':
- if (key_value_count & 0x1) putchar(':');
- putchar(*s);
- key_value_count = 0;
- break;
- case '}':
- putchar(*s);
- key_value_count = 2;
- break;
+ bcon bci = *bcp;
+ switch (t) {
+ case 'l':
+ if (key_value_count & 0x1) putchar(':');
+ printf("%ld", bci.l);
+ t = 0;
+ key_value_count++;
+ break;
+ case 's': /* fall through */
default:
- if (key_value_count & 0x1) putchar(':');
- else if (key_value_count > 1) putchar(',');
- printf("\"%s\"", s);
- t = 0;
- key_value_count++;
- break;
+ s = bci.s;
+ switch (*s) {
+ case ':':
+ ++s;
+ t = *++s;
+ break;
+ case '{':
+ if (key_value_count & 0x1) putchar(':');
+ putchar(*s);
+ key_value_count = 0;
+ break;
+ case '}':
+ putchar(*s);
+ key_value_count = 2;
+ break;
+ default:
+ if (key_value_count & 0x1) putchar(':');
+ else if (key_value_count > 1) putchar(',');
+ printf("\"%s\"", s);
+ t = 0;
+ key_value_count++;
+ break;
+ }
+ break;
}
- break;
- }
}
putchar('}');
}
View
21 src/bson.c
@@ -53,11 +53,11 @@ static int ( *oid_inc_func )( void ) = NULL;
------------------------------ */
MONGO_EXPORT bson* bson_create( void ) {
- return (bson*)bson_malloc(sizeof(bson));
+ return (bson*)bson_malloc(sizeof(bson));
}
MONGO_EXPORT void bson_dispose(bson* b) {
- bson_free(b);
+ bson_free(b);
}
MONGO_EXPORT bson *bson_empty( bson *obj ) {
@@ -527,7 +527,8 @@ MONGO_EXPORT void bson_iterator_code_scope( const bson_iterator *i, bson *scope
bson_init_data( scope, ( void * )( bson_iterator_value( i )+8+code_len ) );
_bson_reset( scope );
scope->finished = 1;
- } else {
+ }
+ else {
bson_empty( scope );
}
}
@@ -741,7 +742,7 @@ MONGO_EXPORT int bson_append_undefined( bson *b, const char *name ) {
}
static int bson_append_string_base( bson *b, const char *name,
- const char *value, int len, bson_type type ) {
+ const char *value, int len, bson_type type ) {
int sl = len + 1;
if ( bson_check_string( b, ( const char * )value, sl - 1 ) == BSON_ERROR )
@@ -780,7 +781,7 @@ MONGO_EXPORT int bson_append_code_n( bson *b, const char *name, const char *valu
}
MONGO_EXPORT int bson_append_code_w_scope_n( bson *b, const char *name,
- const char *code, int len, const bson *scope ) {
+ const char *code, int len, const bson *scope ) {
int sl, size;
if ( !scope ) return BSON_ERROR;
@@ -808,7 +809,8 @@ MONGO_EXPORT int bson_append_binary( bson *b, const char *name, char type, const
bson_append_byte( b, type );
bson_append32( b, &len );
bson_append( b, str, len );
- } else {
+ }
+ else {
if ( bson_append_estart( b, BSON_BINDATA, name, 4+1+len ) == BSON_ERROR )
return BSON_ERROR;
bson_append32( b, &len );
@@ -862,7 +864,8 @@ MONGO_EXPORT int bson_append_element( bson *b, const char *name_or_null, const b
if( bson_ensure_space( b, size ) == BSON_ERROR )
return BSON_ERROR;
bson_append( b, elem->cur, size );
- } else {
+ }
+ else {
int data_size = size - 2 - strlen( bson_iterator_key( elem ) );
bson_append_estart( b, elem->cur[0], name_or_null, data_size );
bson_append( b, bson_iterator_value( elem ), data_size );
@@ -926,7 +929,7 @@ MONGO_EXPORT int bson_append_finish_object( bson *b ) {
}
MONGO_EXPORT double bson_int64_to_double( int64_t i64 ) {
- return (double)i64;
+ return (double)i64;
}
MONGO_EXPORT int bson_append_finish_array( bson *b ) {
@@ -963,7 +966,7 @@ void *bson_realloc( void *ptr, int size ) {
int _bson_errprintf( const char *format, ... ) {
va_list ap;
- int ret;
+ int ret = 0;
va_start( ap, format );
#ifndef R_SAFETY_NET
ret = vfprintf( stderr, format, ap );
View
22 src/bson.h
@@ -22,23 +22,23 @@
#define BSON_H_
#include <time.h>
-#include <stdlib.h>
#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
#include <stdarg.h>
#ifdef __GNUC__
- #define MONGO_INLINE static __inline__
- #define MONGO_EXPORT
+#define MONGO_INLINE static __inline__
+#define MONGO_EXPORT
#else
- #define MONGO_INLINE static
- #ifdef MONGO_STATIC_BUILD
- #define MONGO_EXPORT
- #elif defined(MONGO_DLL_BUILD)
- #define MONGO_EXPORT __declspec(dllexport)
- #else
- #define MONGO_EXPORT __declspec(dllimport)
- #endif
+#define MONGO_INLINE static
+#ifdef MONGO_STATIC_BUILD
+#define MONGO_EXPORT
+#elif defined(MONGO_DLL_BUILD)
+#define MONGO_EXPORT __declspec(dllexport)
+#else
+#define MONGO_EXPORT __declspec(dllimport)
+#endif
#endif
#ifdef __cplusplus
View
14 src/encoding.c
@@ -106,17 +106,17 @@ static int bson_string_is_db_ref( const unsigned char *string, const int length
int result = 0;
if( length >= 4 ) {
- if( string[1] == 'r' && string[2] == 'e' && string[3] == 'f' )
- result = 1;
+ if( string[1] == 'r' && string[2] == 'e' && string[3] == 'f' )
+ result = 1;
}
else if( length >= 3 ) {
- if( string[1] == 'i' && string[2] == 'd' )
- result = 1;
- else if( string[1] == 'd' && string[2] == 'b' )
- result = 1;
+ if( string[1] == 'i' && string[2] == 'd' )
+ result = 1;
+ else if( string[1] == 'd' && string[2] == 'b' )
+ result = 1;
}
- return result;
+ return result;
}
static int bson_validate_string( bson *b, const unsigned char *string,
View
554 src/env.c
@@ -0,0 +1,554 @@
+#if !defined(MONGO_ENV_STANDARD) && (defined(_WIN32) || defined(_WIN64))
+
+/* env_win32.c */
+
+/* Copyright 2009-2012 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Networking and other niceties for WIN32. */
+#include "env.h"
+#include <string.h>
+
+#ifdef _MSC_VER
+#include <ws2tcpip.h> /* send,recv,socklen_t etc */
+#include <wspiapi.h> /* addrinfo */
+#else
+#include <ws2tcpip.h> /* send,recv,socklen_t etc */
+#include <winsock2.h>
+typedef int socklen_t;
+#endif
+
+#ifndef NI_MAXSERV
+# define NI_MAXSERV 32
+#endif
+
+int mongo_env_close_socket( int socket ) {
+ return closesocket( socket );
+}
+
+int mongo_env_write_socket( mongo *conn, const void *buf, int len ) {
+ const char *cbuf = buf;
+ int flags = 0;
+
+ while ( len ) {
+ int sent = send( conn->sock, cbuf, len, flags );
+ if ( sent == -1 ) {
+ __mongo_set_error( conn, MONGO_IO_ERROR, NULL, WSAGetLastError() );
+ conn->connected = 0;
+ return MONGO_ERROR;
+ }
+ cbuf += sent;
+ len -= sent;
+ }
+
+ return MONGO_OK;
+}
+
+int mongo_env_read_socket( mongo *conn, void *buf, int len ) {
+ char *cbuf = buf;
+
+ while ( len ) {
+ int sent = recv( conn->sock, cbuf, len, 0 );
+ if ( sent == 0 || sent == -1 ) {
+ __mongo_set_error( conn, MONGO_IO_ERROR, NULL, WSAGetLastError() );
+ return MONGO_ERROR;
+ }
+ cbuf += sent;
+ len -= sent;
+ }
+
+ return MONGO_OK;
+}
+
+int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
+ if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&millis,
+ sizeof( millis ) ) == -1 ) {
+ __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_RCVTIMEO failed.",
+ WSAGetLastError() );
+ return MONGO_ERROR;
+ }
+
+ if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&millis,
+ sizeof( millis ) ) == -1 ) {
+ __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_SNDTIMEO failed.",
+ WSAGetLastError() );
+ return MONGO_ERROR;
+ }
+
+ return MONGO_OK;
+}
+
+int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
+ char port_str[NI_MAXSERV];
+ char errstr[MONGO_ERR_LEN];
+ int status;
+
+ struct addrinfo ai_hints;
+ struct addrinfo *ai_list = NULL;
+ struct addrinfo *ai_ptr = NULL;
+
+ conn->sock = 0;
+ conn->connected = 0;
+
+ bson_sprintf( port_str, "%d", port );
+
+ memset( &ai_hints, 0, sizeof( ai_hints ) );
+ ai_hints.ai_family = AF_UNSPEC;
+ ai_hints.ai_socktype = SOCK_STREAM;
+ ai_hints.ai_protocol = IPPROTO_TCP;
+
+ status = getaddrinfo( host, port_str, &ai_hints, &ai_list );
+ if ( status != 0 ) {
+ bson_sprintf( errstr, "getaddrinfo failed with error %d", status );
+ __mongo_set_error( conn, MONGO_CONN_ADDR_FAIL, errstr, WSAGetLastError() );
+ return MONGO_ERROR;
+ }
+
+ for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) {
+ conn->sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype,
+ ai_ptr->ai_protocol );
+
+ if ( conn->sock < 0 ) {
+ __mongo_set_error( conn, MONGO_SOCKET_ERROR, "socket() failed",
+ WSAGetLastError() );
+ conn->sock = 0;
+ continue;
+ }
+
+ status = connect( conn->sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
+ if ( status != 0 ) {
+ __mongo_set_error( conn, MONGO_SOCKET_ERROR, "connect() failed",
+ WSAGetLastError() );
+ mongo_env_close_socket( conn->sock );
+ conn->sock = 0;
+ continue;
+ }
+
+ if ( ai_ptr->ai_protocol == IPPROTO_TCP ) {
+ int flag = 1;
+
+ setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY,
+ ( void * ) &flag, sizeof( flag ) );
+
+ if ( conn->op_timeout_ms > 0 )
+ mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms );
+ }
+
+ conn->connected = 1;
+ break;
+ }
+
+ freeaddrinfo( ai_list );
+
+ if ( ! conn->connected ) {
+ conn->err = MONGO_CONN_FAIL;
+ return MONGO_ERROR;
+ }
+ else {
+ mongo_clear_errors( conn );
+ return MONGO_OK;
+ }
+}
+
+MONGO_EXPORT int mongo_env_sock_init( void ) {
+
+ WSADATA wsaData;
+ WORD wVers;
+ static int called_once;
+ static int retval;
+
+ if (called_once) return retval;
+
+ called_once = 1;
+ wVers = MAKEWORD(1, 1);
+ retval = (WSAStartup(wVers, &wsaData) == 0);
+
+ return retval;
+}
+
+
+#elif !defined(MONGO_ENV_STANDARD) && (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix))
+
+/* env_posix.c */
+
+/* Copyright 2009-2012 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Networking and other niceties for POSIX systems. */
+#include "env.h"
+#include <string.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#ifndef NI_MAXSERV
+# define NI_MAXSERV 32
+#endif
+
+int mongo_env_close_socket( int socket ) {
+ return close( socket );
+}
+
+int mongo_env_sock_init( void ) {
+ return 0;
+}
+
+int mongo_env_write_socket( mongo *conn, const void *buf, int len ) {
+ const char *cbuf = buf;
+#ifdef __APPLE__
+ int flags = 0;
+#else
+ int flags = MSG_NOSIGNAL;
+#endif
+
+ while ( len ) {
+ int sent = send( conn->sock, cbuf, len, flags );
+ if ( sent == -1 ) {
+ if (errno == EPIPE)
+ conn->connected = 0;
+ __mongo_set_error( conn, MONGO_IO_ERROR, strerror( errno ), errno );
+ return MONGO_ERROR;
+ }
+ cbuf += sent;
+ len -= sent;
+ }
+
+ return MONGO_OK;
+}
+
+int mongo_env_read_socket( mongo *conn, void *buf, int len ) {
+ char *cbuf = buf;
+ while ( len ) {
+ int sent = recv( conn->sock, cbuf, len, 0 );
+ if ( sent == 0 || sent == -1 ) {
+ __mongo_set_error( conn, MONGO_IO_ERROR, strerror( errno ), errno );
+ return MONGO_ERROR;
+ }
+ cbuf += sent;
+ len -= sent;
+ }
+
+ return MONGO_OK;
+}
+
+int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
+ struct timeval tv;
+ tv.tv_sec = millis / 1000;
+ tv.tv_usec = ( millis % 1000 ) * 1000;
+
+ if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof( tv ) ) == -1 ) {
+ conn->err = MONGO_IO_ERROR;
+ __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_RCVTIMEO failed.", errno );
+ return MONGO_ERROR;
+ }
+
+ if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof( tv ) ) == -1 ) {
+ __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_SNDTIMEO failed.", errno );
+ return MONGO_ERROR;
+ }
+
+ return MONGO_OK;
+}
+
+static int mongo_env_unix_socket_connect( mongo *conn, const char *sock_path ) {
+ struct sockaddr_un addr;
+ int status, len;
+
+ conn->connected = 0;
+
+ conn->sock = socket( AF_UNIX, SOCK_STREAM, 0 );
+
+ if ( conn->sock < 0 ) {
+ conn->sock = 0;
+ return MONGO_ERROR;
+ }
+
+ addr.sun_family = AF_UNIX;
+ strncpy( addr.sun_path, sock_path, sizeof(addr.sun_path) - 1 );
+ len = sizeof( addr );
+
+ status = connect( conn->sock, (struct sockaddr *) &addr, len );
+ if( status < 0 ) {
+ mongo_env_close_socket( conn->sock );
+ conn->sock = 0;
+ conn->err = MONGO_CONN_FAIL;
+ return MONGO_ERROR;
+ }
+
+ conn->connected = 1;
+
+ return MONGO_OK;
+}
+
+int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
+ char port_str[NI_MAXSERV];
+ int status;
+
+ struct addrinfo ai_hints;
+ struct addrinfo *ai_list = NULL;
+ struct addrinfo *ai_ptr = NULL;
+
+ if ( port < 0 ) {
+ return mongo_env_unix_socket_connect( conn, host );
+ }
+
+ conn->sock = 0;
+ conn->connected = 0;
+ sprintf(port_str,"%d",port);
+
+ bson_sprintf( port_str, "%d", port );
+
+ memset( &ai_hints, 0, sizeof( ai_hints ) );
+#ifdef AI_ADDRCONFIG
+ ai_hints.ai_flags = AI_ADDRCONFIG;
+#endif
+ ai_hints.ai_family = AF_UNSPEC;
+ ai_hints.ai_socktype = SOCK_STREAM;
+
+ status = getaddrinfo( host, port_str, &ai_hints, &ai_list );
+ if ( status != 0 ) {
+ bson_errprintf( "getaddrinfo failed: %s", gai_strerror( status ) );
+ conn->err = MONGO_CONN_ADDR_FAIL;
+ return MONGO_ERROR;
+ }
+
+ for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) {
+ conn->sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol );
+ if ( conn->sock < 0 ) {
+ conn->sock = 0;
+ continue;
+ }
+
+ status = connect( conn->sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
+ if ( status != 0 ) {
+ mongo_env_close_socket( conn->sock );
+ conn->sock = 0;
+ continue;
+ }
+
+ if ( ai_ptr->ai_protocol == IPPROTO_TCP ) {
+ int flag = 1;
+
+ setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY,
+ ( void * ) &flag, sizeof( flag ) );
+ if ( conn->op_timeout_ms > 0 )
+ mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms );
+ }
+
+ conn->connected = 1;
+ break;
+ }
+
+ freeaddrinfo( ai_list );
+
+ if ( ! conn->connected ) {
+ conn->err = MONGO_CONN_FAIL;
+ return MONGO_ERROR;
+ }
+
+ return MONGO_OK;
+}
+
+#else
+/* env_standard.c */
+
+/* Copyright 2009-2012 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* Vanilla networking designed to work on all systems. */
+#include "env.h"
+#include <errno.h>
+#include <string.h>
+
+#ifdef _WIN32
+#ifdef _MSC_VER
+#include <ws2tcpip.h> /* send,recv,socklen_t etc */
+#include <wspiapi.h> /* addrinfo */
+#else
+#include <windows.h>
+#include <winsock.h>
+typedef int socklen_t;
+#endif
+#else
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <fcntl.h>
+#include <unistd.h>
+#endif
+
+#ifndef NI_MAXSERV
+# define NI_MAXSERV 32
+#endif
+
+int mongo_env_close_socket( int socket ) {
+#ifdef _WIN32
+ return closesocket( socket );
+#else
+ return close( socket );
+#endif
+}
+
+int mongo_env_write_socket( mongo *conn, const void *buf, int len ) {
+ const char *cbuf = buf;
+#ifdef _WIN32
+ int flags = 0;
+#else
+#ifdef __APPLE__
+ int flags = 0;
+#else
+ int flags = MSG_NOSIGNAL;
+#endif
+#endif
+
+ while ( len ) {
+ int sent = send( conn->sock, cbuf, len, flags );
+ if ( sent == -1 ) {
+ if (errno == EPIPE)
+ conn->connected = 0;
+ conn->err = MONGO_IO_ERROR;
+ return MONGO_ERROR;
+ }
+ cbuf += sent;
+ len -= sent;
+ }
+
+ return MONGO_OK;
+}
+
+int mongo_env_read_socket( mongo *conn, void *buf, int len ) {
+ char *cbuf = buf;
+ while ( len ) {
+ int sent = recv( conn->sock, cbuf, len, 0 );
+ if ( sent == 0 || sent == -1 ) {
+ conn->err = MONGO_IO_ERROR;
+ return MONGO_ERROR;
+ }
+ cbuf += sent;
+ len -= sent;
+ }
+
+ return MONGO_OK;
+}
+
+/* This is a no-op in the generic implementation. */
+int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
+ return MONGO_OK;
+}
+
+int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
+ struct sockaddr_in sa;
+ socklen_t addressSize;
+ int flag = 1;
+
+ if ( ( conn->sock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {
+ conn->sock = 0;
+ conn->err = MONGO_CONN_NO_SOCKET;
+ return MONGO_ERROR;
+ }
+
+ memset( sa.sin_zero , 0 , sizeof( sa.sin_zero ) );
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons( port );
+ sa.sin_addr.s_addr = inet_addr( host );
+ addressSize = sizeof( sa );
+
+ if ( connect( conn->sock, ( struct sockaddr * )&sa, addressSize ) == -1 ) {
+ mongo_env_close_socket( conn->sock );
+ conn->connected = 0;
+ conn->sock = 0;
+ conn->err = MONGO_CONN_FAIL;
+ return MONGO_ERROR;
+ }
+
+ setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * ) &flag, sizeof( flag ) );
+
+ if( conn->op_timeout_ms > 0 )
+ mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms );
+
+ conn->connected = 1;
+
+ return MONGO_OK;
+}
+
+MONGO_EXPORT int mongo_env_sock_init( void ) {
+
+#if defined(_WIN32)
+ WSADATA wsaData;
+ WORD wVers;
+#elif defined(SIGPIPE)
+ struct sigaction act;
+#endif
+
+ static int called_once;
+ static int retval;
+ if (called_once) return retval;
+ called_once = 1;
+
+#if defined(_WIN32)
+ wVers = MAKEWORD(1, 1);
+ retval = (WSAStartup(wVers, &wsaData) == 0);
+#elif defined(MACINTOSH)
+ GUSISetup(GUSIwithInternetSockets);
+ retval = 1;
+#elif defined(SIGPIPE)
+ retval = 1;
+ if (sigaction(SIGPIPE, (struct sigaction *)NULL, &act) < 0)
+ retval = 0;
+ else if (act.sa_handler == SIG_DFL) {
+ act.sa_handler = SIG_IGN;
+ if (sigaction(SIGPIPE, &act, (struct sigaction *)NULL) < 0)
+ retval = 0;
+ }
+#endif
+ return retval;
+}
+
+#endif
View
200 src/env_posix.c
@@ -1,200 +0,0 @@
-/* env_posix.c */
-
-/* Copyright 2009-2012 10gen Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Networking and other niceties for POSIX systems. */
-#include "env.h"
-#include "mongo.h"
-#include <string.h>
-#include <errno.h>
-#include <sys/time.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#ifndef NI_MAXSERV
-# define NI_MAXSERV 32
-#endif
-
-int mongo_env_close_socket( int socket ) {
- return close( socket );
-}
-
-int mongo_env_sock_init( void ) {
- return 0;
-}
-
-int mongo_env_write_socket( mongo *conn, const void *buf, int len ) {
- const char *cbuf = buf;
-#ifdef __APPLE__
- int flags = 0;
-#else
- int flags = MSG_NOSIGNAL;
-#endif
-
- while ( len ) {
- int sent = send( conn->sock, cbuf, len, flags );
- if ( sent == -1 ) {
- if (errno == EPIPE)
- conn->connected = 0;
- __mongo_set_error( conn, MONGO_IO_ERROR, strerror( errno ), errno );
- return MONGO_ERROR;
- }
- cbuf += sent;
- len -= sent;
- }
-
- return MONGO_OK;
-}
-
-int mongo_env_read_socket( mongo *conn, void *buf, int len ) {
- char *cbuf = buf;
- while ( len ) {
- int sent = recv( conn->sock, cbuf, len, 0 );
- if ( sent == 0 || sent == -1 ) {
- __mongo_set_error( conn, MONGO_IO_ERROR, strerror( errno ), errno );
- return MONGO_ERROR;
- }
- cbuf += sent;
- len -= sent;
- }
-
- return MONGO_OK;
-}
-
-int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
- struct timeval tv;
- tv.tv_sec = millis / 1000;
- tv.tv_usec = ( millis % 1000 ) * 1000;
-
- if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof( tv ) ) == -1 ) {
- conn->err = MONGO_IO_ERROR;
- __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_RCVTIMEO failed.", errno );
- return MONGO_ERROR;
- }
-
- if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof( tv ) ) == -1 ) {
- __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_SNDTIMEO failed.", errno );
- return MONGO_ERROR;
- }
-
- return MONGO_OK;
-}
-
-static int mongo_env_unix_socket_connect( mongo *conn, const char *sock_path ) {
- struct sockaddr_un addr;
- int status, len;
-
- conn->connected = 0;
-
- conn->sock = socket( AF_UNIX, SOCK_STREAM, 0 );
-
- if ( conn->sock < 0 ) {
- conn->sock = 0;
- return MONGO_ERROR;
- }
-
- addr.sun_family = AF_UNIX;
- strncpy( addr.sun_path, sock_path, sizeof(addr.sun_path) - 1 );
- len = sizeof( addr );
-
- status = connect( conn->sock, (struct sockaddr *) &addr, len );
- if( status < 0 ){
- mongo_env_close_socket( conn->sock );
- conn->sock = 0;
- conn->err = MONGO_CONN_FAIL;
- return MONGO_ERROR;
- }
-
- conn->connected = 1;
-
- return MONGO_OK;
-}
-
-int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
- char port_str[NI_MAXSERV];
- int status;
-
- struct addrinfo ai_hints;
- struct addrinfo *ai_list = NULL;
- struct addrinfo *ai_ptr = NULL;
-
- if ( port < 0 ) {
- return mongo_env_unix_socket_connect( conn, host );
- }
-
- conn->sock = 0;
- conn->connected = 0;
- sprintf(port_str,"%d",port);
-
- bson_sprintf( port_str, "%d", port );
-
- memset( &ai_hints, 0, sizeof( ai_hints ) );
-#ifdef AI_ADDRCONFIG
- ai_hints.ai_flags = AI_ADDRCONFIG;
-#endif
- ai_hints.ai_family = AF_UNSPEC;
- ai_hints.ai_socktype = SOCK_STREAM;
-
- status = getaddrinfo( host, port_str, &ai_hints, &ai_list );
- if ( status != 0 ) {
- bson_errprintf( "getaddrinfo failed: %s", gai_strerror( status ) );
- conn->err = MONGO_CONN_ADDR_FAIL;
- return MONGO_ERROR;
- }
-
- for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) {
- conn->sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol );
- if ( conn->sock < 0 ) {
- conn->sock = 0;
- continue;
- }
-
- status = connect( conn->sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
- if ( status != 0 ) {
- mongo_env_close_socket( conn->sock );
- conn->sock = 0;
- continue;
- }
-
- if ( ai_ptr->ai_protocol == IPPROTO_TCP ) {
- int flag = 1;
-
- setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY,
- ( void * ) &flag, sizeof( flag ) );
- if ( conn->op_timeout_ms > 0 )
- mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms );
- }
-
- conn->connected = 1;
- break;
- }
-
- freeaddrinfo( ai_list );
-
- if ( ! conn->connected ) {
- conn->err = MONGO_CONN_FAIL;
- return MONGO_ERROR;
- }
-
- return MONGO_OK;
-}
View
168 src/env_standard.c
@@ -1,168 +0,0 @@
-/* env_standard.c */
-
-/* Copyright 2009-2012 10gen Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Vanilla networking designed to work on all systems. */
-#include "env.h"
-#include <errno.h>
-#include <string.h>
-
-#ifdef _WIN32
- #ifdef _MSC_VER
- #include <ws2tcpip.h> /* send,recv,socklen_t etc */
- #include <wspiapi.h> /* addrinfo */
- #else
- #include <windows.h>
- #include <winsock.h>
- typedef int socklen_t;
- #endif
-#else
-#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <fcntl.h>
-#include <unistd.h>
-#endif
-
-#ifndef NI_MAXSERV
-# define NI_MAXSERV 32
-#endif
-
-int mongo_env_close_socket( int socket ) {
-#ifdef _WIN32
- return closesocket( socket );
-#else
- return close( socket );
-#endif
-}
-
-int mongo_env_write_socket( mongo *conn, const void *buf, int len ) {
- const char *cbuf = buf;
-#ifdef _WIN32
- int flags = 0;
-#else
-#ifdef __APPLE__
- int flags = 0;
-#else
- int flags = MSG_NOSIGNAL;
-#endif
-#endif
-
- while ( len ) {
- int sent = send( conn->sock, cbuf, len, flags );
- if ( sent == -1 ) {
- if (errno == EPIPE)
- conn->connected = 0;
- conn->err = MONGO_IO_ERROR;
- return MONGO_ERROR;
- }
- cbuf += sent;
- len -= sent;
- }
-
- return MONGO_OK;
-}
-
-int mongo_env_read_socket( mongo *conn, void *buf, int len ) {
- char *cbuf = buf;
- while ( len ) {
- int sent = recv( conn->sock, cbuf, len, 0 );
- if ( sent == 0 || sent == -1 ) {
- conn->err = MONGO_IO_ERROR;
- return MONGO_ERROR;
- }
- cbuf += sent;
- len -= sent;
- }
-
- return MONGO_OK;
-}
-
-/* This is a no-op in the generic implementation. */
-int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
- return MONGO_OK;
-}
-
-int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
- struct sockaddr_in sa;
- socklen_t addressSize;
- int flag = 1;
-
- if ( ( conn->sock = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 ) {
- conn->sock = 0;
- conn->err = MONGO_CONN_NO_SOCKET;
- return MONGO_ERROR;
- }
-
- memset( sa.sin_zero , 0 , sizeof( sa.sin_zero ) );
- sa.sin_family = AF_INET;
- sa.sin_port = htons( port );
- sa.sin_addr.s_addr = inet_addr( host );
- addressSize = sizeof( sa );
-
- if ( connect( conn->sock, ( struct sockaddr * )&sa, addressSize ) == -1 ) {
- mongo_env_close_socket( conn->sock );
- conn->connected = 0;
- conn->sock = 0;
- conn->err = MONGO_CONN_FAIL;
- return MONGO_ERROR;
- }
-
- setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY, ( char * ) &flag, sizeof( flag ) );
-
- if( conn->op_timeout_ms > 0 )
- mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms );
-
- conn->connected = 1;
-
- return MONGO_OK;
-}
-
-MONGO_EXPORT int mongo_env_sock_init( void ) {
-
-#if defined(_WIN32)
- WSADATA wsaData;
- WORD wVers;
-#elif defined(SIGPIPE)
- struct sigaction act;
-#endif
-
- static int called_once;
- static int retval;
- if (called_once) return retval;
- called_once = 1;
-
-#if defined(_WIN32)
- wVers = MAKEWORD(1, 1);
- retval = (WSAStartup(wVers, &wsaData) == 0);
-#elif defined(MACINTOSH)
- GUSISetup(GUSIwithInternetSockets);
- retval = 1;
-#elif defined(SIGPIPE)
- retval = 1;
- if (sigaction(SIGPIPE, (struct sigaction *)NULL, &act) < 0)
- retval = 0;
- else if (act.sa_handler == SIG_DFL) {
- act.sa_handler = SIG_IGN;
- if (sigaction(SIGPIPE, &act, (struct sigaction *)NULL) < 0)
- retval = 0;
- }
-#endif
- return retval;
-}
View
178 src/env_win32.c
@@ -1,178 +0,0 @@
-/* env_win32.c */
-
-/* Copyright 2009-2012 10gen Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/* Networking and other niceties for WIN32. */
-#include "env.h"
-#include "mongo.h"
-#include <string.h>
-
-#ifdef _MSC_VER
-#include <ws2tcpip.h> /* send,recv,socklen_t etc */
-#include <wspiapi.h> /* addrinfo */
-#else
-#include <ws2tcpip.h> /* send,recv,socklen_t etc */
-#include <winsock2.h>
-typedef int socklen_t;
-#endif
-
-#ifndef NI_MAXSERV
-# define NI_MAXSERV 32
-#endif
-
-int mongo_env_close_socket( int socket ) {
- return closesocket( socket );
-}
-
-int mongo_env_write_socket( mongo *conn, const void *buf, int len ) {
- const char *cbuf = buf;
- int flags = 0;
-
- while ( len ) {
- int sent = send( conn->sock, cbuf, len, flags );
- if ( sent == -1 ) {
- __mongo_set_error( conn, MONGO_IO_ERROR, NULL, WSAGetLastError() );
- conn->connected = 0;
- return MONGO_ERROR;
- }
- cbuf += sent;
- len -= sent;
- }
-
- return MONGO_OK;
-}
-
-int mongo_env_read_socket( mongo *conn, void *buf, int len ) {
- char *cbuf = buf;
-
- while ( len ) {
- int sent = recv( conn->sock, cbuf, len, 0 );
- if ( sent == 0 || sent == -1 ) {
- __mongo_set_error( conn, MONGO_IO_ERROR, NULL, WSAGetLastError() );
- return MONGO_ERROR;
- }
- cbuf += sent;
- len -= sent;
- }
-
- return MONGO_OK;
-}
-
-int mongo_env_set_socket_op_timeout( mongo *conn, int millis ) {
- if ( setsockopt( conn->sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&millis,
- sizeof( millis ) ) == -1 ) {
- __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_RCVTIMEO failed.",
- WSAGetLastError() );
- return MONGO_ERROR;
- }
-
- if ( setsockopt( conn->sock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&millis,
- sizeof( millis ) ) == -1 ) {
- __mongo_set_error( conn, MONGO_IO_ERROR, "setsockopt SO_SNDTIMEO failed.",
- WSAGetLastError() );
- return MONGO_ERROR;
- }
-
- return MONGO_OK;
-}
-
-int mongo_env_socket_connect( mongo *conn, const char *host, int port ) {
- char port_str[NI_MAXSERV];
- char errstr[MONGO_ERR_LEN];
- int status;
-
- struct addrinfo ai_hints;
- struct addrinfo *ai_list = NULL;
- struct addrinfo *ai_ptr = NULL;
-
- conn->sock = 0;
- conn->connected = 0;
-
- bson_sprintf( port_str, "%d", port );
-
- memset( &ai_hints, 0, sizeof( ai_hints ) );
- ai_hints.ai_family = AF_UNSPEC;
- ai_hints.ai_socktype = SOCK_STREAM;
- ai_hints.ai_protocol = IPPROTO_TCP;
-
- status = getaddrinfo( host, port_str, &ai_hints, &ai_list );
- if ( status != 0 ) {
- bson_sprintf( errstr, "getaddrinfo failed with error %d", status );
- __mongo_set_error( conn, MONGO_CONN_ADDR_FAIL, errstr, WSAGetLastError() );
- return MONGO_ERROR;
- }
-
- for ( ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next ) {
- conn->sock = socket( ai_ptr->ai_family, ai_ptr->ai_socktype,
- ai_ptr->ai_protocol );
-
- if ( conn->sock < 0 ) {
- __mongo_set_error( conn, MONGO_SOCKET_ERROR, "socket() failed",
- WSAGetLastError() );
- conn->sock = 0;
- continue;
- }
-
- status = connect( conn->sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen );
- if ( status != 0 ) {
- __mongo_set_error( conn, MONGO_SOCKET_ERROR, "connect() failed",
- WSAGetLastError() );
- mongo_env_close_socket( conn->sock );
- conn->sock = 0;
- continue;
- }
-
- if ( ai_ptr->ai_protocol == IPPROTO_TCP ) {
- int flag = 1;
-
- setsockopt( conn->sock, IPPROTO_TCP, TCP_NODELAY,
- ( void * ) &flag, sizeof( flag ) );
-
- if ( conn->op_timeout_ms > 0 )
- mongo_env_set_socket_op_timeout( conn, conn->op_timeout_ms );
- }
-
- conn->connected = 1;
- break;
- }
-
- freeaddrinfo( ai_list );
-
- if ( ! conn->connected ) {
- conn->err = MONGO_CONN_FAIL;
- return MONGO_ERROR;
- }
- else {
- mongo_clear_errors( conn );
- return MONGO_OK;
- }
-}
-
-MONGO_EXPORT int mongo_env_sock_init( void ) {
-
- WSADATA wsaData;
- WORD wVers;
- static int called_once;
- static int retval;
-
- if (called_once) return retval;
-
- called_once = 1;
- wVers = MAKEWORD(1, 1);
- retval = (WSAStartup(wVers, &wsaData) == 0);
-
- return retval;
-}
View
33 src/gridfs.c
@@ -60,7 +60,7 @@ static void chunk_free( bson *oChunk ) {
}
int gridfs_init( mongo *client, const char *dbname, const char *prefix,
- gridfs *gfs ) {
+ gridfs *gfs ) {
int options;
bson b;
@@ -134,8 +134,8 @@ MONGO_EXPORT void gridfs_destroy( gridfs *gfs ) {
}
static int gridfs_insert_file( gridfs *gfs, const char *name,
- const bson_oid_t id, gridfs_offset length,
- const char *contenttype ) {
+ const bson_oid_t id, gridfs_offset length,
+ const char *contenttype ) {
bson command;
bson ret;
bson res;
@@ -177,8 +177,8 @@ static int gridfs_insert_file( gridfs *gfs, const char *name,
}
MONGO_EXPORT int gridfs_store_buffer( gridfs *gfs, const char *data,
- gridfs_offset length, const char *remotename,
- const char *contenttype ) {
+ gridfs_offset length, const char *remotename,
+ const char *contenttype ) {
char const *end = data + length;
const char *data_ptr = data;
@@ -209,7 +209,7 @@ MONGO_EXPORT int gridfs_store_buffer( gridfs *gfs, const char *data,
}
MONGO_EXPORT void gridfile_writer_init( gridfile *gfile, gridfs *gfs,
- const char *remote_name, const char *content_type ) {
+ const char *remote_name, const char *content_type ) {
gfile->gfs = gfs;
bson_oid_gen( &( gfile->id ) );
@@ -226,7 +226,7 @@ MONGO_EXPORT void gridfile_writer_init( gridfile *gfile, gridfs *gfs,
}
MONGO_EXPORT void gridfile_write_buffer( gridfile *gfile, const char *data,
- gridfs_offset length ) {
+ gridfs_offset length ) {
int bytes_left = 0;
int data_partial_len = 0;
@@ -239,13 +239,15 @@ MONGO_EXPORT void gridfile_write_buffer( gridfile *gfile, const char *data,
if( gfile->pending_data ) {
gfile->pending_data = ( char * )bson_realloc( ( void * )gfile->pending_data, gfile->pending_len + to_write );
memcpy( gfile->pending_data + gfile->pending_len, data, length );
- } else if ( to_write > 0 ) {
+ }
+ else if ( to_write > 0 ) {
gfile->pending_data = ( char * )bson_malloc( to_write );
memcpy( gfile->pending_data, data, length );
}
gfile->pending_len += length;
- } else { /* At least one chunk of data to write */
+ }
+ else { /* At least one chunk of data to write */
chunks_to_write = to_write / DEFAULT_CHUNK_SIZE;
bytes_left = to_write % DEFAULT_CHUNK_SIZE;
@@ -319,7 +321,7 @@ MONGO_EXPORT int gridfile_writer_done( gridfile *gfile ) {
}
int gridfs_store_file( gridfs *gfs, const char *filename,
- const char *remotename, const char *contenttype ) {
+ const char *remotename, const char *contenttype ) {
char buffer[DEFAULT_CHUNK_SIZE];
FILE *fd;
@@ -349,7 +351,8 @@ int gridfs_store_file( gridfs *gfs, const char *filename,
length += chunkLen;
chunkNumber++;
chunkLen = fread( buffer, 1, DEFAULT_CHUNK_SIZE, fd );
- } while ( chunkLen != 0 );
+ }
+ while ( chunkLen != 0 );
/* Close the file stream */
if ( fd != stdin ) fclose( fd );
@@ -564,7 +567,7 @@ MONGO_EXPORT int gridfile_get_numchunks( gridfile *gfile ) {
MONGO_EXPORT void gridfile_get_chunk( gridfile *gfile, int n, bson* out ) {
bson query;
-
+
bson_iterator it;
bson_oid_t id;
int result;
@@ -603,7 +606,8 @@ MONGO_EXPORT mongo_cursor *gridfile_get_chunks( gridfile *gfile, int start, int
bson_append_oid( &query, "files_id", &id );
if ( size == 1 ) {
bson_append_int( &query, "n", start );
- } else {
+ }
+ else {
bson_init( &gte );
bson_append_int( &gte, "$gte", start );
bson_finish( &gte );
@@ -692,7 +696,8 @@ MONGO_EXPORT gridfs_offset gridfile_read( gridfile *gfile, gridfs_offset size, c
memcpy( buf, chunk_data, chunk_len );
bytes_left -= chunk_len;
buf += chunk_len;
- } else {
+ }
+ else {
memcpy( buf, chunk_data, bytes_left );
}
}
View
12 src/gridfs.h
@@ -67,7 +67,7 @@ MONGO_EXPORT void gridfile_get_descriptor(gridfile* gf, bson* out);
* @return - MONGO_OK or MONGO_ERROR.
*/
MONGO_EXPORT int gridfs_init( mongo *client, const char *dbname,
- const char *prefix, gridfs *gfs );
+ const char *prefix, gridfs *gfs );
/**
* Destroys a GridFS object. Call this when finished with
@@ -84,7 +84,7 @@ MONGO_EXPORT void gridfs_destroy( gridfs *gfs );
*
*/
MONGO_EXPORT void gridfile_writer_init( gridfile *gfile, gridfs *gfs, const char *remote_name,
- const char *content_type );
+ const char *content_type );
/**
* Write to a GridFS file incrementally. You can call this function any number
@@ -93,7 +93,7 @@ MONGO_EXPORT void gridfile_writer_init( gridfile *gfile, gridfs *gfs, const char
*
*/
MONGO_EXPORT void gridfile_write_buffer( gridfile *gfile, const char *data,
- gridfs_offset length );
+ gridfs_offset length );
/**
* Signal that writing of this gridfile is complete by
@@ -115,8 +115,8 @@ MONGO_EXPORT int gridfile_writer_done( gridfile *gfile );
* @return - MONGO_OK or MONGO_ERROR.
*/
MONGO_EXPORT int gridfs_store_buffer( gridfs *gfs, const char *data, gridfs_offset length,
- const char *remotename,
- const char *contenttype );
+ const char *remotename,
+ const char *contenttype );
/**
* Open the file referenced by filename and store it as a GridFS file.
@@ -128,7 +128,7 @@ MONGO_EXPORT int gridfs_store_buffer( gridfs *gfs, const char *data, gridfs_offs
* @return - MONGO_OK or MONGO_ERROR.
*/
MONGO_EXPORT int gridfs_store_file( gridfs *gfs, const char *filename,
- const char *remotename, const char *contenttype );
+ const char *remotename, const char *contenttype );
/**
* Removes the files referenced by filename from the db
View
187 src/md5.c
@@ -27,7 +27,7 @@
This code implements the MD5 Algorithm defined in RFC 1321, whose
text is available at
- http://www.ietf.org/rfc/rfc1321.txt
+ http://www.ietf.org/rfc/rfc1321.txt
The code is derived from the text of the RFC, including the test suite
(section A.5) but excluding the rest of Appendix A. It does not include
any code or documentation that is identified in the RFC as being
@@ -38,14 +38,14 @@
that follows (in reverse chronological order):
2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order
- either statically or dynamically; added missing #include <string.h>
- in library.
+ either statically or dynamically; added missing #include <string.h>
+ in library.
2002-03-11 lpd Corrected argument list for main(), and added int return
- type, in test program and T value program.
+ type, in test program and T value program.
2002-02-21 lpd Added missing #include <stdio.h> in test program.
2000-07-03 lpd Patched to eliminate warnings about "constant is
- unsigned in ANSI C, signed in traditional"; made test program
- self-checking.
+ unsigned in ANSI C, signed in traditional"; made test program
+ self-checking.
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
1999-05-03 lpd Original version.
@@ -54,7 +54,7 @@
#include "md5.h"
#include <string.h>
-#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
+#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
#ifdef MONGO_BIG_ENDIAN
# define BYTE_ORDER 1
#else
@@ -129,11 +129,10 @@
static void
-mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
-{
+mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/) {
mongo_md5_word_t
- a = pms->abcd[0], b = pms->abcd[1],
- c = pms->abcd[2], d = pms->abcd[3];
+ a = pms->abcd[0], b = pms->abcd[1],
+ c = pms->abcd[2], d = pms->abcd[3];
mongo_md5_word_t t;
#if BYTE_ORDER > 0
/* Define storage only for big-endian CPUs. */
@@ -146,51 +145,52 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
{
#if BYTE_ORDER == 0
- /*
- * Determine dynamically whether this is a big-endian or
- * little-endian machine, since we can use a more efficient
- * algorithm on the latter.
- */
- static const int w = 1;
-
- if (*((const mongo_md5_byte_t *)&w)) /* dynamic little-endian */
+ /*
+ * Determine dynamically whether this is a big-endian or
+ * little-endian machine, since we can use a more efficient
+ * algorithm on the latter.
+ */
+ static const int w = 1;
+
+ if (*((const mongo_md5_byte_t *)&w)) /* dynamic little-endian */
#endif
-#if BYTE_ORDER <= 0 /* little-endian */
- {
- /*
- * On little-endian machines, we can process properly aligned
- * data without copying it.
- */
- if (!((data - (const mongo_md5_byte_t *)0) & 3)) {
- /* data are properly aligned */
- X = (const mongo_md5_word_t *)data;
- } else {
- /* not aligned */
- memcpy(xbuf, data, 64);
- X = xbuf;
- }
- }
+#if BYTE_ORDER <= 0 /* little-endian */
+ {
+ /*
+ * On little-endian machines, we can process properly aligned
+ * data without copying it.
+ */
+ if (!((data - (const mongo_md5_byte_t *)0) & 3)) {
+ /* data are properly aligned */
+ X = (const mongo_md5_word_t *)data;
+ }
+ else {
+ /* not aligned */
+ memcpy(xbuf, data, 64);
+ X = xbuf;
+ }
+ }
#endif
#if BYTE_ORDER == 0
- else /* dynamic big-endian */
+ else /* dynamic big-endian */
#endif
-#if BYTE_ORDER >= 0 /* big-endian */
- {
- /*
- * On big-endian machines, we must arrange the bytes in the
- * right order.
- */
- const mongo_md5_byte_t *xp = data;
- int i;
+#if BYTE_ORDER >= 0 /* big-endian */
+ {
+ /*
+ * On big-endian machines, we must arrange the bytes in the
+ * right order.
+ */
+ const mongo_md5_byte_t *xp = data;
+ int i;
# if BYTE_ORDER == 0
- X = xbuf; /* (dynamic only) */
+ X = xbuf; /* (dynamic only) */
# else
-# define xbuf X /* (static only) */
+# define xbuf X /* (static only) */
# endif
- for (i = 0; i < 16; ++i, xp += 4)
- xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
- }
+ for (i = 0; i < 16; ++i, xp += 4)
+ xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
+ }
#endif
}
@@ -201,8 +201,8 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
#define SET(a, b, c, d, k, s, Ti)\
- t = a + F(b,c,d) + X[k] + Ti;\
- a = ROTATE_LEFT(t, s) + b
+ t = a + F(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
/* Do the following 16 operations. */
SET(a, b, c, d, 0, 7, T1);
SET(d, a, b, c, 1, 12, T2);
@@ -222,14 +222,14 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
SET(b, c, d, a, 15, 22, T16);
#undef SET
- /* Round 2. */
- /* Let [abcd k s i] denote the operation
- a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
+ /* Round 2. */
+ /* Let [abcd k s i] denote the operation
+ a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
#define SET(a, b, c, d, k, s, Ti)\
- t = a + G(b,c,d) + X[k] + Ti;\
- a = ROTATE_LEFT(t, s) + b
- /* Do the following 16 operations. */
+ t = a + G(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
SET(a, b, c, d, 1, 5, T17);
SET(d, a, b, c, 6, 9, T18);
SET(c, d, a, b, 11, 14, T19);
@@ -248,14 +248,14 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
SET(b, c, d, a, 12, 20, T32);
#undef SET
- /* Round 3. */
- /* Let [abcd k s t] denote the operation
- a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
+ /* Round 3. */
+ /* Let [abcd k s t] denote the operation
+ a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define SET(a, b, c, d, k, s, Ti)\
- t = a + H(b,c,d) + X[k] + Ti;\
- a = ROTATE_LEFT(t, s) + b
- /* Do the following 16 operations. */
+ t = a + H(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
SET(a, b, c, d, 5, 4, T33);
SET(d, a, b, c, 8, 11, T34);
SET(c, d, a, b, 11, 16, T35);
@@ -274,14 +274,14 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
SET(b, c, d, a, 2, 23, T48);
#undef SET
- /* Round 4. */
- /* Let [abcd k s t] denote the operation
- a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
+ /* Round 4. */
+ /* Let [abcd k s t] denote the operation
+ a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
#define SET(a, b, c, d, k, s, Ti)\
- t = a + I(b,c,d) + X[k] + Ti;\
- a = ROTATE_LEFT(t, s) + b
- /* Do the following 16 operations. */
+ t = a + I(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
SET(a, b, c, d, 0, 6, T49);
SET(d, a, b, c, 7, 10, T50);
SET(c, d, a, b, 14, 15, T51);
@@ -300,9 +300,9 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
SET(b, c, d, a, 9, 21, T64);
#undef SET
- /* Then perform the following additions. (That is increment each
- of the four registers by the value it had before this block
- was started.) */
+ /* Then perform the following additions. (That is increment each
+ of the four registers by the value it had before this block
+ was started.) */
pms->abcd[0] += a;
pms->abcd[1] += b;
pms->abcd[2] += c;
@@ -310,8 +310,7 @@ mongo_md5_process(mongo_md5_state_t *pms, const mongo_md5_byte_t *data /*[64]*/)
}
MONGO_EXPORT void
-mongo_md5_init(mongo_md5_state_t *pms)
-{
+mongo_md5_init(mongo_md5_state_t *pms) {
pms->count[0] = pms->count[1] = 0;
pms->abcd[0] = 0x67452301;
pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
@@ -320,62 +319,60 @@ mongo_md5_init(mongo_md5_state_t *pms)
}
MONGO_EXPORT void
-mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes)
-{
+mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes) {
const mongo_md5_byte_t *p = data;
int left = nbytes;
int offset = (pms->count[0] >> 3) & 63;
mongo_md5_word_t nbits = (mongo_md5_word_t)(nbytes << 3);
if (nbytes <= 0)
- return;
+ return;
/* Update the message length. */
pms->count[1] += nbytes >> 29;
pms->count[0] += nbits;
if (pms->count[0] < nbits)
- pms->count[1]++;
+ pms->count[1]++;
/* Process an initial partial block. */
if (offset) {
- int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
-
- memcpy(pms->buf + offset, p, copy);
- if (offset + copy < 64)
- return;
- p += copy;
- left -= copy;
- mongo_md5_process(pms, pms->buf);
+ int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
+
+ memcpy(pms->buf + offset, p, copy);
+ if (offset + copy < 64)
+ return;
+ p += copy;
+ left -= copy;
+ mongo_md5_process(pms, pms->buf);
}
/* Process full blocks. */
for (; left >= 64; p += 64, left -= 64)
- mongo_md5_process(pms, p);
+ mongo_md5_process(pms, p);
/* Process a final partial block. */
if (left)
- memcpy(pms->buf, p, left);
+ memcpy(pms->buf, p, left);
}
MONGO_EXPORT void
-mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16])
-{
+mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16]) {
static const mongo_md5_byte_t pad[64] = {
- 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
mongo_md5_byte_t data[8];
int i;
/* Save the length before padding. */
for (i = 0; i < 8; ++i)
- data[i] = (mongo_md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
+ data[i] = (mongo_md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
/* Pad to 56 bytes mod 64. */
mongo_md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
/* Append the length. */
mongo_md5_append(pms, data, 8);
for (i = 0; i < 16; ++i)
- digest[i] = (mongo_md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
+ digest[i] = (mongo_md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
}
View
28 src/md5.h
@@ -27,7 +27,7 @@
This code implements the MD5 Algorithm defined in RFC 1321, whose
text is available at
- http://www.ietf.org/rfc/rfc1321.txt
+ http://www.ietf.org/rfc/rfc1321.txt
The code is derived from the text of the RFC, including the test suite
(section A.5) but excluding the rest of Appendix A. It does not include
any code or documentation that is identified in the RFC as being
@@ -38,12 +38,12 @@
that follows (in reverse chronological order):
2002-04-13 lpd Removed support for non-ANSI compilers; removed
- references to Ghostscript; clarified derivation from RFC 1321;
- now handles byte order either statically or dynamically.
+ references to Ghostscript; clarified derivation from RFC 1321;
+ now handles byte order either statically or dynamically.
1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5);
- added conditionalization for C++ compilation from Martin
- Purschke <purschke@bnl.gov>.
+ added conditionalization for C++ compilation from Martin
+ Purschke <purschke@bnl.gov>.
1999-05-03 lpd Original version.
*/
@@ -66,9 +66,9 @@ typedef unsigned int mongo_md5_word_t; /* 32-bit word */
/* Define the state of the MD5 Algorithm. */
typedef struct mongo_md5_state_s {
- mongo_md5_word_t count[2]; /* message length in bits, lsw first */
- mongo_md5_word_t abcd[4]; /* digest buffer */
- mongo_md5_byte_t buf[64]; /* accumulate block */
+ mongo_md5_word_t count[2]; /* message length in bits, lsw first */
+ mongo_md5_word_t abcd[4]; /* digest buffer */
+ mongo_md5_byte_t buf[64]; /* accumulate block */
} mongo_md5_state_t;
#ifdef __cplusplus
@@ -76,14 +76,14 @@ extern "C"
{
#endif
-/* Initialize the algorithm. */
-MONGO_EXPORT void mongo_md5_init(mongo_md5_state_t *pms);
+ /* Initialize the algorithm. */
+ MONGO_EXPORT void mongo_md5_init(mongo_md5_state_t *pms);
-/* Append a string to the message. */
-MONGO_EXPORT void mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes);
+ /* Append a string to the message. */
+ MONGO_EXPORT void mongo_md5_append(mongo_md5_state_t *pms, const mongo_md5_byte_t *data, int nbytes);
-/* Finish the message and return the digest. */
-MONGO_EXPORT void mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16]);
+ /* Finish the message and return the digest. */
+ MONGO_EXPORT void mongo_md5_finish(mongo_md5_state_t *pms, mongo_md5_byte_t digest[16]);
#ifdef __cplusplus
} /* end extern "C" */
View
143 src/mongo.c
@@ -19,10 +19,7 @@
#include "md5.h"
#include "env.h"
-#include <stdlib.h>
-#include <stdio.h>
#include <string.h>
-#include <stdlib.h>
MONGO_EXPORT mongo* mongo_create( void ) {
return (mongo*)bson_malloc(sizeof(mongo));
@@ -30,7 +27,7 @@ MONGO_EXPORT mongo* mongo_create( void ) {
MONGO_EXPORT void mongo_dispose(mongo* conn) {
- free(conn);
+ bson_free(conn);
}
MONGO_EXPORT int mongo_get_err(mongo* conn) {
@@ -100,7 +97,7 @@ MONGO_EXPORT mongo_cursor* mongo_cursor_create( void ) {
MONGO_EXPORT void mongo_cursor_dispose(mongo_cursor* cursor) {
- free(cursor);
+ bson_free(cursor);
}
@@ -114,7 +111,7 @@ MONGO_EXPORT const char* mongo_get_server_err_string(mongo* conn) {
}
MONGO_EXPORT void __mongo_set_error( mongo *conn, mongo_error_t err, const char *str,
- int errcode ) {
+ int errcode ) {
int errstr_size, str_size;
conn->err = err;
@@ -143,7 +140,7 @@ static char *mongo_ns_to_cmd_db( const char *ns ) {
int len = 0;
for( current = (char *)ns; *current != '.'; current++ ) {
- len++;
+ len++;
}
cmd_db_name = (char *)bson_malloc( len + 6 );
@@ -201,15 +198,15 @@ MONGO_EXPORT int mongo_validate_ns( mongo *conn, const char *ns ) {
/* Go back and validate the database name. */
for( current = (char *)db_name; *current != '.'; current++ ) {
switch( *current ) {
- case ' ':
- case '$':
- case '/':
- case '\\':
- __mongo_set_error( conn, MONGO_NS_INVALID,
- "Database name may not contain ' ', '$', '/', or '\\'", 0 );
- return MONGO_ERROR;
- default:
- break;
+ case ' ':
+ case '$':
+ case '/':
+ case '\\':
+ __mongo_set_error( conn, MONGO_NS_INVALID,
+ "Database name may not contain ' ', '$', '/', or '\\'", 0 );
+ return MONGO_ERROR;
+ default:
+ break;
}
ns_len++;
@@ -223,15 +220,15 @@ MONGO_EXPORT int mongo_validate_ns( mongo *conn, const char *ns ) {
/* Cannot have two consecutive dots. */
if( last && *last == '.' && *current == '.' ) {
- __mongo_set_error( conn, MONGO_NS_INVALID,
- "Collection may not contain two consecutive '.'", 0 );
+ __mongo_set_error( conn, MONGO_NS_INVALID,
+ "Collection may not contain two consecutive '.'", 0 );
return MONGO_ERROR;
}
/* Cannot contain a '$' */
if( *current == '$' ) {
__mongo_set_error( conn, MONGO_NS_INVALID,
- "Collection may not contain '$'", 0 );
+ "Collection may not contain '$'", 0 );
return MONGO_ERROR;
}
@@ -249,7 +246,7 @@ MONGO_EXPORT int mongo_validate_ns( mongo *conn, const char *ns ) {
/* Cannot end with a '.' */
if( *(current - 1) == '.' ) {
__mongo_set_error( conn, MONGO_NS_INVALID,
- "Collection may not end with '.'", 0 );
+ "Collection may not end with '.'", 0 );
return MONGO_ERROR;
}
@@ -376,10 +373,11 @@ static int mongo_check_is_master( mongo *conn ) {
if( bson_find( &it, &out, "ismaster" ) )
ismaster = bson_iterator_bool( &it );
if( bson_find( &it, &out, "maxBsonObjectSize" ) ) {
- max_bson_size = bson_iterator_int( &it );
+ max_bson_size = bson_iterator_int( &it );
}
conn->max_bson_size = max_bson_size;
- } else {
+ }
+ else {
return MONGO_ERROR;
}
@@ -429,7 +427,7 @@ MONGO_EXPORT int mongo_client( mongo *conn , const char *host, int port ) {
MONGO_EXPORT int mongo_connect( mongo *conn , const char *host, int port ) {
int ret;
- fprintf(stderr, "WARNING: mongo_connect() is deprecated, please use mongo_client()\n");
+ bson_errprintf("WARNING: mongo_connect() is deprecated, please use mongo_client()\n");
ret = mongo_client( conn, host, port );
mongo_set_write_concern( conn, 0 );
return ret;
@@ -451,7 +449,7 @@ MONGO_EXPORT void mongo_replica_set_init( mongo *conn, const char *name ) {
}
MONGO_EXPORT void mongo_replset_init( mongo *conn, const char *name ) {
- fprintf(stderr, "WARNING: mongo_replset_init() is deprecated, please use mongo_replica_set_init()\n");
+ bson_errprintf("WARNING: mongo_replset_init() is deprecated, please use mongo_replica_set_init()\n");
mongo_replica_set_init( conn, name );
}
@@ -489,7 +487,7 @@ MONGO_EXPORT void mongo_replica_set_add_seed( mongo *conn, const char *host, int
}
MONGO_EXPORT void mongo_replset_add_seed( mongo *conn, const char *host, int port ) {
- fprintf(stderr, "WARNING: mongo_replset_add_seed() is deprecated, please use mongo_replica_set_add_seed()\n");
+ bson_errprintf("WARNING: mongo_replset_add_seed() is deprecated, please use mongo_replica_set_add_seed()\n");
mongo_replica_set_add_node( &conn->replica_set->seeds, host, port );
}
@@ -547,7 +545,7 @@ static void mongo_replica_set_check_seed( mongo *conn ) {
if( host_port ) {
mongo_parse_host( host_string, host_port );
mongo_replica_set_add_node( &conn->replica_set->hosts,
- host_port->host, host_port->port );
+ host_port->host, host_port->port );
bson_free( host_port );
host_port = NULL;
@@ -582,7 +580,7 @@ static int mongo_replica_set_check_host( mongo *conn ) {
ismaster = bson_iterator_bool( &it );
if( bson_find( &it, &out, "maxBsonObjectSize" ) )
- max_bson_size = bson_iterator_int( &it );
+ max_bson_size = bson_iterator_int( &it );
conn->max_bson_size = max_bson_size;
if( bson_find( &it, &out, "setName" ) ) {
@@ -599,7 +597,8 @@ static int mongo_replica_set_check_host( mongo *conn ) {
if( ismaster ) {
conn->replica_set->primary_connected = 1;
- } else {
+ }
+ else {
mongo_env_close_socket( conn->sock );
}
@@ -632,7 +631,8 @@ MONGO_EXPORT int mongo_replica_set_client( mongo *conn ) {
if( !conn->replica_set->hosts ) {
conn->err = MONGO_CONN_NO_PRIMARY;
return MONGO_ERROR;
- } else {
+ }
+ else {
node = conn->replica_set->hosts;
while( node != NULL ) {
@@ -668,7 +668,7 @@ MONGO_EXPORT int mongo_replica_set_client( mongo *conn ) {
MONGO_EXPORT int mongo_replset_connect( mongo *conn ) {
int ret;
- fprintf(stderr, "WARNING: mongo_replset_connect() is deprecated, please use mongo_replica_set_client()\n");
+ bson_errprintf("WARNING: mongo_replset_connect() is deprecated, please use mongo_replica_set_client()\n");
ret = mongo_replica_set_client( conn );
mongo_set_write_concern( conn, 0 );
return ret;
@@ -692,7 +692,8 @@ MONGO_EXPORT int mongo_reconnect( mongo *conn ) {
conn->replica_set->hosts = NULL;
res = mongo_replica_set_client( conn );
return res;
- } else
+ }
+ else
return mongo_env_socket_connect( conn, conn->primary->host, conn->primary->port );
}
@@ -791,7 +792,7 @@ static int mongo_cursor_bson_valid( mongo_cursor *cursor, const bson *bson ) {
}
static int mongo_check_last_error( mongo *conn, const char *ns,
- mongo_write_concern *write_concern ) {
+ mongo_write_concern *write_concern ) {
int ret = MONGO_OK;
bson response = {NULL, 0};
bson fields;
@@ -806,10 +807,10 @@ static int mongo_check_last_error( mongo *conn, const char *ns,
ret = MONGO_ERROR;
else {
if( ( bson_find( &it, &response, "$err" ) == BSON_STRING ) ||
- ( bson_find( &it, &response, "err" ) == BSON_STRING ) ) {
+ ( bson_find( &it, &response, "err" ) == BSON_STRING ) ) {
__mongo_set_error( conn, MONGO_WRITE_ERROR,
- "See conn->lasterrstr for details.", 0 );
+ "See conn->lasterrstr for details.", 0 );
mongo_set_last_error( conn, &it, &response );
ret = MONGO_ERROR;
}
@@ -819,8 +820,8 @@ static int mongo_check_last_error( mongo *conn, const char *ns,
}
static int mongo_choose_write_concern( mongo *conn,
- mongo_write_concern *custom_write_concern,
- mongo_write_concern **write_concern ) {
+ mongo_write_concern *custom_write_concern,
+ mongo_write_concern **write_concern ) {