Changeset: e4e169c373e3 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=e4e169c373e3
Modified Files:
        clients/mapilib/mapi.c
Branch: Jul2015
Log Message:

A little cleanup: don't use stat twice when once suffices.


diffs (104 lines):

diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -2121,7 +2121,10 @@ mapi_mapi(const char *host, int port, co
                        struct dirent *e;
                        struct stat st;
                        char found = 0;
-                       int socks[24];
+                       struct {
+                               int port;
+                               uid_t owner;
+                       } socks[24];
                        int i = 0;
                        int len;
                        uid_t me = getuid();
@@ -2133,56 +2136,55 @@ mapi_mapi(const char *host, int port, co
                                                continue;
                                        if (snprintf(buf, sizeof(buf), 
"/tmp/%s", e->d_name) >= (int) sizeof(buf))
                                                continue; /* ignore long name */
-                                       if (stat(buf, &st) != -1 && 
S_ISSOCK(st.st_mode))
-                                               socks[i++] = atoi(e->d_name + 
11);
-                                       if (i == sizeof(socks))
+                                       if (stat(buf, &st) != -1 && 
S_ISSOCK(st.st_mode)) {
+                                               socks[i].owner = st.st_uid;
+                                               socks[i++].port = 
atoi(e->d_name + 11);
+                                       }
+                                       if (i == sizeof(socks) / 
sizeof(socks[0]))
                                                break;
                                }
                                closedir(d);
                                len = i;
                                /* case 2bI) first those with a matching owner 
*/
                                for (i = 0; found == 0 && i < len; i++) {
-                                       snprintf(buf, sizeof(buf), 
"/tmp/.s.monetdb.%d", socks[i]);
-                                       if (socks[i] != 0 &&
-                                                       stat(buf, &st) != -1 && 
st.st_uid == me)
-                                       {
+                                       if (socks[i].port != 0 &&
+                                           socks[i].owner == me) {
                                                Mapi tmid;
                                                /* try this server for the 
database */
-                                               tmid = mapi_mapi("/tmp", 
socks[i], "mero", "mero",
-                                                               lang, dbname);
+                                               tmid = mapi_mapi("/tmp", 
socks[i].port, "mero", "mero",
+                                                                lang, dbname);
                                                tmid->redirmax = 0;
                                                if (connect_to_server(tmid) == 
MOK &&
-                                                               
(mapi_start_talking(tmid) == MOK ||
-                                                                
*tmid->redirects != NULL ||
-                                                                
(tmid->errorstr != NULL &&
-                                                                 
strstr(tmid->errorstr, "under maintenance") != NULL)))
-                                               {
+                                                   (mapi_start_talking(tmid) 
== MOK ||
+                                                    *tmid->redirects != NULL ||
+                                                    (tmid->errorstr != NULL &&
+                                                     strstr(tmid->errorstr, 
"under maintenance") != NULL))) {
+                                                       snprintf(buf, 
sizeof(buf), "/tmp/.s.monetdb.%d", socks[i].port);
                                                        host = buf;
-                                                       port = socks[i];
+                                                       port = socks[i].port;
                                                        found = 1;
                                                }
                                                mapi_disconnect(tmid);
                                                mapi_destroy(tmid);
-                                               socks[i] = 0; /* don't need to 
try again */
+                                               socks[i].port = 0; /* don't 
need to try again */
                                        }
                                }
                                /* case 2bII) the other sockets */
                                for (i = 0; found == 0 && i < len; i++) {
-                                       snprintf(buf, sizeof(buf), 
"/tmp/.s.monetdb.%d", socks[i]);
-                                       if (socks[i] != 0 && stat(buf, &st) != 
-1) {
+                                       if (socks[i].port != 0) {
                                                Mapi tmid;
                                                /* try this server for the 
database */
-                                               tmid = mapi_mapi("/tmp", 
socks[i], "mero", "mero",
+                                               tmid = mapi_mapi("/tmp", 
socks[i].port, "mero", "mero",
                                                                lang, dbname);
                                                tmid->redirmax = 0;
                                                if (connect_to_server(tmid) == 
MOK &&
-                                                               
(mapi_start_talking(tmid) == MOK ||
-                                                                
*tmid->redirects != NULL ||
-                                                                
(tmid->errorstr != NULL &&
-                                                                 
strstr(tmid->errorstr, "under maintenance") != NULL)))
-                                               {
+                                                   (mapi_start_talking(tmid) 
== MOK ||
+                                                    *tmid->redirects != NULL ||
+                                                    (tmid->errorstr != NULL &&
+                                                     strstr(tmid->errorstr, 
"under maintenance") != NULL))) {
+                                                       snprintf(buf, 
sizeof(buf), "/tmp/.s.monetdb.%d", socks[i].port);
                                                        host = buf;
-                                                       port = socks[i];
+                                                       port = socks[i].port;
                                                        found = 1;
                                                }
                                                mapi_disconnect(tmid);
@@ -2559,6 +2561,7 @@ mapi_start_talking(Mapi mid)
        check_stream(mid, mid->from, "Connection terminated", 
"mapi_start_talking", (mid->blk.eos = 1, mid->error));
 
        assert(len < BLOCK);
+       buf[len] = 0;
 
        /* buf at this point looks like "challenge:servertype:protover[:.*]" */
        chal = buf;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to