Skip to Content.
Sympa Menu

freetds - Re: [freetds] ct_config set CS_IFILE property is not working?

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Dariusz Ostolski <dariusz.ostolski AT gmail.com>
  • To: FreeTDS Development Group <freetds AT lists.ibiblio.org>
  • Cc: "James K. Lowden" <jklowden AT freetds.org>
  • Subject: Re: [freetds] ct_config set CS_IFILE property is not working?
  • Date: Mon, 12 Mar 2012 21:37:10 +0100

Hello,


W dniu 17.01.2012 05:13, James K. Lowden pisze:
I would have taken it verbatim if I could apply it to CVS HEAD. If you redo it, I would suggest doing it in a way that e.g. the tds_read_config_info() signature doesn't change. Instead of adding a parameter, move the "interfaces" argument to the context, too. We prefer patches for CVS HEAD because that's always the way forward. Working patches for the current release are also acceptable, but risk regression on the next release if no one adapts it to HEAD. Regards, --jkl

I'm sending You a patch to add support for CS_IFILE property (in attachement), I hope I checked out/diffed correct module. I added basic unit tests, I think I'll add more soon.


Regards,
Darek
? Makefile-freetds.mk
? cs_ifile.patch
? install-sh
? nbproject
Index: .cvsignore
===================================================================
RCS file: /cvsroot/freetds/freetds/.cvsignore,v
retrieving revision 1.13
diff -u -r1.13 .cvsignore
--- .cvsignore	14 Jan 2012 19:46:32 -0000	1.13
+++ .cvsignore	12 Mar 2012 20:29:18 -0000
@@ -1,22 +1,23 @@
-config.log
-config.cache
-config.status
-config.sub
+autom4te.cache
 config.guess
-configure
-Makefile
-Makefile.in
-libtool
-ltmain.sh
-ltconfig
+config.sub
+config.status
 ltcf-c.sh
-aclocal.m4
-autom4te.cache
-depcomp
 so_locations
+.gitignore
+test-dist.log
 freetds.spec
+aclocal.m4
 PWD
-doxyfile
-test-dist.log
+depcomp
+libtool
+ltmain.sh
+config.log
+Makefile.in
 compile
-.gitignore
+ltconfig
+doxyfile
+config.cache
+.dep.inc
+configure
+Makefile
Index: include/tds.h
===================================================================
RCS file: /cvsroot/freetds/freetds/include/tds.h,v
retrieving revision 1.397
diff -u -r1.397 tds.h
--- include/tds.h	11 Mar 2012 15:52:22 -0000	1.397
+++ include/tds.h	12 Mar 2012 20:29:19 -0000
@@ -524,6 +524,7 @@
 	DSTR ip_addr;	  	/**< ip of server */
 	DSTR instance_name;
 	DSTR dump_file;
+        DSTR interfaces;   /** interfaces file that should be used contextually */
 	int debug_flags;
 	int text_size;
 
@@ -915,6 +916,7 @@
 	int (*msg_handler) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
 	int (*err_handler) (const TDSCONTEXT *, TDSSOCKET *, TDSMESSAGE *);
 	int (*int_handler) (void *);
+        DSTR interfaces;
 };
 
 enum TDS_ICONV_ENTRY
@@ -1069,6 +1071,8 @@
 TDSRET tds_set_interfaces_file_loc(const char *interfloc);
 extern const char STD_DATETIME_FMT[];
 int tds_config_boolean(const char *value);
+int tds_config_set_ctxt_interfaces_file(TDSCONTEXT *ctxt, void *buffer, size_t buflen);
+int tds_config_get_ctxt_interfaces_file(TDSCONTEXT *ctxt, void *buffer, size_t buflen, int *outlen);
 
 TDSLOCALE *tds_get_locale(void);
 TDSRET tds_alloc_row(TDSRESULTINFO * res_info);
Index: src/ctlib/ct.c
===================================================================
RCS file: /cvsroot/freetds/freetds/src/ctlib/ct.c,v
retrieving revision 1.225
diff -u -r1.225 ct.c
--- src/ctlib/ct.c	11 Mar 2012 15:52:22 -0000	1.225
+++ src/ctlib/ct.c	12 Mar 2012 20:29:19 -0000
@@ -2371,6 +2371,41 @@
 
 }
 
