Skip to content

Commit

Permalink
file.c: split rb_home_dir
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Jul 25, 2013
1 parent dda113e commit 7ec4a44
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 33 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Thu Jul 25 13:06:46 2013 Nobuyoshi Nakada <[email protected]>

* 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 <[email protected]>

* ext/openssl/ossl.c: support additional three thread synchronization
Expand Down
10 changes: 8 additions & 2 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 35 additions & 30 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -2885,40 +2885,19 @@ 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;
#endif
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
}
dirlen = strlen(dir);
rb_str_resize(result, dirlen);
memcpy(buf = RSTRING_PTR(result), dir, dirlen);
enc = rb_filesystem_encoding();
rb_enc_associate(result, enc);
#if defined DOSISH || defined __CYGWIN__
Expand All @@ -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)
Expand Down Expand Up @@ -2980,20 +2986,19 @@ 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);
userlen = s - b;
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,
Expand Down
3 changes: 2 additions & 1 deletion internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
22 changes: 22 additions & 0 deletions test/ruby/test_dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,26 @@ def test_glob_metachar
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

0 comments on commit 7ec4a44

Please sign in to comment.