Project

General

Profile

Feature #5767 » find_file_safe_and_cache.patch

make find_file_safe and find_file_ext_safe concerning about cache - funny_falcon (Yura Sokolov), 12/17/2011 09:07 PM

View differences:

file.c
buflen = RSTRING_LEN(result),\
pend = p + buflen)
#define EXPAND_PATH()\
if ( !(abs_mode & FEP_DIR_EXPANDED) ) { \
file_expand_path(dname, Qnil, abs_mode, result); \
} \
else { \
size_t dlen = RSTRING_LEN(dname); \
BUFCHECK(dlen > buflen); \
strncpy(buf, RSTRING_PTR(dname), dlen + 1); \
}
VALUE
rb_home_dir(const char *user, VALUE result)
{
......
return result;
}
#define FEP_FILE_ABSOLUTE 1
#define FEP_DIR_EXPANDED 2
static VALUE
file_expand_path(VALUE fname, VALUE dname, int abs_mode, VALUE result)
{
......
BUFINIT();
tainted = OBJ_TAINTED(fname);
if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */
if (s[0] == '~' && !(abs_mode & FEP_FILE_ABSOLUTE)) { /* execute only if NOT absolute_path() */
long userlen = 0;
tainted = 1;
if (isdirsep(s[1]) || s[1] == '\0') {
......
/* specified drive, but not full path */
int same = 0;
if (!NIL_P(dname) && !not_same_drive(dname, s[0])) {
file_expand_path(dname, Qnil, abs_mode, result);
EXPAND_PATH();
BUFINIT();
if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) {
/* ok, same drive */
......
#endif
else if (!rb_is_absolute_path(s)) {
if (!NIL_P(dname)) {
file_expand_path(dname, Qnil, abs_mode, result);
EXPAND_PATH();
BUFINIT();
rb_enc_associate(result, rb_enc_check(result, fname));
}
......
rb_file_absolute_path(VALUE fname, VALUE dname)
{
check_expand_path_args(fname, dname);
return file_expand_path(fname, dname, 1, EXPAND_PATH_BUFFER());
return file_expand_path(fname, dname, FEP_FILE_ABSOLUTE, EXPAND_PATH_BUFFER());
}
/*
......
return rb_find_file_ext_safe(filep, ext, rb_safe_level());
}
#define GET_LOAD_PATH() \
if (cached_expanded_load_path) { \
RB_GC_GUARD(load_path) = rb_get_expanded_load_path(); \
dirs_mode = FEP_DIR_EXPANDED; \
} \
else { \
RB_GC_GUARD(load_path) = rb_get_load_path(); \
dirs_mode = 0; \
}
int
rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
{
const char *f = StringValueCStr(*filep);
VALUE fname = *filep, load_path, tmp;
long i, j, fnlen;
int expanded = 0;
int expanded = 0, dirs_mode;
if (!ext[0]) return 0;
......
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
}
RB_GC_GUARD(load_path) = rb_get_load_path();
GET_LOAD_PATH();
if (!load_path) return 0;
fname = rb_str_dup(*filep);
......
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
if (RSTRING_LEN(str) == 0) continue;
file_expand_path(fname, str, 0, tmp);
file_expand_path(fname, str, dirs_mode, tmp);
if (file_load_ok(RSTRING_PTR(tmp))) {
*filep = copy_path_class(tmp, *filep);
return (int)(j+1);
......
{
VALUE tmp, load_path;
const char *f = StringValueCStr(path);
int expanded = 0;
int expanded = 0, dirs_mode;
if (f[0] == '~') {
tmp = file_expand_path_1(path);
......
rb_raise(rb_eSecurityError, "loading from non-absolute path %s", f);
}
RB_GC_GUARD(load_path) = rb_get_load_path();
GET_LOAD_PATH();
if (load_path) {
long i;
......
VALUE str = RARRAY_PTR(load_path)[i];
RB_GC_GUARD(str) = rb_get_path_check(str, safe_level);
if (RSTRING_LEN(str) > 0) {
file_expand_path(path, str, 0, tmp);
file_expand_path(path, str, dirs_mode, tmp);
f = RSTRING_PTR(tmp);
if (file_load_ok(f)) goto found;
}
internal.h
/* load.c */
VALUE rb_get_load_path(void);
VALUE rb_get_expanded_load_path(void);
RUBY_EXTERN int cached_expanded_load_path;
/* math.c */
VALUE rb_math_atan2(VALUE, VALUE);
load.c
static VALUE rb_checked_expanded_cache();
static void rb_set_expanded_cache(VALUE);
static int cached_expanded_load_path = 1;
int cached_expanded_load_path = 1;
VALUE
rb_get_expanded_load_path(void)
......
VALUE load_path = rb_get_load_path();
long i;
if (!load_path) return 0;
expanded = rb_ary_new2(RARRAY_LEN(load_path));
for (i = 0; i < RARRAY_LEN(load_path); ++i) {
VALUE path = rb_file_expand_path(RARRAY_PTR(load_path)[i], Qnil);
(3-3/5)