[Groonga-commit] groonga/groonga [master] [win32] guess default document root path from DLL path.

Back to archive index

null+****@clear***** null+****@clear*****
2011年 4月 29日 (金) 19:13:17 JST


Kouhei Sutou	2011-04-29 10:13:17 +0000 (Fri, 29 Apr 2011)

  New Revision: 60d9bbd074e167d63ec10ce2c5fab1614cef3c72

  Log:
    [win32] guess default document root path from DLL path.
    
    PATH_SEPARATOR is removed.
    Groonga always uses "/" as its internal path separator.

  Modified files:
    configure.ac
    lib/groonga_in.h
    lib/plugin.c
    lib/proc.c
    lib/str.c
    lib/util.c
    lib/util.h
    src/groonga.c

  Modified: configure.ac (+1 -0)
===================================================================
--- configure.ac    2011-04-29 04:03:24 +0000 (9e78186)
+++ configure.ac    2011-04-29 10:13:17 +0000 (dd076b4)
@@ -799,6 +799,7 @@ GRN_DEFS="$GRN_DEFS -DGRN_PLUGINS_DIR=\\\"\"\$(pluginsdir)\"\\\""
 GRN_DEFS="$GRN_DEFS -DGRN_RELATIVE_PLUGINS_DIR=\\\"\"lib/\$(relative_pluginsdir)\"\\\""
 GRN_DEFS="$GRN_DEFS -DGRN_LOG_PATH=\\\"\"\$(grn_log_path)\"\\\""
 GRN_DEFS="$GRN_DEFS -DGRN_DEFAULT_DOCUMENT_ROOT=\\\"\"\$(pkgdatadir)/admin_html\"\\\""
+GRN_DEFS="$GRN_DEFS -DGRN_DEFAULT_RELATIVE_DOCUMENT_ROOT=\\\"\"share/\$(PACKAGE)/admin_html\"\\\""
 AC_SUBST(GRN_DEFS)
 CFLAGS="$CFLAGS $OPT_CFLAGS "
 LIBS="$LIBS $ZLIB_LIBS $LZO_LIBS $RT_LIBS $PTHREAD_LIBS $M_LIBS $NSL_LIBS $SOCKET_LIBS $WINDOWS_LIBS"

  Modified: lib/groonga_in.h (+1 -3)
===================================================================
--- lib/groonga_in.h    2011-04-29 04:03:24 +0000 (89a7a3e)
+++ lib/groonga_in.h    2011-04-29 10:13:17 +0000 (1f4a107)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2009 Brazil
+/* Copyright(C) 2009-2011 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -146,7 +146,6 @@
 
 #undef MSG_WAITALL
 #define MSG_WAITALL 0 /* before Vista, not supported... */
-#define PATH_SEPARATOR "/"
 #define fpclassify _fpclass
 #define CASE_FP_NAN case _FPCLASS_SNAN: case _FPCLASS_QNAN:
 #define CASE_FP_INFINITE case _FPCLASS_NINF: case _FPCLASS_PINF:
@@ -186,7 +185,6 @@ typedef char int_least8_t;
 # ifndef UINT_LEAST8_MAX
 typedef unsigned char uint_least8_t;
 # endif /* UINT_LEAST8_MAX */
-# define PATH_SEPARATOR "/"
 typedef int grn_sock;
 # define grn_sock_close close
 # define CALLBACK

  Modified: lib/plugin.c (+19 -76)
===================================================================
--- lib/plugin.c    2011-04-29 04:03:24 +0000 (37fc45b)
+++ lib/plugin.c    2011-04-29 10:13:17 +0000 (61ef742)
@@ -20,6 +20,7 @@
 #include "db.h"
 #include "plugin_in.h"
 #include "ql.h"
+#include "util.h"
 
 static grn_hash *grn_plugins = NULL;
 