+static CS_RETCODE 
+_ct_set_ifile(CS_CONTEXT * ctx, CS_VOID * buffer, CS_INT buflen)
+{
+    
+    if ( buflen==CS_UNUSED ) {
+        return CS_FAIL;
+    }
+    
+    if ( buflen==CS_NULLTERM ) {
+        buflen = strlen(buffer);
+    }
+    
+    if ( tds_config_set_ctxt_interfaces_file(ctx->tds_ctx, buffer, buflen)==TDS_SUCCESS ) {
+        return CS_SUCCEED;
+    }
+    return CS_FAIL;
+}
+
+static CS_RETCODE 
+_ct_get_ifile(CS_CONTEXT * ctx, CS_VOID * buffer, CS_INT buflen, CS_INT * outlen)
+{
+    if ( buflen==CS_UNUSED ) {
+        return CS_FAIL;
+    }
+    
+    if ( buflen==CS_NULLTERM ) {
+        return CS_FAIL;
+    }
+    
+    if ( tds_config_get_ctxt_interfaces_file(ctx->tds_ctx, buffer, buflen, outlen)==TDS_SUCCESS ) {
+        return CS_SUCCEED;
+    }
+    return CS_FAIL;
+}
+
 CS_RETCODE
 ct_config(CS_CONTEXT * ctx, CS_INT action, CS_INT property, CS_VOID * buffer, CS_INT buflen, CS_INT * outlen)
 {
@@ -2383,6 +2418,20 @@
 		    CS_GET ? "CS_GET" : CS_SET ? "CS_SET" : CS_SUPPORTED ? "CS_SUPPORTED" : "CS_CLEAR", property);
 
 	switch (property) {
+        case CS_IFILE: {
+                ret = CS_FAIL;
+                    switch (action) {
+                    case CS_SET:
+                           ret = _ct_set_ifile(ctx, buffer, buflen);
+                           break;
+                    case CS_GET:
+                           ret = _ct_get_ifile(ctx, buffer, buflen, outlen);
+                           break;
+                    default:
+                           ret = CS_FAIL;
+                    }
+                }
+		break;
 	case CS_EXPOSE_FMTS:
 		switch (action) {
 		case CS_SUPPORTED:
Index: src/ctlib/unittests/cs_config.c
===================================================================
RCS file: /cvsroot/freetds/freetds/src/ctlib/unittests/cs_config.c,v
retrieving revision 1.6
diff -u -r1.6 cs_config.c
--- src/ctlib/unittests/cs_config.c	16 May 2011 08:51:40 -0000	1.6
+++ src/ctlib/unittests/cs_config.c	12 Mar 2012 20:29:20 -0000
@@ -18,8 +18,8 @@
 {
 	int verbose = 1;
 	CS_CONTEXT *ctx;
-
-	CS_CHAR string_in[16], string_out[16];
+        
+	CS_CHAR string_in[16], string_out[16], interfaces_in[64],  interfaces_out[64];
 	CS_INT  int_in,        int_out;
 	CS_INT ret_len;
 
@@ -109,6 +109,38 @@
 		return 1;
 	}
 
+        strcpy(interfaces_in, "/home/sybase_ase/interfaces");
+        if (ct_config(ctx, CS_SET, CS_IFILE, (CS_VOID *)&interfaces_in, CS_NULLTERM, NULL)
+	    != CS_SUCCEED) {
+		fprintf(stderr, "cs_config() set failed\n");
+		return 1;
+	}
+
+        /*
+         * it should fail, but we should get the apropriate size in ret_len
+        */
+        if (ct_config(ctx, CS_GET, CS_IFILE, (CS_VOID *)&interfaces_out, 0, &ret_len)
+	    != CS_FAIL) {
+		fprintf(stderr, "cs_config() get failed\n");
+		return 1;
+	}
+
+        if(ret_len!=strlen(interfaces_in)+1)
+        {
+            fprintf(stderr, "returned value >%d< not as expected >%d<\n", ret_len, strlen(interfaces_in)+1);
+            return 1;
+        }
+
+        if (ct_config(ctx, CS_GET, CS_IFILE, (CS_VOID *)&interfaces_out, sizeof(interfaces_out), &ret_len)
+	    != CS_SUCCEED) {
+		fprintf(stderr, "cs_config() set failed\n");
+		return 1;
+	}
+
+        if (strcmp(interfaces_in, interfaces_out)!=0) {
+		fprintf(stdout, "returned value >%s< not as expected >%s<\n", interfaces_in, interfaces_out);
+		return 1;
+	}
 	cs_ctx_drop(ctx);
 
 	return 0;
Index: src/tds/config.c
===================================================================
RCS file: /cvsroot/freetds/freetds/src/tds/config.c,v
retrieving revision 1.177
diff -u -r1.177 config.c
--- src/tds/config.c	11 Mar 2012 15:52:22 -0000	1.177
+++ src/tds/config.c	12 Mar 2012 20:29:20 -0000
@@ -152,8 +152,11 @@
 	if (!connection || !tds_init_login(connection, locale)) {
 		tds_free_login(connection);
 		return NULL;
+            
 	}
 
+        tds_dstr_dup(&connection->interfaces,  &tds_get_ctx(tds)->interfaces);
+        
 	s = getenv("TDSDUMPCONFIG");
 	if (s) {
 		if (*s) {
@@ -172,7 +175,7 @@
 	tdsdump_log(TDS_DBG_INFO1, "Getting connection information for [%s].\n", 
 			    tds_dstr_cstr(&login->server_name));	/* (The server name is set in login.c.) */
 
-	/* Read the config files. */
+        /* Read the config files. */
 	tdsdump_log(TDS_DBG_INFO1, "Attempting to read conf files.\n");
 	found = tds_read_conf_file(connection, tds_dstr_cstr(&login->server_name));
 	if (!found) {
@@ -328,7 +331,11 @@
 	char *eptr = NULL;
 	int found = 0;
 
-	if (interf_file) {
+	if (!tds_dstr_isempty(&login->interfaces)) {
+		found = tds_try_conf_file(tds_dstr_cstr(&login->interfaces), "set contextually", server, login);
+	}
+
+	if (!found && interf_file) {
 		found = tds_try_conf_file(interf_file, "set programmatically", server, login);
 	}
 
@@ -1061,10 +1068,19 @@
 	}
 	tdsdump_log(TDS_DBG_INFO1, "Looking for server %s....\n", server);
 
+        /*
+	 * Look for the server in the interf_file if interf_file has been set
+         *  in the context.
+	 */
+	if ( !tds_dstr_isempty(&login->interfaces)) {
+		tdsdump_log(TDS_DBG_INFO1, "Looking for server in file %s.\n", tds_dstr_cstr(&login->interfaces));
+		found = search_interface_file(login, "", tds_dstr_cstr(&login->interfaces), server);
+	}
+
 	/*
 	 * Look for the server in the interf_file iff interf_file has been set.
 	 */
-	if (interf_file) {
+	if (!found && interf_file) {
 		tdsdump_log(TDS_DBG_INFO1, "Looking for server in file %s.\n", interf_file);
 		found = search_interface_file(login, "", interf_file, server);
 	}
@@ -1245,5 +1261,96 @@
 
 	return &settings;
 }
+/**
+ *
+ * @param path
+ * @param buffer
+ * @param buflen
+ * @param outlen
+ * @return
+ */
+static int
+tds_config_get_file(const char *path, void *buffer, size_t buflen, int *outlen)
+{
+    int len = 0;
+    if (path) {
+
+        len = strlen(path)+1;
+
+        /* if outlen was given we can inform how big buffer should be allocated */
+        if (outlen) {
+            *outlen = len;
+        }
+
+        if(buffer && buflen>=len) {
+            memcpy(buffer, path, len);
+            return TDS_SUCCESS;
+        }
+    }
+    return TDS_FAIL;
+}
 
+int
+tds_config_get_ctxt_interfaces_file(TDSCONTEXT *ctxt, void *buffer, size_t buflen, int *outlen)
+{
+    int result = 0;
+    char *path = NULL;
+    char *eptr = NULL;
+
+    if (ctxt && !tds_dstr_isempty(&ctxt->interfaces)) {
+        return tds_config_get_file(tds_dstr_cstr(&ctxt->interfaces), buffer, buflen, outlen);
+    }
+
+    if (interf_file) {
+        return tds_config_get_file(interf_file, buffer, buflen, outlen);
+    }
+
+    path = getenv("FREETDSCONF");
+    if (path) {
+        return tds_config_get_file(path, buffer, buflen, outlen);
+    }
+
+    eptr = getenv("FREETDS");
+    if (eptr) {
+            if (asprintf(&path, freetds_conf, eptr) >= 0) {
+                    result = tds_config_get_file(path, buffer, buflen, outlen);
+                    free(path);
+                    return result;
+            }
+    }
+
+    path = tds_get_home_file(".freetds.conf");
+    if (path) {
+            result = tds_config_get_file(path, buffer, buflen, outlen);
+            free(path);
+            return result;
+    }
+
+    return tds_config_get_file(FREETDS_SYSCONFFILE, buffer, buflen, outlen);
+}
+
+/**
+ *
+ * @param ctxt
+ * @param buffer
+ * @param buflen
+ * @return
+ */
+int
+tds_config_set_ctxt_interfaces_file(TDSCONTEXT *ctxt, void *buffer, size_t buflen)
+{
+    
+    char *buff = buffer;
+
+   /* If no filename passed, leave it NULL */
+    if ((buflen == 0 ) || (buffer == NULL) || (buff[0] == '\0') ) {
+            return TDS_SUCCESS;
+    }
+
+    /* Set to new value */        
+    if ( tds_dstr_copyn(&ctxt->interfaces, buffer, buflen) == NULL ) {
+        return TDS_FAIL;
+    }
+    return TDS_SUCCESS;
+}
 /** @} */
Index: src/tds/login.c
===================================================================
RCS file: /cvsroot/freetds/freetds/src/tds/login.c,v
retrieving revision 1.224
diff -u -r1.224 login.c
--- src/tds/login.c	25 Sep 2011 11:40:45 -0000	1.224
+++ src/tds/login.c	12 Mar 2012 20:29:20 -0000
@@ -258,6 +258,7 @@
 	ctx->ctx.locale = old_ctx->locale;
 	ctx->ctx.msg_handler = tds_save_msg;
 	ctx->ctx.err_handler = tds_save_err;
+        tds_dstr_dup(&ctx->ctx, old_ctx);
 }
 
 static void
Index: src/tds/mem.c
===================================================================
RCS file: /cvsroot/freetds/freetds/src/tds/mem.c,v
retrieving revision 1.225
diff -u -r1.225 mem.c
--- src/tds/mem.c	11 Mar 2012 15:52:22 -0000	1.225
+++ src/tds/mem.c	12 Mar 2012 20:29:21 -0000
@@ -651,6 +651,7 @@
 	}
 	context->locale = locale;
 	context->parent = parent;
+        tds_dstr_init(&context->interfaces);
 
 	return context;
 }
@@ -662,6 +663,7 @@
 		return;
 
 	tds_free_locale(context->locale);
+        tds_dstr_free(&context->interfaces);
 	free(context);
 }
 
@@ -790,6 +792,7 @@
 	char *lc_all, *tok = NULL;
 #endif
 
+        tds_dstr_init(&login->interfaces);
 	/*
 	 * TDS 7.0:
 	 * 0x02 indicates ODBC driver
@@ -1040,6 +1043,7 @@
 	tds_dstr_free(&login->database);
 	tds_dstr_free(&login->dump_file);
 	tds_dstr_free(&login->instance_name);
+        tds_dstr_free(&login->interfaces);
 	tds_dstr_free(&login->server_realm_name);
 	free(login);
 }



Archive powered by MHonArc 2.6.24.

Top of Page