Navigation Menu

Skip to content

Commit

Permalink
windows: support install path that has non-ascii characters again
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jun 22, 2018
1 parent c5d355c commit de6e9ac
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 133 deletions.
26 changes: 22 additions & 4 deletions lib/ctx_impl_mrb.c
Expand Up @@ -23,6 +23,7 @@
#ifdef GRN_WITH_MRUBY
# include "grn_ctx_impl_mrb.h"

# include "grn_encoding.h"
# include "grn_mrb.h"
# include "mrb/mrb_converter.h"
# include "mrb/mrb_error.h"
Expand Down Expand Up @@ -98,11 +99,14 @@ static mrb_value
mrb_kernel_load(mrb_state *mrb, mrb_value self)
{
grn_ctx *ctx = (grn_ctx *)mrb->ud;
char *path;
char *utf8_path;
const char *path;

mrb_get_args(mrb, "z", &path);
mrb_get_args(mrb, "z", &utf8_path);

path = grn_encoding_convert_to_locale_from_utf8(ctx, utf8_path, -1, NULL);
grn_mrb_load(ctx, path);
grn_encoding_converted_free(ctx, path);
if (mrb->exc) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->exc));
}
Expand All @@ -126,15 +130,29 @@ mrb_groonga_init(mrb_state *mrb, mrb_value self)
{
mrb_value load_path;
const char *plugins_dir;
const char *utf8_plugins_dir;
const char *system_ruby_scripts_dir;
const char *utf8_system_ruby_scripts_dir;

load_path = mrb_ary_new(mrb);

plugins_dir = grn_plugin_get_system_plugins_dir();
utf8_plugins_dir =
grn_encoding_convert_to_utf8_from_locale(ctx, plugins_dir, -1, NULL);
mrb_ary_push(mrb, load_path,
mrb_str_new_cstr(mrb, plugins_dir));
mrb_str_new_cstr(mrb, utf8_plugins_dir));
grn_encoding_converted_free(ctx, utf8_plugins_dir);

system_ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx);
utf8_system_ruby_scripts_dir =
grn_encoding_convert_to_utf8_from_locale(ctx,
system_ruby_scripts_dir,
-1,
NULL);
mrb_ary_push(mrb, load_path,
mrb_str_new_cstr(mrb, system_ruby_scripts_dir));
mrb_str_new_cstr(mrb, utf8_system_ruby_scripts_dir));
grn_encoding_converted_free(ctx, utf8_system_ruby_scripts_dir);

mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$LOAD_PATH"), load_path);
}

Expand Down
31 changes: 27 additions & 4 deletions lib/mrb.c
Expand Up @@ -18,6 +18,7 @@

#include "grn_mrb.h"
#include "grn_ctx_impl.h"
#include "grn_encoding.h"
#include "grn_util.h"

#include <string.h>
Expand Down Expand Up @@ -124,7 +125,8 @@ grn_mrb_expand_script_path(grn_ctx *ctx, const char *path,
{
const char *ruby_scripts_dir;
char dir_last_char;
int path_length, max_path_length;
int path_length;
int max_path_length;

if (grn_mrb_is_absolute_path(path)) {
expanded_path[0] = '\0';
Expand All @@ -144,10 +146,17 @@ grn_mrb_expand_script_path(grn_ctx *ctx, const char *path,
path_length = strlen(path);
max_path_length = PATH_MAX - strlen(expanded_path) - 1;
if (path_length > max_path_length) {
const char *grn_encoding_path;
grn_encoding_path =
grn_encoding_convert_from_locale(ctx,
path,
path_length,
NULL);
ERR(GRN_INVALID_ARGUMENT,
"script path is too long: %d (max: %d) <%s%s>",
path_length, max_path_length,
expanded_path, path);
expanded_path, grn_encoding_path);
grn_encoding_converted_free(ctx, grn_encoding_path);
return GRN_FALSE;
}

Expand Down Expand Up @@ -197,9 +206,14 @@ grn_mrb_load(grn_ctx *ctx, const char *path)

file = grn_fopen(expanded_path, "r");
if (!file) {
const char *grn_encoding_expanded_path;
mrb_value exception;

grn_encoding_expanded_path =
grn_encoding_convert_from_locale(ctx, expanded_path, -1, NULL);
SERR("fopen: failed to open mruby script file: <%s>",
expanded_path);
grn_encoding_expanded_path);
grn_encoding_converted_free(ctx, grn_encoding_expanded_path);
exception = mrb_exc_new(mrb, E_LOAD_ERROR,
ctx->errbuf, strlen(ctx->errbuf));
mrb->exc = mrb_obj_ptr(exception);
Expand All @@ -218,7 +232,16 @@ grn_mrb_load(grn_ctx *ctx, const char *path)
}

parser = mrb_parser_new(mrb);
mrb_parser_set_filename(parser, expanded_path);
{
const char *utf8_expanded_path;
utf8_expanded_path =
grn_encoding_convert_to_utf8_from_locale(ctx,
expanded_path,
-1,
NULL);
mrb_parser_set_filename(parser, utf8_expanded_path);
grn_encoding_converted_free(ctx, utf8_expanded_path);
}
parser->s = parser->send = NULL;
parser->f = file;
mrb_parser_parse(parser, NULL);
Expand Down

0 comments on commit de6e9ac

Please sign in to comment.