@@ -280,11 +281,11 @@ grn_plugin_register_by_path(grn_ctx *ctx, const char *path)
       } else {
         const char *base_name;
 
-        base_name = strrchr(path, PATH_SEPARATOR[0]);
+        base_name = strrchr(path, '/');
         if (base_name) {
           complemented_libs_path[0] = '\0';
           strncat(complemented_libs_path, path, base_name - path);
-          strcat(complemented_libs_path, PATH_SEPARATOR ".libs");
+          strcat(complemented_libs_path, "/.libs");
           strcat(complemented_libs_path, base_name);
           strcat(complemented_libs_path, GRN_PLUGIN_SUFFIX);
           plugin_file = fopen(complemented_libs_path, "r");
@@ -324,79 +325,25 @@ static const char *
 default_plugins_dir(void)
 {
   if (!win32_plugins_dir) {
-    wchar_t dll_filename[MAX_PATH];
-    DWORD dll_filename_size;
-    dll_filename_size = GetModuleFileNameW(NULL, dll_filename, MAX_PATH);
-    if (dll_filename_size == 0) {
-      win32_plugins_dir = GRN_PLUGINS_DIR;
-    } else {
-      char *plugins_dir;
-      DWORD ansi_dll_filename_size;
-      ansi_dll_filename_size =
-        WideCharToMultiByte(CP_ACP, 0, dll_filename, dll_filename_size,
-                            NULL, 0, NULL, NULL);
-      if (ansi_dll_filename_size == 0) {
-        win32_plugins_dir = GRN_PLUGINS_DIR;
-      } else {
-        char *path;
-        const char *relative_path = GRN_RELATIVE_PLUGINS_DIR;
-        plugins_dir = malloc(ansi_dll_filename_size +
-                             strlen(relative_path));
-        WideCharToMultiByte(CP_ACP, 0, dll_filename, dll_filename_size,
-                            plugins_dir, ansi_dll_filename_size,
-                            NULL, NULL);
-        if ((path = strrchr(plugins_dir, '\\'))) {
-          *path = '\0';
-        }
-        path = strrchr(plugins_dir, '\\');
-        if (path && (strcasecmp(path + 1, "bin") == 0 ||
-                     strcasecmp(path + 1, "lib") == 0)) {
-          *path = '\0';
-        } else {
-          path = plugins_dir + strlen(plugins_dir);
-        }
-        *path = '\\';
-        path++;
-        while (*relative_path) {
-          if (*relative_path == '/') {
-            *path = '\\';
-          } else {
-            *path = *relative_path;
-          }
-          relative_path++;
-          path++;
-        }
-        *path = '\0';
-        win32_plugins_dir = plugins_dir;
-      }
-    }
+    const char *base_dir;
+    const char *relative_path = GRN_RELATIVE_PLUGINS_DIR;
+    char *plugins_dir;
+    char *path;
+    size_t base_dir_length;
+
+    base_dir = grn_win32_base_dir();
+    base_dir_length = strlen(base_dir);
+    plugins_dir = malloc(base_dir_length + strlen("/") + strlen(relative_path));
+    strcpy(plugins_dir, base_dir);
+    strcat(plugins_dir, "/");
+    strcat(plugins_dir, relative_path);
+    win32_plugins_dir = plugins_dir;
   }
   return win32_plugins_dir;
 }
 
-static void
-normalize_path_separator_in_path(char *path)
-{
-  for (; *path; path++) {
-    if (*path == '\\') {
-      *path = PATH_SEPARATOR[0];
-    }
-  }
-}
-
-static void
-normalize_path_separator_in_name(char *name)
-{
-  for (; *name; name++) {
-    if (*name == '/') {
-      *name = PATH_SEPARATOR[0];
-    }
-  }
-}
 #else /* WIN32 */
 #  define default_plugins_dir() GRN_PLUGINS_DIR
-#  define normalize_path_separator_in_path(path)
-#  define normalize_path_separator_in_name(name)
 #endif /* WIN32 */
 
 grn_rc
@@ -413,11 +360,10 @@ grn_plugin_register(grn_ctx *ctx, const char *name)
     plugins_dir = default_plugins_dir();
   }
   strcpy(path, plugins_dir);
-  normalize_path_separator_in_path(path);
 
   dir_last_char = plugins_dir[strlen(path) - 1];
-  if (dir_last_char != PATH_SEPARATOR[0]) {
-    strcat(path, PATH_SEPARATOR);
+  if (dir_last_char != '/') {
+    strcat(path, "/");
   }
 
   name_length = strlen(name);
@@ -430,10 +376,7 @@ grn_plugin_register(grn_ctx *ctx, const char *name)
     return ctx->rc;
   }
 
-  strcpy(normalized_name, name);
-  normalize_path_separator_in_name(normalized_name);
-
-  strcat(path, normalized_name);
+  strcat(path, name);
 
   return grn_plugin_register_by_path(ctx, path);
 }

  Modified: lib/proc.c (+4 -4)
