* dir.c (dir_s_home): use rb_home_dir_of and rb_default_home_dir.
* file.c (rb_home_dir_of): split from rb_home_dir() for the home
  directry of the given user, and the user name is a VALUE, not a bare
  pointer.  should raise if the user does not exist.
* file.c (rb_default_home_dir): split from rb_home_dir() for the home
  directry of the current user.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42160 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-07-25 04:06:50 +00:00
Родитель dda113e3ff
Коммит 7ec4a44762
5 изменённых файлов: 78 добавлений и 33 удалений

Просмотреть файл

@ -1,3 +1,14 @@
Thu Jul 25 13:06:46 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* dir.c (dir_s_home): use rb_home_dir_of and rb_default_home_dir.
* file.c (rb_home_dir_of): split from rb_home_dir() for the home
directry of the given user, and the user name is a VALUE, not a bare
pointer. should raise if the user does not exist.
* file.c (rb_default_home_dir): split from rb_home_dir() for the home
directry of the current user.
Thu Jul 25 12:32:11 2013 Koichi Sasada <ko1@atdot.net>
* ext/openssl/ossl.c: support additional three thread synchronization

10
dir.c
Просмотреть файл

@ -2125,12 +2125,18 @@ dir_s_home(int argc, VALUE *argv, VALUE obj)
VALUE user;
const char *u = 0;
rb_scan_args(argc, argv, "01", &user);
rb_check_arity(argc, 0, 1);
user = (argc > 0) argv[0] : Qnil;
if (!NIL_P(user)) {
SafeStringValue(user);
rb_must_asciicompat(user);
u = StringValueCStr(user);
if (*u) {
return rb_home_dir_of(user, rb_str_new(0, 0));
}
return rb_home_dir(u, rb_str_new(0, 0));
}
return rb_default_home_dir(rb_str_new(0, 0));
}
#if 0

59
file.c
Просмотреть файл

@ -2885,10 +2885,9 @@ ntfs_tail(const char *path, const char *end, rb_encoding *enc)
buflen = RSTRING_LEN(result),\
pend = p + buflen)
VALUE
rb_home_dir(const char *user, VALUE result)
static VALUE
copy_home_path(VALUE result, const char *dir)
{
const char *dir;
char *buf;
#if defined DOSISH || defined __CYGWIN__
char *p, *bend;
@ -2896,29 +2895,9 @@ rb_home_dir(const char *user, VALUE result)
long dirlen;
rb_encoding *enc;
if (!user || !*user) {
if (!(dir = getenv("HOME"))) {
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
}
dirlen = strlen(dir);
rb_str_resize(result, dirlen);
memcpy(buf = RSTRING_PTR(result), dir, dirlen);
}
else {
#ifdef HAVE_PWD_H
struct passwd *pwPtr = getpwnam(user);
if (!pwPtr) {
endpwent();
return Qnil;
}
dirlen = strlen(pwPtr->pw_dir);
rb_str_resize(result, dirlen);
memcpy(buf = RSTRING_PTR(result), pwPtr->pw_dir, dirlen + 1);
endpwent();
#else
return Qnil;
#endif
}
enc = rb_filesystem_encoding();
rb_enc_associate(result, enc);
#if defined DOSISH || defined __CYGWIN__
@ -2931,6 +2910,33 @@ rb_home_dir(const char *user, VALUE result)
return result;
}
VALUE
rb_home_dir_of(VALUE user, VALUE result)
{
#ifdef HAVE_PWD_H
struct passwd *pwPtr = getpwnam(RSTRING_PTR(user));
if (!pwPtr) {
endpwent();
#endif
rb_raise(rb_eArgError, "user %"PRIsVALUE" doesn't exist", user);
#ifdef HAVE_PWD_H
}
copy_home_path(result, pwPtr->pw_dir);
endpwent();
#endif
return result;
}
VALUE
rb_default_home_dir(VALUE result)
{
const char *dir = getenv("HOME");
if (!dir) {
rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
}
return copy_home_path(result, dir);
}
#ifndef _WIN32
static char *
append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc)
@ -2980,6 +2986,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
b = 0;
rb_str_set_len(result, 0);
if (*++s) ++s;
rb_default_home_dir(result);
}
else {
s = nextdirsep(b = s, fend, enc);
@ -2987,13 +2994,11 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
BUFCHECK(bdiff + userlen >= buflen);
memcpy(p, b, userlen);
rb_str_set_len(result, userlen);
rb_enc_associate(result, enc);
rb_home_dir_of(result, result);
buf = p + 1;
p += userlen;
}
if (NIL_P(rb_home_dir(buf, result))) {
rb_enc_raise(enc, rb_eArgError, "%.0"PRIsVALUE"user %s doesn't exist", fname,
buf);
}
if (!rb_is_absolute_path(RSTRING_PTR(result))) {
if (userlen) {
rb_enc_raise(enc, rb_eArgError, "non-absolute home of %.*s%.0"PRIsVALUE,

Просмотреть файл

@ -246,7 +246,8 @@ void rb_call_end_proc(VALUE data);
void rb_mark_end_proc(void);
/* file.c */
VALUE rb_home_dir(const char *user, VALUE result);
VALUE rb_home_dir_of(VALUE user, VALUE result);
VALUE rb_default_home_dir(VALUE result);
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict);
void rb_file_const(const char*, VALUE);
int rb_file_load_ok(const char *);

Просмотреть файл

@ -217,4 +217,26 @@ class TestDir < Test::Unit::TestCase
bug8597 = '[ruby-core:55764] [Bug #8597]'
assert_empty(Dir.glob(File.join(@root, "<")), bug8597)
end
def test_home
env_home = ENV["HOME"]
env_logdir = ENV["LOGDIR"]
ENV.delete("HOME")
ENV.delete("LOGDIR")
assert_raise(ArgumentError) { Dir.home }
assert_raise(ArgumentError) { Dir.home("") }
ENV["HOME"] = @nodir
assert_nothing_raised(ArgumentError) {
assert_equal(@nodir, Dir.home)
assert_equal(@nodir, Dir.home(""))
}
%W[no:such:user \u{7559 5b88}:\u{756a}].each do |user|
assert_raise_with_message(ArgumentError, /#{user}/) {Dir.home(user)}
end
ensure
ENV["HOME"] = env_home
ENV["LOGDIR"] = env_logdir
end
end