===================================================================
--- lib/proc.c    2011-04-29 04:03:24 +0000 (c42ab18)
+++ lib/proc.c    2011-04-29 10:13:17 +0000 (376d752)
@@ -1037,12 +1037,12 @@ proc_missing(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
       return NULL;
     }
     grn_document_root_len = (int)l;
-    if (l > 0 && grn_document_root[l - 1] == PATH_SEPARATOR[0]) { grn_document_root_len--; }
+    if (l > 0 && grn_document_root[l - 1] == '/') { grn_document_root_len--; }
   }
   if ((plen = GRN_TEXT_LEN(VAR(0))) + grn_document_root_len < PATH_MAX) {
     char path[PATH_MAX];
     memcpy(path, grn_document_root, grn_document_root_len);
-    path[grn_document_root_len] = PATH_SEPARATOR[0];
+    path[grn_document_root_len] = '/';
     grn_str_url_path_normalize(ctx,
                                GRN_TEXT_VALUE(VAR(0)),
                                GRN_TEXT_LEN(VAR(0)),
@@ -1052,8 +1052,8 @@ proc_missing(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
   } else {
     uint32_t abbrlen = 32;
     ERR(GRN_INVALID_ARGUMENT,
-        "too long path name: <%s%c%.*s...> %u(%u)",
-        grn_document_root, PATH_SEPARATOR[0],
+        "too long path name: <%s/%.*s...> %u(%u)",
+        grn_document_root,
         abbrlen < plen ? abbrlen : plen, GRN_TEXT_VALUE(VAR(0)),
         plen + grn_document_root_len, PATH_MAX);
   }

  Modified: lib/str.c (+4 -4)
===================================================================
--- lib/str.c    2011-04-29 04:03:24 +0000 (236c72a)
+++ lib/str.c    2011-04-29 10:13:17 +0000 (4476acd)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2009 Brazil
+/* Copyright(C) 2009-2011 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -3219,9 +3219,9 @@ grn_str_url_path_normalize(grn_ctx *ctx, const char *path, size_t path_len,
       if (pc == p + 2 && *(p + 1) == '.') {
         /* '..' */
         if (b - buf >= 2) {
-          for (b -= 2; *b != PATH_SEPARATOR[0] && b >= buf; b--) {}
+          for (b -= 2; *b != '/' && b >= buf; b--) {}
         }
-        if (*b == PATH_SEPARATOR[0]) {
+        if (*b == '/') {
           b++;
           ERR(GRN_INVALID_ARGUMENT, "parent path doesn't exist.");
         }
@@ -3238,7 +3238,7 @@ grn_str_url_path_normalize(grn_ctx *ctx, const char *path, size_t path_len,
       b += pc - p;
       p = pc;
       if (p < pe && *pc == '/' && be > b) {
-        *b++ = PATH_SEPARATOR[0];
+        *b++ = '/';
         p++;
       }
     }

  Modified: lib/util.c (+49 -1)
===================================================================
--- lib/util.c    2011-04-29 04:03:24 +0000 (dea483f)
+++ lib/util.c    2011-04-29 10:13:17 +0000 (7190b69)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2010 Brazil
+/* Copyright(C) 2010-2011 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -691,3 +691,51 @@ grn_p_geo_point(grn_ctx *ctx, grn_geo_point *point)
   grn_p(ctx, &obj);
   grn_obj_unlink(ctx, &obj);
 }
+
+#ifdef WIN32
+static char *win32_base_dir = NULL;
+const char *
+grn_win32_base_dir(void)
+{
+  if (!win32_base_dir) {
+    wchar_t dll_filename[MAX_PATH];
+    DWORD dll_filename_size;
+    dll_filename_size = GetModuleFileNameW(NULL, dll_filename, MAX_PATH);
+    if (dll_filename_size == 0) {
+      win32_base_dir = ".";
+    } else {
+      DWORD ansi_dll_filename_size;
+      ansi_dll_filename_size =
+        WideCharToMultiByte(CP_ACP, 0, dll_filename, dll_filename_size,
+                            NULL, 0, NULL, NULL);
+      if (ansi_dll_filename_size == 0) {
+        win32_base_dir = ".";
+      } else {
+        char *path;
+        win32_base_dir = malloc(ansi_dll_filename_size);
+        WideCharToMultiByte(CP_ACP, 0, dll_filename, dll_filename_size,
+                            win32_base_dir, ansi_dll_filename_size,
+                            NULL, NULL);
+        win32_base_dir[ansi_dll_filename_size] = '\0';
+        if ((path = strrchr(win32_base_dir, '\\'))) {
+          *path = '\0';
+        }
+        path = strrchr(win32_base_dir, '\\');
+        if (path && (strcasecmp(path + 1, "bin") == 0 ||
+                     strcasecmp(path + 1, "lib") == 0)) {
+          *path = '\0';
+        } else {
+          path = win32_base_dir + strlen(win32_base_dir);
+          *path = '\0';
+        }
+        for (path = win32_base_dir; *path; path++) {
+          if (*path == '\\') {
+            *path = '/';
+          }
+        }
+      }
+    }
+  }
+  return win32_base_dir;
+}
+#endif

  Modified: lib/util.h (+4 -1)
===================================================================
--- lib/util.h    2011-04-29 04:03:24 +0000 (748a89e)
+++ lib/util.h    2011-04-29 10:13:17 +0000 (a698675)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2010 Brazil
+/* Copyright(C) 2010-2011 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -36,6 +36,9 @@ grn_obj *grn_inspect_name(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj);
 void grn_p(grn_ctx *ctx, grn_obj *obj);
 void grn_p_geo_point(grn_ctx *ctx, grn_geo_point *point);
 
+GRN_API const char *grn_win32_base_dir(void);
+GRN_API char *grn_path_separator_to_system(char *dest, char *groonga_path);
+
 #ifdef __cplusplus
 }
 #endif

  Modified: src/groonga.c (+16 -2)
===================================================================
--- src/groonga.c    2011-04-29 04:03:24 +0000 (87b0d6a)
+++ src/groonga.c    2011-04-29 10:13:17 +0000 (f347340)
@@ -1,5 +1,5 @@
 /* -*- c-basic-offset: 2 -*- */
-/* Copyright(C) 2009-2010 Brazil
+/* Copyright(C) 2009-2011 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -2059,6 +2059,20 @@ show_config(FILE *out, const grn_str_getopt_opt *opts, int flags)
   }
 }
 
+#ifdef WIN32
+static char win32_default_document_root[PATH_MAX];
+static char *
+default_document_root(void)
+{
+  strcpy(win32_default_document_root, grn_win32_base_dir());
+  strcat(win32_default_document_root, "/");
+  strcat(win32_default_document_root, GRN_DEFAULT_RELATIVE_DOCUMENT_ROOT);
+  return win32_default_document_root;
+}
+#else
+#  define default_document_root() GRN_DEFAULT_DOCUMENT_ROOT
+#endif
+
 int
 main(int argc, char **argv)
 {
@@ -2195,7 +2209,7 @@ main(int argc, char **argv)
     if (admin_html_path) {
       grn_document_root = admin_html_path;
     } else {
-      grn_document_root = GRN_DEFAULT_DOCUMENT_ROOT;
+      grn_document_root = default_document_root();
     }
   }
   if (protocol) {




Groonga-commit メーリングリストの案内
Back to archive index